Skip to content

Commit

Permalink
instance template sweeper (#4718)
Browse files Browse the repository at this point in the history
  • Loading branch information
megan07 authored Apr 20, 2021
1 parent 381401f commit 3439cc6
Showing 1 changed file with 71 additions and 0 deletions.
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
}

0 comments on commit 3439cc6

Please sign in to comment.