From 2818c863870be94bd86b6ac36420bdcd2b7803e2 Mon Sep 17 00:00:00 2001 From: yann degat Date: Mon, 27 Apr 2020 13:32:32 +0000 Subject: [PATCH] Fix sweepers on iploadbalancing_vrack_network --- ...resource_ovh_cloud_network_private_test.go | 6 +- ...e_ovh_iploadbalancing_tcp_frontend_test.go | 3 +- ..._ovh_iploadbalancing_vrack_network_test.go | 83 ++++++++++++++----- ovh/resource_ovh_vrack_cloudproject_test.go | 10 +++ 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/ovh/resource_ovh_cloud_network_private_test.go b/ovh/resource_ovh_cloud_network_private_test.go index 96368968e..18db2dfa6 100644 --- a/ovh/resource_ovh_cloud_network_private_test.go +++ b/ovh/resource_ovh_cloud_network_private_test.go @@ -82,12 +82,14 @@ func testSweepCloudNetworkPrivate(region string) error { vrack := os.Getenv("OVH_VRACK") if vrack == "" { - return fmt.Errorf("OVH_VRACK must be set") + log.Print("[DEBUG] OVH_VRACK is not set. No cloud_network_private to sweep") + return nil } projectId := os.Getenv("OVH_PUBLIC_CLOUD") if projectId == "" { - return fmt.Errorf("OVH_PUBLIC_CLOUD must be set") + log.Print("[DEBUG] OVH_PUBLIC_CLOUD is not set. No cloud_network_private to sweep") + return nil } networks := []CloudNetworkPrivateResponse{} diff --git a/ovh/resource_ovh_iploadbalancing_tcp_frontend_test.go b/ovh/resource_ovh_iploadbalancing_tcp_frontend_test.go index ff1330566..d3202fa06 100644 --- a/ovh/resource_ovh_iploadbalancing_tcp_frontend_test.go +++ b/ovh/resource_ovh_iploadbalancing_tcp_frontend_test.go @@ -26,7 +26,8 @@ func testSweepIploadbalancingTcpFrontend(region string) error { iplb := os.Getenv("OVH_IPLB_SERVICE") if iplb == "" { - return fmt.Errorf("OVH_IPLB_SERVICE must be set") + log.Print("[DEBUG] OVH_IPLB_SERVICE is not set. No iploadbalancing_vrack_network to sweep") + return nil } frontends := make([]int64, 0) diff --git a/ovh/resource_ovh_iploadbalancing_vrack_network_test.go b/ovh/resource_ovh_iploadbalancing_vrack_network_test.go index acc7ff1e2..4cde487ec 100644 --- a/ovh/resource_ovh_iploadbalancing_vrack_network_test.go +++ b/ovh/resource_ovh_iploadbalancing_vrack_network_test.go @@ -2,6 +2,7 @@ package ovh import ( "fmt" + "log" "net/url" "os" "testing" @@ -56,49 +57,87 @@ func testSweepIpLoadbalancingVrackNetwork(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } + serviceName := os.Getenv("OVH_IPLB_SERVICE") if serviceName == "" { - return fmt.Errorf("OVH_IPLB_SERVICE env var is required") + log.Print("[DEBUG] OVH_IPLB_SERVICE is not set. No iploadbalancing_vrack_network to sweep") + return nil } - endpoint := fmt.Sprintf( - "/ipLoadbalancing/%s/vrack/network?subnet=%s", - url.PathEscape(serviceName), - url.PathEscape(testAccIpLoadbalancingVrackNetworkSubnet), - ) + log.Print("[DEBUG] Sweeping iploadbalancing_vrack_network") + + get_network_ids := func(vlanId string) ([]int64, error) { + endpoint := fmt.Sprintf( + "/ipLoadbalancing/%s/vrack/network?vlan=%s", + url.PathEscape(serviceName), + url.PathEscape(vlanId), + ) - result := make([]int64, 0) + result := make([]int64, 0) - if err := client.Get(endpoint, result); err != nil { - if err.(*ovh.APIError).Code == 404 { - return nil + if err := client.Get(endpoint, &result); err != nil { + if err.(*ovh.APIError).Code == 404 { + return nil, nil + } + return nil, err } - return err + + return result, nil } - for _, id := range result { - // delete farms, then delete vrack network - endpoint = fmt.Sprintf( - "/ipLoadbalancing/%s/tcp/farm?vrackNetworkId=%d", + delete_farms := func(farmType string, networkId int64) error { + endpoint := fmt.Sprintf( + "/ipLoadbalancing/%s/%s/farm?vrackNetworkId=%d", url.PathEscape(serviceName), - id, + url.PathEscape(farmType), + networkId, ) - farms := make([]int64, 0) - if err := client.Get(endpoint, farms); err != nil && !(err.(*ovh.APIError).Code == 404) { + result := make([]int64, 0) + + if err := client.Get(endpoint, &result); err != nil { + if err.(*ovh.APIError).Code == 404 { + return nil + } return err } - for _, farmId := range farms { - endpoint = fmt.Sprintf( - "/ipLoadbalancing/%s/tcp/farm/%d", + + for _, farmId := range result { + endpoint := fmt.Sprintf( + "/ipLoadbalancing/%s/%s/farm/%d", url.PathEscape(serviceName), + url.PathEscape(farmType), farmId, ) // delete the farm + log.Printf("[DEBUG] Calling DELETE on %v", endpoint) if err := client.Delete(endpoint, nil); err != nil { return fmt.Errorf("Error calling DELETE %s:\n\t %q", endpoint, err) } } + return nil + } + + resultVlan1001, err := get_network_ids(testAccIpLoadbalancingVrackNetworkVlan1001) + if err != nil { + return err + } + + resultVlan1002, err := get_network_ids(testAccIpLoadbalancingVrackNetworkVlan1002) + if err != nil { + return err + } + + result := append(resultVlan1001, resultVlan1002...) + for _, id := range result { + // delete farms, then delete vrack network + if err := delete_farms("http", id); err != nil { + return err + } + + if err := delete_farms("tcp", id); err != nil { + return err + } // delete the vrack network endpoint := fmt.Sprintf( @@ -106,6 +145,8 @@ func testSweepIpLoadbalancingVrackNetwork(region string) error { url.PathEscape(serviceName), id, ) + + log.Printf("[DEBUG] Calling DELETE on %v", endpoint) if err := client.Delete(endpoint, nil); err != nil { return fmt.Errorf("Error calling DELETE %s:\n\t %q", endpoint, err) } diff --git a/ovh/resource_ovh_vrack_cloudproject_test.go b/ovh/resource_ovh_vrack_cloudproject_test.go index 9d9d66fea..980a6dd8a 100644 --- a/ovh/resource_ovh_vrack_cloudproject_test.go +++ b/ovh/resource_ovh_vrack_cloudproject_test.go @@ -2,6 +2,7 @@ package ovh import ( "fmt" + "log" "net/url" "os" "testing" @@ -40,7 +41,16 @@ func testSweepVrackCloudProject(region string) error { } vrackId := os.Getenv("OVH_VRACK") + if vrackId == "" { + log.Print("[DEBUG] OVH_VRACK is not set. No vrack_cloud_project to sweep") + return nil + } + projectId := os.Getenv("OVH_PUBLIC_CLOUD") + if projectId == "" { + log.Print("[DEBUG] OVH_PUBLIC_CLOUD is not set. No vrack_cloud_project to sweep") + return nil + } endpoint := fmt.Sprintf("/vrack/%s/cloudProject/%s", url.PathEscape(vrackId),