From f9222a6d5568249640b1cdfee4cfd8f1dc6f43c3 Mon Sep 17 00:00:00 2001 From: Deepak Selvakumar <77007253+deepaksibm@users.noreply.github.com> Date: Thu, 18 Jan 2024 15:01:41 +0530 Subject: [PATCH] fix(vpc-routing-table): support removing of advertise routes and accept routes from array (#5039) * fix(vpc-routing-table): support removing of advertise routes and accept routes from arrays * updated test cases --- .../vpc/resource_ibm_is_vpc_routing_table.go | 31 ++++++-- .../resource_ibm_is_vpc_routing_table_test.go | 79 +++++++++++++++++-- 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/ibm/service/vpc/resource_ibm_is_vpc_routing_table.go b/ibm/service/vpc/resource_ibm_is_vpc_routing_table.go index 2935d1817b..1908868ee2 100644 --- a/ibm/service/vpc/resource_ibm_is_vpc_routing_table.go +++ b/ibm/service/vpc/resource_ibm_is_vpc_routing_table.go @@ -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 } @@ -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 { diff --git a/ibm/service/vpc/resource_ibm_is_vpc_routing_table_test.go b/ibm/service/vpc/resource_ibm_is_vpc_routing_table_test.go index 39007bf996..91e876d6a9 100644 --- a/ibm/service/vpc/resource_ibm_is_vpc_routing_table_test.go +++ b/ibm/service/vpc/resource_ibm_is_vpc_routing_table_test.go @@ -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"), ), }, }, @@ -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) +}