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