Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
877b90a
WIP
shubhangi-google Jun 16, 2025
e036c60
wip
shubhangi-google Jun 20, 2025
33a8d55
wip
shubhangi-google Jun 20, 2025
7f5d121
get and list working
shubhangi-google Jun 20, 2025
1dcd5bb
pause_disable_resume_working
shubhangi-google Jun 24, 2025
f99baa4
adding all samples
shubhangi-google Jul 1, 2025
65d5833
adding comments
shubhangi-google Jul 3, 2025
d8498e4
adding comment
shubhangi-google Jul 3, 2025
842bdf2
updates
shubhangi-google Jul 3, 2025
bd0b9cd
Update storage_control_create_anywhere_cache.rb
shubhangi-google Jul 3, 2025
3b9cda3
Update storage_control_disable_anywhere_cache.rb
shubhangi-google Jul 3, 2025
feb8f47
updates
shubhangi-google Jul 3, 2025
af3c7ef
updates
shubhangi-google Jul 4, 2025
e69000b
Update storage_control_create_anywhere_cache.rb
shubhangi-google Jul 4, 2025
808d1e9
Update storage_control_pause_anywhere_cache.rb
shubhangi-google Jul 4, 2025
54f2c14
updating
shubhangi-google Jul 4, 2025
7edf04d
removing reference for V2 library
shubhangi-google Jul 11, 2025
4b4d0a6
fix lint
shubhangi-google Jul 15, 2025
90f3a0b
Update storage_control_update_anywhere_cache.rb
shubhangi-google Aug 1, 2025
93af0da
try refreshing operation status
shubhangi-google Sep 2, 2025
2b29efc
try operation refresh
shubhangi-google Sep 2, 2025
b78928f
try get request
shubhangi-google Sep 2, 2025
b6020d3
fix syntax error
shubhangi-google Sep 3, 2025
fecdaa6
updating test cases with delay
shubhangi-google Sep 7, 2025
78b6dbf
fix lint issue
shubhangi-google Sep 7, 2025
cecd6d9
lint fix
shubhangi-google Sep 7, 2025
2b508ea
Update storage_control_anywhere_cache_test.rb
shubhangi-google Sep 7, 2025
ed9c641
Update README.md
shubhangi-google Sep 7, 2025
3dace3d
Update storage_control_anywhere_cache_test.rb
shubhangi-google Sep 7, 2025
3a70b81
merging main
shubhangi-google Sep 22, 2025
ace1464
lint fix
shubhangi-google Sep 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions google-cloud-storage-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ The `google-apis-` clients have wide coverage across Google services, so you
might need to use one if there is no modern client available for the service.
However, if a modern client is available, we generally recommend it over the
older `google-apis-` clients.

## Note

Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The phrasing 'Anywhere cache Sample' is a bit awkward. For better grammar and clarity, I suggest changing it to 'the Anywhere Cache samples'.

Suggested change
Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations.
Tests for the Anywhere Cache samples are not included due to the long operation times typical for cache operations.


Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2025 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
#
# http://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.

require_relative "helper"
require_relative "../storage_control_create_anywhere_cache"
require_relative "../storage_control_list_anywhere_caches"
require_relative "../storage_control_get_anywhere_cache"
require_relative "../storage_control_update_anywhere_cache"
require_relative "../storage_control_pause_anywhere_cache"
require_relative "../storage_control_resume_anywhere_cache"
require_relative "../storage_control_disable_anywhere_cache"

# require 'pry'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This commented-out require 'pry' seems to be a development artifact and should be removed to keep the code clean.


describe "Storage Control Anywhere Cache" do
let(:bucket_name) { random_bucket_name }
let(:storage_client) { Google::Cloud::Storage.new }
let(:zone) {'us-east1-b'}
# let(:zone) {@bucket.location}

before :all do
@bucket= storage_client.bucket bucket_name
@bucket = create_bucket_helper bucket_name
end
Comment on lines +30 to +32
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This before :all block has a few issues:

  1. It attempts to use storage_client and bucket_name, which are defined with let and are not available in a before :all context. This will cause the test setup to fail. You should use a before (or before :each) block instead.
  2. Line 33 is redundant as @bucket is immediately reassigned on the next line.
  3. Line 34 has an extra leading space, which affects readability.
  before do
    @bucket = create_bucket_helper bucket_name
  end


# after do
# delete_bucket_helper bucket_name
# end

it "create Anywhere cache" do
create_anywhere_cache bucket_name: bucket_name, zone: zone
end

# it "list Anywhere cache" do
# # create_anywhere_cache bucket_name: bucket_name, zone: zone

# out, _err = capture_io do
# list_anywhere_caches bucket_name: bucket_name
# end

# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
# end

# it "Get Anywhere cache" do
# out, _err = capture_io do
# get_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
# end
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
# end

# it "Pause Anywhere cache" do
# out, _err = capture_io do
# pause_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
# end
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} paused"
# end

# it "Resume Anywhere cache" do
# out, _err = capture_io do
# resume_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
# end
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} running"
# end

# it "Disable Anywhere cache" do
# out, _err = capture_io do
# disable_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
# end
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone} disabled"
# end

