Skip to content

Commit e7300d5

Browse files
WIP
wip wip get and list working pause_disable_resume_working adding all samples adding comments adding comment updates Update storage_control_create_anywhere_cache.rb Update storage_control_disable_anywhere_cache.rb updates updates Update storage_control_create_anywhere_cache.rb Update storage_control_pause_anywhere_cache.rb updating removing reference for V2 library fix lint Update storage_control_update_anywhere_cache.rb try refreshing operation status try operation refresh try get request fix syntax error updating test cases with delay fix lint issue lint fix wip updated updated update try try try try try
1 parent c4bf7c2 commit e7300d5

9 files changed

Lines changed: 463 additions & 2 deletions

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

Lines changed: 34 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 ":", "-"
@@ -26,9 +27,11 @@ def random_folder_name prefix: "ruby-storage-control-folder-samples-test-"
2627
t = Time.now.utc.iso8601.gsub ":", "-"
2728
"#{prefix}-#{t}-#{SecureRandom.hex 4}".downcase
2829
end
30+
def storage_client
31+
@storage_client ||= Google::Cloud::Storage.new
32+
end
2933

3034
def create_bucket_helper bucket_name, uniform_bucket_level_access: nil, hierarchical_namespace: nil
31-
storage_client = Google::Cloud::Storage.new
3235
retry_resource_exhaustion do
3336
storage_client.create_bucket bucket_name do |b|
3437
b.uniform_bucket_level_access = uniform_bucket_level_access
@@ -38,7 +41,6 @@ def create_bucket_helper bucket_name, uniform_bucket_level_access: nil, hierarch
3841
end
3942

