Skip to content

Commit ce42117

Browse files
committed
adressed comments
1 parent 45d4cd3 commit ce42117

File tree

5 files changed

+98
-59
lines changed

5 files changed

+98
-59
lines changed

mmv1/products/storage/AnywhereCache.yaml

+4-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ examples:
3131
- name: 'storage_anywhere_cache_basic'
3232
primary_resource_id: 'cache'
3333
vars:
34-
bucket_name: 'my-bucket'
34+
bucket_name: 'bucket-name'
35+
test_vars_overrides:
36+
bucket_name: '"anywhere-cache-bucket" + acctest.RandString(t, 10)'
3537
external_providers: ["time"]
3638
identity:
3739
- bucket
@@ -41,13 +43,12 @@ custom_code:
4143
autogen_async: false
4244
timeouts:
4345
insert_minutes: 240
44-
update_minutes: 240
46+
update_minutes: 20
4547
delete_minutes: 20
4648
async:
4749
type: 'OpAsync'
4850
actions:
4951
- create
50-
- update
5152
operation:
5253
full_url: 'selfLink'
5354
result:
@@ -89,10 +90,6 @@ properties:
8990
type: Time
9091
output: true
9192
description: The modification time of the cache instance metadata in RFC 3339 format.
92-
- name: 'id'
93-
type: String
94-
output: true
95-
description: The ID of the resource, it is in format of `{bucket_name}/{anywhere_cache_id}`
9693
- name: 'pendingUpdate'
9794
type: Boolean
9895
output: true

mmv1/templates/terraform/examples/storage_anywhere_cache_basic.tf.tmpl

+5-14
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@ resource "google_storage_bucket" "bucket" {
33
location = "US"
44
}
55

