diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 7afc71d14415..f52369be664e 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" @@ -120,6 +122,31 @@ end end + describe "storage_soft_deleted_bucket" do + let(:new_bucket_name) { random_bucket_name } + let(:new_bucket) { create_bucket_helper new_bucket_name } + let(:new_generation) { new_bucket.generation } + before do + delete_bucket_helper new_bucket.name + end + + it "get soft deleted bucket, its soft_delete_time and hard_delete_time" do + # fetching a soft deleted bucket + output, _err = capture_io do + get_soft_deleted_bucket bucket_name: new_bucket_name, generation: new_generation + end + assert_includes output, "soft_delete_time for #{new_bucket_name} is" + end + + it "lists soft deleted buckets" do + # fetching list of soft deleted buckets + list_deleted_bucket, _err = capture_io do + list_soft_deleted_buckets + end + assert_includes list_deleted_bucket, new_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/acceptance/helper.rb b/google-cloud-storage/samples/acceptance/helper.rb index 61a8f288c315..ae423b07315c 100644 --- a/google-cloud-storage/samples/acceptance/helper.rb +++ b/google-cloud-storage/samples/acceptance/helper.rb @@ -25,6 +25,20 @@ require "ostruct" +def grant_storage_permissions bucket_name: + storage = Google::Cloud::Storage.new + bucket = storage.bucket bucket_name + + storage_admin = "roles/storage.admin" + member = "serviceAccount:#{storage.service_account_email}" + + bucket.policy do |p| + p.add storage_admin, + member + end + bucket +end + def fixture_bucket storage_client = Google::Cloud::Storage.new storage_client.bucket($fixture_bucket_name) || @@ -36,6 +50,7 @@ def create_bucket_helper bucket_name retry_resource_exhaustion do storage_client.create_bucket bucket_name end + grant_storage_permissions bucket_name: bucket_name end def delete_bucket_helper bucket_name @@ -43,7 +58,6 @@ def delete_bucket_helper bucket_name retry_resource_exhaustion do bucket = storage_client.bucket bucket_name return unless bucket - bucket.files.each(&:delete) bucket.delete end diff --git a/google-cloud-storage/samples/acceptance/project_test.rb b/google-cloud-storage/samples/acceptance/project_test.rb index 2de54569a0ba..6152c06bd71d 100644 --- a/google-cloud-storage/samples/acceptance/project_test.rb +++ b/google-cloud-storage/samples/acceptance/project_test.rb @@ -14,9 +14,11 @@ require_relative "helper" require_relative "../storage_get_service_account" +require_relative "../storage_restore_bucket" describe "Storage Quickstart" do let(:project) { Google::Cloud::Storage.new } + let(:bucket) { fixture_bucket } it "get_service_account" do email = nil @@ -28,3 +30,22 @@ assert_includes out, "@gs-project-accounts.iam.gserviceaccount.com" end end + + +describe "storage_soft_deleted_bucket" do + let(:new_bucket_name) { random_bucket_name } + let(:new_bucket) { create_bucket_helper new_bucket_name } + let(:generation) { new_bucket.generation } + + before do + delete_bucket_helper new_bucket.name + end + + it "restores a soft deleted bucket" do + # restoring deleted bucket + _out, _err = capture_io do + restore_bucket bucket_name: new_bucket.name, generation: generation + end + assert "#{new_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 new file mode 100644 index 000000000000..4e4aba44476c --- /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" + # The generation no of your GCS bucket + # generation = "1234567896987" + + require "google/cloud/storage" + + storage = Google::Cloud::Storage.new + # fetching soft deleted bucket with soft_delete_time and hard_delete_time + puts "project_name while fetching the deleted bucket" + puts storage.project + 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 + puts "soft_delete_time for #{deleted_bucket_fetch.name} is - #{soft_delete_time}" + puts "hard_delete_time for #{deleted_bucket_fetch.name} is - #{hard_delete_time}" + end +end +# [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_list_soft_deleted_buckets.rb b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb new file mode 100644 index 000000000000..4869fb70d7fc --- /dev/null +++ b/google-cloud-storage/samples/storage_list_soft_deleted_buckets.rb @@ -0,0 +1,30 @@ +# 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 + require "google/cloud/storage" + + 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__ 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..c2cee7944174 --- /dev/null +++ b/google-cloud-storage/samples/storage_restore_bucket.rb @@ -0,0 +1,36 @@ +# 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_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" + + storage = Google::Cloud::Storage.new + + bucket_restored = storage.restore_bucket bucket_name, generation + + if bucket_restored.name == bucket_name + puts "#{bucket_name} Bucket restored" + else + puts "#{bucket_name} Bucket not restored" + end +end +# [END storage_restore_bucket] + +restore_bucket bucket_name: ARGV.shift, generation: ARGV.shift if $PROGRAM_NAME == __FILE__