# it "Update Anywhere cache" do
# out, _err = capture_io do
# update_anywhere_cache bucket_name: bucket_name, anywhere_cache_id: zone
# end
# assert_includes out, "#{bucket_name}/anywhereCaches/#{zone}"
# end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_create_anywhere_cache]
require "google/cloud/storage/control"

def create_anywhere_cache bucket_name:, zone:
# The ID 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
parent = "projects/_/buckets/#{bucket_name}"
name = "#{parent}/anywhereCaches/#{zone}"

anywhere_cache = Google::Cloud::Storage::Control::V2::AnywhereCache.new(
name: name,
zone: zone
)
# Create a request.
request = Google::Cloud::Storage::Control::V2::CreateAnywhereCacheRequest.new(
parent: parent,
anywhere_cache: anywhere_cache
)
# The request creates a new cache in the specified zone.
# The cache is created in the specified bucket.
begin
operation = storage_control_client.create_anywhere_cache request

puts "********************AnywhereCache operation created - #{operation.name}"
while !operation.done?
sleep 3600 # Wait for 1 hour before checking again
operation.refresh!
end
puts "********************AnywhereCache create operation completed - #{operation.name}"

rescue StandardError => e
puts "Error creating AnywhereCache: #{e.message}"
end
Comment on lines +42 to +60
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation for handling the long-running operation is not ideal. Sleeping for a fixed hour is inefficient and provides a poor user experience. The client library provides a much better way to handle this using operation.wait_until_done!.

This method polls the operation status with exponential backoff and also simplifies error handling. Additionally, rescuing StandardError is too broad; it's better to rescue more specific exceptions like Google::Cloud::Error.

The output messages could also be more descriptive.

  begin
    operation = storage_control_client.create_anywhere_cache request

    puts "Waiting for Anywhere Cache creation operation to complete... (name: #{operation.name})"
    operation.wait_until_done!

    if operation.error?
      puts "Error creating AnywhereCache: #{operation.error.message}"
    else
      puts "AnywhereCache created: #{operation.response.name}"
    end
  rescue Google::Cloud::Error => e
    puts "Error creating AnywhereCache: #{e.message}"
  end

end
# [END storage_control_create_anywhere_cache]
create_anywhere_cache bucket_name: ARGV.shift, zone: ARGV.shift if $PROGRAM_NAME == __FILE__
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_disable_anywhere_cache]
require "google/cloud/storage/control"

def disable_anywhere_cache bucket_name:, anywhere_cache_id:
# The ID 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
parent = "projects/_/buckets/#{bucket_name}"
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's an extra space in this assignment, which should be removed for code consistency.

  name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"


# Create a request.
request = Google::Cloud::Storage::Control::V2::DisableAnywhereCacheRequest.new(
name: name
)
# The request disables the cache, but does not delete it.
# The cache can be re-enabled later.
begin
result = storage_control_client.disable_anywhere_cache request
puts "AnywhereCache #{result.name} #{result.state}"
rescue StandardError => e
puts "Error disabling AnywhereCache: #{e.message}"
end
Comment on lines +37 to +42
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block can be improved by:

  1. Rescuing a more specific Google::Cloud::Error instead of the broad StandardError.
  2. Providing a more user-friendly output message.
  begin
    result = storage_control_client.disable_anywhere_cache request
    puts "AnywhereCache #{result.name} is now #{result.state.to_s.downcase}."
  rescue Google::Cloud::Error => e
    puts "Error disabling AnywhereCache: #{e.message}"
  end

end
# [END storage_control_disable_anywhere_cache]
disable_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_get_anywhere_cache]
require "google/cloud/storage/control"

def get_anywhere_cache bucket_name:, anywhere_cache_id:
# The ID 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
parent = "projects/_/buckets/#{bucket_name}"
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's an extra space in this assignment, which should be removed for code consistency.

  name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"


# Create a request. Replace the placeholder values with actual data.
request = Google::Cloud::Storage::Control::V2::GetAnywhereCacheRequest.new(
name: name
)
# The request retrieves the cache in the specified bucket.
# The cache is identified by the specified ID.
# The cache is in the specified bucket.

begin
result = storage_control_client.get_anywhere_cache request
puts "AnywhereCache fetched - #{result.name}"
rescue StandardError => e
puts "Error fetching AnywhereCache: #{e.message}"
end
Comment on lines +39 to +44
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block can be improved by:

  1. Rescuing a more specific Google::Cloud::Error instead of StandardError.
  2. Providing a more informative output for the user. For a 'get' operation, it's helpful to display some of the resource's attributes.
  begin
    result = storage_control_client.get_anywhere_cache request
    puts "Fetched AnywhereCache:"
    puts "- Name: #{result.name}"
    puts "- State: #{result.state}"
    puts "- TTL: #{result.ttl.seconds} seconds"
  rescue Google::Cloud::Error => e
    puts "Error fetching AnywhereCache: #{e.message}"
  end

end
# [END storage_control_get_anywhere_cache]
get_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_list_anywhere_caches]
require "google/cloud/storage/control"

