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 +}