|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | +) |
| 9 | + |
| 10 | +// This will sweep GCE Disk resources |
| 11 | +func init() { |
| 12 | + resource.AddTestSweepers("BigtableInstance", &resource.Sweeper{ |
| 13 | + Name: "BigtableInstance", |
| 14 | + F: testSweepBigtableInstance, |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +// At the time of writing, the CI only passes us-central1 as the region |
| 19 | +// We don't have a way to filter the list by zone, and it's not clear it's worth the |
| 20 | +// effort as we only create within us-central1. |
| 21 | +func testSweepBigtableInstance(region string) error { |
| 22 | + resourceName := "BigtableInstance" |
| 23 | + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) |
| 24 | + |
| 25 | + config, err := sharedConfigForRegion(region) |
| 26 | + if err != nil { |
| 27 | + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + err = config.LoadAndValidate(context.Background()) |
| 32 | + if err != nil { |
| 33 | + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) |
| 34 | + return err |
| 35 | + } |
| 36 | + servicesUrl := "https://bigtableadmin.googleapis.com/v2/projects/" + config.Project + "/instances" |
| 37 | + res, err := sendRequest(config, "GET", config.Project, servicesUrl, config.userAgent, nil) |
| 38 | + if err != nil { |
| 39 | + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", servicesUrl, err) |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + resourceList, ok := res["instances"] |
| 44 | + if !ok { |
| 45 | + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + rl := resourceList.([]interface{}) |
| 50 | + |
| 51 | + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) |
| 52 | + // Count items that weren't sweeped. |
| 53 | + nonPrefixCount := 0 |
| 54 | + for _, ri := range rl { |
| 55 | + obj := ri.(map[string]interface{}) |
| 56 | + if obj["name"] == nil { |
| 57 | + log.Printf("[INFO][SWEEPER_LOG] %s resource id was nil", resourceName) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + id := obj["displayName"].(string) |
| 62 | + // Increment count and skip if resource is not sweepable. |
| 63 | + if !isSweepableTestResource(id) { |
| 64 | + nonPrefixCount++ |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + deleteUrl := servicesUrl + "/" + id |
| 69 | + // Don't wait on operations as we may have a lot to delete |
| 70 | + _, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil) |
| 71 | + if err != nil { |
| 72 | + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) |
| 73 | + } else { |
| 74 | + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, id) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if nonPrefixCount > 0 { |
| 79 | + log.Printf("[INFO][SWEEPER_LOG] %d items without tf-test prefix remain.", nonPrefixCount) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
0 commit comments