Skip to content

Commit 5ec998b

Browse files
authored
Vpngwconn fix (IBM-Cloud#5917)
* fixed cidrs update on vpn gateway connections * added test cases
1 parent 0960168 commit 5ec998b

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

ibm/service/vpc/resource_ibm_is_vpn_gateway_connection_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,3 +1539,110 @@ func testAccCheckIBMISVPNGatewayConnectionNullPatchConfig(vpc, subnet, vpnname,
15391539
`, vpc, subnet, acc.ISZoneName, acc.ISCIDR, vpnname, ikepolicyname, ipsecpolicyname, name, noNullPass, noNullPass)
15401540

15411541
}
1542+
1543+
func TestAccIBMISVPNGatewayConnection_CIDRUpdates(t *testing.T) {
1544+
var VPNGatewayConnection string
1545+
vpcname := fmt.Sprintf("tfvpngc-vpc-%d", acctest.RandIntRange(100, 200))
1546+
subnetname1 := fmt.Sprintf("tfvpngc-subnet-%d", acctest.RandIntRange(100, 200))
1547+
subnetname2 := fmt.Sprintf("tfvpngc-subnet-%d", acctest.RandIntRange(100, 200))
1548+
vpnname := fmt.Sprintf("tfvpngc-vpn-%d", acctest.RandIntRange(100, 200))
1549+
name := fmt.Sprintf("tfvpngc-conn-%d", acctest.RandIntRange(100, 200))
1550+
1551+
resource.Test(t, resource.TestCase{
1552+
PreCheck: func() { acc.TestAccPreCheck(t) },
1553+
Providers: acc.TestAccProviders,
1554+
CheckDestroy: testAccCheckIBMISVPNGatewayConnectionDestroy,
1555+
Steps: []resource.TestStep{
1556+
// Initial configuration
1557+
{
1558+
Config: testAccCheckIBMISVPNGatewayConnectionCIDRConfig(vpcname, subnetname1, subnetname2, vpnname, name, false),
1559+
Check: resource.ComposeTestCheckFunc(
1560+
testAccCheckIBMISVPNGatewayConnectionExists("ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", VPNGatewayConnection),
1561+
resource.TestCheckResourceAttr(
1562+
"ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", "name", name),
1563+
resource.TestCheckResourceAttr(
1564+
"ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", "peer.0.cidrs.#", "1"),
1565+
resource.TestCheckResourceAttr(
1566+
"ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", "local.0.cidrs.#", "1"),
1567+
),
1568+
},
1569+
// Add additional CIDRs
1570+
{
1571+
Config: testAccCheckIBMISVPNGatewayConnectionCIDRConfig(vpcname, subnetname1, subnetname2, vpnname, name, true),
1572+
Check: resource.ComposeTestCheckFunc(
1573+
testAccCheckIBMISVPNGatewayConnectionExists("ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", VPNGatewayConnection),
1574+
resource.TestCheckResourceAttr(
1575+
"ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", "peer.0.cidrs.#", "2"),
1576+
resource.TestCheckResourceAttr(
1577+
"ibm_is_vpn_gateway_connection.testacc_VPNGatewayConnection", "local.0.cidrs.#", "2"),
1578+
),
1579+
},
1580+
},
1581+
})
1582+
}
1583+
1584+
func testAccCheckIBMISVPNGatewayConnectionCIDRConfig(vpc, subnet1, subnet2, vpnname, name string, additionalCIDRs bool) string {
1585+
base := fmt.Sprintf(`
1586+
resource "ibm_is_vpc" "testacc_vpc" {
1587+
name = "%s"
1588+
}
1589+
1590+
resource "ibm_is_subnet" "testacc_subnet1" {
1591+
name = "%s"
1592+
vpc = ibm_is_vpc.testacc_vpc.id
1593+
zone = "%s"
1594+
total_ipv4_address_count = 64
1595+
}
1596+
1597+
resource "ibm_is_subnet" "testacc_subnet2" {
1598+
name = "%s"
1599+
vpc = ibm_is_vpc.testacc_vpc.id
1600+
zone = "%s"
1601+
total_ipv4_address_count = 64
1602+
}
1603+
1604+
resource "ibm_is_vpn_gateway" "testacc_VPNGateway" {
1605+
name = "%s"
1606+
subnet = ibm_is_subnet.testacc_subnet1.id
1607+
mode = "policy"
1608+
}
1609+
`, vpc, subnet1, acc.ISZoneName, subnet2, acc.ISZoneName, vpnname)
1610+
1611+
if !additionalCIDRs {
1612+
return base + fmt.Sprintf(`
1613+
resource "ibm_is_vpn_gateway_connection" "testacc_VPNGatewayConnection" {
1614+
name = "%s"
1615+
vpn_gateway = ibm_is_vpn_gateway.testacc_VPNGateway.id
1616+
peer {
1617+
cidrs = [ibm_is_subnet.testacc_subnet1.ipv4_cidr_block]
1618+
address = cidrhost(ibm_is_subnet.testacc_subnet1.ipv4_cidr_block, 14)
1619+
}
1620+
local {
1621+
cidrs = [ibm_is_subnet.testacc_subnet1.ipv4_cidr_block]
1622+
}
1623+
preshared_key = "VPNDemoPassword"
1624+
}
1625+
`, name)
1626+
}
1627+
1628+
return base + fmt.Sprintf(`
1629+
resource "ibm_is_vpn_gateway_connection" "testacc_VPNGatewayConnection" {
1630+
name = "%s"
1631+
vpn_gateway = ibm_is_vpn_gateway.testacc_VPNGateway.id
1632+
peer {
1633+
cidrs = [
1634+
ibm_is_subnet.testacc_subnet1.ipv4_cidr_block,
1635+
ibm_is_subnet.testacc_subnet2.ipv4_cidr_block
1636+
]
1637+
address = cidrhost(ibm_is_subnet.testacc_subnet1.ipv4_cidr_block, 14)
1638+
}
1639+
local {
1640+
cidrs = [
1641+
ibm_is_subnet.testacc_subnet1.ipv4_cidr_block,
1642+
ibm_is_subnet.testacc_subnet2.ipv4_cidr_block
1643+
]
1644+
}
1645+
preshared_key = "VPNDemoPassword"
1646+
}
1647+
`, name)
1648+
}

ibm/service/vpc/resource_ibm_is_vpn_gateway_connections.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,112 @@ func vpngwconUpdate(d *schema.ResourceData, meta interface{}, gID, gConnID strin
752752
vpnGatewayConnectionPatchModel.EstablishMode = &newEstablishMode
753753
hasChanged = true
754754
}
755+
756+
if d.HasChange("local.0.cidrs") {
757+
o, n := d.GetChange("local.0.cidrs")
758+
oldSet := o.(*schema.Set)
759+
newSet := n.(*schema.Set)
760+
761+
// Find items to remove (present in old but not in new)
762+
toRemove := oldSet.Difference(newSet)
763+
if toRemove.Len() > 0 {
764+
for _, cidr := range toRemove.List() {
765+
cidrStr := cidr.(string)
766+
removeVPNGatewayConnectionsLocalCIDROptions := &vpcv1.RemoveVPNGatewayConnectionsLocalCIDROptions{
767+
VPNGatewayID: &gID,
768+
ID: &gConnID,
769+
CIDR: &cidrStr,
770+
}
771+
772+
res, err := sess.RemoveVPNGatewayConnectionsLocalCIDR(removeVPNGatewayConnectionsLocalCIDROptions)
773+
if err != nil {
774+
return fmt.Errorf("error removing VPN Gateway Connection Local CIDR %s: %w", cidrStr, err)
775+
}
776+
777+
if res.StatusCode != 201 && res.StatusCode != 204 {
778+
return fmt.Errorf("unexpected status code %d while removing Local CIDR %s", res.StatusCode, cidrStr)
779+
}
780+
}
781+
}
782+
783+
// Find items to add (present in new but not in old)
784+
toAdd := newSet.Difference(oldSet)
785+
if toAdd.Len() > 0 {
786+
for _, cidr := range toAdd.List() {
787+
cidrStr := cidr.(string)
788+
addVPNGatewayConnectionsLocalCIDROptions := &vpcv1.AddVPNGatewayConnectionsLocalCIDROptions{
789+
VPNGatewayID: &gID,
790+
ID: &gConnID,
791+
CIDR: &cidrStr,
792+
}
793+
794+
res, err := sess.AddVPNGatewayConnectionsLocalCIDR(addVPNGatewayConnectionsLocalCIDROptions)
795+
if err != nil {
796+
return fmt.Errorf("error adding VPN Gateway Connection Local CIDR %s: %w", cidrStr, err)
797+
}
798+
799+
if res.StatusCode != 201 && res.StatusCode != 204 {
800+
return fmt.Errorf("unexpected status code %d while adding Local CIDR %s", res.StatusCode, cidrStr)
801+
}
802+
}
803+
}
804+
}
805+
755806
if d.HasChange("peer") {
756807
peer, err := resourceIBMIsVPNGatewayConnectionMapToVPNGatewayConnectionPeerPatch(d.Get("peer.0").(map[string]interface{}))
757808
if err != nil {
758809
return err
759810
}
811+
if d.HasChange("peer.0.cidrs") {
812+
o, n := d.GetChange("peer.0.cidrs")
813+
oldSet := o.(*schema.Set)
814+
newSet := n.(*schema.Set)
815+
816+
// Find items to remove (present in old but not in new)
817+
toRemove := oldSet.Difference(newSet)
818+
if toRemove.Len() > 0 {
819+
for _, cidr := range toRemove.List() {
820+
cidrStr := cidr.(string)
821+
removeVPNGatewayConnectionsPeerCIDROptions := &vpcv1.RemoveVPNGatewayConnectionsPeerCIDROptions{
822+
VPNGatewayID: &gID,
823+
ID: &gConnID,
824+
CIDR: &cidrStr,
825+
}
826+
827+
res, err := sess.RemoveVPNGatewayConnectionsPeerCIDR(removeVPNGatewayConnectionsPeerCIDROptions)
828+
if err != nil {
829+
return fmt.Errorf("error removing VPN Gateway Connection Peer CIDR %s: %w", cidrStr, err)
830+
}
831+
832+
if res.StatusCode != 201 && res.StatusCode != 204 {
833+
return fmt.Errorf("unexpected status code %d while removing CIDR %s", res.StatusCode, cidrStr)
834+
}
835+
}
836+
}
837+
838+
// Find items to add (present in new but not in old)
839+
toAdd := newSet.Difference(oldSet)
840+
if toAdd.Len() > 0 {
841+
for _, cidr := range toAdd.List() {
842+
cidrStr := cidr.(string)
843+
addVPNGatewayConnectionsPeerCIDROptions := &vpcv1.AddVPNGatewayConnectionsPeerCIDROptions{
844+
VPNGatewayID: &gID,
845+
ID: &gConnID,
846+
CIDR: &cidrStr,
847+
}
848+
849+
res, err := sess.AddVPNGatewayConnectionsPeerCIDR(addVPNGatewayConnectionsPeerCIDROptions)
850+
if err != nil {
851+
return fmt.Errorf("error adding VPN Gateway Connection Peer CIDR %s: %w", cidrStr, err)
852+
}
853+
854+
if res.StatusCode != 201 && res.StatusCode != 204 {
855+
return fmt.Errorf("unexpected status code %d while adding CIDR %s", res.StatusCode, cidrStr)
856+
}
857+
}
858+
}
859+
860+
}
760861
vpnGatewayConnectionPatchModel.Peer = peer
761862
hasChanged = true
762863
}

0 commit comments

Comments
 (0)