Skip to content

Commit 267c543

Browse files
updated
1 parent 23ee3d3 commit 267c543

9 files changed

Lines changed: 72 additions & 54 deletions

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

Lines changed: 31 additions & 0 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 ":", "-"
@@ -61,3 +62,33 @@ def retry_resource_exhaustion
6162
end
6263
raise Google::Cloud::ResourceExhaustedError, "Maybe take a break from creating and deleting buckets for a bit"
6364
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 = nil
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

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
# limitations under the License.
1414

1515
require_relative "helper"
16-
require "google/cloud/storage/control"
1716
require_relative "../storage_control_create_anywhere_cache"
1817
require_relative "../storage_control_list_anywhere_caches"
1918
require_relative "../storage_control_get_anywhere_cache"
2019
require_relative "../storage_control_update_anywhere_cache"
2120
require_relative "../storage_control_pause_anywhere_cache"
2221
require_relative "../storage_control_resume_anywhere_cache"
2322
require_relative "../storage_control_disable_anywhere_cache"
24-
require 'pry'
2523

2624
describe "Storage Control Anywhere Cache" do
2725
let(:bucket_name) { random_bucket_name }
@@ -30,11 +28,11 @@
3028
let(:anywhere_cache_name) { "projects/_/buckets/#{bucket_name}/anywhereCaches/#{zone}" }
3129

3230
before :all do
33-
bucket = create_bucket_helper bucket_name
31+
create_bucket_helper bucket_name
3432
end
3533

3634
after :all do
37-
delete_bucket_helper bucket_name #{}until count_anywhere_caches(bucket_name).zero?
35+
delete_bucket_helper bucket_name until count_anywhere_caches(bucket_name).zero?
3836
end
3937

4038
it "handles Anywhere cache lifecycle in sequence" do
@@ -74,15 +72,3 @@
7472
assert_includes out_disable, "AnywhereCache #{anywhere_cache_name} disabled"
7573
end
7674
end
77-
78-
# def count_anywhere_caches bucket_name
79-
# sleep 900
80-
# storage_control_client = Google::Cloud::Storage::Control.storage_control
81-
# # Set project to "_" to signify global bucket
82-
# parent = "projects/_/buckets/#{bucket_name}"
83-
# request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
84-
# parent: parent
85-
# )
86-
# result = storage_control_client.list_anywhere_caches request
87-
# result.response.anywhere_caches.count
88-
# end

google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def create_anywhere_cache bucket_name:, zone:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# Zone where you want to create cache
@@ -41,25 +41,21 @@ def create_anywhere_cache bucket_name:, zone:
4141
# The cache is created in the specified bucket.
4242
begin
4343
operation = storage_control_client.create_anywhere_cache request
44-
if operation.instance_of? Gapic::Operation
45-
puts "AnywhereCache operation created - #{operation.name}"
46-
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
47-
name: name
48-
)
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
4955
result = storage_control_client.get_anywhere_cache get_request
50-
min_delay = 900 # 15 minutes
51-
max_delay = 1800 # 30 minutes
52-
while result.state != "running"
53-
puts "Cache not running yet, current state is #{result.state}. Retrying in #{min_delay} seconds."
54-
sleep min_delay
55-
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
56-
result = storage_control_client.get_anywhere_cache get_request
57-
end
58-
puts "AnywhereCache created - #{result.name}"
59-
else
60-
puts "AnywhereCache create operation failed"
6156
end
62-
rescue Google::Cloud::Error=> e
57+
puts "AnywhereCache created - #{result.name}"
58+
rescue Google::Cloud::Error => e
6359
puts "Error creating AnywhereCache: #{e.message}"
6460
end
6561
end

google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def disable_anywhere_cache bucket_name:, anywhere_cache_id:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# A value that, along with the bucket's name, uniquely identifies the cache
@@ -37,7 +37,7 @@ def disable_anywhere_cache bucket_name:, anywhere_cache_id:
3737
begin
3838
result = storage_control_client.disable_anywhere_cache request
3939
puts "AnywhereCache #{result.name} #{result.state} "
40-
rescue StandardError => e
40+
rescue Google::Cloud::Error => e
4141
puts "Error disabling AnywhereCache: #{e.message}"
4242
end
4343
end

