diff --git a/storagecontrol/create_anywhere_cache.py b/storagecontrol/create_anywhere_cache.py new file mode 100644 index 00000000000..52b21144528 --- /dev/null +++ b/storagecontrol/create_anywhere_cache.py @@ -0,0 +1,56 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_create_anywhere_cache] +from google.cloud import storage_control_v2 + + +def create_anywhere_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone where the anywhere cache will be created + # zone = "us-central1-a" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + project_path = storage_control_client.common_project_path("_") + bucket_path = f"{project_path}/buckets/{bucket_name}" + + anywhere_cache = storage_control_v2.AnywhereCache( + zone=zone, + ) + + request = storage_control_v2.CreateAnywhereCacheRequest( + parent=bucket_path, + anywhere_cache=anywhere_cache, + ) + + # Start a create operation and block until it completes. Real applications + # may want to setup a callback, wait on a coroutine, or poll until it + # completes. + operation = storage_control_client.create_anywhere_cache(request=request) + response = operation.result() + + print(f"Created anywhere cache: {response.name}") + + +# [END storage_control_create_anywhere_cache] + + +if __name__ == "__main__": + create_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/disable_anywhere_cache.py b/storagecontrol/disable_anywhere_cache.py new file mode 100644 index 00000000000..24dca838e85 --- /dev/null +++ b/storagecontrol/disable_anywhere_cache.py @@ -0,0 +1,47 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_disable_anywhere_cache] +from google.cloud import storage_control_v2 + + +def disable_anywhere_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone of the anywhere cache to disable + # zone = "us-central1-a" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + anywhere_cache_path = storage_control_client.anywhere_cache_path( + "_", bucket_name, zone + ) + + request = storage_control_v2.DisableAnywhereCacheRequest( + name=anywhere_cache_path, + ) + response = storage_control_client.disable_anywhere_cache(request=request) + + print(f"Disabled anywhere cache: {response.name}") + + +# [END storage_control_disable_anywhere_cache] + + +if __name__ == "__main__": + disable_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/get_anywhere_cache.py b/storagecontrol/get_anywhere_cache.py new file mode 100644 index 00000000000..739e0f14e4c --- /dev/null +++ b/storagecontrol/get_anywhere_cache.py @@ -0,0 +1,47 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_get_anywhere_cache] +from google.cloud import storage_control_v2 + + +def get_anywhere_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone of the anywhere cache to get + # zone = "us-central1-a" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + anywhere_cache_path = storage_control_client.anywhere_cache_path( + "_", bucket_name, zone + ) + + request = storage_control_v2.GetAnywhereCacheRequest( + name=anywhere_cache_path, + ) + response = storage_control_client.get_anywhere_cache(request=request) + + print(f"Got anywhere cache: {response.name}") + + +# [END storage_control_get_anywhere_cache] + + +if __name__ == "__main__": + get_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/list_anywhere_caches.py b/storagecontrol/list_anywhere_caches.py new file mode 100644 index 00000000000..80a944c6a57 --- /dev/null +++ b/storagecontrol/list_anywhere_caches.py @@ -0,0 +1,44 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_list_anywhere_caches] +from google.cloud import storage_control_v2 + + +def list_anywhere_caches(bucket_name: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + project_path = storage_control_client.common_project_path("_") + bucket_path = f"{project_path}/buckets/{bucket_name}" + + request = storage_control_v2.ListAnywhereCachesRequest( + parent=bucket_path, + ) + page_result = storage_control_client.list_anywhere_caches(request=request) + + for response in page_result: + print(response.name) + + +# [END storage_control_list_anywhere_caches] + + +if __name__ == "__main__": + list_anywhere_caches(bucket_name=sys.argv[1]) diff --git a/storagecontrol/pause_anywhere_cache.py b/storagecontrol/pause_anywhere_cache.py new file mode 100644 index 00000000000..8ee5ce7f079 --- /dev/null +++ b/storagecontrol/pause_anywhere_cache.py @@ -0,0 +1,47 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_pause_anywhere_cache] +from google.cloud import storage_control_v2 + + +def pause_anywhere_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone of the anywhere cache to pause + # zone = "us-central1-a" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + anywhere_cache_path = storage_control_client.anywhere_cache_path( + "_", bucket_name, zone + ) + + request = storage_control_v2.PauseAnywhereCacheRequest( + name=anywhere_cache_path, + ) + response = storage_control_client.pause_anywhere_cache(request=request) + + print(f"Paused anywhere cache: {response.name}") + + +# [END storage_control_pause_anywhere_cache] + + +if __name__ == "__main__": + pause_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/resume_anywhere_cache.py b/storagecontrol/resume_anywhere_cache.py new file mode 100644 index 00000000000..417e7d31041 --- /dev/null +++ b/storagecontrol/resume_anywhere_cache.py @@ -0,0 +1,47 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_resume_anywhere_cache] +from google.cloud import storage_control_v2 + + +def resume_anywhere_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone of the anywhere cache to resume + # zone = "us-central1-a" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + anywhere_cache_path = storage_control_client.anywhere_cache_path( + "_", bucket_name, zone + ) + + request = storage_control_v2.ResumeAnywhereCacheRequest( + name=anywhere_cache_path, + ) + response = storage_control_client.resume_anywhere_cache(request=request) + + print(f"Resumed anywhere cache: {response.name}") + + +# [END storage_control_resume_anywhere_cache] + + +if __name__ == "__main__": + resume_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/snippets_test.py b/storagecontrol/snippets_test.py index 218a3b7f26a..f1b126795fa 100644 --- a/storagecontrol/snippets_test.py +++ b/storagecontrol/snippets_test.py @@ -26,18 +26,29 @@ import managed_folder_list import rename_folder +import create_anywhere_cache +import disable_anywhere_cache +import get_anywhere_cache +import list_anywhere_caches +import pause_anywhere_cache +import resume_anywhere_cache +import update_anywhere_cache # === Folders === # def test_folder_create_get_list_rename_delete( - capsys: pytest.LogCaptureFixture, hns_enabled_bucket: storage.Bucket, uuid_name: str + capsys: pytest.LogCaptureFixture, + hns_enabled_bucket: storage.Bucket, + uuid_name: str, ) -> None: bucket_name = hns_enabled_bucket.name folder_name = uuid_name # Test create folder - create_folder.create_folder(bucket_name=bucket_name, folder_name=folder_name) + create_folder.create_folder( + bucket_name=bucket_name, folder_name=folder_name + ) out, _ = capsys.readouterr() assert folder_name in out @@ -67,6 +78,68 @@ def test_folder_create_get_list_rename_delete( assert new_name in out +# === Anywhere Cache === # + + +def test_anywhere_cache_operations( + capsys: pytest.LogCaptureFixture, + ubla_enabled_bucket: storage.Bucket, +) -> None: + bucket_name = ubla_enabled_bucket.name + zone = "us-central1-a" + + try: + # Test create anywhere cache + create_anywhere_cache.create_anywhere_cache( + bucket_name=bucket_name, zone=zone + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # Test get anywhere cache + get_anywhere_cache.get_anywhere_cache( + bucket_name=bucket_name, zone=zone + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # Test list anywhere caches + list_anywhere_caches.list_anywhere_caches(bucket_name=bucket_name) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # Test update anywhere cache + update_anywhere_cache.update_anywhere_cache( + bucket_name=bucket_name, + zone=zone, + admission_policy="admit-on-second-miss", + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # Test pause anywhere cache + pause_anywhere_cache.pause_anywhere_cache( + bucket_name=bucket_name, zone=zone + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # Test resume anywhere cache + resume_anywhere_cache.resume_anywhere_cache( + bucket_name=bucket_name, zone=zone + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + finally: + # Test disable anywhere cache + disable_anywhere_cache.disable_anywhere_cache( + bucket_name=bucket_name, zone=zone + ) + out, _ = capsys.readouterr() + assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out + + # === Managed Folders === # diff --git a/storagecontrol/update_anywhere_cache.py b/storagecontrol/update_anywhere_cache.py new file mode 100644 index 00000000000..0a2c8693c43 --- /dev/null +++ b/storagecontrol/update_anywhere_cache.py @@ -0,0 +1,68 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys + +# [START storage_control_update_anywhere_cache] +from google.cloud import storage_control_v2 +from google.protobuf import field_mask_pb2 + + +def update_anywhere_cache( + bucket_name: str, zone: str, admission_policy: str +) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone of the anywhere cache to update + # zone = "us-central1-a" + + # The admission policy to set + # admission_policy = "admit-on-second-miss" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage bucket path uses the global access pattern, in which the "_" + # denotes this bucket exists in the global namespace. + anywhere_cache_path = storage_control_client.anywhere_cache_path( + "_", bucket_name, zone + ) + + anywhere_cache = storage_control_v2.AnywhereCache( + name=anywhere_cache_path, + admission_policy=admission_policy, + ) + + update_mask = field_mask_pb2.FieldMask(paths=["admission_policy"]) + + request = storage_control_v2.UpdateAnywhereCacheRequest( + anywhere_cache=anywhere_cache, + update_mask=update_mask, + ) + + # Start an update operation and block until it completes. Real applications + # may want to setup a callback, wait on a coroutine, or poll until it + # completes. + operation = storage_control_client.update_anywhere_cache(request=request) + response = operation.result() + + print(f"Updated anywhere cache: {response.name}") + + +# [END storage_control_update_anywhere_cache] + + +if __name__ == "__main__": + update_anywhere_cache( + bucket_name=sys.argv[1], zone=sys.argv[2], admission_policy=sys.argv[3] + )