Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sweepers on iploadbalancing_vrack_network #142

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ovh/resource_ovh_cloud_network_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
3 changes: 2 additions & 1 deletion ovh/resource_ovh_iploadbalancing_tcp_frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 62 additions & 21 deletions ovh/resource_ovh_iploadbalancing_vrack_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ovh

import (
"fmt"
"log"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -56,56 +57,96 @@ 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(
"/ipLoadbalancing/%s/vrack/network/%d",
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)
}
Expand Down
10 changes: 10 additions & 0 deletions ovh/resource_ovh_vrack_cloudproject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ovh

import (
"fmt"
"log"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -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),
Expand Down