diff --git a/storagecontrol/conftest.py b/storagecontrol/conftest.py index d5958a74a22..4a97d7aaf1c 100644 --- a/storagecontrol/conftest.py +++ b/storagecontrol/conftest.py @@ -15,10 +15,14 @@ import os import uuid +from google.api_core import exceptions from google.cloud import storage +from google.cloud import storage_control_v2 import pytest +import logging + @pytest.fixture(scope="module") def project_id() -> str: @@ -66,6 +70,80 @@ def hns_enabled_bucket(project_id: str, bucket_name: str) -> storage.Bucket: bucket.delete(force=True) +@pytest.fixture(scope="module") +def rapid_cache_zone() -> str: + zone = os.environ.get("RAPID_CACHE_ZONE", "").strip() or "us-central1-a" + parts = zone.split("-") + if len(parts) < 3 or not all(parts): + raise ValueError( + f"Invalid RAPID_CACHE_ZONE {zone!r}; expected a zone like " + "'us-central1-a'." + ) + yield zone + + +@pytest.fixture(scope="function") +def regional_hns_enabled_bucket( + project_id: str, bucket_name: str, rapid_cache_zone: str +) -> storage.Bucket: + """ + Yields and auto-cleans up a regional HNS enabled bucket located in the + region of the Rapid Cache zone. + """ + + storage_client = storage.Client(project=project_id) + bucket = storage_client.bucket(bucket_name) + bucket.iam_configuration.uniform_bucket_level_access_enabled = True + bucket.hierarchical_namespace_enabled = True + region = rapid_cache_zone.rsplit("-", 1)[0] + bucket = storage_client.create_bucket(bucket, location=region) + + yield bucket + + # A bucket cannot be deleted until its disabled Rapid Cache has been fully + # removed, which can take more than an hour. Cleanup is best-effort; if + # deletion fails with Conflict, leave the bucket for later cleanup. + try: + bucket.delete(force=True) + except exceptions.GoogleAPIError as e: + logging.warning("Could not delete bucket %s: %s", bucket.name, e) + + +@pytest.fixture(scope="function") +def rapid_cache_id( + regional_hns_enabled_bucket: storage.Bucket, rapid_cache_zone: str +) -> str: + """ + Yields the ID of a Rapid Cache for the regional HNS enabled bucket. If the + test created the Rapid Cache and left it active, it is disabled on + teardown so that the bucket can be deleted once the cache is removed. + """ + + # The ID of the Rapid Cache. This test uses the zone as the cache ID. + cache_id = rapid_cache_zone + + yield cache_id + + storage_control_client = storage_control_v2.StorageControlClient() + bucket_path = ( + f"{storage_control_client.common_project_path('_')}" + f"/buckets/{regional_hns_enabled_bucket.name}" + ) + # Cleanup is best-effort: errors are logged instead of failing the test. + try: + for cache in storage_control_client.list_rapid_caches(parent=bucket_path): + if cache.state != "disabled": + # Disabling is a long-running operation that completes only once + # the cache is fully removed, so teardown does not wait for it. + storage_control_client.disable_rapid_cache(name=cache.name) + except exceptions.GoogleAPIError as e: + logging.warning( + "Could not disable Rapid Cache in bucket %s: %s", + regional_hns_enabled_bucket.name, + e, + ) + + @pytest.fixture(scope="function") def ubla_enabled_bucket(project_id: str, bucket_name: str) -> storage.Bucket: """ diff --git a/storagecontrol/create_rapid_cache.py b/storagecontrol/create_rapid_cache.py new file mode 100644 index 00000000000..65928107197 --- /dev/null +++ b/storagecontrol/create_rapid_cache.py @@ -0,0 +1,62 @@ +# 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_rapid_cache] +from google.cloud import storage_control_v2 + + +def create_rapid_cache(bucket_name: str, zone: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The zone in which to create the Rapid Cache. The zone must be in the + # same region as the bucket. + # 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}" + + # Only the required fields are set. Optional fields, such as the time to + # live (TTL) for cache entries, use server defaults and can be changed + # later by updating the Rapid Cache. + # Rapid Cache Ultra requires a regional bucket with hierarchical namespace + # enabled. + rapid_cache = storage_control_v2.RapidCache( + zone=zone, + cache_type="rapid-cache-ultra", + ) + request = storage_control_v2.CreateRapidCacheRequest( + parent=bucket_path, + rapid_cache=rapid_cache, + ) + + # Creating a Rapid Cache is a long-running operation. + operation = storage_control_client.create_rapid_cache(request=request) + response = operation.result() + + print(f"Created Rapid Cache: {response.name}") + print(f"State: {response.state}") + print(f"Cache type: {response.cache_type}") + + +# [END storage_control_create_rapid_cache] + + +if __name__ == "__main__": + create_rapid_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) diff --git a/storagecontrol/disable_rapid_cache.py b/storagecontrol/disable_rapid_cache.py new file mode 100644 index 00000000000..f55f5cba6a6 --- /dev/null +++ b/storagecontrol/disable_rapid_cache.py @@ -0,0 +1,52 @@ +# 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_rapid_cache] +from google.cloud import storage_control_v2 + + +def disable_rapid_cache(bucket_name: str, cache_id: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The ID of the Rapid Cache + # cache_id = "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. + rapid_cache_path = storage_control_client.rapid_cache_path( + project="_", bucket=bucket_name, rapid_cache=cache_id + ) + + request = storage_control_v2.DisableRapidCacheRequest(name=rapid_cache_path) + + # Disabling a Rapid Cache is a long-running operation. The cache moves to + # the "disabled" state quickly, but the operation only completes once the + # cache is fully removed (which can take more than an hour; bucket deletion + # is blocked until removal completes). Start the operation without waiting + # and use get_rapid_cache to check the cache state. + operation = storage_control_client.disable_rapid_cache(request=request) + + print(f"Disabling Rapid Cache: {rapid_cache_path}") + print(f"Operation: {operation.operation.name}") + + +# [END storage_control_disable_rapid_cache] + + +if __name__ == "__main__": + disable_rapid_cache(bucket_name=sys.argv[1], cache_id=sys.argv[2]) diff --git a/storagecontrol/get_rapid_cache.py b/storagecontrol/get_rapid_cache.py new file mode 100644 index 00000000000..1d5288a6039 --- /dev/null +++ b/storagecontrol/get_rapid_cache.py @@ -0,0 +1,50 @@ +# 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_rapid_cache] +from google.cloud import storage_control_v2 + + +def get_rapid_cache(bucket_name: str, cache_id: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The ID of the Rapid Cache + # cache_id = "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. + rapid_cache_path = storage_control_client.rapid_cache_path( + project="_", bucket=bucket_name, rapid_cache=cache_id + ) + + request = storage_control_v2.GetRapidCacheRequest(name=rapid_cache_path) + response = storage_control_client.get_rapid_cache(request=request) + + print(f"Got Rapid Cache: {response.name}") + print(f"Zone: {response.zone}") + print(f"State: {response.state}") + print(f"Cache type: {response.cache_type}") + if response.ttl: + print(f"TTL: {response.ttl.total_seconds():.0f} seconds") + + +# [END storage_control_get_rapid_cache] + + +if __name__ == "__main__": + get_rapid_cache(bucket_name=sys.argv[1], cache_id=sys.argv[2]) diff --git a/storagecontrol/list_rapid_caches.py b/storagecontrol/list_rapid_caches.py new file mode 100644 index 00000000000..4ed9eb9a150 --- /dev/null +++ b/storagecontrol/list_rapid_caches.py @@ -0,0 +1,45 @@ +# 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_rapid_caches] +from google.cloud import storage_control_v2 + + +def list_rapid_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.ListRapidCachesRequest(parent=bucket_path) + + # The returned pager handles pagination automatically. + page_result = storage_control_client.list_rapid_caches(request=request) + for rapid_cache in page_result: + print(rapid_cache.name) + + print(f"Listed Rapid Caches in bucket {bucket_name}") + + +# [END storage_control_list_rapid_caches] + + +if __name__ == "__main__": + list_rapid_caches(bucket_name=sys.argv[1]) diff --git a/storagecontrol/rapid_cache_test.py b/storagecontrol/rapid_cache_test.py new file mode 100644 index 00000000000..de49ddc93bb --- /dev/null +++ b/storagecontrol/rapid_cache_test.py @@ -0,0 +1,92 @@ +# 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 os + +import create_rapid_cache +import disable_rapid_cache +import get_rapid_cache +from google.api_core import exceptions +from google.cloud import storage +from google.cloud import storage_control_v2 +import list_rapid_caches +import pytest +import update_rapid_cache + +pytestmark = pytest.mark.skipif( + os.getenv("RUN_RAPID_CACHE_TESTS") != "True", + reason=( + "Rapid Cache tests require an allowlisted project and leave buckets " + "until async cache removal completes. Set RUN_RAPID_CACHE_TESTS=True " + "to run." + ), +) + + +def test_rapid_cache_create_get_list_update_disable( + capsys: pytest.CaptureFixture[str], + regional_hns_enabled_bucket: storage.Bucket, + rapid_cache_zone: str, + rapid_cache_id: str, +) -> None: + bucket_name = regional_hns_enabled_bucket.name + cache_id = rapid_cache_id + cache_name = storage_control_v2.StorageControlClient.rapid_cache_path( + project="_", bucket=bucket_name, rapid_cache=cache_id + ) + + # Test create Rapid Cache + create_rapid_cache.create_rapid_cache( + bucket_name=bucket_name, zone=rapid_cache_zone + ) + out, _ = capsys.readouterr() + assert f"Created Rapid Cache: {cache_name}" in out + assert "State: running" in out + assert "Cache type: rapid-cache-ultra" in out + + # Test get Rapid Cache + get_rapid_cache.get_rapid_cache(bucket_name=bucket_name, cache_id=cache_id) + out, _ = capsys.readouterr() + assert f"Got Rapid Cache: {cache_name}" in out + assert f"Zone: {rapid_cache_zone}" in out + assert "State: running" in out + assert "Cache type: rapid-cache-ultra" in out + assert "TTL:" in out + + # Test list Rapid Caches + list_rapid_caches.list_rapid_caches(bucket_name=bucket_name) + out, _ = capsys.readouterr() + assert cache_name in out + + # Test update Rapid Cache + update_rapid_cache.update_rapid_cache( + bucket_name=bucket_name, cache_id=cache_id, ttl_seconds=172800 + ) + out, _ = capsys.readouterr() + assert f"Updated Rapid Cache: {cache_name}" in out + assert "TTL: 172800 seconds" in out + + # Test disable Rapid Cache + disable_rapid_cache.disable_rapid_cache(bucket_name=bucket_name, cache_id=cache_id) + out, _ = capsys.readouterr() + assert f"Disabling Rapid Cache: {cache_name}" in out + assert "Operation:" in out + + try: + get_rapid_cache.get_rapid_cache(bucket_name=bucket_name, cache_id=cache_id) + out, _ = capsys.readouterr() + assert "State: disabled" in out + except exceptions.NotFound: + # On an empty bucket, full cache removal can finish before get runs. + pass diff --git a/storagecontrol/requirements.txt b/storagecontrol/requirements.txt index e7b93b6c245..e2bea4c66d0 100644 --- a/storagecontrol/requirements.txt +++ b/storagecontrol/requirements.txt @@ -1 +1 @@ -google-cloud-storage-control==1.11.0 \ No newline at end of file +google-cloud-storage-control==1.17.0 \ No newline at end of file diff --git a/storagecontrol/update_rapid_cache.py b/storagecontrol/update_rapid_cache.py new file mode 100644 index 00000000000..e1e9b75b2cc --- /dev/null +++ b/storagecontrol/update_rapid_cache.py @@ -0,0 +1,69 @@ +# 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_rapid_cache] +from google.cloud import storage_control_v2 +from google.protobuf import duration_pb2 +from google.protobuf import field_mask_pb2 + + +def update_rapid_cache(bucket_name: str, cache_id: str, ttl_seconds: int) -> None: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # The ID of the Rapid Cache + # cache_id = "us-central1-a" + + # The new time to live (TTL) for cache entries, in seconds. The TTL must + # be between 1 hour and 7 days. + # ttl_seconds = 172800 # 2 days + + 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. + rapid_cache_path = storage_control_client.rapid_cache_path( + project="_", bucket=bucket_name, rapid_cache=cache_id + ) + + rapid_cache = storage_control_v2.RapidCache( + name=rapid_cache_path, + ttl=duration_pb2.Duration(seconds=ttl_seconds), + ) + # Only the fields listed in the update mask are changed. + update_mask = field_mask_pb2.FieldMask(paths=["ttl"]) + request = storage_control_v2.UpdateRapidCacheRequest( + rapid_cache=rapid_cache, + update_mask=update_mask, + ) + + # Updating a Rapid Cache is a long-running operation. + operation = storage_control_client.update_rapid_cache(request=request) + response = operation.result() + + print(f"Updated Rapid Cache: {response.name}") + # The client library returns the TTL as a datetime.timedelta. + print(f"TTL: {response.ttl.total_seconds():.0f} seconds") + + +# [END storage_control_update_rapid_cache] + + +if __name__ == "__main__": + update_rapid_cache( + bucket_name=sys.argv[1], + cache_id=sys.argv[2], + ttl_seconds=int(sys.argv[3]), + )