From 7a6a3652261e63400cb784f6e52630d40595ab13 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 25 Nov 2024 13:52:13 +0000 Subject: [PATCH 01/25] WIP- able to fetch generation and list of soft deleted bucket --- .../lib/google/cloud/storage/bucket.rb | 15 +++++- .../lib/google/cloud/storage/bucket/list.rb | 21 ++++---- .../lib/google/cloud/storage/project.rb | 19 +++++-- .../samples/storage_restore_bucket.rb | 54 +++++++++++++++++++ 4 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 google-cloud-storage/samples/storage_restore_bucket.rb diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 5f80729ad74a..5e5e47d68441 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -23,6 +23,8 @@ require "google/cloud/storage/policy" require "google/cloud/storage/post_object" require "pathname" +require "pry" + module Google module Cloud @@ -81,6 +83,8 @@ class Bucket # files = bucket.files # Billed to "my-other-project" # attr_accessor :user_project + attr_accessor :soft_deleted + attr_accessor :generation ## # @private Create an empty Bucket object. @@ -222,6 +226,10 @@ def cors cors_builder.freeze # always return frozen objects end + def generation + @gapi.generation + end + ## # Returns the current Object Lifecycle Management rules configuration # for the bucket. @@ -2450,6 +2458,10 @@ def post_object path, policy: policy end + def generation + @gapi.generation + end + ## # Generate a `PostObject` that includes the fields and URL to # upload objects via HTML forms. The resulting `PostObject` is @@ -3144,11 +3156,12 @@ def lazy? ## # @private New Bucket from a Google API Client object. - def self.from_gapi gapi, service, user_project: nil + def self.from_gapi gapi, service, user_project: nil, soft_deleted: nil new.tap do |b| b.gapi = gapi b.service = service b.user_project = user_project + b.soft_deleted = soft_deleted end end diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb index e596fb4fda8a..b6a6492e3b27 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb @@ -14,6 +14,7 @@ require "delegate" +require "pry" module Google module Cloud @@ -146,16 +147,16 @@ def all request_limit: nil, &block # @private New Bucket::List from a Google API Client # Google::Apis::StorageV1::Buckets object. def self.from_gapi gapi_list, service, prefix = nil, max = nil, - user_project: nil - buckets = new(Array(gapi_list.items).map do |gapi_object| - Bucket.from_gapi gapi_object, service, user_project: user_project - end) - buckets.instance_variable_set :@token, gapi_list.next_page_token - buckets.instance_variable_set :@service, service - buckets.instance_variable_set :@prefix, prefix - buckets.instance_variable_set :@max, max - buckets.instance_variable_set :@user_project, user_project - buckets + user_project: nil, soft_deleted: nil + buckets = new(Array(gapi_list.items).map do |gapi_object| + Bucket.from_gapi gapi_object, service, user_project: user_project, soft_deleted: soft_deleted + end) + buckets.instance_variable_set :@token, gapi_list.next_page_token + buckets.instance_variable_set :@service, service + buckets.instance_variable_set :@prefix, prefix + buckets.instance_variable_set :@max, max + buckets.instance_variable_set :@user_project, user_project + buckets end protected diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 89ee56952620..db363b103cc5 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -193,11 +193,11 @@ def add_custom_header header_name, header_value # puts bucket.name # end # - def buckets prefix: nil, token: nil, max: nil, user_project: nil + def buckets prefix: nil, token: nil, max: nil, user_project: nil , soft_deleted: nil gapi = service.list_buckets \ - prefix: prefix, token: token, max: max, user_project: user_project + prefix: prefix, token: token, max: max, user_project: user_project,options: {soft_deleted: soft_deleted} Bucket::List.from_gapi \ - gapi, service, prefix, max, user_project: user_project + gapi, service, prefix, max, user_project: user_project, soft_deleted: soft_deleted end alias find_buckets buckets @@ -259,6 +259,8 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil # def bucket bucket_name, skip_lookup: false, + generation: nil, + soft_deleted: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, user_project: nil @@ -269,8 +271,15 @@ def bucket bucket_name, gapi = service.get_bucket bucket_name, if_metageneration_match: if_metageneration_match, if_metageneration_not_match: if_metageneration_not_match, - user_project: user_project - Bucket.from_gapi gapi, service, user_project: user_project + user_project: user_project, + options: { + soft_deleted: soft_deleted, + generation: generation + + } + + binding.pry + Bucket.from_gapi gapi, service, user_project: user_project,soft_deleted: soft_deleted,generation: generation rescue Google::Cloud::NotFoundError nil end diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb new file mode 100644 index 000000000000..8f62c06b8243 --- /dev/null +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -0,0 +1,54 @@ +# Copyright 2024 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_delete_bucket] +def delete_bucket bucket_name: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + #{}require "google/cloud/storage" + require_relative '../lib/google/cloud/storage' + require_relative '../lib/google/cloud/storage/project' + require_relative '../lib/google/cloud/storage/bucket' + # require_relative '../lib/google/cloud/storage/bucket/list' + require_relative '../lib/google/cloud/storage/service' + + require 'pry' + + storage = Google::Cloud::Storage.new + deleted_bucket = storage.create_bucket bucket_name + + deleted_bucket.delete + + # fetching generation + generation = deleted_bucket.generation + + # fetching soft deleted buckets + deleted_buckets = storage.buckets soft_deleted: true + + #{}storage.bucket deleted_bucket.name, generation: generation, soft_deleted: true + + puts "Deleted bucket: #{deleted_bucket.name}" + puts deleted_bucket + puts "bucket generation #{generation}" + puts "count of soft deleted buckets #{deleted_buckets.count}" + #{}puts Gem.loaded_specs["google-cloud-storage"].full_gem_path + +end +# [END storage_delete_bucket] + +bucket_name = "ruby_try_2" +delete_bucket bucket_name: bucket_name + +#{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ From f0e1d77a20830b141a871bf5e7cee85f8d946bf7 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 27 Nov 2024 05:37:26 +0000 Subject: [PATCH 02/25] wip- fetch soft deleted bucket and restore bucket --- .../lib/google/cloud/storage/bucket.rb | 7 ++--- .../lib/google/cloud/storage/project.rb | 25 ++++++++++++--- .../lib/google/cloud/storage/service.rb | 25 +++++++++++++++ .../samples/storage_restore_bucket.rb | 31 ++++++++++++++----- 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 5e5e47d68441..8174f38049bb 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -2458,10 +2458,6 @@ def post_object path, policy: policy end - def generation - @gapi.generation - end - ## # Generate a `PostObject` that includes the fields and URL to # upload objects via HTML forms. The resulting `PostObject` is @@ -3156,11 +3152,12 @@ def lazy? ## # @private New Bucket from a Google API Client object. - def self.from_gapi gapi, service, user_project: nil, soft_deleted: nil + def self.from_gapi gapi, service, user_project: nil,generation: nil, soft_deleted: nil new.tap do |b| b.gapi = gapi b.service = service b.user_project = user_project + b.generation = generation b.soft_deleted = soft_deleted end end diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index db363b103cc5..ff18095fd2ba 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -272,13 +272,9 @@ def bucket bucket_name, if_metageneration_match: if_metageneration_match, if_metageneration_not_match: if_metageneration_not_match, user_project: user_project, - options: { - soft_deleted: soft_deleted, + soft_deleted: soft_deleted, generation: generation - } - - binding.pry Bucket.from_gapi gapi, service, user_project: user_project,soft_deleted: soft_deleted,generation: generation rescue Google::Cloud::NotFoundError nil @@ -563,6 +559,25 @@ def hmac_keys service_account_email: nil, project_id: nil, max: max, user_project: user_project end + def restore_bucket bucket_name, + generation, + soft_deleted: nil, + timeout: nil, + if_generation_match: nil, + if_generation_not_match: nil, + projection: nil, + user_project: nil, + options: {soft_deleted: nil} + + gapi = service.restore_bucket \ + bucket_name, generation, + if_generation_match: if_generation_match, + if_generation_not_match: if_generation_not_match, + user_project: user_project, + options: options + Bucket.from_gapi gapi, service, user_project: user_project, generation: generation + end + ## # Generates a signed URL. See [Signed # URLs](https://cloud.google.com/storage/docs/access-control/signed-urls) diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index 49abcf43dcb8..3069f0c21242 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -112,12 +112,16 @@ def get_bucket bucket_name, if_metageneration_match: nil, if_metageneration_not_match: nil, user_project: nil, + soft_deleted: nil, + generation: nil, options: {} execute do service.get_bucket bucket_name, if_metageneration_match: if_metageneration_match, if_metageneration_not_match: if_metageneration_not_match, user_project: user_project(user_project), + soft_deleted: soft_deleted, + generation: generation, options: options end end @@ -654,6 +658,27 @@ def delete_file bucket_name, end end + ## + # Restore soft deleted bucket + def restore_bucket bucket_name, + generation, + timeout: nil, + if_generation_match: nil, + if_generation_not_match: nil, + projection: nil, + user_project: nil, + options: {soft_deleted: nil} + if options[:retries].nil? + is_idempotent = retry? generation: generation, if_generation_match: if_generation_match + options = is_idempotent ? {} : { retries: 0 } + end + + execute do + service.restore_bucket bucket_name, generation, + options: options + end + end + ## # Restores a soft-deleted object. def restore_file bucket_name, diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb index 8f62c06b8243..b9abdc824046 100644 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -11,20 +11,20 @@ # 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 'pry' # [START storage_delete_bucket] def delete_bucket bucket_name: # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" - #{}require "google/cloud/storage" + # require "google/cloud/storage" require_relative '../lib/google/cloud/storage' require_relative '../lib/google/cloud/storage/project' require_relative '../lib/google/cloud/storage/bucket' # require_relative '../lib/google/cloud/storage/bucket/list' require_relative '../lib/google/cloud/storage/service' - require 'pry' storage = Google::Cloud::Storage.new deleted_bucket = storage.create_bucket bucket_name @@ -34,21 +34,36 @@ def delete_bucket bucket_name: # fetching generation generation = deleted_bucket.generation - # fetching soft deleted buckets + # fetching soft deleted bucket with soft_delete_time and hard_delete_time + deleted_bucket_fetch= storage.bucket deleted_bucket.name,generation: generation, soft_deleted: true + soft_delete_time= deleted_bucket_fetch.gapi.soft_delete_time + hard_delete_time= deleted_bucket_fetch.gapi.hard_delete_time + + puts "soft_delete_time - #{soft_delete_time}" + puts "hard_delete_time - #{hard_delete_time}" + bucket_restored = storage.restore_bucket deleted_bucket.name, generation, soft_deleted: true + # fetching soft deleted bucket list deleted_buckets = storage.buckets soft_deleted: true - #{}storage.bucket deleted_bucket.name, generation: generation, soft_deleted: true - - puts "Deleted bucket: #{deleted_bucket.name}" - puts deleted_bucket + puts "Deleted bucket: #{deleted_bucket.name} details" puts "bucket generation #{generation}" puts "count of soft deleted buckets #{deleted_buckets.count}" + if JSON.parse(bucket_restored.gapi)["name"] == deleted_bucket.name + puts "#{deleted_bucket.name} Bucket restored" + + else + puts "#{deleted_bucket.name} Bucket not restored" + end + + deleted_bucket.delete + puts "clean up done" + #{}puts Gem.loaded_specs["google-cloud-storage"].full_gem_path end # [END storage_delete_bucket] -bucket_name = "ruby_try_2" +bucket_name = "ruby_try_1" delete_bucket bucket_name: bucket_name #{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ From eeeef7ca2a1abb9b5c56d46de58a05eef3f26148 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 28 Nov 2024 12:46:45 +0000 Subject: [PATCH 03/25] refactor --- .../lib/google/cloud/storage/bucket.rb | 2 +- .../lib/google/cloud/storage/bucket/list.rb | 21 +++++++++---------- .../lib/google/cloud/storage/project.rb | 4 ++-- .../samples/storage_restore_bucket.rb | 2 +- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 8174f38049bb..2731988b684d 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -3152,7 +3152,7 @@ def lazy? ## # @private New Bucket from a Google API Client object. - def self.from_gapi gapi, service, user_project: nil,generation: nil, soft_deleted: nil + def self.from_gapi gapi, service, user_project: nil, generation: nil, soft_deleted: nil new.tap do |b| b.gapi = gapi b.service = service diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb index b6a6492e3b27..e596fb4fda8a 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb @@ -14,7 +14,6 @@ require "delegate" -require "pry" module Google module Cloud @@ -147,16 +146,16 @@ def all request_limit: nil, &block # @private New Bucket::List from a Google API Client # Google::Apis::StorageV1::Buckets object. def self.from_gapi gapi_list, service, prefix = nil, max = nil, - user_project: nil, soft_deleted: nil - buckets = new(Array(gapi_list.items).map do |gapi_object| - Bucket.from_gapi gapi_object, service, user_project: user_project, soft_deleted: soft_deleted - end) - buckets.instance_variable_set :@token, gapi_list.next_page_token - buckets.instance_variable_set :@service, service - buckets.instance_variable_set :@prefix, prefix - buckets.instance_variable_set :@max, max - buckets.instance_variable_set :@user_project, user_project - buckets + user_project: nil + buckets = new(Array(gapi_list.items).map do |gapi_object| + Bucket.from_gapi gapi_object, service, user_project: user_project + end) + buckets.instance_variable_set :@token, gapi_list.next_page_token + buckets.instance_variable_set :@service, service + buckets.instance_variable_set :@prefix, prefix + buckets.instance_variable_set :@max, max + buckets.instance_variable_set :@user_project, user_project + buckets end protected diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index ff18095fd2ba..7f246d12c494 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -193,9 +193,9 @@ def add_custom_header header_name, header_value # puts bucket.name # end # - def buckets prefix: nil, token: nil, max: nil, user_project: nil , soft_deleted: nil + def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: nil gapi = service.list_buckets \ - prefix: prefix, token: token, max: max, user_project: user_project,options: {soft_deleted: soft_deleted} + prefix: prefix, token: token, max: max, user_project: user_project, options: { soft_deleted: soft_deleted } Bucket::List.from_gapi \ gapi, service, prefix, max, user_project: user_project, soft_deleted: soft_deleted end diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb index b9abdc824046..8aa717960491 100644 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -63,7 +63,7 @@ def delete_bucket bucket_name: end # [END storage_delete_bucket] -bucket_name = "ruby_try_1" +bucket_name = "ruby_try_6" delete_bucket bucket_name: bucket_name #{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ From b84ef80cef1f120f5aee45dd1126afcdd2a107c4 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 11 Dec 2024 12:43:52 +0000 Subject: [PATCH 04/25] adding unit tests --- .../lib/google/cloud/storage/bucket/list.rb | 4 +- .../lib/google/cloud/storage/project.rb | 71 +++-- .../lib/google/cloud/storage/service.rb | 25 +- .../test/google/cloud/storage/project_test.rb | 292 +++++++++++++----- google-cloud-storage/test/helper.rb | 150 ++++++--- 5 files changed, 388 insertions(+), 154 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb index e596fb4fda8a..f3a0c1ef26de 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb @@ -146,9 +146,9 @@ def all request_limit: nil, &block # @private New Bucket::List from a Google API Client # Google::Apis::StorageV1::Buckets object. def self.from_gapi gapi_list, service, prefix = nil, max = nil, - user_project: nil + user_project: nil, soft_deleted: nil buckets = new(Array(gapi_list.items).map do |gapi_object| - Bucket.from_gapi gapi_object, service, user_project: user_project + Bucket.from_gapi gapi_object, service, user_project: user_project, soft_deleted: soft_deleted end) buckets.instance_variable_set :@token, gapi_list.next_page_token buckets.instance_variable_set :@service, service diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 7f246d12c494..24e17a8cb9b9 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -193,9 +193,18 @@ def add_custom_header header_name, header_value # puts bucket.name # end # + # @example Retrieve soft deleted + # require "google/cloud/storage" + # + # storage = Google::Cloud::Storage.new + # + # user_buckets = storage.buckets soft_deleted: true + # user_buckets.each do |bucket| + # puts bucket.name + # end def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: nil gapi = service.list_buckets \ - prefix: prefix, token: token, max: max, user_project: user_project, options: { soft_deleted: soft_deleted } + prefix: prefix, token: token, max: max, user_project: user_project, soft_deleted: soft_deleted, options: {} Bucket::List.from_gapi \ gapi, service, prefix, max, user_project: user_project, soft_deleted: soft_deleted end @@ -275,7 +284,7 @@ def bucket bucket_name, soft_deleted: soft_deleted, generation: generation - Bucket.from_gapi gapi, service, user_project: user_project,soft_deleted: soft_deleted,generation: generation + Bucket.from_gapi gapi, service, user_project: user_project, soft_deleted: soft_deleted, generation: generation rescue Google::Cloud::NotFoundError nil end @@ -559,23 +568,49 @@ def hmac_keys service_account_email: nil, project_id: nil, max: max, user_project: user_project end + ## + # Restores a soft deleted bucket with bucket name and generation no. + # + # @param [String] bucket_name Name of a bucket. + # @param [Fixnum] generation generation of a bucket. + # @param [Boolean] skip_lookup Optionally create a Bucket object + # without verifying the bucket resource exists on the Storage service. + # Calls made on this object will raise errors if the bucket resource + # does not exist. Default is `false`. + # @param [Integer] if_metageneration_match Makes the operation conditional + # on whether the bucket's current metageneration matches the given value. + # @param [Boolean] soft_deleted If this parameter is set to + # `true` projects looks in the list of soft deleted buckets + # + # + # @return [Google::Cloud::Storage::Bucket, nil] Returns nil if bucket + # does not exist + # + # @example + # require "google/cloud/storage" + # + # storage = Google::Cloud::Storage.new + # generation= 123 + # + # bucket = storage.bucket "my-bucket", generation, soft_deleted: true + # puts bucket.name + # def restore_bucket bucket_name, - generation, - soft_deleted: nil, - timeout: nil, - if_generation_match: nil, - if_generation_not_match: nil, - projection: nil, - user_project: nil, - options: {soft_deleted: nil} - - gapi = service.restore_bucket \ - bucket_name, generation, - if_generation_match: if_generation_match, - if_generation_not_match: if_generation_not_match, - user_project: user_project, - options: options - Bucket.from_gapi gapi, service, user_project: user_project, generation: generation + generation, + soft_deleted: nil, + timeout: nil, + if_generation_match: nil, + if_generation_not_match: nil, + projection: nil, + user_project: nil, + options: {} + gapi = service.restore_bucket bucket_name, generation, + if_generation_match: if_generation_match, + if_generation_not_match: if_generation_not_match, + user_project: user_project, + soft_deleted: soft_deleted, + options: options + Bucket.from_gapi gapi, service, user_project: user_project, generation: generation end ## diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index 3069f0c21242..1d652c3b7d36 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -97,11 +97,12 @@ def project_service_account ## # Retrieves a list of buckets for the given project. - def list_buckets prefix: nil, token: nil, max: nil, user_project: nil, options: {} + def list_buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: nil, options: {} execute do service.list_buckets \ @project, prefix: prefix, page_token: token, max_results: max, - user_project: user_project(user_project), options: options + user_project: user_project(user_project), + soft_deleted: soft_deleted, options: options end end @@ -663,20 +664,22 @@ def delete_file bucket_name, def restore_bucket bucket_name, generation, timeout: nil, + soft_deleted: nil, if_generation_match: nil, if_generation_not_match: nil, projection: nil, user_project: nil, - options: {soft_deleted: nil} - if options[:retries].nil? - is_idempotent = retry? generation: generation, if_generation_match: if_generation_match - options = is_idempotent ? {} : { retries: 0 } - end + options: {} + if options[:retries].nil? + is_idempotent = retry? generation: generation, if_generation_match: if_generation_match + options = is_idempotent ? {} : { retries: 0 } + end - execute do - service.restore_bucket bucket_name, generation, - options: options - end + execute do + service.restore_bucket bucket_name, generation, + soft_deleted: soft_deleted, + options: options + end end ## diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index 5e1b88ad99da..f86cfac0f033 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -31,22 +31,24 @@ let(:bucket_autoclass_enabled) { true } let(:bucket_requester_pays) { true } let(:bucket_enable_object_retention) { true } - let(:bucket_cors) { [{ max_age_seconds: 300, + let :bucket_cors do + [{ max_age_seconds: 300, origin: ["http://example.org", "https://example.org"], http_method: ["*"], - response_header: ["X-My-Custom-Header"] }] } - let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new **c } } + response_header: ["X-My-Custom-Header"] }] + end + let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new(**c) } } let(:kms_key) { "path/to/encryption_key_name" } - let(:bucket_retention_period) { 86400 } + let(:bucket_retention_period) { 86_400 } let(:metageneration) { 6 } - let(:default_credentials) do + let :default_credentials do creds = OpenStruct.new empty: true def creds.is_a? target target == Google::Auth::Credentials end creds end - let(:default_universe_credentials) do + let :default_universe_credentials do client = OpenStruct.new universe_domain: "googleapis.com" creds = OpenStruct.new empty: true, client: client def creds.is_a? target @@ -130,8 +132,8 @@ def creds.is_a? target "x-goog-2" => ["x-goog-", 2] } - storage.add_custom_header "x-goog-3" , "x-goog-3, x-goog-3" - storage.add_custom_header "x-goog-4" , ["x-goog-4", "x-goog-4"] + storage.add_custom_header "x-goog-3", "x-goog-3, x-goog-3" + storage.add_custom_header "x-goog-4", ["x-goog-4", "x-goog-4"] storage.add_custom_headers headers headers["x-goog-3"] = "x-goog-3, x-goog-3" @@ -156,7 +158,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name @@ -172,7 +175,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, location: bucket_location resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, location: bucket_location @@ -189,7 +193,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, autoclass_enabled: bucket_autoclass_enabled resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, autoclass_enabled: bucket_autoclass_enabled @@ -205,7 +210,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, storage_class: bucket_storage_class resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, storage_class: bucket_storage_class @@ -219,9 +225,11 @@ def creds.is_a? target it "creates a bucket with versioning" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, versioning: Google::Apis::StorageV1::Bucket::Versioning.new(enabled: true) + created_bucket = create_bucket_gapi bucket_name, + versioning: Google::Apis::StorageV1::Bucket::Versioning.new(enabled: true) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, versioning: true @@ -235,12 +243,16 @@ def creds.is_a? target it "creates a bucket with logging bucket and prefix" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, logging: Google::Apis::StorageV1::Bucket::Logging.new(log_bucket: bucket_logging_bucket, log_object_prefix: bucket_logging_prefix) + created_bucket = create_bucket_gapi bucket_name, + logging: Google::Apis::StorageV1::Bucket::Logging.new(log_bucket: bucket_logging_bucket, + log_object_prefix: bucket_logging_prefix) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock - bucket = storage.create_bucket bucket_name, logging_bucket: bucket_logging_bucket, logging_prefix: bucket_logging_prefix + bucket = storage.create_bucket bucket_name, logging_bucket: bucket_logging_bucket, +logging_prefix: bucket_logging_prefix mock.verify @@ -252,9 +264,12 @@ def creds.is_a? target it "creates a bucket with website main and 404" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, website: Google::Apis::StorageV1::Bucket::Website.new(main_page_suffix: bucket_website_main, not_found_page: bucket_website_404) + created_bucket = create_bucket_gapi bucket_name, + website: Google::Apis::StorageV1::Bucket::Website.new(main_page_suffix: bucket_website_main, + not_found_page: bucket_website_404) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, website_main: bucket_website_main, website_404: bucket_website_404 @@ -269,9 +284,11 @@ def creds.is_a? target it "creates a bucket with requester pays" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, + billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| @@ -287,9 +304,11 @@ def creds.is_a? target it "creates a bucket with requester pays and user_project set to true" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, + billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: "test", enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: "test", enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, user_project: true do |b| @@ -306,9 +325,11 @@ def creds.is_a? target it "creates a bucket with requester pays and user_project set to another project ID" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, + billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: "my-other-project", enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: "my-other-project", enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, user_project: "my-other-project" do |b| @@ -327,15 +348,16 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, cors: bucket_cors_gapi resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| b.cors.add_rule ["http://example.org", "https://example.org"], - "*", - headers: "X-My-Custom-Header", - max_age: 300 + "*", + headers: "X-My-Custom-Header", + max_age: 300 end mock.verify @@ -347,9 +369,12 @@ def creds.is_a? target it "creates a bucket with block lifecycle (Object Lifecycle Management)" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, lifecycle: lifecycle_gapi(lifecycle_rule_gapi("SetStorageClass", storage_class: "NEARLINE", age: 32)) + created_bucket = create_bucket_gapi bucket_name, + lifecycle: lifecycle_gapi(lifecycle_rule_gapi("SetStorageClass", + storage_class: "NEARLINE", age: 32)) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -369,12 +394,13 @@ def creds.is_a? target created_bucket = create_bucket_gapi bucket_name created_bucket.labels = { "env" => "production", "foo" => "bar" } resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| - _(b.labels).must_equal Hash.new + _(b.labels).must_equal({}) b.labels = { "env" => "production" } b.labels["foo"] = "bar" end @@ -389,9 +415,10 @@ def creds.is_a? target it "creates a bucket with block encryption" do mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name - created_bucket.encryption = encryption_gapi(kms_key) + created_bucket.encryption = encryption_gapi kms_key resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -411,7 +438,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "private", predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "private", +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, acl: "private" @@ -426,7 +454,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "publicRead", predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "publicRead", +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, acl: :public @@ -441,7 +470,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: "private", user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: "private", user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, default_acl: :private @@ -456,7 +486,8 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: "publicRead", user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: "publicRead", user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, default_acl: "public" @@ -477,10 +508,11 @@ def creds.is_a? target resp_bucket = bucket_with_location created_bucket bucket_retention_effective_at = Time.now resp_bucket.retention_policy = Google::Apis::StorageV1::Bucket::RetentionPolicy.new( - retention_period: bucket_retention_period, - effective_time: bucket_retention_effective_at + retention_period: bucket_retention_period, + effective_time: bucket_retention_effective_at ) - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -523,7 +555,8 @@ def creds.is_a? target created_bucket = create_bucket_gapi bucket_name created_bucket.default_event_based_hold = true resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -545,11 +578,12 @@ def creds.is_a? target created_bucket.rpo = "ASYNC_TURBO" resp_bucket = bucket_with_location created_bucket, location_type: "dual-region" - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| - b.rpo= :ASYNC_TURBO + b.rpo = :ASYNC_TURBO end mock.verify @@ -566,7 +600,8 @@ def creds.is_a? target created_bucket.hierarchical_namespace = hierarchical_namespace_object resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| @@ -586,7 +621,8 @@ def creds.is_a? target created_bucket.hierarchical_namespace = { enabled: false } resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, +predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, hierarchical_namespace: { enabled: false } @@ -601,7 +637,7 @@ def creds.is_a? target bucket_name = "" stub = Object.new - def stub.insert_bucket *args + def stub.insert_bucket *_args raise Google::Apis::ClientError.new("invalid argument", status_code: 400) end storage.service.mocked_service = stub @@ -615,7 +651,8 @@ def stub.insert_bucket *args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -633,7 +670,8 @@ def stub.insert_bucket *args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -644,10 +682,32 @@ def stub.insert_bucket *args _(buckets.size).must_equal num_buckets end + it "lists deleted buckets" do + num_buckets = 3 + + mock = Minitest::Mock.new + mock.expect :list_buckets, list_deleted_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: true, options: {} + + storage.service.mocked_service = mock + buckets = storage.buckets soft_deleted: true + + mock.verify + + _(buckets.size).must_equal num_buckets + bucket = buckets.first + _(bucket).must_be_kind_of Google::Cloud::Storage::Bucket + _(bucket.gapi.soft_delete_time).wont_be_nil + # refute_nil _(bucket).soft_delete_tme , "softDeleteTime should not be nil" + # _(bucket.soft_delete_tme).must_equal "multi-region" + end + it "paginates buckets" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -666,13 +726,15 @@ def stub.insert_bucket *args it "paginates buckets with max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock first_buckets = storage.buckets max: 3 - second_buckets = storage.buckets token: first_buckets.token, max: 3 + second_buckets = storage.buckets token: first_buckets.token, max: 3 mock.verify @@ -688,7 +750,8 @@ def stub.insert_bucket *args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -705,8 +768,10 @@ def stub.insert_bucket *args it "paginates buckets with next? and next" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -724,8 +789,10 @@ def stub.insert_bucket *args it "paginates buckets with next? and next and max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -743,8 +810,10 @@ def stub.insert_bucket *args it "paginates buckets with all" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -757,8 +826,10 @@ def stub.insert_bucket *args it "paginates buckets with all and max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", +max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -771,12 +842,14 @@ def stub.insert_bucket *args it "iterates buckets with all using Enumerator" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, +page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock - buckets = storage.buckets.all.take(5) + buckets = storage.buckets.all.take 5 mock.verify @@ -785,8 +858,10 @@ def stub.insert_bucket *args it "iterates buckets with all and request_limit set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, +page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -799,8 +874,10 @@ def stub.insert_bucket *args it "iterates buckets with all and user_project set to true" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: "test", options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: "test", options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: "test", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, +page_token: "next_page_token", max_results: nil, user_project: "test", soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -814,8 +891,10 @@ def stub.insert_bucket *args it "iterates buckets with all and user_project set to another project ID" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: "my-other-project", options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: "my-other-project", options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, +max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, +page_token: "next_page_token", max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -840,9 +919,48 @@ def stub.insert_bucket *args mock.verify _(bucket.name).must_equal bucket_name + _(bucket.generation).wont_be_nil _(bucket).wont_be :lazy? end + it "finds a deleted bucket" do + bucket_name = "found-bucket" + generation = 1_733_393_981_548_601_746 + + mock = Minitest::Mock.new + mock.expect :get_bucket, find_deleted_bucket_gapi(bucket_name), + [bucket_name], **get_bucket_args(soft_deleted: true, generation: generation) + + storage.service.mocked_service = mock + bucket = storage.bucket bucket_name, soft_deleted: true, generation: generation + + mock.verify + + _(bucket.name).must_equal bucket_name + _(bucket.generation).must_equal generation + _(bucket.gapi.hard_delete_time).wont_be_nil + _(bucket.gapi.hard_delete_time).wont_be_nil + + _(bucket).wont_be :lazy? + end + + it "restores a deleted bucket" do + bucket_name = "found-bucket" + generation = 1_733_393_981_548_601_746 + + mock = Minitest::Mock.new + mock.expect :restore_bucket, restored_bucket_gapi(bucket_name, generation), + [bucket_name, generation], soft_deleted: true, options: {} + + storage.service.mocked_service = mock + bucket = storage.restore_bucket bucket_name, generation, soft_deleted: true + + mock.verify + + _(bucket.name).must_equal bucket_name + _(bucket.generation).must_equal generation + end + it "finds a bucket with find_bucket alias" do bucket_name = "found-bucket" @@ -863,7 +981,8 @@ def stub.insert_bucket *args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(if_metageneration_match: metageneration) + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], + **get_bucket_args(if_metageneration_match: metageneration) storage.service.mocked_service = mock @@ -879,7 +998,8 @@ def stub.insert_bucket *args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(if_metageneration_not_match: metageneration) + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], + **get_bucket_args(if_metageneration_not_match: metageneration) storage.service.mocked_service = mock @@ -911,7 +1031,8 @@ def stub.insert_bucket *args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(user_project: "my-other-project") + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], + **get_bucket_args(user_project: "my-other-project") storage.service.mocked_service = mock @@ -1005,16 +1126,20 @@ def create_bucket_gapi name = nil, name: name, location: location, storage_class: storage_class, versioning: versioning, logging: logging, website: website, cors_configurations: cors, billing: billing, lifecycle: lifecycle, - autoclass: Google::Apis::StorageV1::Bucket::Autoclass.new( enabled: autoclass_enabled ), + autoclass: Google::Apis::StorageV1::Bucket::Autoclass.new(enabled: autoclass_enabled), object_retention: object_retention_param(enable_object_retention) }.delete_if { |_, v| v.nil? } - Google::Apis::StorageV1::Bucket.new **options + Google::Apis::StorageV1::Bucket.new(**options) end def find_bucket_gapi name = nil Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json end + def find_deleted_bucket_gapi name = nil + Google::Apis::StorageV1::Bucket.from_json random_deleted_bucket_hash(name: name).to_json + end + def list_buckets_gapi count = 2, token = nil buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_bucket_hash.to_json } Google::Apis::StorageV1::Buckets.new( @@ -1022,6 +1147,17 @@ def list_buckets_gapi count = 2, token = nil ) end + def list_deleted_buckets_gapi count = 2, token = nil + buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_deleted_bucket_hash.to_json } + Google::Apis::StorageV1::Buckets.new( + kind: "storage#buckets", items: buckets, next_page_token: token + ) + end + + def restored_bucket_gapi name, _generation + Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json + end + def object_retention_param enable_object_retention enable_object_retention ? Google::Apis::StorageV1::Bucket::ObjectRetention.new(mode: "Enabled") : nil end diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index 50455d7c7e16..b94d33e923eb 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -26,29 +26,38 @@ ## # Monkey-Patch Google API Client to support Mocks -module Google::Apis::Core::Hashable - ## - # Minitest Mock depends on === to match same-value objects. - # By default, the Google API Client objects do not match with ===. - # Therefore, we must add this capability. - # This module seems like as good a place as any... - def === other - return(to_h === other.to_h) if other.respond_to? :to_h - super +module Google + module Apis + module Core + module Hashable + ## + # Minitest Mock depends on === to match same-value objects. + # By default, the Google API Client objects do not match with ===. + # Therefore, we must add this capability. + # This module seems like as good a place as any... + def === other + return(to_h === other.to_h) if other.respond_to? :to_h + super + end + end + end end end class MockStorage < Minitest::Spec let(:project) { "test" } - let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: Proc.new {})) } - let(:storage) { Google::Cloud::Storage::Project.new(Google::Cloud::Storage::Service.new(project, credentials, upload_chunk_size: 5 * 1024 * 1024 )) } + let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: proc {})) } + let :storage do + Google::Cloud::Storage::Project.new Google::Cloud::Storage::Service.new(project, credentials, + upload_chunk_size: 5 * 1024 * 1024) + end let(:pubsub_topic_name) { "my-topic-name" } - let(:file_obj) { StringIO.new("My test file") } + let(:file_obj) { StringIO.new "My test file" } let(:file_name) { "my_test_file.txt" } let(:acl) { "authenticated_read" } # Register this spec type for when :mock_storage is used. - register_spec_type(self) do |desc, *addl| + register_spec_type self do |_desc, *addl| addl.include? :mock_storage end @@ -70,7 +79,7 @@ def random_bucket_hash name: random_bucket_name, autoclass_terminal_storage_class: nil, enable_object_retention: nil, effective_time: DateTime.now, - retention_duration_seconds: 604800, # 7 days + retention_duration_seconds: 604_800, # 7 days hierarchical_namespace: nil versioning_config = { "enabled" => versioning } if versioning { "kind" => "storage#bucket", @@ -79,6 +88,7 @@ def random_bucket_hash name: random_bucket_name, "projectNumber" => "1234567890", "name" => name, "timeCreated" => Time.now, + "generation" => "1733393981548601746", "metageneration" => "1", "owner" => { "entity" => "project-owners-1234567890" }, "location" => location, @@ -95,11 +105,60 @@ def random_bucket_hash name: random_bucket_name, "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), "enableObjectRetention" => enable_object_retention, "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), - "hierarchicalNamespace" => hierarchical_namespace - }.delete_if { |_, v| v.nil? } - end - - def soft_delete_policy_object retention_duration_seconds: 604800 # 7 days + "hierarchicalNamespace" => hierarchical_namespace }.delete_if { |_, v| v.nil? } + end + + def random_deleted_bucket_hash name: random_bucket_name, + url_root: "https://www.googleapis.com/storage/v1", + location: "US", + storage_class: "STANDARD", + versioning: nil, + logging_bucket: nil, + logging_prefix: nil, + website_main: nil, + website_404: nil, + cors: [], + requester_pays: nil, + lifecycle: nil, + location_type: "multi-region", + rpo: "DEFAULT", + autoclass_enabled: nil, + autoclass_terminal_storage_class: nil, + enable_object_retention: nil, + effective_time: DateTime.now, + retention_duration_seconds: 604_800, # 7 days + hierarchical_namespace: nil + versioning_config = { "enabled" => versioning } if versioning + { "kind" => "storage#bucket", + "id" => name, + "selfLink" => "#{url_root}/b/#{name}", + "projectNumber" => "1234567890", + "name" => name, + "timeCreated" => Time.now, + "generation" => "1733393981548601746", + "metageneration" => "1", + "owner" => { "entity" => "project-owners-1234567890" }, + "location" => location, + "locationType" => location_type, + "rpo" => rpo, + "cors" => cors, + "lifecycle" => lifecycle, + "logging" => logging_hash(logging_bucket, logging_prefix), + "storageClass" => storage_class, + "versioning" => versioning_config, + "website" => website_hash(website_main, website_404), + "billing" => billing_hash(requester_pays), + "etag" => "CAE=", + "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), + "enableObjectRetention" => enable_object_retention, + "softDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, + "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, + "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), + "hierarchicalNamespace" => hierarchical_namespace }.delete_if { |_, v| v.nil? } + end + + # 7 days + def soft_delete_policy_object retention_duration_seconds: 604_800 Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new( effective_time: DateTime.now, retention_duration_seconds: retention_duration_seconds @@ -112,38 +171,34 @@ def hierarchical_namespace_object enabled: true ) end - def autoclass_config_hash(enabled, terminal_storage_class) + def autoclass_config_hash enabled, terminal_storage_class { "enabled" => enabled, - "terminalStorageClass" => terminal_storage_class - }.delete_if { |_, v| v.nil? } if !enabled.nil? || terminal_storage_class + "terminalStorageClass" => terminal_storage_class }.delete_if { |_, v| v.nil? } if !enabled.nil? || terminal_storage_class end - def logging_hash(bucket, prefix) + def logging_hash bucket, prefix { "logBucket" => bucket, - "logObjectPrefix" => prefix, - }.delete_if { |_, v| v.nil? } if bucket || prefix + "logObjectPrefix" => prefix }.delete_if { |_, v| v.nil? } if bucket || prefix end - def website_hash(website_main, website_404) + def website_hash website_main, website_404 { "mainPageSuffix" => website_main, - "notFoundPage" => website_404, - }.delete_if { |_, v| v.nil? } if website_main || website_404 + "notFoundPage" => website_404 }.delete_if { |_, v| v.nil? } if website_main || website_404 end - def billing_hash(requester_pays) - { "requesterPays" => requester_pays} unless requester_pays.nil? + def billing_hash requester_pays + { "requesterPays" => requester_pays } unless requester_pays.nil? end - def file_retention_hash(retention_params) - { "mode" => retention_params[:mode], - "retainUntilTime" => retention_params[:retain_until_time] - }.delete_if { |_, v| v.nil? } if !retention_params.nil? && !retention_params.empty? + def file_retention_hash retention_params + { "mode" => retention_params[:mode], + "retainUntilTime" => retention_params[:retain_until_time] }.delete_if { |_, v| v.nil? } if !retention_params.nil? && !retention_params.empty? end - def random_file_hash bucket=random_bucket_name, - name=random_file_path, - generation="1234567890", - kms_key_name="path/to/encryption_key_name", + def random_file_hash bucket = random_bucket_name, + name = random_file_path, + generation = "1234567890", + kms_key_name = "path/to/encryption_key_name", custom_time: nil, retention_params: nil, override_unlocked_retention: nil, @@ -152,9 +207,9 @@ def random_file_hash bucket=random_bucket_name, { "kind" => "storage#object", "id" => "#{bucket}/#{name}/1234567890", "selfLink" => "https://www.googleapis.com/storage/v1/b/#{bucket}/o/#{name}", - "name" => "#{name}", + "name" => name.to_s, "timeCreated" => Time.now, - "bucket" => "#{bucket}", + "bucket" => bucket.to_s, "generation" => generation, "metageneration" => "1", "cacheControl" => "public, max-age=3600", @@ -216,7 +271,7 @@ def random_notification_gapi id: "1", topic: "//pubsub.googleapis.com/projects/t def download_http_resp gzip: nil headers = {} headers["Content-Encoding"] = ["gzip"] if gzip - OpenStruct.new(header: headers) + OpenStruct.new header: headers end def encryption_gapi key_name @@ -282,11 +337,15 @@ def policy_gapi etag: "CAE=", version: 1, bindings: [] def get_bucket_args if_metageneration_match: nil, if_metageneration_not_match: nil, user_project: nil, + generation: nil, + soft_deleted: nil, options: {} { if_metageneration_match: if_metageneration_match, if_metageneration_not_match: if_metageneration_not_match, user_project: user_project, + generation: generation, + soft_deleted: soft_deleted, options: options } end @@ -416,7 +475,7 @@ def patch_object_args generation: nil, user_project: nil, override_unlocked_retention: nil, options: {} - opts = { + { generation: generation, if_generation_match: if_generation_match, if_generation_not_match: if_generation_not_match, @@ -515,7 +574,6 @@ def restore_object_args copy_source_acl: nil, } end - def compose_request source_files, destination_gapi = nil, if_source_generation_match: nil source_objects = source_files.map do |file| if file.is_a? String @@ -547,10 +605,12 @@ def compose_request source_files, destination_gapi = nil, if_source_generation_m def list_files_gapi count = 2, token = nil, prefixes = nil files = count.times.map { Google::Apis::StorageV1::Object.from_json random_file_hash.to_json } - Google::Apis::StorageV1::Objects.new kind: "storage#objects", items: files, next_page_token: token, prefixes: prefixes + Google::Apis::StorageV1::Objects.new kind: "storage#objects", items: files, + next_page_token: token, + prefixes: prefixes end - def restore_file_gapi bucket, file_name, generation=nil + def restore_file_gapi bucket, file_name, generation file_hash = random_file_hash(bucket, file_name, generation).to_json Google::Apis::StorageV1::Object.from_json file_hash end From 9521eed97763f0c55a5c9e6eb2732dbf48acda1a Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 12 Dec 2024 05:06:44 +0000 Subject: [PATCH 05/25] refactor --- .../lib/google/cloud/storage/bucket.rb | 11 ++- .../samples/storage_restore_bucket.rb | 69 ------------------- .../test/google/cloud/storage/project_test.rb | 4 +- google-cloud-storage/test/helper.rb | 2 +- 4 files changed, 12 insertions(+), 74 deletions(-) delete mode 100644 google-cloud-storage/samples/storage_restore_bucket.rb diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 2731988b684d..33b97939e527 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -23,7 +23,6 @@ require "google/cloud/storage/policy" require "google/cloud/storage/post_object" require "pathname" -require "pry" module Google @@ -85,6 +84,8 @@ class Bucket attr_accessor :user_project attr_accessor :soft_deleted attr_accessor :generation + attr_accessor :soft_delete_time + attr_accessor :hard_delete_time ## # @private Create an empty Bucket object. @@ -1224,6 +1225,14 @@ def soft_delete_policy @gapi.soft_delete_policy end + def soft_delete_time + @gapi.soft_delete_time + end + + def hard_delete_time + @gapi.soft_delete_time + end + ## # Sets the value for Soft Delete Policy in the bucket. This value can # be queried by calling {#soft_delete_policy}. diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb deleted file mode 100644 index 8aa717960491..000000000000 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright 2024 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 'pry' - -# [START storage_delete_bucket] -def delete_bucket bucket_name: - # The ID of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # require "google/cloud/storage" - require_relative '../lib/google/cloud/storage' - require_relative '../lib/google/cloud/storage/project' - require_relative '../lib/google/cloud/storage/bucket' - # require_relative '../lib/google/cloud/storage/bucket/list' - require_relative '../lib/google/cloud/storage/service' - - - storage = Google::Cloud::Storage.new - deleted_bucket = storage.create_bucket bucket_name - - deleted_bucket.delete - - # fetching generation - generation = deleted_bucket.generation - - # fetching soft deleted bucket with soft_delete_time and hard_delete_time - deleted_bucket_fetch= storage.bucket deleted_bucket.name,generation: generation, soft_deleted: true - soft_delete_time= deleted_bucket_fetch.gapi.soft_delete_time - hard_delete_time= deleted_bucket_fetch.gapi.hard_delete_time - - puts "soft_delete_time - #{soft_delete_time}" - puts "hard_delete_time - #{hard_delete_time}" - bucket_restored = storage.restore_bucket deleted_bucket.name, generation, soft_deleted: true - # fetching soft deleted bucket list - deleted_buckets = storage.buckets soft_deleted: true - - puts "Deleted bucket: #{deleted_bucket.name} details" - puts "bucket generation #{generation}" - puts "count of soft deleted buckets #{deleted_buckets.count}" - if JSON.parse(bucket_restored.gapi)["name"] == deleted_bucket.name - puts "#{deleted_bucket.name} Bucket restored" - - else - puts "#{deleted_bucket.name} Bucket not restored" - end - - deleted_bucket.delete - puts "clean up done" - - #{}puts Gem.loaded_specs["google-cloud-storage"].full_gem_path - -end -# [END storage_delete_bucket] - -bucket_name = "ruby_try_6" -delete_bucket bucket_name: bucket_name - -#{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index f86cfac0f033..7bb0936c6b79 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -697,9 +697,7 @@ def stub.insert_bucket *_args _(buckets.size).must_equal num_buckets bucket = buckets.first _(bucket).must_be_kind_of Google::Cloud::Storage::Bucket - _(bucket.gapi.soft_delete_time).wont_be_nil - # refute_nil _(bucket).soft_delete_tme , "softDeleteTime should not be nil" - # _(bucket.soft_delete_tme).must_equal "multi-region" + _(bucket.soft_delete_time).wont_be_nil end it "paginates buckets" do diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index b94d33e923eb..6af72907c8a4 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -610,7 +610,7 @@ def list_files_gapi count = 2, token = nil, prefixes = nil prefixes: prefixes end - def restore_file_gapi bucket, file_name, generation + def restore_file_gapi bucket, file_name, generation = nil file_hash = random_file_hash(bucket, file_name, generation).to_json Google::Apis::StorageV1::Object.from_json file_hash end From 768a8fb5afc8d55c20483d1d9fb8ca8a940db9f8 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 12 Dec 2024 07:36:13 +0000 Subject: [PATCH 06/25] update --- google-cloud-storage/lib/google/cloud/storage/project.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 24e17a8cb9b9..4524b22659ac 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -592,7 +592,7 @@ def hmac_keys service_account_email: nil, project_id: nil, # storage = Google::Cloud::Storage.new # generation= 123 # - # bucket = storage.bucket "my-bucket", generation, soft_deleted: true + # bucket = storage.restore_bucket "my-bucket", generation, soft_deleted: true # puts bucket.name # def restore_bucket bucket_name, From 66dfd07c673f9b204689fce94aead8c8ac195d83 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Fri, 13 Dec 2024 10:50:06 +0000 Subject: [PATCH 07/25] fixing lint issues --- .../lib/google/cloud/storage/bucket.rb | 28 ++++++++----------- .../lib/google/cloud/storage/project.rb | 10 +------ .../lib/google/cloud/storage/service.rb | 10 ------- .../test/google/cloud/storage/project_test.rb | 2 ++ google-cloud-storage/test/helper.rb | 11 +++++--- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 33b97939e527..3b229e5882dd 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -83,9 +83,6 @@ class Bucket # attr_accessor :user_project attr_accessor :soft_deleted - attr_accessor :generation - attr_accessor :soft_delete_time - attr_accessor :hard_delete_time ## # @private Create an empty Bucket object. @@ -227,9 +224,6 @@ def cors cors_builder.freeze # always return frozen objects end - def generation - @gapi.generation - end ## # Returns the current Object Lifecycle Management rules configuration @@ -1225,14 +1219,6 @@ def soft_delete_policy @gapi.soft_delete_policy end - def soft_delete_time - @gapi.soft_delete_time - end - - def hard_delete_time - @gapi.soft_delete_time - end - ## # Sets the value for Soft Delete Policy in the bucket. This value can # be queried by calling {#soft_delete_policy}. @@ -2057,6 +2043,18 @@ def compose sources, alias compose_file compose alias combine compose + def generation + @generation = @gapi.generation + end + + def soft_delete_time + @soft_delete_time = @gapi.soft_delete_time + end + + def hard_delete_time + @hard_delete_time = @gapi.hard_delete_time + end + ## # Generates a signed URL. See [Signed # URLs](https://cloud.google.com/storage/docs/access-control/signed-urls) @@ -3166,8 +3164,6 @@ def self.from_gapi gapi, service, user_project: nil, generation: nil, soft_delet b.gapi = gapi b.service = service b.user_project = user_project - b.generation = generation - b.soft_deleted = soft_deleted end end diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 4524b22659ac..1f6d96eedf57 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -598,19 +598,11 @@ def hmac_keys service_account_email: nil, project_id: nil, def restore_bucket bucket_name, generation, soft_deleted: nil, - timeout: nil, - if_generation_match: nil, - if_generation_not_match: nil, - projection: nil, - user_project: nil, options: {} gapi = service.restore_bucket bucket_name, generation, - if_generation_match: if_generation_match, - if_generation_not_match: if_generation_not_match, - user_project: user_project, soft_deleted: soft_deleted, options: options - Bucket.from_gapi gapi, service, user_project: user_project, generation: generation + Bucket.from_gapi gapi, service, generation: generation end ## diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index 1d652c3b7d36..ce40abe94771 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -663,18 +663,8 @@ def delete_file bucket_name, # Restore soft deleted bucket def restore_bucket bucket_name, generation, - timeout: nil, soft_deleted: nil, - if_generation_match: nil, - if_generation_not_match: nil, - projection: nil, - user_project: nil, options: {} - if options[:retries].nil? - is_idempotent = retry? generation: generation, if_generation_match: if_generation_match - options = is_idempotent ? {} : { retries: 0 } - end - execute do service.restore_bucket bucket_name, generation, soft_deleted: soft_deleted, diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index 7bb0936c6b79..e34d9df4ce0d 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -697,6 +697,8 @@ def stub.insert_bucket *_args _(buckets.size).must_equal num_buckets bucket = buckets.first _(bucket).must_be_kind_of Google::Cloud::Storage::Bucket + _(bucket.generation).wont_be_nil + _(bucket.hard_delete_time).wont_be_nil _(bucket.soft_delete_time).wont_be_nil end diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index 6af72907c8a4..5e91c3fc9dfc 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -80,7 +80,8 @@ def random_bucket_hash name: random_bucket_name, enable_object_retention: nil, effective_time: DateTime.now, retention_duration_seconds: 604_800, # 7 days - hierarchical_namespace: nil + hierarchical_namespace: nil, + generation: "1733393981548601746" versioning_config = { "enabled" => versioning } if versioning { "kind" => "storage#bucket", "id" => name, @@ -88,7 +89,7 @@ def random_bucket_hash name: random_bucket_name, "projectNumber" => "1234567890", "name" => name, "timeCreated" => Time.now, - "generation" => "1733393981548601746", + "generation" => generation, "metageneration" => "1", "owner" => { "entity" => "project-owners-1234567890" }, "location" => location, @@ -126,6 +127,7 @@ def random_deleted_bucket_hash name: random_bucket_name, autoclass_terminal_storage_class: nil, enable_object_retention: nil, effective_time: DateTime.now, + generation: "1733393981548601746", retention_duration_seconds: 604_800, # 7 days hierarchical_namespace: nil versioning_config = { "enabled" => versioning } if versioning @@ -135,7 +137,7 @@ def random_deleted_bucket_hash name: random_bucket_name, "projectNumber" => "1234567890", "name" => name, "timeCreated" => Time.now, - "generation" => "1733393981548601746", + "generation" => generation, "metageneration" => "1", "owner" => { "entity" => "project-owners-1234567890" }, "location" => location, @@ -152,7 +154,8 @@ def random_deleted_bucket_hash name: random_bucket_name, "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), "enableObjectRetention" => enable_object_retention, "softDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, - "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, + "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time + .to_time + retention_duration_seconds, "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), "hierarchicalNamespace" => hierarchical_namespace }.delete_if { |_, v| v.nil? } end From 28d853cb5ef6cbb5e081e51d9dd9812606c8d64d Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Fri, 13 Dec 2024 11:18:22 +0000 Subject: [PATCH 08/25] refactor --- google-cloud-storage/lib/google/cloud/storage/bucket.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 3b229e5882dd..b6b79e8bca3a 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -224,7 +224,6 @@ def cors cors_builder.freeze # always return frozen objects end - ## # Returns the current Object Lifecycle Management rules configuration # for the bucket. From c1b23bfcde96ed69996f7113e63564867ae5aac3 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 16 Dec 2024 19:09:46 +0000 Subject: [PATCH 09/25] removing unwanted changes --- .../lib/google/cloud/storage/bucket.rb | 21 +- .../lib/google/cloud/storage/project.rb | 14 +- .../lib/google/cloud/storage/service.rb | 2 - .../samples/storage_restore_bucket.rb | 66 +++++ .../test/google/cloud/storage/project_test.rb | 248 +++++++----------- google-cloud-storage/test/helper.rb | 192 +++++++------- 6 files changed, 267 insertions(+), 276 deletions(-) create mode 100644 google-cloud-storage/samples/storage_restore_bucket.rb diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index b6b79e8bca3a..ad33d3aeee99 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -82,8 +82,7 @@ class Bucket # files = bucket.files # Billed to "my-other-project" # attr_accessor :user_project - attr_accessor :soft_deleted - + ## # @private Create an empty Bucket object. def initialize @@ -2042,17 +2041,17 @@ def compose sources, alias compose_file compose alias combine compose - def generation - @generation = @gapi.generation - end + # def generation + # @generation = @gapi.generation + # end - def soft_delete_time - @soft_delete_time = @gapi.soft_delete_time - end + # def soft_delete_time + # @soft_delete_time = @gapi.soft_delete_time + # end - def hard_delete_time - @hard_delete_time = @gapi.hard_delete_time - end + # def hard_delete_time + # @hard_delete_time = @gapi.hard_delete_time + # end ## # Generates a signed URL. See [Signed diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 1f6d96eedf57..fc4f7cbaf7ea 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -193,18 +193,18 @@ def add_custom_header header_name, header_value # puts bucket.name # end # - # @example Retrieve soft deleted + # @example Retrieve soft deleted buckets # require "google/cloud/storage" # # storage = Google::Cloud::Storage.new # - # user_buckets = storage.buckets soft_deleted: true - # user_buckets.each do |bucket| + # soft_deleted_buckets = storage.buckets soft_deleted: true + # soft_deleted_buckets.each do |bucket| # puts bucket.name # end def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: nil gapi = service.list_buckets \ - prefix: prefix, token: token, max: max, user_project: user_project, soft_deleted: soft_deleted, options: {} + prefix: prefix, token: token, max: max, user_project: user_project, soft_deleted: soft_deleted Bucket::List.from_gapi \ gapi, service, prefix, max, user_project: user_project, soft_deleted: soft_deleted end @@ -569,7 +569,7 @@ def hmac_keys service_account_email: nil, project_id: nil, end ## - # Restores a soft deleted bucket with bucket name and generation no. + # Restores a soft deleted bucket with bucket name and generation. # # @param [String] bucket_name Name of a bucket. # @param [Fixnum] generation generation of a bucket. @@ -580,7 +580,7 @@ def hmac_keys service_account_email: nil, project_id: nil, # @param [Integer] if_metageneration_match Makes the operation conditional # on whether the bucket's current metageneration matches the given value. # @param [Boolean] soft_deleted If this parameter is set to - # `true` projects looks in the list of soft deleted buckets + # `true` project looks in the list of soft deleted buckets # # # @return [Google::Cloud::Storage::Bucket, nil] Returns nil if bucket @@ -597,10 +597,8 @@ def hmac_keys service_account_email: nil, project_id: nil, # def restore_bucket bucket_name, generation, - soft_deleted: nil, options: {} gapi = service.restore_bucket bucket_name, generation, - soft_deleted: soft_deleted, options: options Bucket.from_gapi gapi, service, generation: generation end diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index ce40abe94771..838445227ebd 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -663,11 +663,9 @@ def delete_file bucket_name, # Restore soft deleted bucket def restore_bucket bucket_name, generation, - soft_deleted: nil, options: {} execute do service.restore_bucket bucket_name, generation, - soft_deleted: soft_deleted, options: options end end diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb new file mode 100644 index 000000000000..34a27c2093d1 --- /dev/null +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -0,0 +1,66 @@ +# Copyright 2024 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 'pry' + +# [START storage_delete_bucket] +def delete_bucket bucket_name: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # require "google/cloud/storage" + require_relative '../lib/google/cloud/storage' + require_relative '../lib/google/cloud/storage/project' + require_relative '../lib/google/cloud/storage/bucket' + # require_relative '../lib/google/cloud/storage/bucket/list' + require_relative '../lib/google/cloud/storage/service' + + + storage = Google::Cloud::Storage.new + deleted_bucket = storage.create_bucket bucket_name + + deleted_bucket.delete + + # fetching generation + generation = deleted_bucket.gapi.generation + + # fetching soft deleted bucket with soft_delete_time and hard_delete_time + deleted_bucket_fetch = storage.bucket deleted_bucket.name, generation: generation, soft_deleted: true + + soft_delete_time= deleted_bucket_fetch.gapi.soft_delete_time + hard_delete_time= deleted_bucket_fetch.gapi.hard_delete_time + + puts "soft_delete_time - #{soft_delete_time}" + puts "hard_delete_time - #{hard_delete_time}" + binding.pry + + bucket_restored = storage.restore_bucket deleted_bucket.name, generation + # fetching soft deleted bucket list + deleted_buckets = storage.buckets soft_deleted: true + + puts "Deleted bucket: #{deleted_bucket.name} details" + puts "bucket generation #{generation}" + puts "count of soft deleted buckets #{deleted_buckets.count}" + + deleted_bucket.delete + puts "clean up done" + + #{}puts Gem.loaded_specs["google-cloud-storage"].full_gem_path + +end +# [END storage_delete_bucket] + +bucket_name = "ruby_try_8" +delete_bucket bucket_name: bucket_name + +#{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ \ No newline at end of file diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index e34d9df4ce0d..c5cfb172742f 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -31,24 +31,22 @@ let(:bucket_autoclass_enabled) { true } let(:bucket_requester_pays) { true } let(:bucket_enable_object_retention) { true } - let :bucket_cors do - [{ max_age_seconds: 300, + let(:bucket_cors) { [{ max_age_seconds: 300, origin: ["http://example.org", "https://example.org"], http_method: ["*"], - response_header: ["X-My-Custom-Header"] }] - end - let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new(**c) } } + response_header: ["X-My-Custom-Header"] }] } + let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new **c } } let(:kms_key) { "path/to/encryption_key_name" } - let(:bucket_retention_period) { 86_400 } + let(:bucket_retention_period) { 86400 } let(:metageneration) { 6 } - let :default_credentials do + let(:default_credentials) do creds = OpenStruct.new empty: true def creds.is_a? target target == Google::Auth::Credentials end creds end - let :default_universe_credentials do + let(:default_universe_credentials) do client = OpenStruct.new universe_domain: "googleapis.com" creds = OpenStruct.new empty: true, client: client def creds.is_a? target @@ -132,8 +130,8 @@ def creds.is_a? target "x-goog-2" => ["x-goog-", 2] } - storage.add_custom_header "x-goog-3", "x-goog-3, x-goog-3" - storage.add_custom_header "x-goog-4", ["x-goog-4", "x-goog-4"] + storage.add_custom_header "x-goog-3" , "x-goog-3, x-goog-3" + storage.add_custom_header "x-goog-4" , ["x-goog-4", "x-goog-4"] storage.add_custom_headers headers headers["x-goog-3"] = "x-goog-3, x-goog-3" @@ -158,8 +156,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name @@ -175,8 +172,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, location: bucket_location resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, location: bucket_location @@ -193,8 +189,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, autoclass_enabled: bucket_autoclass_enabled resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, autoclass_enabled: bucket_autoclass_enabled @@ -210,8 +205,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, storage_class: bucket_storage_class resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, storage_class: bucket_storage_class @@ -225,11 +219,9 @@ def creds.is_a? target it "creates a bucket with versioning" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - versioning: Google::Apis::StorageV1::Bucket::Versioning.new(enabled: true) + created_bucket = create_bucket_gapi bucket_name, versioning: Google::Apis::StorageV1::Bucket::Versioning.new(enabled: true) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, versioning: true @@ -243,16 +235,12 @@ def creds.is_a? target it "creates a bucket with logging bucket and prefix" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - logging: Google::Apis::StorageV1::Bucket::Logging.new(log_bucket: bucket_logging_bucket, - log_object_prefix: bucket_logging_prefix) + created_bucket = create_bucket_gapi bucket_name, logging: Google::Apis::StorageV1::Bucket::Logging.new(log_bucket: bucket_logging_bucket, log_object_prefix: bucket_logging_prefix) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock - bucket = storage.create_bucket bucket_name, logging_bucket: bucket_logging_bucket, -logging_prefix: bucket_logging_prefix + bucket = storage.create_bucket bucket_name, logging_bucket: bucket_logging_bucket, logging_prefix: bucket_logging_prefix mock.verify @@ -264,12 +252,9 @@ def creds.is_a? target it "creates a bucket with website main and 404" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - website: Google::Apis::StorageV1::Bucket::Website.new(main_page_suffix: bucket_website_main, - not_found_page: bucket_website_404) + created_bucket = create_bucket_gapi bucket_name, website: Google::Apis::StorageV1::Bucket::Website.new(main_page_suffix: bucket_website_main, not_found_page: bucket_website_404) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, website_main: bucket_website_main, website_404: bucket_website_404 @@ -284,11 +269,9 @@ def creds.is_a? target it "creates a bucket with requester pays" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| @@ -304,11 +287,9 @@ def creds.is_a? target it "creates a bucket with requester pays and user_project set to true" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: "test", enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: "test", enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, user_project: true do |b| @@ -325,11 +306,9 @@ def creds.is_a? target it "creates a bucket with requester pays and user_project set to another project ID" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) + created_bucket = create_bucket_gapi bucket_name, billing: Google::Apis::StorageV1::Bucket::Billing.new(requester_pays: bucket_requester_pays) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: "my-other-project", enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: "my-other-project", enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, user_project: "my-other-project" do |b| @@ -348,16 +327,15 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name, cors: bucket_cors_gapi resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| b.cors.add_rule ["http://example.org", "https://example.org"], - "*", - headers: "X-My-Custom-Header", - max_age: 300 + "*", + headers: "X-My-Custom-Header", + max_age: 300 end mock.verify @@ -369,12 +347,9 @@ def creds.is_a? target it "creates a bucket with block lifecycle (Object Lifecycle Management)" do mock = Minitest::Mock.new - created_bucket = create_bucket_gapi bucket_name, - lifecycle: lifecycle_gapi(lifecycle_rule_gapi("SetStorageClass", - storage_class: "NEARLINE", age: 32)) + created_bucket = create_bucket_gapi bucket_name, lifecycle: lifecycle_gapi(lifecycle_rule_gapi("SetStorageClass", storage_class: "NEARLINE", age: 32)) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -394,13 +369,12 @@ def creds.is_a? target created_bucket = create_bucket_gapi bucket_name created_bucket.labels = { "env" => "production", "foo" => "bar" } resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| - _(b.labels).must_equal({}) + _(b.labels).must_equal Hash.new b.labels = { "env" => "production" } b.labels["foo"] = "bar" end @@ -415,10 +389,9 @@ def creds.is_a? target it "creates a bucket with block encryption" do mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name - created_bucket.encryption = encryption_gapi kms_key + created_bucket.encryption = encryption_gapi(kms_key) resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -438,8 +411,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "private", -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "private", predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, acl: "private" @@ -454,8 +426,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "publicRead", -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: "publicRead", predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, acl: :public @@ -470,8 +441,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: "private", user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: "private", user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, default_acl: :private @@ -486,8 +456,7 @@ def creds.is_a? target mock = Minitest::Mock.new created_bucket = create_bucket_gapi bucket_name resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: "publicRead", user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: "publicRead", user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, default_acl: "public" @@ -508,11 +477,10 @@ def creds.is_a? target resp_bucket = bucket_with_location created_bucket bucket_retention_effective_at = Time.now resp_bucket.retention_policy = Google::Apis::StorageV1::Bucket::RetentionPolicy.new( - retention_period: bucket_retention_period, - effective_time: bucket_retention_effective_at + retention_period: bucket_retention_period, + effective_time: bucket_retention_effective_at ) - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -555,8 +523,7 @@ def creds.is_a? target created_bucket = create_bucket_gapi bucket_name created_bucket.default_event_based_hold = true resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock @@ -578,12 +545,11 @@ def creds.is_a? target created_bucket.rpo = "ASYNC_TURBO" resp_bucket = bucket_with_location created_bucket, location_type: "dual-region" - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| - b.rpo = :ASYNC_TURBO + b.rpo= :ASYNC_TURBO end mock.verify @@ -600,8 +566,7 @@ def creds.is_a? target created_bucket.hierarchical_namespace = hierarchical_namespace_object resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name do |b| @@ -621,8 +586,7 @@ def creds.is_a? target created_bucket.hierarchical_namespace = { enabled: false } resp_bucket = bucket_with_location created_bucket - mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, -predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} + mock.expect :insert_bucket, resp_bucket, [project, created_bucket], predefined_acl: nil, predefined_default_object_acl: nil, user_project: nil, enable_object_retention: nil, options: {} storage.service.mocked_service = mock bucket = storage.create_bucket bucket_name, hierarchical_namespace: { enabled: false } @@ -637,7 +601,7 @@ def creds.is_a? target bucket_name = "" stub = Object.new - def stub.insert_bucket *_args + def stub.insert_bucket *args raise Google::Apis::ClientError.new("invalid argument", status_code: 400) end storage.service.mocked_service = stub @@ -651,8 +615,7 @@ def stub.insert_bucket *_args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -670,8 +633,7 @@ def stub.insert_bucket *_args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -697,17 +659,15 @@ def stub.insert_bucket *_args _(buckets.size).must_equal num_buckets bucket = buckets.first _(bucket).must_be_kind_of Google::Cloud::Storage::Bucket - _(bucket.generation).wont_be_nil - _(bucket.hard_delete_time).wont_be_nil - _(bucket.soft_delete_time).wont_be_nil + _(bucket.gapi.generation).wont_be_nil + _(bucket.gapi.hard_delete_time).wont_be_nil + _(bucket.gapi.soft_delete_time).wont_be_nil end it "paginates buckets" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -726,15 +686,13 @@ def stub.insert_bucket *_args it "paginates buckets with max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: 3, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock first_buckets = storage.buckets max: 3 - second_buckets = storage.buckets token: first_buckets.token, max: 3 + second_buckets = storage.buckets token: first_buckets.token, max: 3 mock.verify @@ -750,8 +708,7 @@ def stub.insert_bucket *_args num_buckets = 3 mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -768,10 +725,8 @@ def stub.insert_bucket *_args it "paginates buckets with next? and next" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -789,10 +744,8 @@ def stub.insert_bucket *_args it "paginates buckets with next? and next and max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: 3, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -810,10 +763,8 @@ def stub.insert_bucket *_args it "paginates buckets with all" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -826,10 +777,8 @@ def stub.insert_bucket *_args it "paginates buckets with all and max set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: 3, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", -max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: 3, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(2), [project], prefix: nil, page_token: "next_page_token", max_results: 3, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -842,14 +791,12 @@ def stub.insert_bucket *_args it "iterates buckets with all using Enumerator" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, -page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} storage.service.mocked_service = mock - buckets = storage.buckets.all.take 5 + buckets = storage.buckets.all.take(5) mock.verify @@ -858,10 +805,8 @@ def stub.insert_bucket *_args it "iterates buckets with all and request_limit set" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: nil, soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, -page_token: "next_page_token", max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: nil,soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -874,10 +819,8 @@ def stub.insert_bucket *_args it "iterates buckets with all and user_project set to true" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: "test", soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, -page_token: "next_page_token", max_results: nil, user_project: "test", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: "test", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: "test", soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -891,10 +834,8 @@ def stub.insert_bucket *_args it "iterates buckets with all and user_project set to another project ID" do mock = Minitest::Mock.new - mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, -max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} - mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, -page_token: "next_page_token", max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "next_page_token"), [project], prefix: nil, page_token: nil, max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} + mock.expect :list_buckets, list_buckets_gapi(3, "second_page_token"), [project], prefix: nil, page_token: "next_page_token", max_results: nil, user_project: "my-other-project", soft_deleted: nil, options: {} storage.service.mocked_service = mock @@ -919,11 +860,10 @@ def stub.insert_bucket *_args mock.verify _(bucket.name).must_equal bucket_name - _(bucket.generation).wont_be_nil _(bucket).wont_be :lazy? end - it "finds a deleted bucket" do + it "finds a deleted bucket" do bucket_name = "found-bucket" generation = 1_733_393_981_548_601_746 @@ -937,7 +877,7 @@ def stub.insert_bucket *_args mock.verify _(bucket.name).must_equal bucket_name - _(bucket.generation).must_equal generation + _(bucket.gapi.generation).must_equal generation _(bucket.gapi.hard_delete_time).wont_be_nil _(bucket.gapi.hard_delete_time).wont_be_nil @@ -950,15 +890,13 @@ def stub.insert_bucket *_args mock = Minitest::Mock.new mock.expect :restore_bucket, restored_bucket_gapi(bucket_name, generation), - [bucket_name, generation], soft_deleted: true, options: {} + [bucket_name, generation], options: {} storage.service.mocked_service = mock - bucket = storage.restore_bucket bucket_name, generation, soft_deleted: true - + bucket = storage.restore_bucket bucket_name, generation mock.verify - _(bucket.name).must_equal bucket_name - _(bucket.generation).must_equal generation + _(bucket.gapi.generation).must_equal generation end it "finds a bucket with find_bucket alias" do @@ -981,8 +919,7 @@ def stub.insert_bucket *_args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], - **get_bucket_args(if_metageneration_match: metageneration) + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(if_metageneration_match: metageneration) storage.service.mocked_service = mock @@ -998,8 +935,7 @@ def stub.insert_bucket *_args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], - **get_bucket_args(if_metageneration_not_match: metageneration) + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(if_metageneration_not_match: metageneration) storage.service.mocked_service = mock @@ -1031,8 +967,7 @@ def stub.insert_bucket *_args bucket_name = "found-bucket" mock = Minitest::Mock.new - mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], - **get_bucket_args(user_project: "my-other-project") + mock.expect :get_bucket, find_bucket_gapi(bucket_name), [bucket_name], **get_bucket_args(user_project: "my-other-project") storage.service.mocked_service = mock @@ -1126,20 +1061,20 @@ def create_bucket_gapi name = nil, name: name, location: location, storage_class: storage_class, versioning: versioning, logging: logging, website: website, cors_configurations: cors, billing: billing, lifecycle: lifecycle, - autoclass: Google::Apis::StorageV1::Bucket::Autoclass.new(enabled: autoclass_enabled), + autoclass: Google::Apis::StorageV1::Bucket::Autoclass.new( enabled: autoclass_enabled ), object_retention: object_retention_param(enable_object_retention) }.delete_if { |_, v| v.nil? } - Google::Apis::StorageV1::Bucket.new(**options) - end - - def find_bucket_gapi name = nil - Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json + Google::Apis::StorageV1::Bucket.new **options end def find_deleted_bucket_gapi name = nil Google::Apis::StorageV1::Bucket.from_json random_deleted_bucket_hash(name: name).to_json end + def find_bucket_gapi name = nil + Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json + end + def list_buckets_gapi count = 2, token = nil buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_bucket_hash.to_json } Google::Apis::StorageV1::Buckets.new( @@ -1158,7 +1093,8 @@ def restored_bucket_gapi name, _generation Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json end + def object_retention_param enable_object_retention enable_object_retention ? Google::Apis::StorageV1::Bucket::ObjectRetention.new(mode: "Enabled") : nil end -end +end \ No newline at end of file diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index 5e91c3fc9dfc..bfeb842e1692 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -26,38 +26,29 @@ ## # Monkey-Patch Google API Client to support Mocks -module Google - module Apis - module Core - module Hashable - ## - # Minitest Mock depends on === to match same-value objects. - # By default, the Google API Client objects do not match with ===. - # Therefore, we must add this capability. - # This module seems like as good a place as any... - def === other - return(to_h === other.to_h) if other.respond_to? :to_h - super - end - end - end +module Google::Apis::Core::Hashable + ## + # Minitest Mock depends on === to match same-value objects. + # By default, the Google API Client objects do not match with ===. + # Therefore, we must add this capability. + # This module seems like as good a place as any... + def === other + return(to_h === other.to_h) if other.respond_to? :to_h + super end end class MockStorage < Minitest::Spec let(:project) { "test" } - let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: proc {})) } - let :storage do - Google::Cloud::Storage::Project.new Google::Cloud::Storage::Service.new(project, credentials, - upload_chunk_size: 5 * 1024 * 1024) - end + let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: Proc.new {})) } + let(:storage) { Google::Cloud::Storage::Project.new(Google::Cloud::Storage::Service.new(project, credentials, upload_chunk_size: 5 * 1024 * 1024 )) } let(:pubsub_topic_name) { "my-topic-name" } - let(:file_obj) { StringIO.new "My test file" } + let(:file_obj) { StringIO.new("My test file") } let(:file_name) { "my_test_file.txt" } let(:acl) { "authenticated_read" } # Register this spec type for when :mock_storage is used. - register_spec_type self do |_desc, *addl| + register_spec_type(self) do |desc, *addl| addl.include? :mock_storage end @@ -79,7 +70,7 @@ def random_bucket_hash name: random_bucket_name, autoclass_terminal_storage_class: nil, enable_object_retention: nil, effective_time: DateTime.now, - retention_duration_seconds: 604_800, # 7 days + retention_duration_seconds: 604800, # 7 days hierarchical_namespace: nil, generation: "1733393981548601746" versioning_config = { "enabled" => versioning } if versioning @@ -106,62 +97,62 @@ def random_bucket_hash name: random_bucket_name, "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), "enableObjectRetention" => enable_object_retention, "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), - "hierarchicalNamespace" => hierarchical_namespace }.delete_if { |_, v| v.nil? } + "hierarchicalNamespace" => hierarchical_namespace + }.delete_if { |_, v| v.nil? } end - def random_deleted_bucket_hash name: random_bucket_name, - url_root: "https://www.googleapis.com/storage/v1", - location: "US", - storage_class: "STANDARD", - versioning: nil, - logging_bucket: nil, - logging_prefix: nil, - website_main: nil, - website_404: nil, - cors: [], - requester_pays: nil, - lifecycle: nil, - location_type: "multi-region", - rpo: "DEFAULT", - autoclass_enabled: nil, - autoclass_terminal_storage_class: nil, - enable_object_retention: nil, - effective_time: DateTime.now, - generation: "1733393981548601746", - retention_duration_seconds: 604_800, # 7 days - hierarchical_namespace: nil + url_root: "https://www.googleapis.com/storage/v1", + location: "US", + storage_class: "STANDARD", + versioning: nil, + logging_bucket: nil, + logging_prefix: nil, + website_main: nil, + website_404: nil, + cors: [], + requester_pays: nil, + lifecycle: nil, + location_type: "multi-region", + rpo: "DEFAULT", + autoclass_enabled: nil, + autoclass_terminal_storage_class: nil, + enable_object_retention: nil, + effective_time: DateTime.now, + retention_duration_seconds: 604800, # 7 days + hierarchical_namespace: nil, + generation: "1733393981548601746" versioning_config = { "enabled" => versioning } if versioning { "kind" => "storage#bucket", - "id" => name, - "selfLink" => "#{url_root}/b/#{name}", - "projectNumber" => "1234567890", - "name" => name, - "timeCreated" => Time.now, - "generation" => generation, - "metageneration" => "1", - "owner" => { "entity" => "project-owners-1234567890" }, - "location" => location, - "locationType" => location_type, - "rpo" => rpo, - "cors" => cors, - "lifecycle" => lifecycle, - "logging" => logging_hash(logging_bucket, logging_prefix), - "storageClass" => storage_class, - "versioning" => versioning_config, - "website" => website_hash(website_main, website_404), - "billing" => billing_hash(requester_pays), - "etag" => "CAE=", - "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), - "enableObjectRetention" => enable_object_retention, - "softDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, - "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time - .to_time + retention_duration_seconds, - "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), - "hierarchicalNamespace" => hierarchical_namespace }.delete_if { |_, v| v.nil? } - end - - # 7 days - def soft_delete_policy_object retention_duration_seconds: 604_800 + "id" => name, + "selfLink" => "#{url_root}/b/#{name}", + "projectNumber" => "1234567890", + "name" => name, + "timeCreated" => Time.now, + "generation" => generation, + "metageneration" => "1", + "owner" => { "entity" => "project-owners-1234567890" }, + "location" => location, + "locationType" => location_type, + "rpo" => rpo, + "cors" => cors, + "lifecycle" => lifecycle, + "logging" => logging_hash(logging_bucket, logging_prefix), + "storageClass" => storage_class, + "versioning" => versioning_config, + "website" => website_hash(website_main, website_404), + "billing" => billing_hash(requester_pays), + "etag" => "CAE=", + "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), + "enableObjectRetention" => enable_object_retention, + "softDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, + "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time + .to_time + retention_duration_seconds, + "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), + "hierarchicalNamespace" => hierarchical_namespace + }.delete_if { |_, v| v.nil? } + end + + def soft_delete_policy_object retention_duration_seconds: 604800 # 7 days Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new( effective_time: DateTime.now, retention_duration_seconds: retention_duration_seconds @@ -174,34 +165,38 @@ def hierarchical_namespace_object enabled: true ) end - def autoclass_config_hash enabled, terminal_storage_class + def autoclass_config_hash(enabled, terminal_storage_class) { "enabled" => enabled, - "terminalStorageClass" => terminal_storage_class }.delete_if { |_, v| v.nil? } if !enabled.nil? || terminal_storage_class + "terminalStorageClass" => terminal_storage_class + }.delete_if { |_, v| v.nil? } if !enabled.nil? || terminal_storage_class end - def logging_hash bucket, prefix + def logging_hash(bucket, prefix) { "logBucket" => bucket, - "logObjectPrefix" => prefix }.delete_if { |_, v| v.nil? } if bucket || prefix + "logObjectPrefix" => prefix, + }.delete_if { |_, v| v.nil? } if bucket || prefix end - def website_hash website_main, website_404 + def website_hash(website_main, website_404) { "mainPageSuffix" => website_main, - "notFoundPage" => website_404 }.delete_if { |_, v| v.nil? } if website_main || website_404 + "notFoundPage" => website_404, + }.delete_if { |_, v| v.nil? } if website_main || website_404 end - def billing_hash requester_pays - { "requesterPays" => requester_pays } unless requester_pays.nil? + def billing_hash(requester_pays) + { "requesterPays" => requester_pays} unless requester_pays.nil? end - def file_retention_hash retention_params - { "mode" => retention_params[:mode], - "retainUntilTime" => retention_params[:retain_until_time] }.delete_if { |_, v| v.nil? } if !retention_params.nil? && !retention_params.empty? + def file_retention_hash(retention_params) + { "mode" => retention_params[:mode], + "retainUntilTime" => retention_params[:retain_until_time] + }.delete_if { |_, v| v.nil? } if !retention_params.nil? && !retention_params.empty? end - def random_file_hash bucket = random_bucket_name, - name = random_file_path, - generation = "1234567890", - kms_key_name = "path/to/encryption_key_name", + def random_file_hash bucket=random_bucket_name, + name=random_file_path, + generation="1234567890", + kms_key_name="path/to/encryption_key_name", custom_time: nil, retention_params: nil, override_unlocked_retention: nil, @@ -210,9 +205,9 @@ def random_file_hash bucket = random_bucket_name, { "kind" => "storage#object", "id" => "#{bucket}/#{name}/1234567890", "selfLink" => "https://www.googleapis.com/storage/v1/b/#{bucket}/o/#{name}", - "name" => name.to_s, + "name" => "#{name}", "timeCreated" => Time.now, - "bucket" => bucket.to_s, + "bucket" => "#{bucket}", "generation" => generation, "metageneration" => "1", "cacheControl" => "public, max-age=3600", @@ -274,7 +269,7 @@ def random_notification_gapi id: "1", topic: "//pubsub.googleapis.com/projects/t def download_http_resp gzip: nil headers = {} headers["Content-Encoding"] = ["gzip"] if gzip - OpenStruct.new header: headers + OpenStruct.new(header: headers) end def encryption_gapi key_name @@ -478,7 +473,7 @@ def patch_object_args generation: nil, user_project: nil, override_unlocked_retention: nil, options: {} - { + opts = { generation: generation, if_generation_match: if_generation_match, if_generation_not_match: if_generation_not_match, @@ -577,6 +572,7 @@ def restore_object_args copy_source_acl: nil, } end + def compose_request source_files, destination_gapi = nil, if_source_generation_match: nil source_objects = source_files.map do |file| if file.is_a? String @@ -608,13 +604,11 @@ def compose_request source_files, destination_gapi = nil, if_source_generation_m def list_files_gapi count = 2, token = nil, prefixes = nil files = count.times.map { Google::Apis::StorageV1::Object.from_json random_file_hash.to_json } - Google::Apis::StorageV1::Objects.new kind: "storage#objects", items: files, - next_page_token: token, - prefixes: prefixes + Google::Apis::StorageV1::Objects.new kind: "storage#objects", items: files, next_page_token: token, prefixes: prefixes end - def restore_file_gapi bucket, file_name, generation = nil + def restore_file_gapi bucket, file_name, generation=nil file_hash = random_file_hash(bucket, file_name, generation).to_json Google::Apis::StorageV1::Object.from_json file_hash end -end +end \ No newline at end of file From d3de7e8a626bc49f7cc8d9ea41aa2fb9fada22df Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 16 Dec 2024 19:10:53 +0000 Subject: [PATCH 10/25] removing commented code --- .../lib/google/cloud/storage/bucket.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index ad33d3aeee99..814a423f0978 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -82,7 +82,7 @@ class Bucket # files = bucket.files # Billed to "my-other-project" # attr_accessor :user_project - + ## # @private Create an empty Bucket object. def initialize @@ -2041,18 +2041,6 @@ def compose sources, alias compose_file compose alias combine compose - # def generation - # @generation = @gapi.generation - # end - - # def soft_delete_time - # @soft_delete_time = @gapi.soft_delete_time - # end - - # def hard_delete_time - # @hard_delete_time = @gapi.hard_delete_time - # end - ## # Generates a signed URL. See [Signed # URLs](https://cloud.google.com/storage/docs/access-control/signed-urls) From a0d10a598e5d9d0ae60f08ee27a99020f8c0918c Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 16 Dec 2024 19:16:04 +0000 Subject: [PATCH 11/25] removing unwanted file change --- .../samples/storage_restore_bucket.rb | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 google-cloud-storage/samples/storage_restore_bucket.rb diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb deleted file mode 100644 index 34a27c2093d1..000000000000 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright 2024 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 'pry' - -# [START storage_delete_bucket] -def delete_bucket bucket_name: - # The ID of your GCS bucket - # bucket_name = "your-unique-bucket-name" - - # require "google/cloud/storage" - require_relative '../lib/google/cloud/storage' - require_relative '../lib/google/cloud/storage/project' - require_relative '../lib/google/cloud/storage/bucket' - # require_relative '../lib/google/cloud/storage/bucket/list' - require_relative '../lib/google/cloud/storage/service' - - - storage = Google::Cloud::Storage.new - deleted_bucket = storage.create_bucket bucket_name - - deleted_bucket.delete - - # fetching generation - generation = deleted_bucket.gapi.generation - - # fetching soft deleted bucket with soft_delete_time and hard_delete_time - deleted_bucket_fetch = storage.bucket deleted_bucket.name, generation: generation, soft_deleted: true - - soft_delete_time= deleted_bucket_fetch.gapi.soft_delete_time - hard_delete_time= deleted_bucket_fetch.gapi.hard_delete_time - - puts "soft_delete_time - #{soft_delete_time}" - puts "hard_delete_time - #{hard_delete_time}" - binding.pry - - bucket_restored = storage.restore_bucket deleted_bucket.name, generation - # fetching soft deleted bucket list - deleted_buckets = storage.buckets soft_deleted: true - - puts "Deleted bucket: #{deleted_bucket.name} details" - puts "bucket generation #{generation}" - puts "count of soft deleted buckets #{deleted_buckets.count}" - - deleted_bucket.delete - puts "clean up done" - - #{}puts Gem.loaded_specs["google-cloud-storage"].full_gem_path - -end -# [END storage_delete_bucket] - -bucket_name = "ruby_try_8" -delete_bucket bucket_name: bucket_name - -#{}compose_file bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ \ No newline at end of file From be6bde2419705d79ce047013f7df12054f262a1d Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 16 Dec 2024 19:32:29 +0000 Subject: [PATCH 12/25] refactor --- google-cloud-storage/lib/google/cloud/storage/bucket.rb | 2 +- google-cloud-storage/lib/google/cloud/storage/bucket/list.rb | 2 +- google-cloud-storage/lib/google/cloud/storage/project.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 814a423f0978..e514ad651048 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -3145,7 +3145,7 @@ def lazy? ## # @private New Bucket from a Google API Client object. - def self.from_gapi gapi, service, user_project: nil, generation: nil, soft_deleted: nil + def self.from_gapi gapi, service, user_project: nil new.tap do |b| b.gapi = gapi b.service = service diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb index f3a0c1ef26de..3ccc34c1d041 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb @@ -148,7 +148,7 @@ def all request_limit: nil, &block def self.from_gapi gapi_list, service, prefix = nil, max = nil, user_project: nil, soft_deleted: nil buckets = new(Array(gapi_list.items).map do |gapi_object| - Bucket.from_gapi gapi_object, service, user_project: user_project, soft_deleted: soft_deleted + Bucket.from_gapi gapi_object, service, user_project: user_project end) buckets.instance_variable_set :@token, gapi_list.next_page_token buckets.instance_variable_set :@service, service diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index fc4f7cbaf7ea..cfce50472365 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -284,7 +284,7 @@ def bucket bucket_name, soft_deleted: soft_deleted, generation: generation - Bucket.from_gapi gapi, service, user_project: user_project, soft_deleted: soft_deleted, generation: generation + Bucket.from_gapi gapi, service, user_project: user_project rescue Google::Cloud::NotFoundError nil end @@ -600,7 +600,7 @@ def restore_bucket bucket_name, options: {} gapi = service.restore_bucket bucket_name, generation, options: options - Bucket.from_gapi gapi, service, generation: generation + Bucket.from_gapi gapi, service end ## From 63b065988d696136de9a930b6691c81fdc8986ca Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 18 Dec 2024 10:15:11 +0000 Subject: [PATCH 13/25] refactor --- .../lib/google/cloud/storage/bucket.rb | 25 +++++++++++++++++++ .../test/google/cloud/storage/project_test.rb | 20 +++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index e514ad651048..17624c8fc086 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -2281,6 +2281,31 @@ def signed_url path = nil, end end + # Fetches generation no. of bucket + # @example + # require "google/cloud/storage" + # storage = Google::Cloud::Storage.new + # bucket = storage.bucket "my-bucket" + # generation= bucket.generation + def generation + @generation = @gapi.generation + end + + # Fetches soft_delete_time of a soft deleted bucket + # @example + # bucket.delete + # bucket.soft_delete_time + def soft_delete_time + @soft_delete_time = @gapi.soft_delete_time + end + + # Fetches hard_delete_time of a soft deleted bucket + # @example + # bucket.hard_delete_time + def hard_delete_time + @hard_delete_time = @gapi.hard_delete_time + end + ## # Generate a PostObject that includes the fields and URL to # upload objects via HTML forms. diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index c5cfb172742f..0019d1e7ff29 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -659,9 +659,9 @@ def stub.insert_bucket *args _(buckets.size).must_equal num_buckets bucket = buckets.first _(bucket).must_be_kind_of Google::Cloud::Storage::Bucket - _(bucket.gapi.generation).wont_be_nil - _(bucket.gapi.hard_delete_time).wont_be_nil - _(bucket.gapi.soft_delete_time).wont_be_nil + _(bucket.generation).wont_be_nil + _(bucket.hard_delete_time).wont_be_nil + _(bucket.soft_delete_time).wont_be_nil end it "paginates buckets" do @@ -863,7 +863,7 @@ def stub.insert_bucket *args _(bucket).wont_be :lazy? end - it "finds a deleted bucket" do + it "finds a deleted bucket" do bucket_name = "found-bucket" generation = 1_733_393_981_548_601_746 @@ -877,9 +877,9 @@ def stub.insert_bucket *args mock.verify _(bucket.name).must_equal bucket_name - _(bucket.gapi.generation).must_equal generation - _(bucket.gapi.hard_delete_time).wont_be_nil - _(bucket.gapi.hard_delete_time).wont_be_nil + _(bucket.generation).must_equal generation + _(bucket.soft_delete_time).wont_be_nil + _(bucket.hard_delete_time).wont_be_nil _(bucket).wont_be :lazy? end @@ -889,14 +889,14 @@ def stub.insert_bucket *args generation = 1_733_393_981_548_601_746 mock = Minitest::Mock.new - mock.expect :restore_bucket, restored_bucket_gapi(bucket_name, generation), + mock.expect :restore_bucket, restored_bucket_gapi(bucket_name), [bucket_name, generation], options: {} storage.service.mocked_service = mock bucket = storage.restore_bucket bucket_name, generation mock.verify _(bucket.name).must_equal bucket_name - _(bucket.gapi.generation).must_equal generation + _(bucket.generation).must_equal generation end it "finds a bucket with find_bucket alias" do @@ -1089,7 +1089,7 @@ def list_deleted_buckets_gapi count = 2, token = nil ) end - def restored_bucket_gapi name, _generation + def restored_bucket_gapi name Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json end From c3f5f8bb47a0918ae1b08b12958252be2eabc8d7 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 12 Dec 2024 07:31:50 +0000 Subject: [PATCH 14/25] WIP-samples --- .../samples/acceptance/buckets_test.rb | 37 +++++++++++++++ .../storage_get_soft_deleted_bucket.rb | 44 ++++++++++++++++++ .../storage_list_soft_deleted_buckets.rb | 38 +++++++++++++++ .../samples/storage_restore_bucket.rb | 46 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb create mode 100644 google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb create mode 100644 google-cloud-storage/samples/storage_restore_bucket.rb diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index f4f416744fd1..03ff7c29e932 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -40,8 +40,10 @@ require_relative "../storage_get_public_access_prevention" require_relative "../storage_get_requester_pays_status" require_relative "../storage_get_retention_policy" +require_relative "../storage_get_soft_deleted_bucket" require_relative "../storage_get_uniform_bucket_level_access" require_relative "../storage_list_buckets" +require_relative "../storage_list_soft_deleted_buckets" require_relative "../storage_lock_retention_policy" require_relative "../storage_remove_bucket_label" require_relative "../storage_remove_cors_configuration" @@ -53,6 +55,7 @@ require_relative "../storage_set_retention_policy" require_relative "../storage_get_autoclass" require_relative "../storage_set_autoclass" +require "pry" describe "Buckets Snippets" do let(:storage_client) { Google::Cloud::Storage.new } @@ -119,6 +122,40 @@ end end + describe "storage_soft_deleted_bucket" do + let(:generation) {bucket.generation} + + # it "get soft deleted bucket generation" do + # out, _err = capture_io do + # bucket.generation + # end + # assert generation, "Bucket generation should be present" + # # end + + + it "get soft deleted bucket soft_delete_time and hard_delete_time" do + # @bucket.delete + binding.pry + bucket.delete + out, _err = capture_io do + get_soft_deleted_bucket bucket_name: bucket.name, generation:generation + end + assert "soft_delete_time", "Bucket soft_delete_time should be present" + assert "hard_delete_time", "Bucket hard_delete_time should be present" + end + + it "lists soft deleted buckets" do + # delete_bucket bucket_name: bucket_name + # list_soft_deleted_buckets + list_deleted_bucket, _err = capture_io do + list_soft_deleted_buckets + end + assert_includes list_deleted_bucket, bucket.name + end + + end + + describe "storage_create_bucket_dual_region" do it "creates dual region bucket" do location = "US" diff --git a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb new file mode 100644 index 000000000000..e6be702df16e --- /dev/null +++ b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb @@ -0,0 +1,44 @@ +# Copyright 2024 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_get_soft_deleted_bucket] +def get_soft_deleted_bucket bucket_name:, generation: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + require "google/cloud/storage" + # require_relative '../lib/google/cloud/storage' + # require_relative '../lib/google/cloud/storage/project' + # require_relative '../lib/google/cloud/storage/bucket' + # # require_relative '../lib/google/cloud/storage/bucket/list' + # require_relative '../lib/google/cloud/storage/service' + # require "pry" + + storage = Google::Cloud::Storage.new + bucket_name= bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") + + + # fetching soft deleted bucket with soft_delete_time and hard_delete_time + deleted_bucket_fetch = storage.bucket bucket_name, generation: generation, soft_deleted: true + + soft_delete_time = deleted_bucket_fetch.soft_delete_time + hard_delete_time = deleted_bucket_fetch.hard_delete_time + + puts "soft_delete_time - #{soft_delete_time}" + puts "hard_delete_time - #{hard_delete_time}" +end +# [END storage_delete_bucket] + + +get_soft_deleted_bucket bucket_name: ARGV.shift, generation: ARGV.shift if $PROGRAM_NAME == __FILE__ diff --git a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb new file mode 100644 index 000000000000..ec81eac9b7da --- /dev/null +++ b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb @@ -0,0 +1,38 @@ +# Copyright 2024 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_list_soft_deleted_buckets] +def list_soft_deleted_buckets + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + # {}require "google/cloud/storage" + require_relative "../lib/google/cloud/storage" + require_relative "../lib/google/cloud/storage/project" + require_relative "../lib/google/cloud/storage/bucket" + # require_relative '../lib/google/cloud/storage/bucket/list' + require_relative "../lib/google/cloud/storage/service" + + storage = Google::Cloud::Storage.new + + # fetching soft deleted bucket list + deleted_buckets = storage.buckets soft_deleted: true + + deleted_buckets.each do |bucket| + puts bucket.name + end +end +# [END storage_list_soft_deleted_buckets] + +list_soft_deleted_buckets if $PROGRAM_NAME == __FILE__ \ No newline at end of file diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb new file mode 100644 index 000000000000..a338570983df --- /dev/null +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -0,0 +1,46 @@ +# Copyright 2024 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 'pry' + +# [START storage_delete_bucket] +def restore_bucket bucket_name:, generation: + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + + #{}require "google/cloud/storage" + require_relative '../lib/google/cloud/storage' + require_relative '../lib/google/cloud/storage/project' + require_relative '../lib/google/cloud/storage/bucket' + # require_relative '../lib/google/cloud/storage/bucket/list' + require_relative '../lib/google/cloud/storage/service' + require "pry" + + + storage = Google::Cloud::Storage.new + + bucket_restored = storage.restore_bucket bucket_name, generation, soft_deleted: true + # fetching soft deleted bucket list +# deleted_buckets = storage.buckets soft_deleted: true + binding.pry + + if JSON.parse(bucket_restored.gapi)["name"] == deleted_bucket.name + puts "#{deleted_bucket.name} Bucket restored" + else + puts "#{deleted_bucket.name} Bucket not restored" + end + +end +# [END storage_delete_bucket] + +restore_bucket bucket_name: ARGV.shift, generation: ARGV.shift if $PROGRAM_NAME == __FILE__ From ebe0e74e7233890170a0e775e839db2d186d6213 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Tue, 17 Dec 2024 10:36:32 +0000 Subject: [PATCH 15/25] adding samples and tests --- .../samples/acceptance/buckets_test.rb | 19 ++++++++----------- .../samples/acceptance/project_test.rb | 16 ++++++++++++++++ .../storage_get_soft_deleted_bucket.rb | 18 +++++++++--------- .../storage_list_soft_deleted_buckets.rb | 2 +- .../samples/storage_restore_bucket.rb | 10 +++++----- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 03ff7c29e932..e2b0c22df76d 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -123,19 +123,17 @@ end describe "storage_soft_deleted_bucket" do - let(:generation) {bucket.generation} + let(:generation) {bucket.gapi.generation} - # it "get soft deleted bucket generation" do - # out, _err = capture_io do - # bucket.generation - # end - # assert generation, "Bucket generation should be present" - # # end + it "get soft deleted bucket generation" do + out, _err = capture_io do + bucket.gapi.generation + end + assert generation, "Bucket generation should be present" + end it "get soft deleted bucket soft_delete_time and hard_delete_time" do - # @bucket.delete - binding.pry bucket.delete out, _err = capture_io do get_soft_deleted_bucket bucket_name: bucket.name, generation:generation @@ -145,8 +143,7 @@ end it "lists soft deleted buckets" do - # delete_bucket bucket_name: bucket_name - # list_soft_deleted_buckets + list_deleted_bucket, _err = capture_io do list_soft_deleted_buckets end diff --git a/google-cloud-storage/samples/acceptance/project_test.rb b/google-cloud-storage/samples/acceptance/project_test.rb index 2de54569a0ba..babe7bf01985 100644 --- a/google-cloud-storage/samples/acceptance/project_test.rb +++ b/google-cloud-storage/samples/acceptance/project_test.rb @@ -14,6 +14,7 @@ require_relative "helper" require_relative "../storage_get_service_account" +require_relative "../storage_restore_bucket" describe "Storage Quickstart" do let(:project) { Google::Cloud::Storage.new } @@ -28,3 +29,18 @@ assert_includes out, "@gs-project-accounts.iam.gserviceaccount.com" end end + +describe "storage_soft_deleted_bucket" do + let(:storage_client) { Google::Cloud::Storage.new } + let(:bucket) { fixture_bucket } + let(:generation) {bucket.gapi.generation} + + it "restores a soft deleted bucket" do + bucket.delete + out, _err = capture_io do + restore_bucket bucket_name: bucket.name, generation:generation + end + assert "soft_delete_time", "#{bucket.name} Bucket restored" + end + +end diff --git a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb index e6be702df16e..cef00059fec6 100644 --- a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb +++ b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb @@ -17,13 +17,13 @@ def get_soft_deleted_bucket bucket_name:, generation: # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" - require "google/cloud/storage" - # require_relative '../lib/google/cloud/storage' - # require_relative '../lib/google/cloud/storage/project' - # require_relative '../lib/google/cloud/storage/bucket' - # # require_relative '../lib/google/cloud/storage/bucket/list' - # require_relative '../lib/google/cloud/storage/service' - # require "pry" + # require "google/cloud/storage" + require_relative '../lib/google/cloud/storage' + require_relative '../lib/google/cloud/storage/project' + require_relative '../lib/google/cloud/storage/bucket' + # require_relative '../lib/google/cloud/storage/bucket/list' + require_relative '../lib/google/cloud/storage/service' + require "pry" storage = Google::Cloud::Storage.new bucket_name= bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") @@ -32,8 +32,8 @@ def get_soft_deleted_bucket bucket_name:, generation: # fetching soft deleted bucket with soft_delete_time and hard_delete_time deleted_bucket_fetch = storage.bucket bucket_name, generation: generation, soft_deleted: true - soft_delete_time = deleted_bucket_fetch.soft_delete_time - hard_delete_time = deleted_bucket_fetch.hard_delete_time + soft_delete_time = deleted_bucket_fetch.gapi.soft_delete_time + hard_delete_time = deleted_bucket_fetch.gapi.hard_delete_time puts "soft_delete_time - #{soft_delete_time}" puts "hard_delete_time - #{hard_delete_time}" diff --git a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb index ec81eac9b7da..1859e4b378aa 100644 --- a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb +++ b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb @@ -17,7 +17,7 @@ def list_soft_deleted_buckets # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" - # {}require "google/cloud/storage" + # require "google/cloud/storage" require_relative "../lib/google/cloud/storage" require_relative "../lib/google/cloud/storage/project" require_relative "../lib/google/cloud/storage/bucket" diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb index a338570983df..a14189ebc0bb 100644 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -18,7 +18,7 @@ def restore_bucket bucket_name:, generation: # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" - #{}require "google/cloud/storage" + # require "google/cloud/storage" require_relative '../lib/google/cloud/storage' require_relative '../lib/google/cloud/storage/project' require_relative '../lib/google/cloud/storage/bucket' @@ -29,15 +29,15 @@ def restore_bucket bucket_name:, generation: storage = Google::Cloud::Storage.new - bucket_restored = storage.restore_bucket bucket_name, generation, soft_deleted: true + bucket_restored = storage.restore_bucket bucket_name, generation # fetching soft deleted bucket list # deleted_buckets = storage.buckets soft_deleted: true binding.pry - if JSON.parse(bucket_restored.gapi)["name"] == deleted_bucket.name - puts "#{deleted_bucket.name} Bucket restored" + if bucket_restored.name == bucket_name + puts "#{bucket_name} Bucket restored" else - puts "#{deleted_bucket.name} Bucket not restored" + puts "#{bucket_name} Bucket not restored" end end From 1ef91e818157d97690cb504f2da711c94a719efc Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 18 Dec 2024 13:20:19 +0000 Subject: [PATCH 16/25] refactor --- .../samples/acceptance/buckets_test.rb | 9 ++++----- .../samples/storage_get_soft_deleted_bucket.rb | 17 ++++++++++------- .../samples/storage_restore_bucket.rb | 9 +-------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index e2b0c22df76d..17d20a1063b5 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -55,7 +55,6 @@ require_relative "../storage_set_retention_policy" require_relative "../storage_get_autoclass" require_relative "../storage_set_autoclass" -require "pry" describe "Buckets Snippets" do let(:storage_client) { Google::Cloud::Storage.new } @@ -127,7 +126,7 @@ it "get soft deleted bucket generation" do out, _err = capture_io do - bucket.gapi.generation + bucket.generation end assert generation, "Bucket generation should be present" end @@ -138,12 +137,12 @@ out, _err = capture_io do get_soft_deleted_bucket bucket_name: bucket.name, generation:generation end - assert "soft_delete_time", "Bucket soft_delete_time should be present" - assert "hard_delete_time", "Bucket hard_delete_time should be present" + assert "soft_delete_time - #{bucket.soft_delete_time}", "Bucket soft_delete_time should be present" + assert "hard_delete_time - #{bucket.hard_delete_time}", "Bucket hard_delete_time should be present" end it "lists soft deleted buckets" do - + bucket.delete list_deleted_bucket, _err = capture_io do list_soft_deleted_buckets end diff --git a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb index cef00059fec6..9850442c9b33 100644 --- a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb +++ b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb @@ -23,7 +23,6 @@ def get_soft_deleted_bucket bucket_name:, generation: require_relative '../lib/google/cloud/storage/bucket' # require_relative '../lib/google/cloud/storage/bucket/list' require_relative '../lib/google/cloud/storage/service' - require "pry" storage = Google::Cloud::Storage.new bucket_name= bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") @@ -32,13 +31,17 @@ def get_soft_deleted_bucket bucket_name:, generation: # fetching soft deleted bucket with soft_delete_time and hard_delete_time deleted_bucket_fetch = storage.bucket bucket_name, generation: generation, soft_deleted: true - soft_delete_time = deleted_bucket_fetch.gapi.soft_delete_time - hard_delete_time = deleted_bucket_fetch.gapi.hard_delete_time - - puts "soft_delete_time - #{soft_delete_time}" - puts "hard_delete_time - #{hard_delete_time}" + soft_delete_time = deleted_bucket_fetch.soft_delete_time + hard_delete_time = deleted_bucket_fetch.hard_delete_time + + if (soft_delete_time && hard_delete_time).nil? + puts "Not Found" + else + puts "soft_delete_time - #{soft_delete_time}" + puts "hard_delete_time - #{hard_delete_time}" + end end -# [END storage_delete_bucket] +# [END storage_get_soft_deleted_bucket] get_soft_deleted_bucket bucket_name: ARGV.shift, generation: ARGV.shift if $PROGRAM_NAME == __FILE__ diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb index a14189ebc0bb..f8ea80a92ead 100644 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -11,7 +11,6 @@ # 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 'pry' # [START storage_delete_bucket] def restore_bucket bucket_name:, generation: @@ -24,22 +23,16 @@ def restore_bucket bucket_name:, generation: require_relative '../lib/google/cloud/storage/bucket' # require_relative '../lib/google/cloud/storage/bucket/list' require_relative '../lib/google/cloud/storage/service' - require "pry" - storage = Google::Cloud::Storage.new bucket_restored = storage.restore_bucket bucket_name, generation - # fetching soft deleted bucket list -# deleted_buckets = storage.buckets soft_deleted: true - binding.pry if bucket_restored.name == bucket_name puts "#{bucket_name} Bucket restored" else - puts "#{bucket_name} Bucket not restored" + puts "#{bucket_name} Bucket not restored" end - end # [END storage_delete_bucket] From c62d19d0b3cc8743d3f20a62a4752a5bc50dc673 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 19 Dec 2024 10:53:46 +0000 Subject: [PATCH 17/25] refactor --- google-cloud-storage/samples/Gemfile | 6 ++---- .../samples/storage_get_soft_deleted_bucket.rb | 10 +++------- .../samples/storage_list_soft_deleted_buckets.rb | 9 +-------- .../samples/storage_restore_bucket.rb | 13 +++++-------- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/google-cloud-storage/samples/Gemfile b/google-cloud-storage/samples/Gemfile index 3b36d79c3373..df5ef4bf5767 100644 --- a/google-cloud-storage/samples/Gemfile +++ b/google-cloud-storage/samples/Gemfile @@ -20,14 +20,12 @@ source "https://rubygems.org" if ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master" gem "google-cloud-kms", group: :test, path: "../../google-cloud-kms" gem "google-cloud-pubsub", group: :test, path: "../../google-cloud-pubsub" - gem "google-cloud-storage", path: "../../google-cloud-storage" else gem "google-cloud-kms" gem "google-cloud-pubsub" - # [START storage_dependencies] - gem "google-cloud-storage" - # [END storage_dependencies] end +gem "google-cloud-storage", path: "../../google-cloud-storage" + group :test do gem "google-style", "~> 1.30.0" diff --git a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb index 9850442c9b33..060ab5404124 100644 --- a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb +++ b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb @@ -16,18 +16,14 @@ def get_soft_deleted_bucket bucket_name:, generation: # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" + # The generation no of your GCS bucket + # generation = "1234567896987" - # require "google/cloud/storage" - require_relative '../lib/google/cloud/storage' - require_relative '../lib/google/cloud/storage/project' - require_relative '../lib/google/cloud/storage/bucket' - # require_relative '../lib/google/cloud/storage/bucket/list' - require_relative '../lib/google/cloud/storage/service' + require "google/cloud/storage" storage = Google::Cloud::Storage.new bucket_name= bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") - # fetching soft deleted bucket with soft_delete_time and hard_delete_time deleted_bucket_fetch = storage.bucket bucket_name, generation: generation, soft_deleted: true diff --git a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb index 1859e4b378aa..1f980fa4e098 100644 --- a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb +++ b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb @@ -14,15 +14,8 @@ # [START storage_list_soft_deleted_buckets] def list_soft_deleted_buckets - # The ID of your GCS bucket - # bucket_name = "your-unique-bucket-name" - # require "google/cloud/storage" - require_relative "../lib/google/cloud/storage" - require_relative "../lib/google/cloud/storage/project" - require_relative "../lib/google/cloud/storage/bucket" - # require_relative '../lib/google/cloud/storage/bucket/list' - require_relative "../lib/google/cloud/storage/service" + require "google/cloud/storage" storage = Google::Cloud::Storage.new diff --git a/google-cloud-storage/samples/storage_restore_bucket.rb b/google-cloud-storage/samples/storage_restore_bucket.rb index f8ea80a92ead..c2cee7944174 100644 --- a/google-cloud-storage/samples/storage_restore_bucket.rb +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -12,17 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START storage_delete_bucket] +# [START storage_restore_bucket] def restore_bucket bucket_name:, generation: # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" + # The generation no of your GCS bucket + # generation = "1234567896987" - # require "google/cloud/storage" - require_relative '../lib/google/cloud/storage' - require_relative '../lib/google/cloud/storage/project' - require_relative '../lib/google/cloud/storage/bucket' - # require_relative '../lib/google/cloud/storage/bucket/list' - require_relative '../lib/google/cloud/storage/service' + require "google/cloud/storage" storage = Google::Cloud::Storage.new @@ -34,6 +31,6 @@ def restore_bucket bucket_name:, generation: puts "#{bucket_name} Bucket not restored" end end -# [END storage_delete_bucket] +# [END storage_restore_bucket] restore_bucket bucket_name: ARGV.shift, generation: ARGV.shift if $PROGRAM_NAME == __FILE__ From 208a526df26d3d41a75f8c41e0c9fb6bc7d26058 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 19 Dec 2024 11:11:46 +0000 Subject: [PATCH 18/25] updating comments --- .../lib/google/cloud/storage/bucket.rb | 1 - .../lib/google/cloud/storage/project.rb | 24 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 17624c8fc086..4f30c6878444 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -24,7 +24,6 @@ require "google/cloud/storage/post_object" require "pathname" - module Google module Cloud module Storage diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index cfce50472365..ea8923a4a87a 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -232,6 +232,10 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: # account, transit costs will be billed to the given project. This # parameter is required with requester pays-enabled buckets. The # default is `nil`. + # @param [fixedInt] generation generation no of bucket + # on whether the bucket's current metageneration matches the given value. + # @param [Boolean] soft_deleted If true, returns the soft-deleted bucket. + # This parameter is required if generation is specified. # # The value provided will be applied to all operations on the returned # bucket instance and its files. @@ -265,7 +269,16 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: # bucket = storage.bucket "other-project-bucket", # user_project: "my-other-project" # files = bucket.files # Billed to "my-other-project" + # @example With `soft_deleted` set to a true and generation specified: + # require "google/cloud/storage" + # + # storage = Google::Cloud::Storage.new # + # bucket = storage.bucket "my-bucket", + # soft_deleted: true, + # generation: 1234567889 + # puts bucket.name + # def bucket bucket_name, skip_lookup: false, generation: nil, @@ -573,15 +586,6 @@ def hmac_keys service_account_email: nil, project_id: nil, # # @param [String] bucket_name Name of a bucket. # @param [Fixnum] generation generation of a bucket. - # @param [Boolean] skip_lookup Optionally create a Bucket object - # without verifying the bucket resource exists on the Storage service. - # Calls made on this object will raise errors if the bucket resource - # does not exist. Default is `false`. - # @param [Integer] if_metageneration_match Makes the operation conditional - # on whether the bucket's current metageneration matches the given value. - # @param [Boolean] soft_deleted If this parameter is set to - # `true` project looks in the list of soft deleted buckets - # # # @return [Google::Cloud::Storage::Bucket, nil] Returns nil if bucket # does not exist @@ -592,7 +596,7 @@ def hmac_keys service_account_email: nil, project_id: nil, # storage = Google::Cloud::Storage.new # generation= 123 # - # bucket = storage.restore_bucket "my-bucket", generation, soft_deleted: true + # bucket = storage.restore_bucket "my-bucket", generation # puts bucket.name # def restore_bucket bucket_name, From ed9d566b37edcfa5d9126e52fd0a995670b0c30c Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Fri, 20 Dec 2024 05:13:14 +0000 Subject: [PATCH 19/25] fix lint issues --- .../lib/google/cloud/storage/project.rb | 2 +- .../samples/acceptance/buckets_test.rb | 13 ++++++------- .../samples/acceptance/project_test.rb | 9 ++++----- .../samples/storage_get_soft_deleted_bucket.rb | 4 ++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index ea8923a4a87a..43886c6c08b1 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -234,7 +234,7 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: # default is `nil`. # @param [fixedInt] generation generation no of bucket # on whether the bucket's current metageneration matches the given value. - # @param [Boolean] soft_deleted If true, returns the soft-deleted bucket. + # @param [Boolean] soft_deleted If true, returns the soft-deleted bucket. # This parameter is required if generation is specified. # # The value provided will be applied to all operations on the returned diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 17d20a1063b5..68b5a6d30e0d 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -122,33 +122,32 @@ end describe "storage_soft_deleted_bucket" do - let(:generation) {bucket.gapi.generation} + let(:generation) { bucket.gapi.generation } it "get soft deleted bucket generation" do - out, _err = capture_io do + _out, _err = capture_io do bucket.generation end assert generation, "Bucket generation should be present" - end + end it "get soft deleted bucket soft_delete_time and hard_delete_time" do bucket.delete - out, _err = capture_io do - get_soft_deleted_bucket bucket_name: bucket.name, generation:generation + _out, _err = capture_io do + get_soft_deleted_bucket bucket_name: bucket.name, generation: generation end assert "soft_delete_time - #{bucket.soft_delete_time}", "Bucket soft_delete_time should be present" assert "hard_delete_time - #{bucket.hard_delete_time}", "Bucket hard_delete_time should be present" end - it "lists soft deleted buckets" do + it "lists soft deleted buckets" do bucket.delete list_deleted_bucket, _err = capture_io do list_soft_deleted_buckets end assert_includes list_deleted_bucket, bucket.name end - end diff --git a/google-cloud-storage/samples/acceptance/project_test.rb b/google-cloud-storage/samples/acceptance/project_test.rb index babe7bf01985..ab4c32fd2026 100644 --- a/google-cloud-storage/samples/acceptance/project_test.rb +++ b/google-cloud-storage/samples/acceptance/project_test.rb @@ -31,16 +31,15 @@ end describe "storage_soft_deleted_bucket" do - let(:storage_client) { Google::Cloud::Storage.new } + let(:storage_client) { Google::Cloud::Storage.new } let(:bucket) { fixture_bucket } - let(:generation) {bucket.gapi.generation} + let(:generation) { bucket.gapi.generation } it "restores a soft deleted bucket" do bucket.delete - out, _err = capture_io do - restore_bucket bucket_name: bucket.name, generation:generation + _out, _err = capture_io do + restore_bucket bucket_name: bucket.name, generation: generation end assert "soft_delete_time", "#{bucket.name} Bucket restored" end - end diff --git a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb index 060ab5404124..22d59149b9bc 100644 --- a/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb +++ b/google-cloud-storage/samples/storage_get_soft_deleted_bucket.rb @@ -22,14 +22,14 @@ def get_soft_deleted_bucket bucket_name:, generation: require "google/cloud/storage" storage = Google::Cloud::Storage.new - bucket_name= bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") + bucket_name = bucket_name.gsub(/[^a-zA-Z0-9\- ]/, "") # fetching soft deleted bucket with soft_delete_time and hard_delete_time deleted_bucket_fetch = storage.bucket bucket_name, generation: generation, soft_deleted: true soft_delete_time = deleted_bucket_fetch.soft_delete_time hard_delete_time = deleted_bucket_fetch.hard_delete_time - + if (soft_delete_time && hard_delete_time).nil? puts "Not Found" else From 85ab0dffb7175f22b370776eb329db85a9188fbf Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Fri, 20 Dec 2024 05:25:34 +0000 Subject: [PATCH 20/25] fix lint issues --- google-cloud-storage/lib/google/cloud/storage/bucket/list.rb | 1 + google-cloud-storage/lib/google/cloud/storage/project.rb | 2 +- google-cloud-storage/samples/acceptance/project_test.rb | 2 +- .../samples/storage_list_soft_deleted_buckets.rb | 3 +-- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb index 3ccc34c1d041..428300c40df0 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket/list.rb @@ -155,6 +155,7 @@ def self.from_gapi gapi_list, service, prefix = nil, max = nil, buckets.instance_variable_set :@prefix, prefix buckets.instance_variable_set :@max, max buckets.instance_variable_set :@user_project, user_project + buckets.instance_variable_set :@soft_deleted, soft_deleted buckets end diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 43886c6c08b1..f93ec320b1c3 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -278,7 +278,7 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: # soft_deleted: true, # generation: 1234567889 # puts bucket.name - # + # def bucket bucket_name, skip_lookup: false, generation: nil, diff --git a/google-cloud-storage/samples/acceptance/project_test.rb b/google-cloud-storage/samples/acceptance/project_test.rb index ab4c32fd2026..2f2254b11942 100644 --- a/google-cloud-storage/samples/acceptance/project_test.rb +++ b/google-cloud-storage/samples/acceptance/project_test.rb @@ -31,7 +31,7 @@ end describe "storage_soft_deleted_bucket" do - let(:storage_client) { Google::Cloud::Storage.new } + let(:storage_client) { Google::Cloud::Storage.new } let(:bucket) { fixture_bucket } let(:generation) { bucket.gapi.generation } diff --git a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb index 1f980fa4e098..4869fb70d7fc 100644 --- a/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb +++ b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb @@ -14,7 +14,6 @@ # [START storage_list_soft_deleted_buckets] def list_soft_deleted_buckets - require "google/cloud/storage" storage = Google::Cloud::Storage.new @@ -28,4 +27,4 @@ def list_soft_deleted_buckets end # [END storage_list_soft_deleted_buckets] -list_soft_deleted_buckets if $PROGRAM_NAME == __FILE__ \ No newline at end of file +list_soft_deleted_buckets if $PROGRAM_NAME == __FILE__ From 2eb1fce9744b568592ed26238e8b578843e463cd Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Sat, 21 Dec 2024 04:53:24 +0000 Subject: [PATCH 21/25] fix review comments --- .../lib/google/cloud/storage/bucket.rb | 6 +- .../lib/google/cloud/storage/project.rb | 2 +- google-cloud-storage/samples/Gemfile | 6 +- .../samples/acceptance/buckets_test.rb | 16 ++--- .../samples/acceptance/project_test.rb | 2 +- .../test/google/cloud/storage/project_test.rb | 30 +++----- google-cloud-storage/test/helper.rb | 69 ++++--------------- 7 files changed, 38 insertions(+), 93 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index 4f30c6878444..3b5ea98a4c70 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -2287,7 +2287,7 @@ def signed_url path = nil, # bucket = storage.bucket "my-bucket" # generation= bucket.generation def generation - @generation = @gapi.generation + @gapi.generation end # Fetches soft_delete_time of a soft deleted bucket @@ -2295,14 +2295,14 @@ def generation # bucket.delete # bucket.soft_delete_time def soft_delete_time - @soft_delete_time = @gapi.soft_delete_time + @gapi.soft_delete_time end # Fetches hard_delete_time of a soft deleted bucket # @example # bucket.hard_delete_time def hard_delete_time - @hard_delete_time = @gapi.hard_delete_time + @gapi.hard_delete_time end ## diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index f93ec320b1c3..3bb979a9ece6 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -232,7 +232,7 @@ def buckets prefix: nil, token: nil, max: nil, user_project: nil, soft_deleted: # account, transit costs will be billed to the given project. This # parameter is required with requester pays-enabled buckets. The # default is `nil`. - # @param [fixedInt] generation generation no of bucket + # @param [Integer] generation generation no of bucket # on whether the bucket's current metageneration matches the given value. # @param [Boolean] soft_deleted If true, returns the soft-deleted bucket. # This parameter is required if generation is specified. diff --git a/google-cloud-storage/samples/Gemfile b/google-cloud-storage/samples/Gemfile index df5ef4bf5767..3b36d79c3373 100644 --- a/google-cloud-storage/samples/Gemfile +++ b/google-cloud-storage/samples/Gemfile @@ -20,12 +20,14 @@ source "https://rubygems.org" if ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master" gem "google-cloud-kms", group: :test, path: "../../google-cloud-kms" gem "google-cloud-pubsub", group: :test, path: "../../google-cloud-pubsub" + gem "google-cloud-storage", path: "../../google-cloud-storage" else gem "google-cloud-kms" gem "google-cloud-pubsub" + # [START storage_dependencies] + gem "google-cloud-storage" + # [END storage_dependencies] end -gem "google-cloud-storage", path: "../../google-cloud-storage" - group :test do gem "google-style", "~> 1.30.0" diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 68b5a6d30e0d..b40eb4de25e6 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -122,18 +122,11 @@ end describe "storage_soft_deleted_bucket" do - let(:generation) { bucket.gapi.generation } - - it "get soft deleted bucket generation" do - _out, _err = capture_io do - bucket.generation - end - assert generation, "Bucket generation should be present" - end + let(:generation) { bucket.generation } + it "get soft deleted bucket, its soft_delete_time and hard_delete_time" do - it "get soft deleted bucket soft_delete_time and hard_delete_time" do - bucket.delete + delete_bucket_helper bucket.name _out, _err = capture_io do get_soft_deleted_bucket bucket_name: bucket.name, generation: generation end @@ -142,11 +135,10 @@ end it "lists soft deleted buckets" do - bucket.delete list_deleted_bucket, _err = capture_io do list_soft_deleted_buckets end - assert_includes list_deleted_bucket, bucket.name + assert list_deleted_bucket, "List of soft deleted bucket should not be blank" end end diff --git a/google-cloud-storage/samples/acceptance/project_test.rb b/google-cloud-storage/samples/acceptance/project_test.rb index 2f2254b11942..6e3b944109d3 100644 --- a/google-cloud-storage/samples/acceptance/project_test.rb +++ b/google-cloud-storage/samples/acceptance/project_test.rb @@ -36,7 +36,7 @@ let(:generation) { bucket.gapi.generation } it "restores a soft deleted bucket" do - bucket.delete + delete_bucket_helper bucket.name _out, _err = capture_io do restore_bucket bucket_name: bucket.name, generation: generation end diff --git a/google-cloud-storage/test/google/cloud/storage/project_test.rb b/google-cloud-storage/test/google/cloud/storage/project_test.rb index 0019d1e7ff29..942c6177732f 100644 --- a/google-cloud-storage/test/google/cloud/storage/project_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/project_test.rb @@ -646,9 +646,10 @@ def stub.insert_bucket *args it "lists deleted buckets" do num_buckets = 3 + soft_deleted= true mock = Minitest::Mock.new - mock.expect :list_buckets, list_deleted_buckets_gapi(num_buckets), [project], prefix: nil, page_token: nil, + mock.expect :list_buckets, list_buckets_gapi(num_buckets,nil,soft_deleted), [project], prefix: nil, page_token: nil, max_results: nil, user_project: nil, soft_deleted: true, options: {} storage.service.mocked_service = mock @@ -866,13 +867,14 @@ def stub.insert_bucket *args it "finds a deleted bucket" do bucket_name = "found-bucket" generation = 1_733_393_981_548_601_746 + soft_deleted= true mock = Minitest::Mock.new - mock.expect :get_bucket, find_deleted_bucket_gapi(bucket_name), - [bucket_name], **get_bucket_args(soft_deleted: true, generation: generation) + mock.expect :get_bucket, find_bucket_gapi(bucket_name, soft_deleted), + [bucket_name], **get_bucket_args(soft_deleted: soft_deleted, generation: generation) storage.service.mocked_service = mock - bucket = storage.bucket bucket_name, soft_deleted: true, generation: generation + bucket = storage.bucket bucket_name, soft_deleted: soft_deleted, generation: generation mock.verify @@ -1067,23 +1069,13 @@ def create_bucket_gapi name = nil, Google::Apis::StorageV1::Bucket.new **options end - def find_deleted_bucket_gapi name = nil - Google::Apis::StorageV1::Bucket.from_json random_deleted_bucket_hash(name: name).to_json - end - - def find_bucket_gapi name = nil - Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name).to_json - end - def list_buckets_gapi count = 2, token = nil - buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_bucket_hash.to_json } - Google::Apis::StorageV1::Buckets.new( - kind: "storage#buckets", items: buckets, next_page_token: token - ) + def find_bucket_gapi name = nil, soft_deleted= nil + Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(name: name, soft_deleted: soft_deleted).to_json end - def list_deleted_buckets_gapi count = 2, token = nil - buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_deleted_bucket_hash.to_json } + def list_buckets_gapi count = 2, token = nil, soft_deleted = nil + buckets = count.times.map { Google::Apis::StorageV1::Bucket.from_json random_bucket_hash(soft_deleted: soft_deleted).to_json } Google::Apis::StorageV1::Buckets.new( kind: "storage#buckets", items: buckets, next_page_token: token ) @@ -1097,4 +1089,4 @@ def restored_bucket_gapi name def object_retention_param enable_object_retention enable_object_retention ? Google::Apis::StorageV1::Bucket::ObjectRetention.new(mode: "Enabled") : nil end -end \ No newline at end of file +end diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index bfeb842e1692..f53a2aa48734 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -23,6 +23,7 @@ require "base64" require "uri" require "google/cloud/storage" +require "pry" ## # Monkey-Patch Google API Client to support Mocks @@ -70,59 +71,14 @@ def random_bucket_hash name: random_bucket_name, autoclass_terminal_storage_class: nil, enable_object_retention: nil, effective_time: DateTime.now, - retention_duration_seconds: 604800, # 7 days + retention_duration_seconds: 604_800, # 7 days + soft_deleted: nil, hierarchical_namespace: nil, generation: "1733393981548601746" versioning_config = { "enabled" => versioning } if versioning - { "kind" => "storage#bucket", - "id" => name, - "selfLink" => "#{url_root}/b/#{name}", - "projectNumber" => "1234567890", - "name" => name, - "timeCreated" => Time.now, - "generation" => generation, - "metageneration" => "1", - "owner" => { "entity" => "project-owners-1234567890" }, - "location" => location, - "locationType" => location_type, - "rpo" => rpo, - "cors" => cors, - "lifecycle" => lifecycle, - "logging" => logging_hash(logging_bucket, logging_prefix), - "storageClass" => storage_class, - "versioning" => versioning_config, - "website" => website_hash(website_main, website_404), - "billing" => billing_hash(requester_pays), - "etag" => "CAE=", - "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), - "enableObjectRetention" => enable_object_retention, - "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), - "hierarchicalNamespace" => hierarchical_namespace - }.delete_if { |_, v| v.nil? } - end - def random_deleted_bucket_hash name: random_bucket_name, - url_root: "https://www.googleapis.com/storage/v1", - location: "US", - storage_class: "STANDARD", - versioning: nil, - logging_bucket: nil, - logging_prefix: nil, - website_main: nil, - website_404: nil, - cors: [], - requester_pays: nil, - lifecycle: nil, - location_type: "multi-region", - rpo: "DEFAULT", - autoclass_enabled: nil, - autoclass_terminal_storage_class: nil, - enable_object_retention: nil, - effective_time: DateTime.now, - retention_duration_seconds: 604800, # 7 days - hierarchical_namespace: nil, - generation: "1733393981548601746" - versioning_config = { "enabled" => versioning } if versioning - { "kind" => "storage#bucket", + + data = { + "kind" => "storage#bucket", "id" => name, "selfLink" => "#{url_root}/b/#{name}", "projectNumber" => "1234567890", @@ -144,15 +100,18 @@ def random_deleted_bucket_hash name: random_bucket_name, "etag" => "CAE=", "autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class), "enableObjectRetention" => enable_object_retention, - "softDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time, - "hardDeleteTime" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds).effective_time - .to_time + retention_duration_seconds, "softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds), "hierarchicalNamespace" => hierarchical_namespace - }.delete_if { |_, v| v.nil? } + } + if soft_deleted + soft_delete_policy = soft_delete_policy_object retention_duration_seconds: retention_duration_seconds + data["softDeleteTime"] = soft_delete_policy.effective_time + data["hardDeleteTime"] = soft_delete_policy.effective_time.to_time + retention_duration_seconds + end + data.delete_if { |_, v| v.nil? } end - def soft_delete_policy_object retention_duration_seconds: 604800 # 7 days + def soft_delete_policy_object retention_duration_seconds: 604_800 # 7 days Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new( effective_time: DateTime.now, retention_duration_seconds: retention_duration_seconds From 2f101a65764cef3615b5f89bc4925de24aa4cd9c Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Sat, 21 Dec 2024 04:55:50 +0000 Subject: [PATCH 22/25] removing pry --- google-cloud-storage/test/helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index f53a2aa48734..60ed111f0897 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -23,7 +23,6 @@ require "base64" require "uri" require "google/cloud/storage" -require "pry" ## # Monkey-Patch Google API Client to support Mocks From e2265cfeccc2ab962e16d0e1e7dc083c4014fdd2 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Sat, 21 Dec 2024 05:34:00 +0000 Subject: [PATCH 23/25] fix lint --- google-cloud-storage/samples/acceptance/buckets_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index b40eb4de25e6..10f672ce1a7d 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -125,7 +125,6 @@ let(:generation) { bucket.generation } it "get soft deleted bucket, its soft_delete_time and hard_delete_time" do - delete_bucket_helper bucket.name _out, _err = capture_io do get_soft_deleted_bucket bucket_name: bucket.name, generation: generation From bb4831f20352e38526532e8d790756b80d05760d Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 23 Dec 2024 12:38:46 +0000 Subject: [PATCH 24/25] try cli issue fix --- .../lib/google/cloud/storage/project.rb | 4 ++-- .../samples/acceptance/buckets_test.rb | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/project.rb b/google-cloud-storage/lib/google/cloud/storage/project.rb index 3bb979a9ece6..1b340763aac9 100644 --- a/google-cloud-storage/lib/google/cloud/storage/project.rb +++ b/google-cloud-storage/lib/google/cloud/storage/project.rb @@ -584,8 +584,8 @@ def hmac_keys service_account_email: nil, project_id: nil, ## # Restores a soft deleted bucket with bucket name and generation. # - # @param [String] bucket_name Name of a bucket. - # @param [Fixnum] generation generation of a bucket. + # @param [String] bucket_name Name of the bucket. + # @param [Fixnum] generation generation of the bucket. # # @return [Google::Cloud::Storage::Bucket, nil] Returns nil if bucket # does not exist diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 10f672ce1a7d..90b226e7811d 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -122,15 +122,19 @@ end describe "storage_soft_deleted_bucket" do - let(:generation) { bucket.generation } + let(:new_bucket_name) {random_bucket_name} it "get soft deleted bucket, its soft_delete_time and hard_delete_time" do - delete_bucket_helper bucket.name + + new_bucket = storage_client.create_bucket new_bucket_name + new_generation = new_bucket.generation + + delete_bucket_helper new_bucket_name _out, _err = capture_io do - get_soft_deleted_bucket bucket_name: bucket.name, generation: generation + get_soft_deleted_bucket bucket_name: new_bucket_name, generation: new_generation end - assert "soft_delete_time - #{bucket.soft_delete_time}", "Bucket soft_delete_time should be present" - assert "hard_delete_time - #{bucket.hard_delete_time}", "Bucket hard_delete_time should be present" + assert "soft_delete_time ", "Bucket soft_delete_time should be present" + assert "hard_delete_time ", "Bucket hard_delete_time should be present" end it "lists soft deleted buckets" do From 44b6ac33fb23baea68fcc2e1de287aba127d9cc4 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 23 Dec 2024 12:42:08 +0000 Subject: [PATCH 25/25] fix lint --- google-cloud-storage/samples/acceptance/buckets_test.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 90b226e7811d..95dbebdcc8b2 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -122,12 +122,11 @@ end describe "storage_soft_deleted_bucket" do - let(:new_bucket_name) {random_bucket_name} + let(:new_bucket_name) { random_bucket_name } it "get soft deleted bucket, its soft_delete_time and hard_delete_time" do - new_bucket = storage_client.create_bucket new_bucket_name - new_generation = new_bucket.generation + new_generation = new_bucket.generation delete_bucket_helper new_bucket_name _out, _err = capture_io do