Skip to content

Commit

Permalink
Handle aggregatedList case for generated sweepers (#5364)
Browse files Browse the repository at this point in the history
* Use more markdown for Bug

* Consistently use sentences for each bullet

* Rewrite bug reproduction block

* Allow domain mapping to succeed if DNS is pending

Signed-off-by: Modular Magician <[email protected]>

* Updated google_folder.html (#4149)

* Updated google_folder.html

The page in the first example shows that you should use organization_id with value of 1234567. In the Import example, it's not clear whether organization_id is user, or folder_id is used. API call behind this import command is only accepting folder_id (can be checked when setting TF_LOG to trace and viewing the API call)

* Update website/docs/r/google_folder.html.markdown

Co-Authored-By: Dana Hoffman <[email protected]>

Co-authored-by: Dana Hoffman <[email protected]>

* add google_kms_secret_ciphertext resource, deprecate datasource (#5314)

Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Dana Hoffman <[email protected]>

* Allow add/removing Bigtable clusters (#5318)

Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Riley Karson <[email protected]>

* Add bootstrapped test networks for service networking tests (#5316)

Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: emily <[email protected]>

* Update CHANGELOG.md

* fix docs for google_bigquery_default_service_account (#5329)

Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Martin Nowak <[email protected]>

* Nil return for absent Bigtable resources (#5331)

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 (#5335)

Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: emily <[email protected]>

* Add mutex to peering create (#5338)

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 (#5351)

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 (#5353)

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 (#5355)

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: Josh Soref <[email protected]>
Co-authored-by: Chris Stephens <[email protected]>
Co-authored-by: Petar Marinkovic <[email protected]>
Co-authored-by: Dana Hoffman <[email protected]>
Co-authored-by: megan07 <[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: Cameron Thornton <[email protected]>
  • Loading branch information
12 people authored Jan 9, 2020
1 parent 116973d commit fe67bae
Show file tree
Hide file tree
Showing 6 changed files with 723 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/2941.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
144 changes: 144 additions & 0 deletions google/resource_compute_autoscaler_sweeper_test.go
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.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 google/resource_compute_network_endpoint_group_sweeper_test.go
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.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
}
Loading

0 comments on commit fe67bae

Please sign in to comment.