diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance_template_sweeper_test.go b/mmv1/third_party/terraform/resources/resource_compute_instance_template_sweeper_test.go new file mode 100644 index 000000000000..f51c5d3ed3c0 --- /dev/null +++ b/mmv1/third_party/terraform/resources/resource_compute_instance_template_sweeper_test.go @@ -0,0 +1,71 @@ +package google + +import ( + "context" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// This will sweep Compute Instance Templates +func init() { + resource.AddTestSweepers("ComputeInstanceTemplate", &resource.Sweeper{ + Name: "ComputeInstanceTemplate", + F: testSweepComputeInstanceTemplate, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepComputeInstanceTemplate(region string) error { + resourceName := "ComputeInstanceTemplate" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + instanceTemplates, err := config.NewComputeBetaClient(config.userAgent).InstanceTemplates.List(config.Project).Do() + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request instance templates LIST: %s", err) + return nil + } + + numTemplates := len(instanceTemplates.Items) + if numTemplates == 0 { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", numTemplates, resourceName) + // Count items that weren't sweeped. + nonPrefixCount := 0 + for _, instanceTemplate := range instanceTemplates.Items { + // Increment count and skip if resource is not sweepable. + if !isSweepableTestResource(instanceTemplate.Name) { + nonPrefixCount++ + continue + } + + // Don't wait on operations as we may have a lot to delete + _, err := config.NewComputeBetaClient(config.userAgent).InstanceTemplates.Delete(config.Project, instanceTemplate.Name).Do() + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting instance template: %s", instanceTemplate.Name) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, instanceTemplate.Name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items without tf-test prefix remain.", nonPrefixCount) + } + + return nil +}