-
Notifications
You must be signed in to change notification settings - Fork 264
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 (#1619)
* Allow domain mapping to succeed if DNS is pending Signed-off-by: Modular Magician <[email protected]> * Update google_folder import description (#1592) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Dana Hoffman <[email protected]> * add google_kms_secret_ciphertext resource, deprecate datasource (#1586) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Dana Hoffman <[email protected]> * Allow add/removing Bigtable clusters (#1589) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]> * Add bootstrapped test networks for service networking tests (#1588) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: emily <[email protected]> * Update CHANGELOG.md * fix docs for google_bigquery_default_service_account (#1596) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Martin Nowak <[email protected]> * Nil return for absent Bigtable resources (#1597) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Brian Hildebrandt <[email protected]> * add lifecycle_config to dataproc_cluster.cluster_config Signed-off-by: Modular Magician <[email protected]> * Add warnings about custom role format for IAM bindings (#1600) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: emily <[email protected]> * Add mutex to peering create (#1601) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: emily <[email protected]> * Handle aggregatedList case for generated sweepers Signed-off-by: Modular Magician <[email protected]> * Add default_if_empty for quic_override (#1611) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]> * Batch errors now indicate how to disable batching Signed-off-by: Modular Magician <[email protected]> * Add default_if_empty to google_compute_router_nat defaults (#1613) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]> * Allow for retries of single requests in a batch on failure (#1615) Signed-off-by: Modular Magician <[email protected]> Co-authored-by: emily <[email protected]> * Handle aggregatedList case for generated sweepers (#2941) Merged PR #2941. Co-authored-by: Chris Stephens <[email protected]> Co-authored-by: Dana Hoffman <[email protected]> Co-authored-by: Riley Karson <[email protected]> Co-authored-by: emily <[email protected]> Co-authored-by: Paddy <[email protected]> Co-authored-by: Martin Nowak <[email protected]> Co-authored-by: Brian Hildebrandt <[email protected]> Co-authored-by: megan07 <[email protected]> Co-authored-by: Cameron Thornton <[email protected]>
- Loading branch information
1 parent
ff9bacd
commit 30f61f5
Showing
6 changed files
with
723 additions
and
0 deletions.
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,3 @@ | ||
```release-note:none | ||
|
||
``` |
144 changes: 144 additions & 0 deletions
144
google-beta/resource_compute_autoscaler_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("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/beta/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/beta/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.Replace(deleteTemplate, "{{zone}}", zone, -1) | ||
|
||
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-beta/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/beta/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/beta/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.Replace(deleteTemplate, "{{zone}}", zone, -1) | ||
|
||
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.