6-
resource "google_storage_bucket_iam_binding" "binding" {
7-
bucket = google_storage_bucket.bucket.name
8-
role = "roles/storage.admin"
9-
members = [
10-
"allUsers",
11-
]
12-
depends_on = [ google_storage_bucket.bucket ]
13-
}
14-
15-
resource "time_sleep" "wait_7200_seconds" {
6+
resource "time_sleep" "destroy_wait_5000_seconds" {
167
depends_on = [google_storage_bucket.bucket]
17-
destroy_duration = "7200s"
8+
destroy_duration = "5000s"
189
}
1910

2011
resource "google_storage_anywhere_cache" "cache" {
21-
bucket = google_storage_bucket.bucket.name
22-
zone = "us-central1-f"
23-
depends_on = [ google_storage_bucket_iam_binding.binding ]
12+
bucket = google_storage_bucket.bucket.name
13+
zone = "us-central1-f"
14+
ttl = "3601s"
2415
}

mmv1/third_party/terraform/services/storage/resource_storage_anywhere_cache_test.go

+17-37
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
package storage_test
44

55
import (
6+
"testing"
67
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
78
"github.com/hashicorp/terraform-provider-google/google/acctest"
8-
"testing"
99
)
1010

11-
func TestAccStorageAnywhereCache_update(t *testing.T) {
11+
func TestAccStorageAnywhereCache_basic(t *testing.T) {
1212
t.Parallel()
1313

1414
context := map[string]interface{}{
15+
"bucket_name": "anywhere-cache-bucket" + acctest.RandString(t, 10),
1516
"random_suffix": acctest.RandString(t, 10),
1617
}
1718

1819
acctest.VcrTest(t, resource.TestCase{
1920
PreCheck: func() { acctest.AccTestPreCheck(t) },
2021
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
21-
CheckDestroy: testAccCheckStorageAnywhereCacheDestroyProducer(t),
2222
ExternalProviders: map[string]resource.ExternalProvider{
2323
"time": {},
2424
},
@@ -48,61 +48,41 @@ func TestAccStorageAnywhereCache_update(t *testing.T) {
4848
func testAccStorageAnywhereCache_full(context map[string]interface{}) string {
4949
return acctest.Nprintf(`
5050
resource "google_storage_bucket" "bucket" {
51-
name = "tf-test-my-bucket%{random_suffix}"
51+
name = "%{bucket_name}"
5252
location = "US"
5353
}
5454
55-
resource "google_storage_bucket_iam_binding" "binding" {
56-
bucket = google_storage_bucket.bucket.name
57-
role = "roles/storage.admin"
58-
members = [
59-
"allUsers",
60-
]
61-
depends_on = [ google_storage_bucket.bucket ]
62-
}
63-
64-
resource "time_sleep" "wait_4000_seconds" {
55+
resource "time_sleep" "destroy_wait_5000_seconds" {
6556
depends_on = [google_storage_bucket.bucket]
66-
destroy_duration = "4000s"
57+
destroy_duration = "5000s"
6758
}
6859
6960
resource "google_storage_anywhere_cache" "cache" {
70-
bucket = google_storage_bucket.bucket.name
71-
zone = "us-central1-f"
72-
admission_policy = "admit-on-first-miss"
73-
ttl = "90000s"
74-
depends_on = [ google_storage_bucket_iam_binding.binding ]
61+
bucket = google_storage_bucket.bucket.name
62+
zone = "us-central1-f"
63+
admission_policy = "admit-on-first-miss"
64+
ttl = "3601s"
7565
}
7666
`, context)
7767
}
7868

7969
func testAccStorageAnywhereCache_update(context map[string]interface{}) string {
8070
return acctest.Nprintf(`
8171
resource "google_storage_bucket" "bucket" {
82-
name = "tf-test-my-bucket%{random_suffix}"
72+
name = "%{bucket_name}"
8373
location = "US"
8474
}
8575
86-
resource "google_storage_bucket_iam_binding" "binding" {
87-
bucket = google_storage_bucket.bucket.name
88-
role = "roles/storage.admin"
89-
members = [
90-
"allUsers",
91-
]
92-
depends_on = [ google_storage_bucket.bucket ]
93-
}
94-
95-
resource "time_sleep" "wait_4000_seconds" {
76+
resource "time_sleep" "destroy_wait_5000_seconds" {
9677
depends_on = [google_storage_bucket.bucket]
97-
destroy_duration = "4000s"
78+
destroy_duration = "5000s"
9879
}
9980
10081
resource "google_storage_anywhere_cache" "cache" {
101-
bucket = google_storage_bucket.bucket.name
102-
zone = "us-central1-f"
103-
admission_policy = "admit-on-second-miss"
104-
ttl = "100000s"
105-
depends_on = [ google_storage_bucket_iam_binding.binding ]
82+
bucket = google_storage_bucket.bucket.name
83+
zone = "us-central1-f"
84+
admission_policy = "admit-on-second-miss"
85+
ttl = "3620s"
10686
}
10787
`, context)
10888
}

mmv1/third_party/terraform/services/storage/resource_storage_bucket_sweeper.go

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
13
package storage
24

35
import (
46
"context"
57
"fmt"
68
"log"
9+
"strings"
710

811
"github.com/hashicorp/terraform-provider-google/google/sweeper"
912
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
@@ -13,6 +16,60 @@ func init() {
1316
sweeper.AddTestSweepers("StorageBucket", testSweepStorageBucket)
1417
}
1518

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+
1673
// At the time of writing, the CI only passes us-central1 as the region
1774
func testSweepStorageBucket(region string) error {
1875
resourceName := "StorageBucket"
@@ -63,6 +120,7 @@ func testSweepStorageBucket(region string) error {
63120
log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName)
64121
// Count items that weren't sweeped.
65122
nonPrefixCount := 0
123+
bucketWithCaches := 0
66124
for _, ri := range rl {
67125
obj := ri.(map[string]interface{})
68126

@@ -73,6 +131,15 @@ func testSweepStorageBucket(region string) error {
73131
continue
74132
}
75133

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+
76143
deleteUrl := fmt.Sprintf("https://storage.googleapis.com/storage/v1/b/%s", id)
77144
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
78145
Config: config,
@@ -89,7 +156,10 @@ func testSweepStorageBucket(region string) error {
89156
}
90157

91158
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)
93163
}
94164

95165
return nil

mmv1/third_party/terraform/sweeper/gcp_sweeper.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var testResourcePrefixes = []string{
2828
"cluster-", // https://github.com/hashicorp/terraform-provider-google/issues/8924
2929
"k8s-fw-", // firewall rules are getting created and not cleaned up by k8 resources using this prefix
3030
"ext-tf-test", // Cloud Tasks Queues created automatically by tests for `google_firebase_extensions_instance`.
31+
"anywhere-cache-bucket", // GCS bucket with anywhere caches on it.
3132
}
3233

3334
// SharedConfigForRegion returns a common config setup needed for the sweeper

0 commit comments

Comments
 (0)