Skip to content

Commit

Permalink
fix(vpc-routing-table): support removing of advertise routes and acce…
Browse files Browse the repository at this point in the history
…pt routes from array (#5039)

* fix(vpc-routing-table): support removing of  advertise routes and accept routes from arrays

* updated test cases
  • Loading branch information
deepaksibm authored Jan 18, 2024
1 parent f4f9e05 commit f9222a6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 14 deletions.
31 changes: 24 additions & 7 deletions ibm/service/vpc/resource_ibm_is_vpc_routing_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,37 @@ func resourceIBMISVPCRoutingTableUpdate(d *schema.ResourceData, meta interface{}
routingTablePatchModel.Name = core.StringPtr(name)
hasChange = true
}
removeAcceptRoutesFromFilter := false
if d.HasChange("accept_routes_from_resource_type") {
var aroutes []vpcv1.ResourceFilter
acptRoutes := d.Get("accept_routes_from_resource_type").(*schema.Set)
for _, val := range acptRoutes.List() {
value := val.(string)
resourceFilter := vpcv1.ResourceFilter{
ResourceType: &value,
if len(acptRoutes.List()) == 0 {
removeAcceptRoutesFromFilter = true
} else {
for _, val := range acptRoutes.List() {
value := val.(string)
resourceFilter := vpcv1.ResourceFilter{
ResourceType: &value,
}
aroutes = append(aroutes, resourceFilter)
}
aroutes = append(aroutes, resourceFilter)
}
routingTablePatchModel.AcceptRoutesFrom = aroutes
hasChange = true
}
removeAdvertiseRoutesTo := false
if d.HasChange("advertise_routes_to") {
var advertiseRoutesToList []string
advertiseRoutesTo := d.Get("advertise_routes_to").(*schema.Set)

for _, val := range advertiseRoutesTo.List() {
advertiseRoutesToList = append(advertiseRoutesToList, val.(string))
if len(advertiseRoutesTo.List()) == 0 {
removeAdvertiseRoutesTo = true
} else {
for _, val := range advertiseRoutesTo.List() {
advertiseRoutesToList = append(advertiseRoutesToList, val.(string))
}
}

routingTablePatchModel.AdvertiseRoutesTo = advertiseRoutesToList
hasChange = true
}
Expand Down Expand Up @@ -385,6 +396,12 @@ func resourceIBMISVPCRoutingTableUpdate(d *schema.ResourceData, meta interface{}
return fmt.Errorf("[ERROR] Error calling asPatch for RoutingTablePatchModel: %s", asPatchErr)
}

if removeAdvertiseRoutesTo {
routingTablePatchModelAsPatch["advertise_routes_to"] = []string{}
}
if removeAcceptRoutesFromFilter {
routingTablePatchModelAsPatch["accept_routes_from"] = []vpcv1.ResourceFilter{}
}
updateVpcRoutingTableOptions.RoutingTablePatch = routingTablePatchModelAsPatch
_, response, err := sess.UpdateVPCRoutingTable(updateVpcRoutingTableOptions)
if err != nil {
Expand Down
79 changes: 72 additions & 7 deletions ibm/service/vpc/resource_ibm_is_vpc_routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,45 @@ func TestAccIBMISVPCRoutingTable_advertiseRoutesTO(t *testing.T) {
var vpcRouteTables string
name1 := fmt.Sprintf("tfvpc-create-%d", acctest.RandIntRange(10, 100))
routeTableName := fmt.Sprintf("tfvpcrt-create-%d", acctest.RandIntRange(10, 100))
routeTableName1 := fmt.Sprintf("tfvpcrt-up-create-%d", acctest.RandIntRange(10, 100))

advertiseRoutesToDirectLink := "direct_link"
advertiseRoutesToTransit_gateway := "transit_gateway"
acceptRoutesFromVPNServer := "vpn_server"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISVPCRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISVPCRouteTableConfig(routeTableName, name1),
Config: testAccCheckIBMISVPCRouteTableAdvertiseRoutesToConfig(routeTableName, name1, acceptRoutesFromVPNServer, advertiseRoutesToDirectLink, advertiseRoutesToTransit_gateway),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCRouteTableExists("ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", vpcRouteTables),
resource.TestCheckResourceAttrSet(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to"),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to.0", advertiseRoutesToDirectLink),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to.1", advertiseRoutesToTransit_gateway),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "accept_routes_from_resource_type.0", acceptRoutesFromVPNServer),
),
},
{
Config: testAccCheckIBMISVPCRouteTableConfig(routeTableName1, name1),
Config: testAccCheckIBMISVPCRouteTableAdvertiseRoutesToDLConfig(routeTableName, name1, acceptRoutesFromVPNServer, advertiseRoutesToDirectLink),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCRouteTableExists("ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", vpcRouteTables),
resource.TestCheckResourceAttrSet(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to"),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to.0", advertiseRoutesToDirectLink),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "accept_routes_from_resource_type.0", acceptRoutesFromVPNServer),
),
},
{
Config: testAccCheckIBMISVPCRouteTableAdvertiseRoutesToRemovalConfig(routeTableName, name1),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISVPCRouteTableExists("ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", vpcRouteTables),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "advertise_routes_to.#", "0"),
resource.TestCheckResourceAttr(
"ibm_is_vpc_routing_table.test_ibm_is_vpc_routing_table", "accept_routes_from_resource_type.#", "0"),
),
},
},
Expand Down Expand Up @@ -206,3 +224,50 @@ resource "ibm_is_vpc_routing_table" "test_ibm_is_vpc_routing_table" {
accept_routes_from_resource_type=["%s"]
}`, name, rtName, acceptRoutesFromVPNServer)
}

func testAccCheckIBMISVPCRouteTableAdvertiseRoutesToConfig(rtName, name, acceptRoutesFromVPNServer, advertiseRoutesTo1, advertiseRoutesTo2 string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_vpc_routing_table" "test_ibm_is_vpc_routing_table" {
depends_on = [ibm_is_vpc.testacc_vpc]
route_direct_link_ingress = true
route_transit_gateway_ingress = true
vpc = ibm_is_vpc.testacc_vpc.id
name = "%s"
accept_routes_from_resource_type=["%s"]
advertise_routes_to=["%s","%s"]
}`, name, rtName, acceptRoutesFromVPNServer, advertiseRoutesTo1, advertiseRoutesTo2)
}

func testAccCheckIBMISVPCRouteTableAdvertiseRoutesToDLConfig(rtName, name, acceptRoutesFromVPNServer, advertiseRoutesTo1 string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_vpc_routing_table" "test_ibm_is_vpc_routing_table" {
depends_on = [ibm_is_vpc.testacc_vpc]
route_direct_link_ingress = true
route_transit_gateway_ingress = true
vpc = ibm_is_vpc.testacc_vpc.id
name = "%s"
accept_routes_from_resource_type=["%s"]
advertise_routes_to=["%s"]
}`, name, rtName, acceptRoutesFromVPNServer, advertiseRoutesTo1)
}

func testAccCheckIBMISVPCRouteTableAdvertiseRoutesToRemovalConfig(rtName, name string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_vpc_routing_table" "test_ibm_is_vpc_routing_table" {
depends_on = [ibm_is_vpc.testacc_vpc]
route_direct_link_ingress = true
vpc = ibm_is_vpc.testacc_vpc.id
name = "%s"
accept_routes_from_resource_type=[]
advertise_routes_to=[]
}`, name, rtName)
}

0 comments on commit f9222a6

Please sign in to comment.