diff --git a/google-cloud-storage-control/samples/acceptance/helper.rb b/google-cloud-storage-control/samples/acceptance/helper.rb index 5214e53dd6b2..0cad1c7f507f 100644 --- a/google-cloud-storage-control/samples/acceptance/helper.rb +++ b/google-cloud-storage-control/samples/acceptance/helper.rb @@ -82,14 +82,17 @@ def count_anywhere_caches bucket_name parent: parent ) result = storage_control_client.list_anywhere_caches request - min_delay = 180 # 3 minutes + min_delay = 30 # 30 seconds max_delay = 900 # 15 minutes + start_time = Time.now while result.response.anywhere_caches.count != 0 puts "Cache not deleted yet, Retrying in #{min_delay} seconds." sleep min_delay min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay result = storage_control_client.list_anywhere_caches request end - + end_time = Time.now + duration = end_time - start_time + puts "Total waiting time : #{duration.round(2)} seconds." result.response.anywhere_caches.count end diff --git a/google-cloud-storage-control/samples/acceptance/storage_control_anywhere_cache_test.rb b/google-cloud-storage-control/samples/acceptance/storage_control_anywhere_cache_test.rb index 0e42b17feee1..80b3a8063339 100644 --- a/google-cloud-storage-control/samples/acceptance/storage_control_anywhere_cache_test.rb +++ b/google-cloud-storage-control/samples/acceptance/storage_control_anywhere_cache_test.rb @@ -40,36 +40,36 @@ out_create, _err = capture_io do create_anywhere_cache bucket_name: bucket_name, zone: zone end - assert_includes out_create, "AnywhereCache created - #{anywhere_cache_name}" + assert_includes out_create, "Successfully created anywhereCache - #{anywhere_cache_name}." out_list, _err = capture_io do list_anywhere_caches bucket_name: bucket_name end - assert_includes out_list, "AnywhereCache #{anywhere_cache_name} found in list" + assert_includes out_list, "AnywhereCache #{anywhere_cache_name} found in list." out_get, _err = capture_io do get_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone end - assert_includes out_get, "AnywhereCache #{anywhere_cache_name} fetched" + assert_includes out_get, "Successfully fetched anywhereCache - #{anywhere_cache_name}." out_update, _err = capture_io do update_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone end - assert_includes out_update, "AnywhereCache #{anywhere_cache_name} updated" + assert_includes out_update, "Successfully updated anywhereCache - #{anywhere_cache_name}." out_pause, _err = capture_io do pause_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone end - assert_includes out_pause, "AnywhereCache #{anywhere_cache_name} paused" + assert_includes out_pause, "Successfully paused anywhereCache - #{anywhere_cache_name}." out_resume, _err = capture_io do resume_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone end - assert_includes out_resume, "AnywhereCache #{anywhere_cache_name} running" + assert_includes out_resume, "Successfully resumed anywhereCache - #{anywhere_cache_name}." out_disable, _err = capture_io do disable_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone end - assert_includes out_disable, "AnywhereCache #{anywhere_cache_name} disabled" + assert_includes out_disable, "Successfully disabled anywhereCache - #{anywhere_cache_name}." end end diff --git a/google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb index 8bf99bc0af61..4cb0f2871b28 100644 --- a/google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_create_anywhere_cache.rb @@ -15,13 +15,25 @@ # [START storage_control_create_anywhere_cache] require "google/cloud/storage/control" +# Creates a new Anywhere Cache for a specified bucket and waits for it +# to become active. +# +# This method initiates the creation of an Anywhere Cache in the given zone for +# the specified bucket. After sending the creation request, it polls the status +# of the cache with an exponential backoff strategy until the cache's state is +# "running". Progress and final status are printed to the console. +# +# @param bucket_name [String] The name of the bucket. +# @param zone [String] The zone where the Anywhere Cache instance should be +# located (e.g., "us-east1-b"). +# +# @example +# create_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# zone: "us-east1-b" +# ) +# def create_anywhere_cache bucket_name:, zone: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # Zone where you want to create cache - # zone = "your-zone-name" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -46,18 +58,27 @@ def create_anywhere_cache bucket_name:, zone: name: name ) result = storage_control_client.get_anywhere_cache get_request - min_delay = 180 # 3 minutes + min_delay = 30 # 30 seconds max_delay = 900 # 15 minutes - while result.state != "running" - puts "Cache not running yet, current state is #{result.state}. Retrying in #{min_delay} seconds." + start_time = Time.now + while result.state&.downcase != "running" + unless ["paused", "disabled", "creating"].include? result.state&.downcase + raise Google::Cloud::Error, + "AnywhereCache operation failed on the backend with state #{result.state&.downcase}." + end + puts "Cache not running yet, current state is #{result.state&.downcase}. Retrying in #{min_delay} seconds." sleep min_delay min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay result = storage_control_client.get_anywhere_cache get_request end - puts "AnywhereCache created - #{result.name}" + end_time = Time.now + duration = end_time - start_time + puts "Total polling time: #{duration.round(2)} seconds." + message = "Successfully created anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error creating AnywhereCache: #{e.message}" + message = "Failed to create AnywhereCache. Error: #{e.message}" end + puts message end # [END storage_control_create_anywhere_cache] create_anywhere_cache bucket_name: ARGV.shift, zone: ARGV.shift if $PROGRAM_NAME == __FILE__ diff --git a/google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb index 03d6b7c09cf5..32b1234979ea 100644 --- a/google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_disable_anywhere_cache.rb @@ -15,13 +15,21 @@ # [START storage_control_disable_anywhere_cache] require "google/cloud/storage/control" +# Disables a specific Anywhere Cache instance for a bucket. +# This operation disables the cache but does not delete it. A disabled cache +# can be re-enabled later. +# +# @param bucket_name [String] The name of the bucket. +# @param anywhere_cache_id [String] A value that, along with the bucket's +# name, uniquely identifies the cache instance (e.g., "us-east1-b"). +# +# @example +# disable_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# anywhere_cache_id: "us-east1-b" +# ) +# def disable_anywhere_cache bucket_name:, anywhere_cache_id: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # A value that, along with the bucket's name, uniquely identifies the cache - # anywhere_cache_id = "us-east1-b" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -36,9 +44,9 @@ def disable_anywhere_cache bucket_name:, anywhere_cache_id: # The cache can be re-enabled later. begin result = storage_control_client.disable_anywhere_cache request - puts "AnywhereCache #{result.name} #{result.state}" + puts "Successfully #{result.state&.downcase} anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error disabling AnywhereCache: #{e.message}" + puts "Failed to disable AnywhereCache. Error: #{e.message}" end end # [END storage_control_disable_anywhere_cache] diff --git a/google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb index 65b321fc0ccf..fc0675676ef9 100644 --- a/google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_get_anywhere_cache.rb @@ -15,13 +15,19 @@ # [START storage_control_get_anywhere_cache] require "google/cloud/storage/control" +# Retrieves an Anywhere Cache configuration for a given bucket. +# +# @param bucket_name [String] The name of the bucket. +# @param anywhere_cache_id [String] A value that, along with the bucket's +# name, uniquely identifies the cache. (e.g. "us-east1-b") +# +# @example +# get_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# anywhere_cache_id: "us-east1-b" +# ) +# def get_anywhere_cache bucket_name:, anywhere_cache_id: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # A value that, along with the bucket's name, uniquely identifies the cache - # anywhere_cache_id = "us-east1-b" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -38,9 +44,9 @@ def get_anywhere_cache bucket_name:, anywhere_cache_id: begin result = storage_control_client.get_anywhere_cache request - puts "AnywhereCache #{result.name} fetched" + puts "Successfully fetched anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error fetching AnywhereCache: #{e.message}" + puts "Failed to fetch AnywhereCache. Error: #{e.message}" end end # [END storage_control_get_anywhere_cache] diff --git a/google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb b/google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb index 397401531118..595d004f8cc9 100644 --- a/google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb +++ b/google-cloud-storage-control/samples/storage_control_list_anywhere_caches.rb @@ -33,10 +33,10 @@ def list_anywhere_caches bucket_name: begin result = storage_control_client.list_anywhere_caches request result.response.anywhere_caches.each do |item| - puts "AnywhereCache #{item.name} found in list" + puts "AnywhereCache #{item.name} found in list." end rescue Google::Cloud::Error => e - puts "Error listing AnywhereCaches: #{e.message}" + puts "Failed to list AnywhereCaches. Error: #{e.message}" end end # [END storage_control_list_anywhere_caches] diff --git a/google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb index 6c48d53e62a9..eec193b458dc 100644 --- a/google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_pause_anywhere_cache.rb @@ -15,13 +15,21 @@ # [START storage_control_pause_anywhere_cache] require "google/cloud/storage/control" +# Pauses a specific Anywhere Cache instance in a Cloud Storage bucket. +# This operation does not delete the cache; it can be resumed later using +# the `resume_anywhere_cache` method. +# +# @param bucket_name [String] The name of the Cloud Storage bucket. +# @param anywhere_cache_id [String] The ID of the Anywhere Cache instance to pause. +# This is often the zone where the cache is located, e.g., "us-east1-b". +# +# @example +# pause_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# anywhere_cache_id: "us-east1-b" +# ) +# def pause_anywhere_cache bucket_name:, anywhere_cache_id: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # A value that, along with the bucket's name, uniquely identifies the cache - # anywhere_cache_id = "us-east1-b" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -37,9 +45,9 @@ def pause_anywhere_cache bucket_name:, anywhere_cache_id: # The cache is paused in the specified bucket. begin result = storage_control_client.pause_anywhere_cache request - puts "AnywhereCache #{result.name} #{result.state}" + puts "Successfully #{result.state&.downcase} anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error pausing AnywhereCache: #{e.message}" + puts "Failed to pause AnywhereCache. Error: #{e.message}" end end # [END storage_control_pause_anywhere_cache] diff --git a/google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb index 43dce8488410..cce8ab60e570 100644 --- a/google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_resume_anywhere_cache.rb @@ -15,13 +15,23 @@ # [START storage_control_resume_anywhere_cache] require "google/cloud/storage/control" +# Resumes a paused Anywhere Cache instance. +# +# This method sends a request to the Storage Control API to resume a +# specific Anywhere Cache that was previously paused. +# +# @param bucket_name [String] The name of the bucket +# containing the cache. +# @param anywhere_cache_id [String] The unique identifier for the Anywhere +# Cache instance within the bucket. For example: "us-east1-b". +# +# @example +# resume_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# anywhere_cache_id: "us-east1-b" +# ) +# def resume_anywhere_cache bucket_name:, anywhere_cache_id: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # A value that, along with the bucket's name, uniquely identifies the cache - # anywhere_cache_id = "us-east1-b" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -37,9 +47,9 @@ def resume_anywhere_cache bucket_name:, anywhere_cache_id: # The cache is identified by the specified ID. begin result = storage_control_client.resume_anywhere_cache request - puts "AnywhereCache #{result.name} #{result.state}" + puts "Successfully resumed anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error resuming AnywhereCache: #{e.message}" + puts "Failed to resume AnywhereCache. Error: #{e.message}" end end # [END storage_control_resume_anywhere_cache] diff --git a/google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb b/google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb index 8dd3c5a43cac..5c70bc134777 100644 --- a/google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb +++ b/google-cloud-storage-control/samples/storage_control_update_anywhere_cache.rb @@ -15,12 +15,21 @@ # [START storage_control_update_anywhere_cache] require "google/cloud/storage/control" +# Updates an existing Anywhere Cache for a specified +# bucket. After initiating the update, it polls the cache's status with +# exponential backoff until the cache state becomes "running". +# +# @param bucket_name [String] The name of the GCS bucket containing the cache. +# @param anywhere_cache_id [String] The unique identifier for the Anywhere Cache. +# e.g. "us-east1-b" +# +# @example +# update_anywhere_cache( +# bucket_name: "your-unique-bucket-name", +# anywhere_cache_id: "us-east1-b" +# ) +# def update_anywhere_cache bucket_name:, anywhere_cache_id: - # The Name of your GCS bucket - # bucket_name = "your-unique-bucket-name" - # A value that, along with the bucket's name, uniquely identifies the cache - # anywhere_cache_id = "us-east1-b" - # Create a client object. The client can be reused for multiple calls. storage_control_client = Google::Cloud::Storage::Control.storage_control # Set project to "_" to signify global bucket @@ -47,18 +56,27 @@ def update_anywhere_cache bucket_name:, anywhere_cache_id: name: name ) result = storage_control_client.get_anywhere_cache get_request - min_delay = 120 # 2 minutes + min_delay = 30 # 30 seconds max_delay = 600 # 10 minutes - while result.state != "running" - puts "Cache not running yet, current state is #{result.state}. Retrying in #{min_delay} seconds." + start_time = Time.now + while result.state&.downcase != "running" + unless ["paused", "disabled", "creating"].include? result.state&.downcase + raise Google::Cloud::Error, + "AnywhereCache operation failed on the backend with state #{result.state&.downcase}." + end + puts "Cache not running yet, current state is #{result.state&.downcase}. Retrying in #{min_delay} seconds." sleep min_delay min_delay = [min_delay * 2, max_delay].min # Exponential backoff with a max delay result = storage_control_client.get_anywhere_cache get_request end - puts "AnywhereCache #{result.name} updated" + end_time = Time.now + duration = end_time - start_time + puts "Total waiting time : #{duration.round(2)} seconds." + message = "Successfully updated anywhereCache - #{result.name}." rescue Google::Cloud::Error => e - puts "Error updating AnywhereCache: #{e.message}" + message = "Failed to update AnywhereCache. Error: #{e.message}" end + puts message end # [END storage_control_update_anywhere_cache] update_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__