1
+ // Copyright (c) HashiCorp, Inc.
2
+ // SPDX-License-Identifier: MPL-2.0
1
3
package storage
2
4
3
5
import (
4
6
"context"
5
7
"fmt"
6
8
"log"
9
+ "strings"
7
10
8
11
"github.com/hashicorp/terraform-provider-google/google/sweeper"
9
12
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
@@ -13,6 +16,60 @@ func init() {
13
16
sweeper .AddTestSweepers ("StorageBucket" , testSweepStorageBucket )
14
17
}
15
18
19
+ func disableAnywhereCacheIfAny (config * transport_tpg.Config , bucket string ) bool {
20
+ // Define the cache list URL
21
+ cacheListUrl := fmt .Sprintf ("https://storage.googleapis.com/storage/v1/b/%s/anywhereCaches/" , bucket )
22
+
23
+ // Send request to get resource list
24
+ res , err := transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
25
+ Config : config ,
26
+ Method : "GET" ,
27
+ Project : config .Project ,
28
+ RawURL : cacheListUrl ,
29
+ UserAgent : config .UserAgent ,
30
+ })
31
+ if err != nil {
32
+ log .Printf ("[INFO][SWEEPER_LOG] Error fetching caches from url %s: %s" , cacheListUrl , err )
33
+ return false
34
+ }
35
+
36
+ resourceList , ok := res ["items" ]
37
+ if ! ok {
38
+ log .Printf ("[INFO][SWEEPER_LOG] No caches found for %s." , bucket )
39
+ return true
40
+ }
41
+
42
+ rl := resourceList .([]interface {})
43
+
44
+ // Iterate over each object in the resource list
45
+ for _ , item := range rl {
46
+ // Ensure the item is a map
47
+ obj := item .(map [string ]interface {})
48
+
49
+ // Check the state of the object
50
+ state := obj ["state" ].(string )
51
+ if state != "running" && state != "paused" {
52
+ continue
53
+ }
54
+
55
+ // Disable the cache if state is running or paused
56
+ disableUrl := fmt .Sprintf ("https://storage.googleapis.com/storage/v1/b/%s/anywhereCaches/%s/disable" , obj ["bucket" ], obj ["anywhereCacheId" ])
57
+ _ , err := transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
58
+ Config : config ,
59
+ Method : "POST" ,
60
+ Project : config .Project ,
61
+ RawURL : disableUrl ,
62
+ UserAgent : config .UserAgent ,
63
+ })
64
+ if err != nil {
65
+ log .Printf ("[INFO][SWEEPER_LOG] Error disabling cache: %s" , err )
66
+ }
67
+ }
68
+
69
+ // Return true if no items were found, otherwise false
70
+ return len (rl ) == 0
71
+ }
72
+
16
73
// At the time of writing, the CI only passes us-central1 as the region
17
74
func testSweepStorageBucket (region string ) error {
18
75
resourceName := "StorageBucket"
@@ -63,6 +120,7 @@ func testSweepStorageBucket(region string) error {
63
120
log .Printf ("[INFO][SWEEPER_LOG] Found %d items in %s list response." , len (rl ), resourceName )
64
121
// Count items that weren't sweeped.
65
122
nonPrefixCount := 0
123
+ bucketWithCaches := 0
66
124
for _ , ri := range rl {
67
125
obj := ri .(map [string ]interface {})
68
126
@@ -73,6 +131,15 @@ func testSweepStorageBucket(region string) error {
73
131
continue
74
132
}
75
133
134
+ if strings .HasPrefix (id , "anywhere-cache-bucket" ) {
135
+ readyToDeleteBucket := disableAnywhereCacheIfAny (config , id )
136
+ if ! readyToDeleteBucket {
137
+ log .Printf ("[INFO][SWEEPER_LOG] Bucket %s has anywhere caches, requests have been made to backend to disable them, The bucket would be automatically deleted once caches are deleted from bucket" , id )
138
+ bucketWithCaches ++
139
+ continue
140
+ }
141
+ }
142
+
76
143
deleteUrl := fmt .Sprintf ("https://storage.googleapis.com/storage/v1/b/%s" , id )
77
144
_ , err = transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
78
145
Config : config ,
@@ -89,7 +156,10 @@ func testSweepStorageBucket(region string) error {
89
156
}
90
157
91
158
if nonPrefixCount > 0 {
92
- log .Printf ("[INFO][SWEEPER_LOG] %d items without tf-test prefix remain." , nonPrefixCount )
159
+ log .Printf ("[INFO][SWEEPER_LOG] %d items without valid test prefixes remain." , nonPrefixCount )
160
+ }
161
+ if bucketWithCaches > 0 {
162
+ log .Printf ("[INFO][SWEEPER_LOG] %d items with valid test prefixes remain, and can not be deleted due to their underlying resources" , bucketWithCaches )
93
163
}
94
164
95
165
return nil
0 commit comments