Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance template sweeper #8960

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/4718.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:none
```
71 changes: 71 additions & 0 deletions google/resource_compute_instance_template_sweeper_test.go
Original file line number Diff line number Diff line change
@@ -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
}