-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
mmv1/third_party/terraform/resources/resource_compute_instance_template_sweeper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |