From 30f61f57b1225880eea2ecb4ca98bbf03fec05e8 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 9 Jan 2020 13:01:53 -0800 Subject: [PATCH] Handle aggregatedList case for generated sweepers (#1619) * Allow domain mapping to succeed if DNS is pending Signed-off-by: Modular Magician * Update google_folder import description (#1592) Signed-off-by: Modular Magician Co-authored-by: Dana Hoffman * add google_kms_secret_ciphertext resource, deprecate datasource (#1586) Signed-off-by: Modular Magician Co-authored-by: Dana Hoffman * Allow add/removing Bigtable clusters (#1589) Signed-off-by: Modular Magician Co-authored-by: Riley Karson * Add bootstrapped test networks for service networking tests (#1588) Signed-off-by: Modular Magician Co-authored-by: emily * Update CHANGELOG.md * fix docs for google_bigquery_default_service_account (#1596) Signed-off-by: Modular Magician Co-authored-by: Martin Nowak * Nil return for absent Bigtable resources (#1597) Signed-off-by: Modular Magician Co-authored-by: Brian Hildebrandt * add lifecycle_config to dataproc_cluster.cluster_config Signed-off-by: Modular Magician * Add warnings about custom role format for IAM bindings (#1600) Signed-off-by: Modular Magician Co-authored-by: emily * Add mutex to peering create (#1601) Signed-off-by: Modular Magician Co-authored-by: emily * Handle aggregatedList case for generated sweepers Signed-off-by: Modular Magician * Add default_if_empty for quic_override (#1611) Signed-off-by: Modular Magician Co-authored-by: Riley Karson * Batch errors now indicate how to disable batching Signed-off-by: Modular Magician * Add default_if_empty to google_compute_router_nat defaults (#1613) Signed-off-by: Modular Magician Co-authored-by: Riley Karson * Allow for retries of single requests in a batch on failure (#1615) Signed-off-by: Modular Magician Co-authored-by: emily * Handle aggregatedList case for generated sweepers (#2941) Merged PR #2941. Co-authored-by: Chris Stephens Co-authored-by: Dana Hoffman Co-authored-by: Riley Karson Co-authored-by: emily Co-authored-by: Paddy Co-authored-by: Martin Nowak Co-authored-by: Brian Hildebrandt Co-authored-by: megan07 Co-authored-by: Cameron Thornton --- .changelog/2941.txt | 3 + ...esource_compute_autoscaler_sweeper_test.go | 144 ++++++++++++++++++ ...ute_network_endpoint_group_sweeper_test.go | 144 ++++++++++++++++++ ...esource_compute_node_group_sweeper_test.go | 144 ++++++++++++++++++ ...source_compute_reservation_sweeper_test.go | 144 ++++++++++++++++++ ...ce_compute_target_instance_sweeper_test.go | 144 ++++++++++++++++++ 6 files changed, 723 insertions(+) create mode 100644 .changelog/2941.txt create mode 100644 google-beta/resource_compute_autoscaler_sweeper_test.go create mode 100644 google-beta/resource_compute_network_endpoint_group_sweeper_test.go create mode 100644 google-beta/resource_compute_node_group_sweeper_test.go create mode 100644 google-beta/resource_compute_reservation_sweeper_test.go create mode 100644 google-beta/resource_compute_target_instance_sweeper_test.go diff --git a/.changelog/2941.txt b/.changelog/2941.txt new file mode 100644 index 0000000000..8ec013c069 --- /dev/null +++ b/.changelog/2941.txt @@ -0,0 +1,3 @@ +```release-note:none + +``` diff --git a/google-beta/resource_compute_autoscaler_sweeper_test.go b/google-beta/resource_compute_autoscaler_sweeper_test.go new file mode 100644 index 0000000000..d6a7dc44f4 --- /dev/null +++ b/google-beta/resource_compute_autoscaler_sweeper_test.go @@ -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 +} diff --git a/google-beta/resource_compute_network_endpoint_group_sweeper_test.go b/google-beta/resource_compute_network_endpoint_group_sweeper_test.go new file mode 100644 index 0000000000..f6d727473c --- /dev/null +++ b/google-beta/resource_compute_network_endpoint_group_sweeper_test.go @@ -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 +} diff --git a/google-beta/resource_compute_node_group_sweeper_test.go b/google-beta/resource_compute_node_group_sweeper_test.go new file mode 100644 index 0000000000..e8103ecd4d --- /dev/null +++ b/google-beta/resource_compute_node_group_sweeper_test.go @@ -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("ComputeNodeGroup", &resource.Sweeper{ + Name: "ComputeNodeGroup", + F: testSweepComputeNodeGroup, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepComputeNodeGroup(region string) error { + resourceName := "ComputeNodeGroup" + 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/nodeGroups", "?")[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}}/nodeGroups/{{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 +} diff --git a/google-beta/resource_compute_reservation_sweeper_test.go b/google-beta/resource_compute_reservation_sweeper_test.go new file mode 100644 index 0000000000..bd1c5bb264 --- /dev/null +++ b/google-beta/resource_compute_reservation_sweeper_test.go @@ -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("ComputeReservation", &resource.Sweeper{ + Name: "ComputeReservation", + F: testSweepComputeReservation, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepComputeReservation(region string) error { + resourceName := "ComputeReservation" + 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/reservations", "?")[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}}/reservations/{{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 +} diff --git a/google-beta/resource_compute_target_instance_sweeper_test.go b/google-beta/resource_compute_target_instance_sweeper_test.go new file mode 100644 index 0000000000..3137a04697 --- /dev/null +++ b/google-beta/resource_compute_target_instance_sweeper_test.go @@ -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("ComputeTargetInstance", &resource.Sweeper{ + Name: "ComputeTargetInstance", + F: testSweepComputeTargetInstance, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepComputeTargetInstance(region string) error { + resourceName := "ComputeTargetInstance" + 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/targetInstances", "?")[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}}/targetInstances/{{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 +}