Skip to content

Commit

Permalink
extend compute disk sweeper to sweep from all currently utilized zones (
Browse files Browse the repository at this point in the history
#6905) (#13165)

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Dec 2, 2022
1 parent 53295dc commit 8be39ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .changelog/6905.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
75 changes: 40 additions & 35 deletions google/resource_compute_disk_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,55 @@ func testSweepDisk(region string) error {
log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err)
return err
}
servicesUrl := "https://compute.googleapis.com/compute/v1/projects/" + config.Project + "/zones/" + getTestZoneFromEnv() + "/disks"
res, err := sendRequest(config, "GET", config.Project, servicesUrl, config.userAgent, nil)
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{})
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 + "/disks"
res, err := sendRequest(config, "GET", config.Project, servicesUrl, config.userAgent, nil)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", servicesUrl, err)
return nil
}

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)
resourceList, ok := res["items"]
if !ok {
log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.")
return nil
}

id := obj["name"].(string)
// Increment count and skip if resource is not sweepable.
if !isSweepableTestResource(id) {
nonPrefixCount++
continue
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 !isSweepableTestResource(id) {
nonPrefixCount++
continue
}

deleteUrl := servicesUrl + "/" + id
// Don't wait on operations as we may have a lot to delete
_, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil)
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)
}
}

deleteUrl := servicesUrl + "/" + id
// Don't wait on operations as we may have a lot to delete
_, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil)
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)
}
}

if nonPrefixCount > 0 {
log.Printf("[INFO][SWEEPER_LOG] %d items without tf-test prefix remain.", nonPrefixCount)
}

return nil
Expand Down

0 comments on commit 8be39ee

Please sign in to comment.