Skip to content

Commit e71f1dd

Browse files
samples(StorageControl): Add samples for anywhereCache (#31536)
1 parent 054cff2 commit e71f1dd

9 files changed

+528
-2
lines changed

google-cloud-storage-control/samples/acceptance/helper.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require "minitest/autorun"
1717
require "minitest/focus"
1818
require "minitest/hooks/default"
19+
require "google/cloud/storage/control"
1920

2021
def random_bucket_name
2122
t = Time.now.utc.iso8601.gsub ":", "-"
@@ -27,8 +28,11 @@ def random_folder_name prefix: "ruby-storage-control-folder-samples-test-"
2728
"#{prefix}-#{t}-#{SecureRandom.hex 4}".downcase
2829
end
2930

31+
def storage_client
32+
@storage_client ||= Google::Cloud::Storage.new
33+
end
34+
3035
def create_bucket_helper bucket_name, uniform_bucket_level_access: nil, hierarchical_namespace: nil
31-
storage_client = Google::Cloud::Storage.new
3236
retry_resource_exhaustion do
3337
storage_client.create_bucket bucket_name do |b|
3438
b.uniform_bucket_level_access = uniform_bucket_level_access
@@ -38,7 +42,6 @@ def create_bucket_helper bucket_name, uniform_bucket_level_access: nil, hierarch
3842
end
3943

4044
def delete_bucket_helper bucket_name
41-
storage_client = Google::Cloud::Storage.new
4245
retry_resource_exhaustion do
4346
bucket = storage_client.bucket bucket_name
4447
return unless bucket
@@ -60,3 +63,36 @@ def retry_resource_exhaustion
6063
end
6164
raise Google::Cloud::ResourceExhaustedError, "Maybe take a break from creating and deleting buckets for a bit"
6265
end
66+
67+
# Waits until all Anywhere Caches for a given bucket are deleted.
68+
#
69+
# This method polls the Storage Control API, listing the Anywhere Caches
70+
# associated with the specified bucket. If caches are found, it waits and
71+
# retries with an exponential backoff strategy until no caches remain.
72+
#
73+
# @param bucket_name [String] The name of the Google Cloud Storage bucket.
74+
# @return [Integer] The final count of Anywhere Caches, which will be 0
75+
# the method completes successfully after all caches are deleted.
76+
def count_anywhere_caches bucket_name
77+
storage_control_client = Google::Cloud::Storage::Control.storage_control
78+
79+
# Set project to "_" to signify global bucket
80+
parent = "projects/_/buckets/#{bucket_name}"
81+
request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
82+
parent: parent
83+
)
84+
result = storage_control_client.list_anywhere_caches request
85+
min_delay = 30 # 30 seconds
86+
max_delay = 900 # 15 minutes
87+
start_time = Time.now
88+
while result.response.anywhere_caches.count != 0
89+
puts "Cache not deleted yet, Retrying in #{min_delay} seconds."
90+
sleep min_delay
91+
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
92+
result = storage_control_client.list_anywhere_caches request
93+
end
94+
end_time = Time.now
95+
duration = end_time - start_time
96+
puts "Total waiting time : #{duration.round 2} seconds."
97+
result.response.anywhere_caches.count
98+
end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require_relative "helper"
16+
require_relative "../storage_control_create_anywhere_cache"
17+
require_relative "../storage_control_list_anywhere_caches"
18+
require_relative "../storage_control_get_anywhere_cache"
19+
require_relative "../storage_control_update_anywhere_cache"
20+
require_relative "../storage_control_pause_anywhere_cache"
21+
require_relative "../storage_control_resume_anywhere_cache"
22+
require_relative "../storage_control_disable_anywhere_cache"
23+
24+
describe "Storage Control Anywhere Cache" do
25+
let(:bucket_name) { random_bucket_name }
26+
let(:zone) { "us-east1-b" }
27+
# Set project to "_" to signify global bucket
28+
let(:anywhere_cache_name) { "projects/_/buckets/#{bucket_name}/anywhereCaches/#{zone}" }
29+
30+
before :all do
31+
create_bucket_helper bucket_name
32+
end
33+
34+
after :all do
35+
count_anywhere_caches bucket_name # Ensure all caches are deleted before deleting bucket
36+
delete_bucket_helper bucket_name
37+
end
38+
39+
it "handles Anywhere cache lifecycle in sequence" do
40+
out_create, _err = capture_io do
41+
create_anywhere_cache bucket_name: bucket_name, zone: zone
42+
end
43+
assert_includes out_create, "Successfully created anywhereCache - #{anywhere_cache_name}."
44+
45+
out_list, _err = capture_io do
46+
list_anywhere_caches bucket_name: bucket_name
47+
end
48+
assert_includes out_list, "AnywhereCache #{anywhere_cache_name} found in list."
49+
50+
out_get, _err = capture_io do
51+
get_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
52+
end
53+
assert_includes out_get, "Successfully fetched anywhereCache - #{anywhere_cache_name}."
54+
55+
out_update, _err = capture_io do
56+
update_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
57+
end
58+
assert_includes out_update, "Successfully updated anywhereCache - #{anywhere_cache_name}."
59+
60+
out_pause, _err = capture_io do
61+
pause_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
62+
end
63+
assert_includes out_pause, "Successfully paused anywhereCache - #{anywhere_cache_name}."
64+
65+
out_resume, _err = capture_io do
66+
resume_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
67+
end
68+
assert_includes out_resume, "Successfully resumed anywhereCache - #{anywhere_cache_name}."
69+
70+
out_disable, _err = capture_io do
71+
disable_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
72+
end
73+
assert_includes out_disable, "Successfully disabled anywhereCache - #{anywhere_cache_name}."
74+
end
75+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_create_anywhere_cache]
16+
require "google/cloud/storage/control"
17+
18+
# Creates a new Anywhere Cache for a specified bucket and waits for it
19+
# to become active.
20+
#
21+
# This method initiates the creation of an Anywhere Cache in the given zone for
22+
# the specified bucket. After sending the creation request, it polls the status
23+
# of the cache with an exponential backoff strategy until the cache's state is
24+
# "running". Progress and final status are printed to the console.
25+
#
26+
# @param bucket_name [String] The name of the bucket.
27+
# @param zone [String] The zone where the Anywhere Cache instance should be
28+
# located (e.g., "us-east1-b").
29+
#
30+
# @example
31+
# create_anywhere_cache(
32+
# bucket_name: "your-unique-bucket-name",
33+
# zone: "us-east1-b"
34+
# )
35+
#
36+
def create_anywhere_cache bucket_name:, zone:
37+
# Create a client object. The client can be reused for multiple calls.
38+
storage_control_client = Google::Cloud::Storage::Control.storage_control
39+
# Set project to "_" to signify global bucket
40+
parent = "projects/_/buckets/#{bucket_name}"
41+
name = "#{parent}/anywhereCaches/#{zone}"
42+
43+
anywhere_cache = Google::Cloud::Storage::Control::V2::AnywhereCache.new(
44+
name: name,
45+
zone: zone
46+
)
47+
# Create a request.
48+
request = Google::Cloud::Storage::Control::V2::CreateAnywhereCacheRequest.new(
49+
parent: parent,
50+
anywhere_cache: anywhere_cache
51+
)
52+
# The request creates a new cache in the specified zone.
53+
# The cache is created in the specified bucket.
54+
begin
55+
operation = storage_control_client.create_anywhere_cache request
56+
puts "AnywhereCache operation created - #{operation.name}"
57+
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
58+
name: name
59+
)
60+
result = storage_control_client.get_anywhere_cache get_request
61+
min_delay = 30 # 30 seconds
62+
max_delay = 900 # 15 minutes
63+
while result.state&.downcase != "running"
64+
unless ["paused", "disabled", "creating"].include? result.state&.downcase
65+
raise Google::Cloud::Error,
66+
"AnywhereCache operation failed on the backend with state #{result.state&.downcase}."
67+
end
68+
puts "Cache not running yet, current state is #{result.state&.downcase}. Retrying in #{min_delay} seconds."
69+
sleep min_delay
70+
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
71+
result = storage_control_client.get_anywhere_cache get_request
72+
end
73+
puts "Successfully created anywhereCache - #{result.name}."
74+
rescue Google::Cloud::Error => e
75+
puts "Failed to create AnywhereCache. Error: #{e.message}"
76+
end
77+
end
78+
# [END storage_control_create_anywhere_cache]
79+
create_anywhere_cache bucket_name: ARGV.shift, zone: ARGV.shift if $PROGRAM_NAME == __FILE__
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_disable_anywhere_cache]
16+
require "google/cloud/storage/control"
17+
18+
# Disables a specific Anywhere Cache instance for a bucket.
19+
# This operation disables the cache but does not delete it. A disabled cache
20+
# can be re-enabled later.
21+
#
22+
# @param bucket_name [String] The name of the bucket.
23+
# @param anywhere_cache_id [String] A value that, along with the bucket's
24+
# name, uniquely identifies the cache instance (e.g., "us-east1-b").
25+
#
26+
# @example
27+
# disable_anywhere_cache(
28+
# bucket_name: "your-unique-bucket-name",
29+
# anywhere_cache_id: "us-east1-b"
30+
# )
31+
#
32+
def disable_anywhere_cache bucket_name:, anywhere_cache_id:
33+
# Create a client object. The client can be reused for multiple calls.
34+
storage_control_client = Google::Cloud::Storage::Control.storage_control
35+
# Set project to "_" to signify global bucket
36+
parent = "projects/_/buckets/#{bucket_name}"
37+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
38+
39+
# Create a request.
40+
request = Google::Cloud::Storage::Control::V2::DisableAnywhereCacheRequest.new(
41+
name: name
42+
)
43+
# The request disables the cache, but does not delete it.
44+
# The cache can be re-enabled later.
45+
begin
46+
result = storage_control_client.disable_anywhere_cache request
47+
puts "Successfully #{result.state&.downcase} anywhereCache - #{result.name}."
48+
rescue Google::Cloud::Error => e
49+
puts "Failed to disable AnywhereCache. Error: #{e.message}"
50+
end
51+
end
52+
# [END storage_control_disable_anywhere_cache]
53+
disable_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_get_anywhere_cache]
16+
require "google/cloud/storage/control"
17+
18+
# Retrieves an Anywhere Cache configuration for a given bucket.
19+
#
20+
# @param bucket_name [String] The name of the bucket.
21+
# @param anywhere_cache_id [String] A value that, along with the bucket's
22+
# name, uniquely identifies the cache. (e.g. "us-east1-b")
23+
#
24+
# @example
25+
# get_anywhere_cache(
26+
# bucket_name: "your-unique-bucket-name",
27+
# anywhere_cache_id: "us-east1-b"
28+
# )
29+
#
30+
def get_anywhere_cache bucket_name:, anywhere_cache_id:
31+
# Create a client object. The client can be reused for multiple calls.
32+
storage_control_client = Google::Cloud::Storage::Control.storage_control
33+
# Set project to "_" to signify global bucket
34+
parent = "projects/_/buckets/#{bucket_name}"
35+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
36+
37+
# Create a request.
38+
request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
39+
name: name
40+
)
41+
# The request retrieves the cache in the specified bucket.
42+
# The cache is identified by the specified ID.
43+
# The cache is in the specified bucket.
44+
45+
begin
46+
result = storage_control_client.get_anywhere_cache request
47+
puts "Successfully fetched anywhereCache - #{result.name}."
48+
rescue Google::Cloud::Error => e
49+
puts "Failed to fetch AnywhereCache. Error: #{e.message}"
50+
end
51+
end
52+
# [END storage_control_get_anywhere_cache]
53+
get_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START storage_control_list_anywhere_caches]
16+
require "google/cloud/storage/control"
17+
18+
def list_anywhere_caches bucket_name:
19+
# The Name of your GCS bucket
20+
# bucket_name = "your-unique-bucket-name"
21+
22+
# Create a client object. The client can be reused for multiple calls.
23+
storage_control_client = Google::Cloud::Storage::Control.storage_control
24+
# Set project to "_" to signify global bucket
25+
parent = "projects/_/buckets/#{bucket_name}"
26+
27+
# Create a request.
28+
request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
29+
parent: parent
30+
)
31+
# The request lists all caches in the specified bucket.
32+
# The caches are identified by the specified bucket name.
33+
begin
34+
result = storage_control_client.list_anywhere_caches request
35+
result.response.anywhere_caches.each do |item|
36+
puts "AnywhereCache #{item.name} found in list."
37+
end
38+
rescue Google::Cloud::Error => e
39+
puts "Failed to list AnywhereCaches. Error: #{e.message}"
40+
end
41+
end
42+
# [END storage_control_list_anywhere_caches]
43+
list_anywhere_caches bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__

0 commit comments

Comments
 (0)