forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sweeper for storage pool resources. (GoogleCloudPlatform#11571)
- Loading branch information
1 parent
e81906f
commit c690ab7
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
mmv1/third_party/terraform/services/compute/resource_compute_storage_pool_sweeper.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,96 @@ | ||
package compute | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-provider-google/google/sweeper" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
// This will sweep GCE Storage Pool resources | ||
func init() { | ||
sweeper.AddTestSweepers("ComputeStoragePool", testSweepStoragePool) | ||
} | ||
|
||
// At the time of writing, the CI only passes us-central1 as the region | ||
func testSweepStoragePool(region string) error { | ||
resourceName := "ComputeStoragePool" | ||
log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) | ||
|
||
config, err := sweeper.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 | ||
} | ||
|
||
zones := []string{"us-central1-a", "us-central1-b", "us-central1-c", "us-central1-f", "us-east1-b", "us-east1-c", "us-east1-d", "us-west1-a", "us-west1-b", "us-west1-c"} | ||
for _, zone := range zones { | ||
servicesUrl := "https://compute.googleapis.com/compute/v1/projects/" + config.Project + "/zones/" + zone + "/storagePools" | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: config.Project, | ||
RawURL: servicesUrl, | ||
UserAgent: config.UserAgent, | ||
}) | ||
if err != nil { | ||
log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", servicesUrl, err) | ||
return nil | ||
} | ||
|
||
resourceList, ok := res["items"] | ||
if !ok { | ||
log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") | ||
return nil | ||
} | ||
|
||
rl := resourceList.([]interface{}) | ||
|
||
log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) | ||
// Count items that weren't sweeped. | ||
nonPrefixCount := 0 | ||
for _, ri := range rl { | ||
obj := ri.(map[string]interface{}) | ||
if obj["id"] == nil { | ||
log.Printf("[INFO][SWEEPER_LOG] %s resource id was nil", resourceName) | ||
return nil | ||
} | ||
|
||
id := obj["name"].(string) | ||
// Increment count and skip if resource is not sweepable. | ||
if !sweeper.IsSweepableTestResource(id) { | ||
nonPrefixCount++ | ||
continue | ||
} | ||
|
||
deleteUrl := servicesUrl + "/" + id | ||
// Don't wait on operations as we may have a lot to delete | ||
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "DELETE", | ||
Project: config.Project, | ||
RawURL: deleteUrl, | ||
UserAgent: config.UserAgent, | ||
}) | ||
if err != nil { | ||
log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) | ||
} else { | ||
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, id) | ||
} | ||
} | ||
|
||
if nonPrefixCount > 0 { | ||
log.Printf("[INFO][SWEEPER_LOG] %d items without tf-test prefix remain for zone %s", nonPrefixCount, zone) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} |