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
64 changes: 64 additions & 0 deletions storagecontrol/create_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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, admission_policy: str = "ADMIT_ON_FIRST_MISS"
) -> 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"

# The admission policy for the Anywhere Cache
# admission_policy = "ADMIT_ON_FIRST_MISS"

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 = client.common_project_path("_")
bucket_path = f"{project_path}/buckets/{bucket_name}"
Comment on lines +37 to +38
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It is recommended to use the client's built-in path helper methods instead of manually constructing resource names with f-strings. This ensures the resource name follows the correct pattern and is more maintainable.

Suggested change
project_path = client.common_project_path("_")
bucket_path = f"{project_path}/buckets/{bucket_name}"
bucket_path = client.bucket_path("_", bucket_name)


anywhere_cache = storage_control_v2.AnywhereCache(
admission_policy=admission_policy,
)

request = storage_control_v2.CreateAnywhereCacheRequest(
parent=bucket_path,
anywhere_cache=anywhere_cache,
anywhere_cache_id=zone,
)

# create_anywhere_cache is a long-running operation.
# Real applications may want to setup a callback or poll for the operation to complete.
operation = 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], admission_policy=sys.argv[3]
)
Comment on lines +62 to +64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation will raise an IndexError if the optional admission_policy argument is not provided on the command line. Using *sys.argv[1:] allows the function to use its default value when the third argument is missing.

    create_anywhere_cache(*sys.argv[1:])

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
# zone = "us-central1-a"

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 = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
Comment on lines +32 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using the client's path helper methods is preferred over manual string concatenation for resource names. This improves readability and reduces the risk of errors in the resource path structure.

Suggested change
project_path = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
name = client.anywhere_cache_path("_", bucket_name, zone)


request = storage_control_v2.DisableAnywhereCacheRequest(
name=name,
)
response = 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])
49 changes: 49 additions & 0 deletions storagecontrol/get_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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
# zone = "us-central1-a"

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 = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
Comment on lines +32 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Consider using the anywhere_cache_path helper method provided by the client to construct the resource name. This is a best practice for Google Cloud client library samples.

Suggested change
project_path = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
name = client.anywhere_cache_path("_", bucket_name, zone)


request = storage_control_v2.GetAnywhereCacheRequest(
name=name,
)
response = client.get_anywhere_cache(request=request)

print(f"Anywhere Cache: {response.name}")
print(f"Admission Policy: {response.admission_policy}")
print(f"State: {response.state}")


# [END storage_control_get_anywhere_cache]


if __name__ == "__main__":
get_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2])
45 changes: 45 additions & 0 deletions storagecontrol/list_anywhere_cache.py
Original file line number Diff line number Diff line change
@@ -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_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"

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 = client.common_project_path("_")
bucket_path = f"{project_path}/buckets/{bucket_name}"
Comment on lines +29 to +30
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The client provides a bucket_path helper method which is cleaner and more robust than manual f-string construction for the parent resource path.

Suggested change
project_path = client.common_project_path("_")
bucket_path = f"{project_path}/buckets/{bucket_name}"
bucket_path = client.bucket_path("_", bucket_name)


request = storage_control_v2.ListAnywhereCachesRequest(
parent=bucket_path,
)
page_result = client.list_anywhere_caches(request=request)

for response in page_result:
print(f"Anywhere Cache: {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
# zone = "us-central1-a"

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 = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
Comment on lines +32 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using the anywhere_cache_path helper method is recommended for constructing the resource name, ensuring consistency across samples and adherence to client library best practices.

Suggested change
project_path = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
name = client.anywhere_cache_path("_", bucket_name, zone)


request = storage_control_v2.PauseAnywhereCacheRequest(
name=name,
)
response = 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
# zone = "us-central1-a"

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 = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
Comment on lines +32 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It is better to use the anywhere_cache_path helper method to build the resource name instead of manual string formatting.

Suggested change
project_path = client.common_project_path("_")
name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}"
name = client.anywhere_cache_path("_", bucket_name, zone)


request = storage_control_v2.ResumeAnywhereCacheRequest(
name=name,
)
response = 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])
60 changes: 59 additions & 1 deletion storagecontrol/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

import pytest

import create_anywhere_cache
import create_folder
import delete_folder
import disable_anywhere_cache
import get_anywhere_cache
import get_folder
import list_anywhere_cache
import list_folders
import managed_folder_create
import managed_folder_delete
import managed_folder_get
import managed_folder_list
import pause_anywhere_cache
import rename_folder

import resume_anywhere_cache
import update_anywhere_cache

# === Folders === #

Expand Down Expand Up @@ -67,6 +73,58 @@ def test_folder_create_get_list_rename_delete(
assert new_name in out


# === Anywhere Cache === #


def test_anywhere_cache_create_get_list_update_pause_resume_disable(
capsys: pytest.LogCaptureFixture,
ubla_enabled_bucket: storage.Bucket,
) -> None:
bucket_name = ubla_enabled_bucket.name
zone = "us-central1-a"

# Test create anywhere cache
create_anywhere_cache.create_anywhere_cache(
bucket_name=bucket_name, zone=zone, admission_policy="ADMIT_ON_FIRST_MISS"
)
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
assert "ADMIT_ON_FIRST_MISS" in out

# Test list anywhere caches
list_anywhere_cache.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_FIRST_MISS"
)
out, _ = capsys.readouterr()
assert f"buckets/{bucket_name}/anywhereCaches/{zone}" in out
assert "ADMIT_ON_FIRST_MISS" 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

# 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 === #


Expand Down
Loading