def list_anywhere_caches bucket_name:
# The ID of your GCS bucket
# bucket_name = "your-unique-bucket-name"

# Create a client object. The client can be reused for multiple calls.
storage_control_client = Google::Cloud::Storage::Control.storage_control
parent = "projects/_/buckets/#{bucket_name}"

request = Google::Cloud::Storage::Control::V2::ListAnywhereCachesRequest.new(
parent: parent
)
# The request lists all caches in the specified bucket.
# The caches are identified by the specified bucket name.
begin
result = storage_control_client.list_anywhere_caches request
result.response.anywhere_caches.each do |item|
puts item.name
end
rescue StandardError => e
puts "Error listing AnywhereCaches: #{e.message}"
end
Comment on lines +33 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current implementation result.response.anywhere_caches.each will only process the first page of results from the list operation. To correctly iterate through all pages of results, you should iterate directly over the PagedEnumerable object returned by the client.

I also suggest improving the output to be more user-friendly and rescuing a more specific error type than StandardError.

  begin
    puts "Listing Anywhere Caches for bucket \"#{bucket_name}\"..."
    result = storage_control_client.list_anywhere_caches request
    result.each do |cache|
      puts cache.name
    end
  rescue Google::Cloud::Error => e
    puts "Error listing AnywhereCaches: #{e.message}"
  end

end
# [END storage_control_list_anywhere_caches]
list_anywhere_caches bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_pause_anywhere_cache]
require "google/cloud/storage/control"

def pause_anywhere_cache bucket_name:, anywhere_cache_id:
# The ID 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
parent = "projects/_/buckets/#{bucket_name}"
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's an extra space in this assignment, which should be removed for code consistency.

  name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"


# Create a request. Replace the placeholder values with actual data.
request = Google::Cloud::Storage::Control::V2::PauseAnywhereCacheRequest.new(
name: name
)
# The request pauses the cache, but does not delete it.
# The cache can be resumed later.
# The cache is paused in the specified bucket.
begin
result = storage_control_client.pause_anywhere_cache request
puts "AnywhereCache #{result.name} #{result.state}"
Comment on lines +39 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The pause_anywhere_cache method returns a Gapic::Operation for a long-running operation. The code incorrectly treats it as a synchronous call that returns the final resource. You need to handle the LRO, for instance by calling wait_until_done! on the operation to wait for it to finish.

    operation = storage_control_client.pause_anywhere_cache request
    puts "Waiting for pause operation to complete..."
    operation.wait_until_done!
    raise operation.error if operation.error?
    result = operation.result
    puts "AnywhereCache #{result.name} #{result.state}"

rescue StandardError => e
puts "Error pausing AnywhereCache: #{e.message}"
end
Comment on lines +38 to +43
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block can be improved by:

  1. Rescuing a more specific Google::Cloud::Error instead of StandardError.
  2. Providing a more user-friendly output message.
  begin
    result = storage_control_client.pause_anywhere_cache request
    puts "AnywhereCache #{result.name} is now #{result.state.to_s.downcase}."
  rescue Google::Cloud::Error => e
    puts "Error pausing AnywhereCache: #{e.message}"
  end

end
# [END storage_control_pause_anywhere_cache]
pause_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2025 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
#
# http://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.

# [START storage_control_resume_anywhere_cache]
require "google/cloud/storage/control"

def resume_anywhere_cache bucket_name:, anywhere_cache_id:
# The ID 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
parent = "projects/_/buckets/#{bucket_name}"
name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"

# Create a request.
request = Google::Cloud::Storage::Control::V2::ResumeAnywhereCacheRequest.new(
name: name
)
# The request resumes the cache, which was previously paused.
# The cache is resumed in the specified bucket.
# The cache is identified by the specified ID.
begin
result = storage_control_client.resume_anywhere_cache request
puts "AnywhereCache #{result.name} #{result.state}"
Comment on lines +39 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The resume_anywhere_cache method returns a Gapic::Operation for a long-running operation. The code incorrectly treats it as a synchronous call. Please handle the LRO, for example by calling wait_until_done! on the returned operation object.

    operation = storage_control_client.resume_anywhere_cache request
    puts "Waiting for resume operation to complete..."
    operation.wait_until_done!
    raise operation.error if operation.error?
    result = operation.result
    puts "AnywhereCache #{result.name} #{result.state}"

rescue StandardError => e
puts "Error resuming AnywhereCache: #{e.message}"
end
Comment on lines +38 to +43
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block can be improved by:

  1. Rescuing a more specific Google::Cloud::Error instead of StandardError.
  2. Providing a more user-friendly output message. The current state is ACTIVE, but the commented-out test expects running. A clearer message would be better for users.
  begin
    result = storage_control_client.resume_anywhere_cache request
    puts "AnywhereCache #{result.name} has been resumed and is now #{result.state.to_s.downcase}."
  rescue Google::Cloud::Error => e
    puts "Error resuming AnywhereCache: #{e.message}"
  end

end
# [END storage_control_resume_anywhere_cache]
resume_anywhere_cache bucket_name: ARGV.shift, anywhere_cache_id: ARGV.shift if $PROGRAM_NAME == __FILE__
Loading