|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// ---------------------------------------------------------------------------- |
| 5 | +// |
| 6 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 7 | +// |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | +// |
| 10 | +// This file is automatically generated by Magic Modules and manual |
| 11 | +// changes will be clobbered when the file is regenerated. |
| 12 | +// |
| 13 | +// Please read more about how to change this file in |
| 14 | +// .github/CONTRIBUTING.md. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +package firestore |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "log" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
| 27 | + "github.com/hashicorp/terraform-provider-google/google/sweeper" |
| 28 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 29 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + sweeper.AddTestSweepers("FirestoreIndex", testSweepFirestoreIndex) |
| 34 | +} |
| 35 | + |
| 36 | +// At the time of writing, the CI only passes us-central1 as the region |
| 37 | +func testSweepFirestoreIndex(region string) error { |
| 38 | + resourceName := "FirestoreIndex" |
| 39 | + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) |
| 40 | + |
| 41 | + config, err := sweeper.SharedConfigForRegion(region) |
| 42 | + if err != nil { |
| 43 | + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + err = config.LoadAndValidate(context.Background()) |
| 48 | + if err != nil { |
| 49 | + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + t := &testing.T{} |
| 54 | + billingId := envvar.GetTestBillingAccountFromEnv(t) |
| 55 | + |
| 56 | + // Setup variables to replace in list template |
| 57 | + d := &tpgresource.ResourceDataMock{ |
| 58 | + FieldsInSchema: map[string]interface{}{ |
| 59 | + "project": config.Project, |
| 60 | + "region": region, |
| 61 | + "location": region, |
| 62 | + "zone": "-", |
| 63 | + "billing_account": billingId, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + listTemplate := strings.Split("https://firestore.googleapis.com/v1/projects/{{project}}/databases/{{database}}/collectionGroups/{{collection}}/indexes", "?")[0] |
| 68 | + listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) |
| 69 | + if err != nil { |
| 70 | + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 75 | + Config: config, |
| 76 | + Method: "GET", |
| 77 | + Project: config.Project, |
| 78 | + RawURL: listUrl, |
| 79 | + UserAgent: config.UserAgent, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + resourceList, ok := res["indices"] |
| 87 | + if !ok { |
| 88 | + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + rl := resourceList.([]interface{}) |
| 93 | + |
| 94 | + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) |
| 95 | + // Keep count of items that aren't sweepable for logging. |
| 96 | + nonPrefixCount := 0 |
| 97 | + for _, ri := range rl { |
| 98 | + obj := ri.(map[string]interface{}) |
| 99 | + if obj["name"] == nil { |
| 100 | + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) |
| 105 | + // Skip resources that shouldn't be sweeped |
| 106 | + if !sweeper.IsSweepableTestResource(name) { |
| 107 | + nonPrefixCount++ |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + deleteTemplate := "https://firestore.googleapis.com/v1/{{name}}" |
| 112 | + deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate) |
| 113 | + if err != nil { |
| 114 | + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) |
| 115 | + return nil |
| 116 | + } |
| 117 | + deleteUrl = deleteUrl + name |
| 118 | + |
| 119 | + // Don't wait on operations as we may have a lot to delete |
| 120 | + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 121 | + Config: config, |
| 122 | + Method: "DELETE", |
| 123 | + Project: config.Project, |
| 124 | + RawURL: deleteUrl, |
| 125 | + UserAgent: config.UserAgent, |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) |
| 129 | + } else { |
| 130 | + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if nonPrefixCount > 0 { |
| 135 | + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments