Skip to content

Commit eac8289

Browse files
committed
Adds google_storage_anywhere_cache resource
1 parent aa0365e commit eac8289

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2025 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
---
15+
name: 'AnywhereCache'
16+
kind: 'storage#anywhereCache'
17+
description: |
18+
The Google Cloud Storage (GCS) Anywhere Cache feature allows users to
19+
create SSD backed zonal read cache for their buckets. These zonal
20+
caches are co-located with the customers compute engines to provide
21+
cost efficiency.
22+
base_url: 'b/{{bucket}}/anywhereCaches'
23+
self_link: 'b/{{bucket}}/anywhereCaches/{{anywhere_cache_id}}'
24+
create_url: 'b/{{bucket}}/anywhereCaches/'
25+
delete_url: 'b/{{bucket}}/anywhereCaches/{{anywhere_cache_id}}/disable'
26+
update_verb: 'PATCH'
27+
delete_verb: 'POST'
28+
import_format: ['b/{{bucket}}/anywhereCaches/{{anywhere_cache_id}}']
29+
id_format: '{{bucket}}/{{anywhere_cache_id}}'
30+
identity:
31+
- bucket
32+
- anywhereCacheId
33+
custom_code:
34+
custom_create: templates/terraform/custom_create/storage_anywhere_cache_custom_create.go.tmpl
35+
autogen_async: false
36+
timeouts:
37+
insert_minutes: 240
38+
update_minutes: 240
39+
delete_minutes: 20
40+
async:
41+
type: 'OpAsync'
42+
actions:
43+
- create
44+
- update
45+
operation:
46+
full_url: 'selfLink'
47+
result:
48+
resource_inside_response: true
49+
parameters:
50+
- name: 'bucket'
51+
type: ResourceRef
52+
required: true
53+
immutable: true
54+
url_param_only: true
55+
resource: 'Bucket'
56+
imports: 'name'
57+
properties:
58+
- name: 'zone'
59+
type: String
60+
required: true
61+
immutable: true
62+
description: The zone in which the cache instance needs to be created. For example, `us-central1-a.`
63+
- name: 'admissionPolicy'
64+
type: Enum
65+
enum_values:
66+
- 'admit-on-first-miss'
67+
- 'admit-on-second-miss'
68+
default_value: 'admit-on-first-miss'
69+
description: The cache admission policy dictates whether a block should be inserted upon a cache miss.
70+
- name: 'ttl'
71+
type: String
72+
default_value: '86400s'
73+
description: The TTL of all cache entries in whole seconds. e.g., "7200s". It defaults to `86400s`
74+
- name: 'anywhereCacheId'
75+
type: String
76+
output: true
77+
description: The ID of the Anywhere cache instance.
78+
- name: 'createTime'
79+
type: Time
80+
output: true
81+
description: The creation time of the cache instance in RFC 3339 format.
82+
- name: 'updateTime'
83+
type: Time
84+
output: true
85+
description: The modification time of the cache instance metadata in RFC 3339 format.
86+
- name: 'id'
87+
type: String
88+
output: true
89+
description: The ID of the resource, it is in format of `{bucket_name}/{anywhere_cache_id}`
90+
- name: 'pendingUpdate'
91+
type: Boolean
92+
output: true
93+
description: True if the cache instance has an active Update long-running operation.
94+
- name: 'state'
95+
type: String
96+
output: true
97+
description: The current state of the cache instance.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
2+
if err != nil {
3+
return err
4+
}
5+
6+
obj := make(map[string]interface{})
7+
admissionPolicyProp, err := expandStorageAnywhereCacheAdmissionPolicy(d.Get("admission_policy"), d, config)
8+
if err != nil {
9+
return err
10+
} else if v, ok := d.GetOkExists("admission_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(admissionPolicyProp)) && (ok || !reflect.DeepEqual(v, admissionPolicyProp)) {
11+
obj["admissionPolicy"] = admissionPolicyProp
12+
}
13+
ttlProp, err := expandStorageAnywhereCacheTtl(d.Get("ttl"), d, config)
14+
if err != nil {
15+
return err
16+
} else if v, ok := d.GetOkExists("ttl"); !tpgresource.IsEmptyValue(reflect.ValueOf(ttlProp)) && (ok || !reflect.DeepEqual(v, ttlProp)) {
17+
obj["ttl"] = ttlProp
18+
}
19+
zoneProp, err := expandStorageAnywhereCacheZone(d.Get("zone"), d, config)
20+
if err != nil {
21+
return err
22+
} else if v, ok := d.GetOkExists("zone"); !tpgresource.IsEmptyValue(reflect.ValueOf(zoneProp)) && (ok || !reflect.DeepEqual(v, zoneProp)) {
23+
obj["zone"] = zoneProp
24+
}
25+
26+
url, err := tpgresource.ReplaceVars(d, config, "{{"{{StorageBasePath}}b/{{bucket}}/anywhereCaches/"}}")
27+
if err != nil {
28+
return err
29+
}
30+
31+
log.Printf("[DEBUG] Creating new AnywhereCache: %#v", obj)
32+
billingProject := ""
33+
34+
// err == nil indicates that the billing_project value was found
35+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
36+
billingProject = bp
37+
}
38+
39+
headers := make(http.Header)
40+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
41+
Config: config,
42+
Method: "POST",
43+
Project: billingProject,
44+
RawURL: url,
45+
UserAgent: userAgent,
46+
Body: obj,
47+
Timeout: d.Timeout(schema.TimeoutCreate),
48+
Headers: headers,
49+
})
50+
if err != nil {
51+
return fmt.Errorf("Error creating AnywhereCache: %s", err)
52+
}
53+
54+
// Store the ID now
55+
id, err := tpgresource.ReplaceVars(d, config, "{{"b/{{bucket}}/anywhereCaches/{{anywhere_cache_id}}"}}")
56+
if err != nil {
57+
return fmt.Errorf("Error constructing id: %s", err)
58+
}
59+
d.SetId(id)
60+
61+
// Use the resource in the operation response to populate
62+
// identity fields and d.Id() before read
63+
var opRes map[string]interface{}
64+
err = StorageOperationWaitTimeWithResponse(
65+
config, res, &opRes, "Creating AnywhereCache", userAgent,
66+
d.Timeout(schema.TimeoutCreate))
67+
if err != nil {
68+
// The resource didn't actually create
69+
d.SetId("")
70+
71+
return fmt.Errorf("Error waiting to create AnywhereCache: %s", err)
72+
}
73+
74+
nameVal, ok := opRes["name"].(string)
75+
if !ok {
76+
return fmt.Errorf("opRes['name'] is not a string: %v", opRes["name"])
77+
}
78+
79+
nameParts := strings.Split(nameVal, "/")
80+
if len(nameParts) != 6 || nameParts[0] != "projects" || nameParts[2] != "buckets" || nameParts[4] != "anywhereCaches" {
81+
return fmt.Errorf("error parsing the anywhereCacheId from %s", nameVal)
82+
}
83+
84+
anywhereCacheID := nameParts[5]
85+
if err := d.Set("anywhere_cache_id", anywhereCacheID); err != nil {
86+
return err
87+
}
88+
89+
// This may have caused the ID to update - update it if so.
90+
id, err = tpgresource.ReplaceVars(d, config, "{{"b/{{bucket}}/anywhereCaches/{{anywhere_cache_id}}"}}")
91+
if err != nil {
92+
return fmt.Errorf("Error constructing id: %s", err)
93+
}
94+
d.SetId(id)
95+
96+
log.Printf("[DEBUG] Finished creating AnywhereCache %q: %#v", d.Id(), res)
97+
98+
return resourceStorageAnywhereCacheRead(d, meta)

0 commit comments

Comments
 (0)