Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions storagecontrol/create_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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])
47 changes: 47 additions & 0 deletions storagecontrol/disable_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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])
47 changes: 47 additions & 0 deletions storagecontrol/get_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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])
44 changes: 44 additions & 0 deletions storagecontrol/list_anywhere_caches.py
Original file line number Diff line number Diff line change
@@ -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])
47 changes: 47 additions & 0 deletions storagecontrol/pause_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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])
47 changes: 47 additions & 0 deletions storagecontrol/resume_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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])
77 changes: 75 additions & 2 deletions storagecontrol/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Comment on lines +84 to +141
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test test_anywhere_cache_operations lacks cleanup logic in case of failure. If an assertion fails before disable_anywhere_cache, the resource remains active, which may cause orphaned resources or prevent the bucket fixture from cleaning up correctly. Consider using a try...finally block to ensure the cache is disabled regardless of the test outcome.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules add the try...finally block

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the try...finally block to the integration test to ensure that disable_anywhere_cache is always called, which helps with resource cleanup even if earlier parts of the test fail or time out.


# === Managed Folders === #


Expand Down
Loading