Skip to content

Commit 807dfbb

Browse files
committed
feat(vpn-gateway): Development of vpn gateway as spoke of transit gateway
1 parent 99d1b6f commit 807dfbb

28 files changed

+16286
-10819
lines changed

common/github.com/IBM/vpc-go-sdk/vpcv1/vpc_v1.go

Lines changed: 14081 additions & 10766 deletions
Large diffs are not rendered by default.

ibm/provider/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ func Provider() *schema.Provider {
608608

609609
"ibm_is_vpn_gateway_connection_local_cidrs": vpc.DataSourceIBMIsVPNGatewayConnectionLocalCidrs(),
610610
"ibm_is_vpn_gateway_connection_peer_cidrs": vpc.DataSourceIBMIsVPNGatewayConnectionPeerCidrs(),
611+
"ibm_is_vpn_gateway_advertised_cidrs": vpc.DataSourceIBMIsVPNGatewayAdvertisedCidrs(),
612+
"ibm_is_vpn_gateway_service_connections": vpc.DataSourceIBMIsVPNGatewayServiceConnections(),
611613

612614
"ibm_is_vpc_default_routing_table": vpc.DataSourceIBMISVPCDefaultRoutingTable(),
613615
"ibm_is_vpc_routing_table": vpc.DataSourceIBMIBMIsVPCRoutingTable(),
@@ -1321,6 +1323,7 @@ func Provider() *schema.Provider {
13211323
"ibm_is_volume": vpc.ResourceIBMISVolume(),
13221324
"ibm_is_vpn_gateway": vpc.ResourceIBMISVPNGateway(),
13231325
"ibm_is_vpn_gateway_connection": vpc.ResourceIBMISVPNGatewayConnection(),
1326+
"ibm_is_vpn_gateway_advertised_cidr": vpc.ResourceIBMISVPNGatewayAdvertisedCidr(),
13241327
"ibm_is_vpc": vpc.ResourceIBMISVPC(),
13251328
"ibm_is_vpc_address_prefix": vpc.ResourceIBMISVpcAddressPrefix(),
13261329
"ibm_is_vpc_dns_resolution_binding": vpc.ResourceIBMIsVPCDnsResolutionBinding(),

ibm/service/vpc/data_source_ibm_is_lb.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,20 +517,20 @@ func lbGetByName(d *schema.ResourceData, meta interface{}, name string) error {
517517
pool[poolProvisioningStatus] = *p.ProvisioningStatus
518518
pool["name"] = *p.Name
519519
if p.HealthMonitor != nil {
520+
lbHealthMonitor := p.HealthMonitor.(*vpcv1.LoadBalancerPoolHealthMonitor)
520521
healthMonitorInfo := make(map[string]interface{})
521-
delayfinal := strconv.FormatInt(*(p.HealthMonitor.Delay), 10)
522+
delayfinal := strconv.FormatInt(*lbHealthMonitor.Delay, 10)
522523
healthMonitorInfo[healthMonitorDelay] = delayfinal
523-
maxRetriesfinal := strconv.FormatInt(*(p.HealthMonitor.MaxRetries), 10)
524-
timeoutfinal := strconv.FormatInt(*(p.HealthMonitor.Timeout), 10)
524+
maxRetriesfinal := strconv.FormatInt(*lbHealthMonitor.MaxRetries, 10)
525+
timeoutfinal := strconv.FormatInt(*lbHealthMonitor.Timeout, 10)
525526
healthMonitorInfo[healthMonitorMaxRetries] = maxRetriesfinal
526527
healthMonitorInfo[healthMonitorTimeout] = timeoutfinal
527-
if p.HealthMonitor.URLPath != nil {
528-
healthMonitorInfo[healthMonitorURLPath] = *(p.HealthMonitor.URLPath)
528+
if lbHealthMonitor.URLPath != nil {
529+
healthMonitorInfo[healthMonitorURLPath] = *lbHealthMonitor.URLPath
529530
}
530-
healthMonitorInfo[healthMonitorType] = *(p.HealthMonitor.Type)
531+
healthMonitorInfo[healthMonitorType] = *lbHealthMonitor.Type
531532
pool[healthMonitor] = healthMonitorInfo
532533
}
533-
534534
if p.SessionPersistence != nil {
535535
sessionPersistenceInfo := make(map[string]interface{})
536536
sessionPersistenceInfo[sessionType] = *p.SessionPersistence.Type

ibm/service/vpc/data_source_ibm_is_lb_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func dataSourceIBMIsLbPoolRead(context context.Context, d *schema.ResourceData,
261261
}
262262

263263
if loadBalancerPool.HealthMonitor != nil {
264-
err = d.Set("health_monitor", dataSourceLoadBalancerPoolFlattenHealthMonitor(*loadBalancerPool.HealthMonitor))
264+
err = d.Set("health_monitor", dataSourceLoadBalancerPoolFlattenHealthMonitor(*loadBalancerPool.HealthMonitor.(*vpcv1.LoadBalancerPoolHealthMonitor)))
265265
if err != nil {
266266
return diag.FromErr(fmt.Errorf("Error setting health_monitor %s", err))
267267
}

ibm/service/vpc/data_source_ibm_is_lb_pools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func dataSourceLoadBalancerPoolCollectionPoolsToMap(poolsItem vpcv1.LoadBalancer
267267
}
268268
if poolsItem.HealthMonitor != nil {
269269
healthMonitorList := []map[string]interface{}{}
270-
healthMonitorMap := dataSourceLoadBalancerPoolCollectionPoolsHealthMonitorToMap(*poolsItem.HealthMonitor)
270+
healthMonitorMap := dataSourceLoadBalancerPoolCollectionPoolsHealthMonitorToMap(*poolsItem.HealthMonitor.(*vpcv1.LoadBalancerPoolHealthMonitor))
271271
healthMonitorList = append(healthMonitorList, healthMonitorMap)
272272
poolsMap["health_monitor"] = healthMonitorList
273273
}

ibm/service/vpc/data_source_ibm_is_vpn_gateway.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ func DataSourceIBMISVPNGateway() *schema.Resource {
267267
Computed: true,
268268
Description: "Route mode VPN gateway.",
269269
},
270+
isVPNGatewayLocalAsn: {
271+
Type: schema.TypeInt,
272+
Optional: true,
273+
Description: "The local autonomous system number (ASN) for this VPN gateway and its connections.",
274+
},
275+
276+
isVPNGatewayAdvertisedCidrs: {
277+
Type: schema.TypeList,
278+
Optional: true,
279+
Description: "The additional CIDRs advertised through any enabled routing protocol (for example, BGP). The routing protocol will advertise routes with these CIDRs and VPC prefixes as route destinations.",
280+
Elem: &schema.Schema{
281+
Type: schema.TypeString,
282+
},
283+
},
270284
"vpc": {
271285
Type: schema.TypeList,
272286
Computed: true,
@@ -438,6 +452,12 @@ func dataSourceIBMIsVPNGatewayRead(context context.Context, d *schema.ResourceDa
438452
if err = d.Set("mode", vpnGateway.Mode); err != nil {
439453
return diag.FromErr(fmt.Errorf("Error setting mode: %s", err))
440454
}
455+
if err = d.Set("advertised_cidrs", vpnGateway.AdvertisedCIDRs); err != nil {
456+
return diag.FromErr(fmt.Errorf("Error setting advertised cidrs: %s", err))
457+
}
458+
if err = d.Set("local_asn", vpnGateway.LocalAsn); err != nil {
459+
return diag.FromErr(fmt.Errorf("Error setting local asn: %s", err))
460+
}
441461
if vpnGateway.VPC != nil {
442462
err = d.Set("vpc", dataSourceVPNGatewayFlattenVPC(vpnGateway.VPC))
443463
if err != nil {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package vpc
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
"time"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+
15+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
16+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
17+
"github.com/IBM/vpc-go-sdk/vpcv1"
18+
)
19+
20+
func DataSourceIBMIsVPNGatewayAdvertisedCidrs() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourceIBMIsVPNGatewayAdvertisedCidrsRead,
23+
24+
Schema: map[string]*schema.Schema{
25+
"vpn_gateway": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
ExactlyOneOf: []string{"vpn_gateway_name", "vpn_gateway"},
29+
Description: "The VPN gateway identifier.",
30+
},
31+
"vpn_gateway_name": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
ExactlyOneOf: []string{"vpn_gateway_name", "vpn_gateway"},
35+
Description: "The VPN gateway name.",
36+
},
37+
"advertised_cidrs": &schema.Schema{
38+
Type: schema.TypeList,
39+
Required: true,
40+
Description: "The additional CIDRs advertised through any enabled routing protocol (for example, BGP). The routing protocol will advertise routes with these CIDRs and VPC prefixes as route destinations.",
41+
Elem: &schema.Schema{
42+
Type: schema.TypeString,
43+
},
44+
},
45+
},
46+
}
47+
}
48+
49+
func dataSourceIBMIsVPNGatewayAdvertisedCidrsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
50+
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
51+
if err != nil {
52+
return diag.FromErr(err)
53+
}
54+
vpn_gateway_id := d.Get("vpn_gateway").(string)
55+
vpn_gateway_name := d.Get("vpn_gateway_name").(string)
56+
57+
if vpn_gateway_name != "" {
58+
listvpnGWOptions := vpcClient.NewListVPNGatewaysOptions()
59+
60+
start := ""
61+
allrecs := []vpcv1.VPNGatewayIntf{}
62+
for {
63+
if start != "" {
64+
listvpnGWOptions.Start = &start
65+
}
66+
availableVPNGateways, detail, err := vpcClient.ListVPNGatewaysWithContext(context, listvpnGWOptions)
67+
if err != nil || availableVPNGateways == nil {
68+
return diag.FromErr(fmt.Errorf("[ERROR] Error reading list of VPN Gateways:%s\n%s", err, detail))
69+
}
70+
start = flex.GetNext(availableVPNGateways.Next)
71+
allrecs = append(allrecs, availableVPNGateways.VPNGateways...)
72+
if start == "" {
73+
break
74+
}
75+
}
76+
vpn_gateway_found := false
77+
for _, vpnGatewayIntfItem := range allrecs {
78+
if *vpnGatewayIntfItem.(*vpcv1.VPNGateway).Name == vpn_gateway_name {
79+
vpnGateway := vpnGatewayIntfItem.(*vpcv1.VPNGateway)
80+
vpn_gateway_id = *vpnGateway.ID
81+
vpn_gateway_found = true
82+
break
83+
}
84+
}
85+
if !vpn_gateway_found {
86+
log.Printf("[DEBUG] No vpn gateway found with given name %s", vpn_gateway_name)
87+
return diag.FromErr(fmt.Errorf("No vpn gateway found with given name %s", vpn_gateway_name))
88+
}
89+
}
90+
91+
listVPNGatewayAdvertisedCIDRsOptions := &vpcv1.ListVPNGatewayAdvertisedCIDRsOptions{}
92+
93+
listVPNGatewayAdvertisedCIDRsOptions.SetVPNGatewayID(vpn_gateway_id)
94+
95+
vpnGatewayAdvertisedCidRs, response, err := vpcClient.ListVPNGatewayAdvertisedCIDRsWithContext(context, listVPNGatewayAdvertisedCIDRsOptions)
96+
if err != nil {
97+
log.Printf("[DEBUG] ListVPNGatewayAdvertisedCIDRsWithContext failed %s\n%s", err, response)
98+
return diag.FromErr(fmt.Errorf("ListVPNGatewayAdvertisedCIDRsWithContext failed %s\n%s", err, response))
99+
}
100+
d.SetId(time.Now().UTC().String())
101+
d.Set("advertised_cidrs", vpnGatewayAdvertisedCidRs.AdvertisedCIDRs)
102+
103+
return nil
104+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package vpc_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
12+
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
13+
)
14+
15+
func TestAccIBMIsVPNGatewayAdvertisedCidrsDataSourceBasic(t *testing.T) {
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { acc.TestAccPreCheck(t) },
18+
Providers: acc.TestAccProviders,
19+
Steps: []resource.TestStep{
20+
resource.TestStep{
21+
Config: testAccCheckIBMIsVPNGatewayAdvertisedCidrsDataSourceConfigBasic(),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttrSet("data.ibm_is_vpn_gateway_advertised_cidrs.is_vpn_gateway_advertised_cidrs", "vpn_gateway"),
24+
resource.TestCheckResourceAttrSet("data.ibm_is_vpn_gateway_advertised_cidrs.is_vpn_gateway_advertised_cidrs", "advertised_cidrs.#"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func TestAccIBMIsVPNGatewayAdvertisedCidrsVPNGatewayNameDataSourceBasic(t *testing.T) {
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { acc.TestAccPreCheck(t) },
34+
Providers: acc.TestAccProviders,
35+
Steps: []resource.TestStep{
36+
resource.TestStep{
37+
Config: testAccCheckIBMIsVPNGatewayAdvertisedCidrsWithVPNGatewayNameDataSourceConfigBasic(),
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttrSet("data.ibm_is_vpn_gateway_advertised_cidrs.is_vpn_gateway_advertised_cidrs", "vpn_gateway"),
40+
resource.TestCheckResourceAttrSet("data.ibm_is_vpn_gateway_advertised_cidrs.is_vpn_gateway_advertised_cidrs", "advertised_cidrs.#"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccCheckIBMIsVPNGatewayAdvertisedCidrsDataSourceConfigBasic() string {
48+
return fmt.Sprintf(`
49+
data "ibm_is_vpn_gateway_advertised_cidrs" "is_vpn_gateway_advertised_cidrs" {
50+
vpn_gateway = "vpn_gateway"
51+
}
52+
`)
53+
}
54+
55+
func testAccCheckIBMIsVPNGatewayAdvertisedCidrsWithVPNGatewayNameDataSourceConfigBasic() string {
56+
return fmt.Sprintf(`
57+
data "ibm_is_vpn_gateway_advertised_cidrs" "is_vpn_gateway_advertised_cidrs" {
58+
vpn_gateway_name = "vpn_gateway_name"
59+
}
60+
`)
61+
}

0 commit comments

Comments
 (0)