Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions storagecontrol/anywhere_cache_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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 in which to create the Anywhere Cache
# 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}"
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

Use the client's built-in path helper methods instead of manual string concatenation for resource names. This is more idiomatic and less error-prone.

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


request = storage_control_v2.CreateAnywhereCacheRequest(
parent=bucket_path,
anywhere_cache=storage_control_v2.AnywhereCache(
zone=zone,
admission_policy="admit-on-second-miss",
),
)
# This operation is long-running. Real-world applications may want to
# use callbacks, coroutines, or polling to handle the response.
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])
Comment thread
nidhiii-27 marked this conversation as resolved.
41 changes: 41 additions & 0 deletions storagecontrol/anywhere_cache_disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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(anywhere_cache_id: str) -> None:
# The full name of the Anywhere Cache
# anywhere_cache_id = "projects/_/buckets/bucket-name/anywhereCaches/zone-id"

storage_control_client = storage_control_v2.StorageControlClient()

request = storage_control_v2.DisableAnywhereCacheRequest(
name=anywhere_cache_id,
)
# The DisableAnywhereCache RPC returns google.protobuf.Empty,
# so we cannot access attributes like .name on the response.
storage_control_client.disable_anywhere_cache(request=request)

print(f"Disabled Anywhere Cache: {anywhere_cache_id}")


# [END storage_control_disable_anywhere_cache]


if __name__ == "__main__":
disable_anywhere_cache(anywhere_cache_id=sys.argv[1])
41 changes: 41 additions & 0 deletions storagecontrol/anywhere_cache_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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(anywhere_cache_id: str) -> None:
# The full name of the Anywhere Cache
# anywhere_cache_id = "projects/_/buckets/bucket-name/anywhereCaches/zone-id"

storage_control_client = storage_control_v2.StorageControlClient()

request = storage_control_v2.GetAnywhereCacheRequest(
name=anywhere_cache_id,
)
response = storage_control_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(anywhere_cache_id=sys.argv[1])
44 changes: 44 additions & 0 deletions storagecontrol/anywhere_cache_list.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(f"Anywhere Cache: {response.name}")


# [END storage_control_list_anywhere_caches]


if __name__ == "__main__":
list_anywhere_caches(bucket_name=sys.argv[1])
40 changes: 40 additions & 0 deletions storagecontrol/anywhere_cache_pause.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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(anywhere_cache_id: str) -> None:
# The full name of the Anywhere Cache
# anywhere_cache_id = "projects/_/buckets/bucket-name/anywhereCaches/zone-id"

storage_control_client = storage_control_v2.StorageControlClient()

request = storage_control_v2.PauseAnywhereCacheRequest(
name=anywhere_cache_id,
)
response = storage_control_client.pause_anywhere_cache(request=request)

print(f"Paused Anywhere Cache: {response.name}")
print(f"State: {response.state}")


# [END storage_control_pause_anywhere_cache]


if __name__ == "__main__":
pause_anywhere_cache(anywhere_cache_id=sys.argv[1])
40 changes: 40 additions & 0 deletions storagecontrol/anywhere_cache_resume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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(anywhere_cache_id: str) -> None:
# The full name of the Anywhere Cache
# anywhere_cache_id = "projects/_/buckets/bucket-name/anywhereCaches/zone-id"

storage_control_client = storage_control_v2.StorageControlClient()

request = storage_control_v2.ResumeAnywhereCacheRequest(
name=anywhere_cache_id,
)
response = storage_control_client.resume_anywhere_cache(request=request)

print(f"Resumed Anywhere Cache: {response.name}")
print(f"State: {response.state}")


# [END storage_control_resume_anywhere_cache]


if __name__ == "__main__":
resume_anywhere_cache(anywhere_cache_id=sys.argv[1])
72 changes: 72 additions & 0 deletions storagecontrol/anywhere_cache_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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.

from google.cloud import storage

import pytest

import anywhere_cache_create
import anywhere_cache_disable
import anywhere_cache_get
import anywhere_cache_list
import anywhere_cache_pause
import anywhere_cache_resume
import anywhere_cache_update


def test_anywhere_cache_lifecycle(
capsys: pytest.LogCaptureFixture, ubla_enabled_bucket: storage.Bucket
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 capsys fixture should be type-hinted as pytest.CaptureFixture. pytest.LogCaptureFixture is intended for the caplog fixture.

Suggested change
capsys: pytest.LogCaptureFixture, ubla_enabled_bucket: storage.Bucket
capsys: pytest.CaptureFixture, ubla_enabled_bucket: storage.Bucket

) -> None:
bucket_name = ubla_enabled_bucket.name
zone = "us-central1-a"
anywhere_cache_id = f"projects/_/buckets/{bucket_name}/anywhereCaches/{zone}"

# Test create
anywhere_cache_create.create_anywhere_cache(bucket_name=bucket_name, zone=zone)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out

# Test get
anywhere_cache_get.get_anywhere_cache(anywhere_cache_id=anywhere_cache_id)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out
assert "admit-on-second-miss" in out

# Test list
anywhere_cache_list.list_anywhere_caches(bucket_name=bucket_name)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out

# Test update
anywhere_cache_update.update_anywhere_cache(
anywhere_cache_id=anywhere_cache_id, admission_policy="admit-on-second-miss"
)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out
assert "admit-on-second-miss" in out
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 update test currently uses the same admission_policy value as the creation step. To effectively test the update functionality, use a different value such as "admit-on-first-miss" and update the assertion accordingly.

Suggested change
anywhere_cache_id=anywhere_cache_id, admission_policy="admit-on-second-miss"
)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out
assert "admit-on-second-miss" in out
anywhere_cache_id=anywhere_cache_id, admission_policy="admit-on-first-miss"
)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out
assert "admit-on-first-miss" in out


# Test pause
anywhere_cache_pause.pause_anywhere_cache(anywhere_cache_id=anywhere_cache_id)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out

# Test resume
anywhere_cache_resume.resume_anywhere_cache(anywhere_cache_id=anywhere_cache_id)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out

# Test disable
anywhere_cache_disable.disable_anywhere_cache(anywhere_cache_id=anywhere_cache_id)
out, _ = capsys.readouterr()
assert anywhere_cache_id in out
54 changes: 54 additions & 0 deletions storagecontrol/anywhere_cache_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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(anywhere_cache_id: str, admission_policy: str) -> None:
# The full name of the Anywhere Cache
# anywhere_cache_id = "projects/_/buckets/bucket-name/anywhereCaches/zone-id"

# The new admission policy
# admission_policy = "admit-on-second-miss"

storage_control_client = storage_control_v2.StorageControlClient()

anywhere_cache = storage_control_v2.AnywhereCache(
name=anywhere_cache_id,
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,
)
# This operation is long-running. Real-world applications may want to
# use callbacks, coroutines, or polling to handle the response.
operation = storage_control_client.update_anywhere_cache(request=request)
response = operation.result()

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


# [END storage_control_update_anywhere_cache]


if __name__ == "__main__":
update_anywhere_cache(anywhere_cache_id=sys.argv[1], admission_policy=sys.argv[2])
Loading