-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle aggregatedList case for generated sweepers
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
f32dd69
commit ec9b866
Showing
6 changed files
with
720 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// This file is automatically generated by Magic Modules and manual | ||
// changes will be clobbered when the file is regenerated. | ||
// | ||
// Please read more about how to change this file in | ||
// .github/CONTRIBUTING.md. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
package google | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("ComputeAutoscaler", &resource.Sweeper{ | ||
Name: "ComputeAutoscaler", | ||
F: testSweepComputeAutoscaler, | ||
}) | ||
} | ||
|
||
// At the time of writing, the CI only passes us-central1 as the region | ||
func testSweepComputeAutoscaler(region string) error { | ||
resourceName := "ComputeAutoscaler" | ||
log.Printf("[INFO] Sweeping %s", resourceName) | ||
|
||
config, err := sharedConfigForRegion(region) | ||
if err != nil { | ||
log.Printf("[INFO] error getting shared config for region: %s", err) | ||
return err | ||
} | ||
|
||
err = config.LoadAndValidate(context.Background()) | ||
if err != nil { | ||
log.Printf("[INFO] error loading: %s", err) | ||
return err | ||
} | ||
|
||
listTemplate := strings.Split("https://www.googleapis.com/compute/v1/projects/{{project}}/aggregated/autoscalers", "?")[0] | ||
|
||
d := &ResourceDataMock{ | ||
FieldsInSchema: map[string]interface{}{ | ||
"project": config.Project, | ||
"region": region, | ||
"location": region, | ||
"zone": "-", | ||
}, | ||
} | ||
|
||
listUrl, err := replaceVars(d, config, listTemplate) | ||
if err != nil { | ||
log.Printf("[INFO] error preparing sweeper list url: %s", err) | ||
return nil | ||
} | ||
|
||
if strings.Count(listUrl, "//") > 1 { | ||
log.Printf("[INFO] Invalid list url for %s sweeper: %s", resourceName, listUrl) | ||
return nil | ||
} | ||
|
||
res, err := sendRequest(config, "GET", config.Project, listUrl, nil) | ||
if err != nil { | ||
log.Printf("[INFO] Unable to list %s: %s", resourceName, err) | ||
return nil | ||
} | ||
|
||
resourceList, ok := res["items"] | ||
if !ok { | ||
log.Printf("[INFO] Nothing found in response.") | ||
return nil | ||
} | ||
var rl []interface{} | ||
zones := resourceList.(map[string]interface{}) | ||
// Loop through every zone in the list response | ||
for _, zonesValue := range zones { | ||
zone := zonesValue.(map[string]interface{}) | ||
for k, v := range zone { | ||
// Zone map either has resources or a warning stating there were no resources found in the zone | ||
if k != "warning" { | ||
resourcesInZone := v.([]interface{}) | ||
rl = append(rl, resourcesInZone...) | ||
} | ||
} | ||
} | ||
|
||
log.Printf("[INFO] Found %d items in %s list response.", len(rl), resourceName) | ||
// items who don't match the tf-test prefix | ||
nonPrefixCount := 0 | ||
for _, ri := range rl { | ||
obj := ri.(map[string]interface{}) | ||
if obj["name"] == nil { | ||
log.Printf("[INFO] %s resource name was nil", resourceName) | ||
return nil | ||
} | ||
|
||
nameSegs := strings.Split(obj["name"].(string), "/") | ||
name := nameSegs[len(nameSegs)-1] | ||
|
||
// Only sweep resources with the test prefix | ||
if !strings.HasPrefix(name, "tf-test") { | ||
nonPrefixCount++ | ||
continue | ||
} | ||
deleteTemplate := "https://www.googleapis.com/compute/v1/projects/{{project}}/zones/{{zone}}/autoscalers/{{name}}" | ||
|
||
if obj["zone"] == nil { | ||
log.Printf("[INFO] %s resource zone was nil", resourceName) | ||
return nil | ||
} | ||
zoneSegs := strings.Split(obj["zone"].(string), "/") | ||
zone := zoneSegs[len(zoneSegs)-1] | ||
deleteTemplate = strings.ReplaceAll(deleteTemplate, "{{zone}}", zone) | ||
|
||
deleteUrl, err := replaceVars(d, config, deleteTemplate) | ||
if err != nil { | ||
log.Printf("[INFO] error preparing delete url: %s", err) | ||
return nil | ||
} | ||
deleteUrl = deleteUrl + name | ||
// Don't wait on operations as we may have a lot to delete | ||
_, err = sendRequest(config, "DELETE", config.Project, deleteUrl, nil) | ||
if err != nil { | ||
log.Printf("[INFO] Error deleting for url %s : %s", deleteUrl, err) | ||
} else { | ||
log.Printf("[INFO] Sent delete request for %s resource: %s", resourceName, name) | ||
} | ||
} | ||
|
||
if nonPrefixCount > 0 { | ||
log.Printf("[INFO] %d items without tf_test prefix remain.", nonPrefixCount) | ||
} | ||
|
||
return nil | ||
} |
144 changes: 144 additions & 0 deletions
144
google/resource_compute_network_endpoint_group_sweeper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// This file is automatically generated by Magic Modules and manual | ||
// changes will be clobbered when the file is regenerated. | ||
// | ||
// Please read more about how to change this file in | ||
// .github/CONTRIBUTING.md. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
package google | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("ComputeNetworkEndpointGroup", &resource.Sweeper{ | ||
Name: "ComputeNetworkEndpointGroup", | ||
F: testSweepComputeNetworkEndpointGroup, | ||
}) | ||
} | ||
|
||
// At the time of writing, the CI only passes us-central1 as the region | ||
func testSweepComputeNetworkEndpointGroup(region string) error { | ||
resourceName := "ComputeNetworkEndpointGroup" | ||
log.Printf("[INFO] Sweeping %s", resourceName) | ||
|
||
config, err := sharedConfigForRegion(region) | ||
if err != nil { | ||
log.Printf("[INFO] error getting shared config for region: %s", err) | ||
return err | ||
} | ||
|
||
err = config.LoadAndValidate(context.Background()) | ||
if err != nil { | ||
log.Printf("[INFO] error loading: %s", err) | ||
return err | ||
} | ||
|
||
listTemplate := strings.Split("https://www.googleapis.com/compute/v1/projects/{{project}}/aggregated/networkEndpointGroups", "?")[0] | ||
|
||
d := &ResourceDataMock{ | ||
FieldsInSchema: map[string]interface{}{ | ||
"project": config.Project, | ||
"region": region, | ||
"location": region, | ||
"zone": "-", | ||
}, | ||
} | ||
|
||
listUrl, err := replaceVars(d, config, listTemplate) | ||
if err != nil { | ||
log.Printf("[INFO] error preparing sweeper list url: %s", err) | ||
return nil | ||
} | ||
|
||
if strings.Count(listUrl, "//") > 1 { | ||
log.Printf("[INFO] Invalid list url for %s sweeper: %s", resourceName, listUrl) | ||
return nil | ||
} | ||
|
||
res, err := sendRequest(config, "GET", config.Project, listUrl, nil) | ||
if err != nil { | ||
log.Printf("[INFO] Unable to list %s: %s", resourceName, err) | ||
return nil | ||
} | ||
|
||
resourceList, ok := res["items"] | ||
if !ok { | ||
log.Printf("[INFO] Nothing found in response.") | ||
return nil | ||
} | ||
var rl []interface{} | ||
zones := resourceList.(map[string]interface{}) | ||
// Loop through every zone in the list response | ||
for _, zonesValue := range zones { | ||
zone := zonesValue.(map[string]interface{}) | ||
for k, v := range zone { | ||
// Zone map either has resources or a warning stating there were no resources found in the zone | ||
if k != "warning" { | ||
resourcesInZone := v.([]interface{}) | ||
rl = append(rl, resourcesInZone...) | ||
} | ||
} | ||
} | ||
|
||
log.Printf("[INFO] Found %d items in %s list response.", len(rl), resourceName) | ||
// items who don't match the tf-test prefix | ||
nonPrefixCount := 0 | ||
for _, ri := range rl { | ||
obj := ri.(map[string]interface{}) | ||
if obj["name"] == nil { | ||
log.Printf("[INFO] %s resource name was nil", resourceName) | ||
return nil | ||
} | ||
|
||
nameSegs := strings.Split(obj["name"].(string), "/") | ||
name := nameSegs[len(nameSegs)-1] | ||
|
||
// Only sweep resources with the test prefix | ||
if !strings.HasPrefix(name, "tf-test") { | ||
nonPrefixCount++ | ||
continue | ||
} | ||
deleteTemplate := "https://www.googleapis.com/compute/v1/projects/{{project}}/zones/{{zone}}/networkEndpointGroups/{{name}}" | ||
|
||
if obj["zone"] == nil { | ||
log.Printf("[INFO] %s resource zone was nil", resourceName) | ||
return nil | ||
} | ||
zoneSegs := strings.Split(obj["zone"].(string), "/") | ||
zone := zoneSegs[len(zoneSegs)-1] | ||
deleteTemplate = strings.ReplaceAll(deleteTemplate, "{{zone}}", zone) | ||
|
||
deleteUrl, err := replaceVars(d, config, deleteTemplate) | ||
if err != nil { | ||
log.Printf("[INFO] error preparing delete url: %s", err) | ||
return nil | ||
} | ||
deleteUrl = deleteUrl + name | ||
// Don't wait on operations as we may have a lot to delete | ||
_, err = sendRequest(config, "DELETE", config.Project, deleteUrl, nil) | ||
if err != nil { | ||
log.Printf("[INFO] Error deleting for url %s : %s", deleteUrl, err) | ||
} else { | ||
log.Printf("[INFO] Sent delete request for %s resource: %s", resourceName, name) | ||
} | ||
} | ||
|
||
if nonPrefixCount > 0 { | ||
log.Printf("[INFO] %d items without tf_test prefix remain.", nonPrefixCount) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.