forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(storagecontrol): Added samples for Anywhere Cache (v2) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nidhiii-27
wants to merge
3
commits into
main
Choose a base branch
from
feat-storagecontrol-anywhere-cache-samples-17401725828640608185
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
772f7a9
feat(storagecontrol): Added samples for Anywhere Cache
google-labs-jules[bot] 265611a
chore(storagecontrol): Address PR comments for Anywhere Cache samples
google-labs-jules[bot] 162251e
chore(storagecontrol): Address PR comments for Anywhere Cache samples
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test
test_anywhere_cache_operationslacks cleanup logic in case of failure. If an assertion fails beforedisable_anywhere_cache, the resource remains active, which may cause orphaned resources or prevent the bucket fixture from cleaning up correctly. Consider using atry...finallyblock to ensure the cache is disabled regardless of the test outcome.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...finallyblock to the integration test to ensure thatdisable_anywhere_cacheis always called, which helps with resource cleanup even if earlier parts of the test fail or time out.