Skip to content

Commit 172bde7

Browse files
feat(storage-control): add Anywhere Cache samples with 2026 copyright
This commit adds 7 code samples for managing Anywhere Cache using the Storage Control API, placed in the `samples/handwritten` directory. Each file features a 2026 copyright header. Samples included: - Create Anywhere Cache - Get Anywhere Cache - List Anywhere Caches - Update Anywhere Cache - Pause Anywhere Cache - Resume Anywhere Cache - Disable Anywhere Cache A corresponding test suite is added in `tests/system` to verify the samples using mocks. Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
1 parent 0ce8e1e commit 172bde7

8 files changed

+437
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_create_anywhere_cache]
16+
def create_anywhere_cache(bucket_name: str, zone: str):
17+
"""
18+
Creates an Anywhere Cache instance.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# zone = "us-central1-a"
22+
23+
from google.cloud import storage_control_v2
24+
25+
client = storage_control_v2.StorageControlClient()
26+
27+
# The resource name of the bucket to which this Anywhere Cache belongs.
28+
parent = f"projects/_/buckets/{bucket_name}"
29+
30+
anywhere_cache = storage_control_v2.AnywhereCache(
31+
zone=zone,
32+
)
33+
34+
request = storage_control_v2.CreateAnywhereCacheRequest(
35+
parent=parent,
36+
anywhere_cache=anywhere_cache,
37+
)
38+
39+
operation = client.create_anywhere_cache(request=request)
40+
41+
print(f"Waiting for operation {operation.operation.name} to complete...")
42+
response = operation.result()
43+
44+
print(f"Anywhere Cache created: {response.name}")
45+
return response
46+
47+
48+
# [END storage_control_create_anywhere_cache]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_disable_anywhere_cache]
16+
def disable_anywhere_cache(bucket_name: str, anywhere_cache_id: str):
17+
"""
18+
Disables an Anywhere Cache instance.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# anywhere_cache_id = "your-anywhere-cache-id"
22+
23+
from google.cloud import storage_control_v2
24+
25+
client = storage_control_v2.StorageControlClient()
26+
27+
# The resource name of the Anywhere Cache instance.
28+
# Format: projects/{project}/buckets/{bucket}/anywhereCaches/{anywhere_cache}
29+
name = f"projects/_/buckets/{bucket_name}/anywhereCaches/{anywhere_cache_id}"
30+
31+
request = storage_control_v2.DisableAnywhereCacheRequest(
32+
name=name,
33+
)
34+
35+
response = client.disable_anywhere_cache(request=request)
36+
37+
print(f"Anywhere Cache disabled: {response.name}")
38+
print(f"Anywhere Cache state: {response.state}")
39+
return response
40+
41+
42+
# [END storage_control_disable_anywhere_cache]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_get_anywhere_cache]
16+
def get_anywhere_cache(bucket_name: str, anywhere_cache_id: str):
17+
"""
18+
Gets an Anywhere Cache instance.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# anywhere_cache_id = "your-anywhere-cache-id"
22+
23+
from google.cloud import storage_control_v2
24+
25+
client = storage_control_v2.StorageControlClient()
26+
27+
# The resource name of the Anywhere Cache instance.
28+
# Format: projects/{project}/buckets/{bucket}/anywhereCaches/{anywhere_cache}
29+
name = f"projects/_/buckets/{bucket_name}/anywhereCaches/{anywhere_cache_id}"
30+
31+
request = storage_control_v2.GetAnywhereCacheRequest(
32+
name=name,
33+
)
34+
35+
response = client.get_anywhere_cache(request=request)
36+
37+
print(f"Anywhere Cache name: {response.name}")
38+
print(f"Anywhere Cache state: {response.state}")
39+
return response
40+
41+
42+
# [END storage_control_get_anywhere_cache]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_list_anywhere_caches]
16+
def list_anywhere_caches(bucket_name: str):
17+
"""
18+
Lists Anywhere Cache instances for a given bucket.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
22+
from google.cloud import storage_control_v2
23+
24+
client = storage_control_v2.StorageControlClient()
25+
26+
# The bucket to which this Anywhere Cache belongs.
27+
# Format: projects/{project}/buckets/{bucket}
28+
parent = f"projects/_/buckets/{bucket_name}"
29+
30+
request = storage_control_v2.ListAnywhereCachesRequest(
31+
parent=parent,
32+
)
33+
34+
page_result = client.list_anywhere_caches(request=request)
35+
36+
for anywhere_cache in page_result:
37+
print(f"Anywhere Cache name: {anywhere_cache.name}")
38+
39+
return page_result
40+
41+
42+
# [END storage_control_list_anywhere_caches]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_pause_anywhere_cache]
16+
def pause_anywhere_cache(bucket_name: str, anywhere_cache_id: str):
17+
"""
18+
Pauses an Anywhere Cache instance.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# anywhere_cache_id = "your-anywhere-cache-id"
22+
23+
from google.cloud import storage_control_v2
24+
25+
client = storage_control_v2.StorageControlClient()
26+
27+
# The resource name of the Anywhere Cache instance.
28+
# Format: projects/{project}/buckets/{bucket}/anywhereCaches/{anywhere_cache}
29+
name = f"projects/_/buckets/{bucket_name}/anywhereCaches/{anywhere_cache_id}"
30+
31+
request = storage_control_v2.PauseAnywhereCacheRequest(
32+
name=name,
33+
)
34+
35+
response = client.pause_anywhere_cache(request=request)
36+
37+
print(f"Anywhere Cache paused: {response.name}")
38+
print(f"Anywhere Cache state: {response.state}")
39+
return response
40+
41+
42+
# [END storage_control_pause_anywhere_cache]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_resume_anywhere_cache]
16+
def resume_anywhere_cache(bucket_name: str, anywhere_cache_id: str):
17+
"""
18+
Resumes a disabled or paused Anywhere Cache instance.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# anywhere_cache_id = "your-anywhere-cache-id"
22+
23+
from google.cloud import storage_control_v2
24+
25+
client = storage_control_v2.StorageControlClient()
26+
27+
# The resource name of the Anywhere Cache instance.
28+
# Format: projects/{project}/buckets/{bucket}/anywhereCaches/{anywhere_cache}
29+
name = f"projects/_/buckets/{bucket_name}/anywhereCaches/{anywhere_cache_id}"
30+
31+
request = storage_control_v2.ResumeAnywhereCacheRequest(
32+
name=name,
33+
)
34+
35+
response = client.resume_anywhere_cache(request=request)
36+
37+
print(f"Anywhere Cache resumed: {response.name}")
38+
print(f"Anywhere Cache state: {response.state}")
39+
return response
40+
41+
42+
# [END storage_control_resume_anywhere_cache]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_update_anywhere_cache]
16+
def update_anywhere_cache(bucket_name: str, anywhere_cache_id: str, admission_policy: str):
17+
"""
18+
Updates an Anywhere Cache instance. Mutable fields include `ttl` and `admission_policy`.
19+
"""
20+
# bucket_name = "your-bucket-name"
21+
# anywhere_cache_id = "your-anywhere-cache-id"
22+
# admission_policy = "admit-on-second-miss"
23+
24+
from google.cloud import storage_control_v2
25+
from google.protobuf import field_mask_pb2
26+
27+
client = storage_control_v2.StorageControlClient()
28+
29+
# The resource name of the Anywhere Cache instance to be updated.
30+
# Format: projects/{project}/buckets/{bucket}/anywhereCaches/{anywhere_cache}
31+
name = f"projects/_/buckets/{bucket_name}/anywhereCaches/{anywhere_cache_id}"
32+
33+
anywhere_cache = storage_control_v2.AnywhereCache(
34+
name=name,
35+
admission_policy=admission_policy,
36+
)
37+
38+
update_mask = field_mask_pb2.FieldMask(paths=["admission_policy"])
39+
40+
request = storage_control_v2.UpdateAnywhereCacheRequest(
41+
anywhere_cache=anywhere_cache,
42+
update_mask=update_mask,
43+
)
44+
45+
operation = client.update_anywhere_cache(request=request)
46+
47+
print(f"Waiting for operation {operation.operation.name} to complete...")
48+
response = operation.result()
49+
50+
print(f"Anywhere Cache updated: {response.name}")
51+
print(f"Admission policy: {response.admission_policy}")
52+
return response
53+
54+
55+
# [END storage_control_update_anywhere_cache]

0 commit comments

Comments
 (0)