4043
def delete_bucket_helper bucket_name
41-
storage_client = Google::Cloud::Storage.new
4244
retry_resource_exhaustion do
4345
bucket = storage_client.bucket bucket_name
4446
return unless bucket
@@ -60,3 +62,33 @@ def retry_resource_exhaustion
6062
end
6163
raise Google::Cloud::ResourceExhaustedError, "Maybe take a break from creating and deleting buckets for a bit"
6264
end
65+
66+
# Waits until all Anywhere Caches for a given bucket are deleted.
67+
#
68+
# This method polls the Storage Control API, listing the Anywhere Caches
69+
# associated with the specified bucket. If caches are found, it waits and
70+
# retries with an exponential backoff strategy until no caches remain.
71+
#
72+
# @param bucket_name [String] The name of the Google Cloud Storage bucket.
73+
# @return [Integer] The final count of Anywhere Caches, which will be 0
74+
# the method completes successfully after all caches are deleted.
75+
def count_anywhere_caches bucket_name
76+
storage_control_client = Google::Cloud::Storage::Control.storage_control
77+
78+
# Set project to "_" to signify global bucket
79+
parent = "projects/_/buckets/#{bucket_name}"
80+
request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
81+
parent: parent
82+
)
83+
result = storage_control_client.list_anywhere_caches request
84+
min_delay = 180 # 3 minutes
85+
max_delay = 900 # 15 minutes
86+
while result.response.anywhere_caches.count != 0
87+
puts "Cache not deleted yet, Retrying in #{min_delay} seconds."
88+
sleep min_delay
89+
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
90+
result = storage_control_client.list_anywhere_caches request
91+
end
92+
93+
result.response.anywhere_caches.count
94+
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, "AnywhereCache created - #{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, "AnywhereCache #{anywhere_cache_name} fetched"
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, "AnywhereCache #{anywhere_cache_name} updated"
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, "AnywhereCache #{anywhere_cache_name} paused"
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, "AnywhereCache #{anywhere_cache_name} running"
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, "AnywhereCache #{anywhere_cache_name} disabled"
74+
end
75+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
def create_anywhere_cache bucket_name:, zone:
19+
# The Name of your GCS bucket
20+
# bucket_name = "your-unique-bucket-name"
21+
22+
# Zone where you want to create cache
23+
# zone = "your-zone-name"
24+
25+
# Create a client object. The client can be reused for multiple calls.
26+
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
28+
parent = "projects/_/buckets/#{bucket_name}"
29+
name = "#{parent}/anywhereCaches/#{zone}"
30+
31+
anywhere_cache = Google::Cloud::Storage::Control::V2::AnywhereCache.new(
32+
name: name,
33+
zone: zone
34+
)
35+
# Create a request.
36+
request = Google::Cloud::Storage::Control::V2::CreateAnywhereCacheRequest.new(
37+
parent: parent,
38+
anywhere_cache: anywhere_cache
39+
)
40+
# The request creates a new cache in the specified zone.
41+
# The cache is created in the specified bucket.
42+
begin
43+
operation = storage_control_client.create_anywhere_cache request
44+
puts "AnywhereCache operation created - #{operation.name}"
45+
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
46+
name: name
47+
)
48+
result = storage_control_client.get_anywhere_cache get_request
49+
min_delay = 180 # 3 minutes
50+
max_delay = 900 # 15 minutes
51+
while result.state != "running"
52+
puts "Cache not running yet, current state is #{result.state}. Retrying in #{min_delay} seconds."
53+
sleep min_delay
54+
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
55+
result = storage_control_client.get_anywhere_cache get_request
56+
end
57+
puts "AnywhereCache created - #{result.name}"
58+
rescue Google::Cloud::Error => e
59+
puts "Error creating AnywhereCache: #{e.message}"
60+
end
61+
end
62+
# [END storage_control_create_anywhere_cache]
63+
create_anywhere_cache bucket_name: ARGV.shift, zone: ARGV.shift if $PROGRAM_NAME == __FILE__
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
def disable_anywhere_cache bucket_name:, anywhere_cache_id:
19+
# The Name of your GCS bucket
20+
# bucket_name = "your-unique-bucket-name"
21+
22+
# A value that, along with the bucket's name, uniquely identifies the cache
23+
# anywhere_cache_id = "us-east1-b"
24+
25+
# Create a client object. The client can be reused for multiple calls.
26+
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
28+
parent = "projects/_/buckets/#{bucket_name}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
30+
31+
# Create a request.
32+
request = Google::Cloud::Storage::Control::V2::DisableAnywhereCacheRequest.new(
33+
name: name
34+
)
35+
# The request disables the cache, but does not delete it.
36+
# The cache can be re-enabled later.
37+
begin
38+
result = storage_control_client.disable_anywhere_cache request
39+
puts "AnywhereCache #{result.name} #{result.state}"
40+
rescue Google::Cloud::Error => e
41+
puts "Error disabling AnywhereCache: #{e.message}"
42+
end
43+
end
44+
# [END storage_control_disable_anywhere_cache]
45+
disable_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
def get_anywhere_cache bucket_name:, anywhere_cache_id:
19+
# The Name of your GCS bucket
20+
# bucket_name = "your-unique-bucket-name"
21+
22+
# A value that, along with the bucket's name, uniquely identifies the cache
23+
# anywhere_cache_id = "us-east1-b"
24+
25+
# Create a client object. The client can be reused for multiple calls.
26+
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
28+
parent = "projects/_/buckets/#{bucket_name}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
30+
31+
# Create a request.
32+
request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
33+
name: name
34+
)
35+
# The request retrieves the cache in the specified bucket.
36+
# The cache is identified by the specified ID.
37+
# The cache is in the specified bucket.
38+
39+
begin
40+
result = storage_control_client.get_anywhere_cache request
41+
puts "AnywhereCache #{result.name} fetched"
42+
rescue Google::Cloud::Error => e
43+
puts "Error fetching AnywhereCache: #{e.message}"
44+
end
45+
end
46+
# [END storage_control_get_anywhere_cache]
47+
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 "Error listing AnywhereCaches: #{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__
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_pause_anywhere_cache]
16+
require "google/cloud/storage/control"
17+
18+
def pause_anywhere_cache bucket_name:, anywhere_cache_id:
19+
# The Name of your GCS bucket
20+
# bucket_name = "your-unique-bucket-name"
21+
22+
# A value that, along with the bucket's name, uniquely identifies the cache
23+
# anywhere_cache_id = "us-east1-b"
24+
25+
# Create a client object. The client can be reused for multiple calls.
26+
storage_control_client = Google::Cloud::Storage::Control.storage_control
27+
# Set project to "_" to signify global bucket
28+
parent = "projects/_/buckets/#{bucket_name}"
29+
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
30+
31+
# Create a request.
32+
request = Google::Cloud::Storage::Control::V2::PauseAnywhereCacheRequest.new(
33+
name: name
34+
)
35+
# The request pauses the cache, but does not delete it.
36+
# The cache can be resumed later.
37+
# The cache is paused in the specified bucket.
38+
begin
39+
result = storage_control_client.pause_anywhere_cache request
40+
puts "AnywhereCache #{result.name} #{result.state}"
41+
rescue Google::Cloud::Error => e
42+
puts "Error pausing AnywhereCache: #{e.message}"
43+
end
44+
end
45+
# [END storage_control_pause_anywhere_cache]
46+
pause_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__

0 commit comments

Comments
 (0)