google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def get_anywhere_cache bucket_name:, anywhere_cache_id:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# A value that, along with the bucket's name, uniquely identifies the cache
@@ -39,7 +39,7 @@ def get_anywhere_cache bucket_name:, anywhere_cache_id:
3939
begin
4040
result = storage_control_client.get_anywhere_cache request
4141
puts "AnywhereCache #{result.name} fetched"
42-
rescue StandardError => e
42+
rescue Google::Cloud::Error => e
4343
puts "Error fetching AnywhereCache: #{e.message}"
4444
end
4545
end

google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def list_anywhere_caches bucket_name:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# Create a client object. The client can be reused for multiple calls.
@@ -35,7 +35,7 @@ def list_anywhere_caches bucket_name:
3535
result.response.anywhere_caches.each do |item|
3636
puts "AnywhereCache #{item.name} found in list"
3737
end
38-
rescue StandardError => e
38+
rescue Google::Cloud::Error => e
3939
puts "Error listing AnywhereCaches: #{e.message}"
4040
end
4141
end

google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def pause_anywhere_cache bucket_name:, anywhere_cache_id:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# A value that, along with the bucket's name, uniquely identifies the cache
@@ -38,7 +38,7 @@ def pause_anywhere_cache bucket_name:, anywhere_cache_id:
3838
begin
3939
result = storage_control_client.pause_anywhere_cache request
4040
puts "AnywhereCache #{result.name} #{result.state}"
41-
rescue StandardError => e
41+
rescue Google::Cloud::Error => e
4242
puts "Error pausing AnywhereCache: #{e.message}"
4343
end
4444
end

google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require "google/cloud/storage/control"
1717

1818
def resume_anywhere_cache bucket_name:, anywhere_cache_id:
19-
# The ID of your GCS bucket
19+
# The Name of your GCS bucket
2020
# bucket_name = "your-unique-bucket-name"
2121

2222
# A value that, along with the bucket's name, uniquely identifies the cache
@@ -38,7 +38,7 @@ def resume_anywhere_cache bucket_name:, anywhere_cache_id:
3838
begin
3939
result = storage_control_client.resume_anywhere_cache request
4040
puts "AnywhereCache #{result.name} #{result.state}"
41-
rescue StandardError => e
41+
rescue Google::Cloud::Error => e
4242
puts "Error resuming AnywhereCache: #{e.message}"
4343
end
4444
end

google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
require "google/cloud/storage/control/v2"
1818

1919
def update_anywhere_cache bucket_name:, anywhere_cache_id:
20-
# The ID of your GCS bucket
20+
# The Name of your GCS bucket
2121
# bucket_name = "your-unique-bucket-name"
2222
# A value that, along with the bucket's name, uniquely identifies the cache
2323
# anywhere_cache_id = "us-east1-b"
@@ -43,17 +43,22 @@ def update_anywhere_cache bucket_name:, anywhere_cache_id:
4343
# The cache is identified by the specified ID.
4444
begin
4545
operation = storage_control_client.update_anywhere_cache request
46-
if operation.instance_of? Gapic::Operation
47-
puts "AnywhereCache operation created - #{operation.name}"
48-
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
49-
name: name
50-
)
46+
puts "AnywhereCache operation created - #{operation.name}"
47+
get_request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
48+
name: name
49+
)
50+
result = storage_control_client.get_anywhere_cache get_request
51+
min_delay = 120 # 2 minutes
52+
max_delay = 600 # 10 minutes
53+
while result.state != "running"
54+
puts "Cache not running yet, current state is #{result.state}. Retrying in #{min_delay} seconds."
55+
sleep min_delay
56+
min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay
5157
result = storage_control_client.get_anywhere_cache get_request
52-
puts "AnywhereCache #{result.name} updated"
53-
else
54-
puts "AnywhereCache update operation failed"
5558
end
56-
rescue StandardError => e
59+
puts "AnywhereCache #{result.name} updated"
60+
61+
rescue Google::Cloud::Error => e
5762
puts "Error updating AnywhereCache: #{e.message}"
5863
end
5964
end

0 commit comments

Comments
 (0)