diff --git a/spanner/Gemfile.lock b/spanner/Gemfile.lock index bf34bf7d2..8125cd0b1 100644 --- a/spanner/Gemfile.lock +++ b/spanner/Gemfile.lock @@ -1,26 +1,31 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.8.6) - public_suffix (>= 2.0.2, < 6.0) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) base64 (0.2.0) - concurrent-ruby (1.2.3) + bigdecimal (3.1.8) + concurrent-ruby (1.3.4) declarative (0.0.20) diff-lcs (1.5.1) - faraday (2.9.0) - faraday-net_http (>= 2.0, < 3.2) - faraday-net_http (3.1.0) - net-http - faraday-retry (2.2.0) + faraday (2.12.2) + faraday-net_http (>= 2.0, < 3.5) + json + logger + faraday-net_http (3.4.0) + net-http (>= 0.5.0) + faraday-retry (2.2.1) faraday (~> 2.0) - gapic-common (0.21.1) + gapic-common (0.24.0) faraday (>= 1.9, < 3.a) faraday-retry (>= 1.0, < 3.a) - google-protobuf (~> 3.18) - googleapis-common-protos (>= 1.4.0, < 2.a) - googleapis-common-protos-types (>= 1.11.0, < 2.a) - googleauth (~> 1.9) - grpc (~> 1.59) + google-cloud-env (~> 2.2) + google-logging-utils (~> 0.1) + google-protobuf (>= 3.25, < 5.a) + googleapis-common-protos (~> 1.6) + googleapis-common-protos-types (~> 1.15) + googleauth (~> 1.12) + grpc (~> 1.66) google-apis-core (0.14.0) addressable (~> 2.5, >= 2.5.1) googleauth (~> 1.9) @@ -31,59 +36,67 @@ GEM rexml google-apis-iam_v1 (0.54.0) google-apis-core (>= 0.14.0, < 2.a) - google-cloud-core (1.6.1) + google-cloud-core (1.7.1) google-cloud-env (>= 1.0, < 3.a) google-cloud-errors (~> 1.0) - google-cloud-env (2.1.1) + google-cloud-env (2.2.1) faraday (>= 1.0, < 3.a) - google-cloud-errors (1.3.1) - google-cloud-spanner (2.21.0) + google-cloud-errors (1.4.0) + google-cloud-spanner (2.24.0) + bigdecimal (~> 3.0) concurrent-ruby (~> 1.0) google-cloud-core (~> 1.5) - google-cloud-spanner-admin-database-v1 (~> 0.1) - google-cloud-spanner-admin-instance-v1 (~> 0.1) - google-cloud-spanner-v1 (~> 0.2) - google-cloud-spanner-admin-database-v1 (0.17.1) + google-cloud-spanner-admin-database-v1 (>= 0.19.0, < 2.a) + google-cloud-spanner-admin-instance-v1 (>= 0.16.0, < 2.a) + google-cloud-spanner-v1 (>= 0.27.0, < 2.a) + google-cloud-spanner-admin-database-v1 (1.2.1) gapic-common (>= 0.21.1, < 2.a) google-cloud-errors (~> 1.0) grpc-google-iam-v1 (~> 1.1) - google-cloud-spanner-admin-instance-v1 (0.13.2) + google-cloud-spanner-admin-instance-v1 (1.3.0) gapic-common (>= 0.21.1, < 2.a) google-cloud-errors (~> 1.0) grpc-google-iam-v1 (~> 1.1) - google-cloud-spanner-v1 (0.24.0) + google-cloud-spanner-v1 (1.3.0) gapic-common (>= 0.21.1, < 2.a) google-cloud-errors (~> 1.0) - google-protobuf (3.25.3) - googleapis-common-protos (1.5.0) - google-protobuf (~> 3.18) + google-logging-utils (0.1.0) + google-protobuf (4.29.1) + bigdecimal + rake (>= 13) + googleapis-common-protos (1.6.0) + google-protobuf (>= 3.18, < 5.a) googleapis-common-protos-types (~> 1.7) grpc (~> 1.41) - googleapis-common-protos-types (1.13.0) - google-protobuf (~> 3.18) - googleauth (1.11.0) + googleapis-common-protos-types (1.16.0) + google-protobuf (>= 3.18, < 5.a) + googleauth (1.12.0) faraday (>= 1.0, < 3.a) - google-cloud-env (~> 2.1) + google-cloud-env (~> 2.2) + google-logging-utils (~> 0.1) jwt (>= 1.4, < 3.0) multi_json (~> 1.11) os (>= 0.9, < 2.0) signet (>= 0.16, < 2.a) - grpc (1.62.0) - google-protobuf (~> 3.25) + grpc (1.68.1) + google-protobuf (>= 3.25, < 5.0) googleapis-common-protos-types (~> 1.0) - grpc-google-iam-v1 (1.7.0) - google-protobuf (~> 3.18) + grpc-google-iam-v1 (1.8.0) + google-protobuf (>= 3.18, < 5.a) googleapis-common-protos (~> 1.4) grpc (~> 1.41) httpclient (2.8.3) - jwt (2.8.0) + json (2.9.0) + jwt (2.9.3) base64 + logger (1.6.2) mini_mime (1.1.5) multi_json (1.15.0) - net-http (0.4.1) + net-http (0.6.0) uri os (1.1.4) - public_suffix (5.0.4) + public_suffix (6.0.1) + rake (13.2.1) representable (3.2.0) declarative (< 0.1.0) trailblazer-option (>= 0.1.1, < 0.2.0) @@ -112,7 +125,7 @@ GEM multi_json (~> 1.10) trailblazer-option (0.1.2) uber (0.1.0) - uri (0.13.0) + uri (1.0.2) PLATFORMS ruby diff --git a/spanner/spanner_create_backup_schedule_config.rb b/spanner/spanner_create_backup_schedule_config.rb new file mode 100644 index 000000000..388a401d0 --- /dev/null +++ b/spanner/spanner_create_backup_schedule_config.rb @@ -0,0 +1,60 @@ +# 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 spanner_create_backup_schedule_config] +require "google/cloud/spanner/admin/database" +require "google/cloud/spanner/admin/database/v1" + +## +# This is a snippet for showcasing how to create a schedule for backups. +# +# @param project_id [String] The ID of the Google Cloud project. +# @param instance_id [String] The ID of the spanner instance. +# @param database_id [String] The ID of the database. +# @param backup_schedule_id [String] The ID of the backup schedule to be created. +# +def spanner_create_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id: + client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id + database_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}" + + # For creating schedule for incremental backup use: + # backup_spec = Google::Cloud::Spanner::Admin::Database::V1::IncrementalBackupSpec.new + backup_spec = Google::Cloud::Spanner::Admin::Database::V1::FullBackupSpec.new + retention_duration = Google::Protobuf::Duration.new seconds: 3600 * 24 + encryption_type = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig::EncryptionType::USE_DATABASE_ENCRYPTION + encryption_config = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig.new( + encryption_type: encryption_type + ) + cron_spec = Google::Cloud::Spanner::Admin::Database::V1::CrontabSpec.new text: "30 12 * * *" + backup_schedule_spec = Google::Cloud::Spanner::Admin::Database::V1::BackupScheduleSpec.new( + cron_spec: cron_spec + ) + + backup_schedule = Google::Cloud::Spanner::Admin::Database::V1::BackupSchedule.new( + full_backup_spec: backup_spec, + retention_duration: retention_duration, + spec: backup_schedule_spec, + encryption_config: encryption_config + ) + + request = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupScheduleRequest.new( + parent: database_name, + backup_schedule_id: backup_schedule_id, + backup_schedule: backup_schedule + ) + created_backup_schedule = client.create_backup_schedule request + puts "Created backup schedule for #{created_backup_schedule.name}" +end + +# [END spanner_create_backup_schedule_config] diff --git a/spanner/spanner_delete_backup_schedule_config.rb b/spanner/spanner_delete_backup_schedule_config.rb new file mode 100644 index 000000000..cb8dc0395 --- /dev/null +++ b/spanner/spanner_delete_backup_schedule_config.rb @@ -0,0 +1,37 @@ +# 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 spanner_delete_backup_schedule_config] +require "google/cloud/spanner/admin/database" +require "google/cloud/spanner/admin/database/v1" + +## +# This is a snippet for showcasing how to delete a schedule for creating backups. +# +# @param project_id [String] The ID of the Google Cloud project. +# @param instance_id [String] The ID of the spanner instance. +# @param database_id [String] The ID of the database. +# @param backup_schedule_id [String] The ID of the backup schedule to be created. +# +def spanner_delete_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id: + client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id + backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}" + + request = Google::Cloud::Spanner::Admin::Database::V1::DeleteBackupScheduleRequest.new name: backup_schedule_name + + client.delete_backup_schedule request + puts "Deleted backup schedule for #{backup_schedule_name}" +end + +# [END spanner_delete_backup_schedule_config] diff --git a/spanner/spanner_get_backup_schedule_config.rb b/spanner/spanner_get_backup_schedule_config.rb new file mode 100644 index 000000000..9ab8e4e97 --- /dev/null +++ b/spanner/spanner_get_backup_schedule_config.rb @@ -0,0 +1,37 @@ +# 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 spanner_get_backup_schedule_config] +require "google/cloud/spanner/admin/database" +require "google/cloud/spanner/admin/database/v1" + +## +# This is a snippet for showcasing how to get a backup creation schedule. +# +# @param project_id [String] The ID of the Google Cloud project. +# @param instance_id [String] The ID of the spanner instance. +# @param database_id [String] The ID of the database. +# @param backup_schedule_id [String] The ID of the backup schedule to be created. +# +def spanner_get_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id: + client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id + backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}" + + request = Google::Cloud::Spanner::Admin::Database::V1::GetBackupScheduleRequest.new name: backup_schedule_name + + backup_schedule = client.get_backup_schedule request + puts "Backup schedule: #{backup_schedule.name}" +end + +# [END spanner_get_backup_schedule_config] diff --git a/spanner/spanner_list_backup_schedules_config.rb b/spanner/spanner_list_backup_schedules_config.rb new file mode 100644 index 000000000..3db199c92 --- /dev/null +++ b/spanner/spanner_list_backup_schedules_config.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 spanner_list_backup_schedules_config] +require "google/cloud/spanner/admin/database" +require "google/cloud/spanner/admin/database/v1" + +## +# This is a snippet for showcasing how to get list of schedules for creating backups of a database. +# +# @param project_id [String] The ID of the Google Cloud project. +# @param instance_id [String] The ID of the spanner instance. +# @param database_id [String] The ID of the database. +# +def spanner_list_backup_schedules project_id:, instance_id:, database_id: + client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id + database_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}" + + request = Google::Cloud::Spanner::Admin::Database::V1::ListBackupSchedulesRequest.new parent: database_name + + backup_schedules_list = client.list_backup_schedules request + + puts "Backup schedules list for #{database_name}" + backup_schedules_list.each { |backup_schedule| puts backup_schedule.name } +end + +# [END spanner_list_backup_schedules_config] diff --git a/spanner/spanner_update_backup_schedule_config.rb b/spanner/spanner_update_backup_schedule_config.rb new file mode 100644 index 000000000..84e7d3525 --- /dev/null +++ b/spanner/spanner_update_backup_schedule_config.rb @@ -0,0 +1,53 @@ +# 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 spanner_update_backup_schedule_config] +require "google/cloud/spanner/admin/database" +require "google/cloud/spanner/admin/database/v1" + +## +# This is a snippet for showcasing how to create a schedule for backups. +# +# @param project_id [String] The ID of the Google Cloud project. +# @param instance_id [String] The ID of the spanner instance. +# @param database_id [String] The ID of the database. +# @param backup_schedule_id [String] The ID of the backup schedule to be created. +# +def spanner_update_backup_schedule project_id:, instance_id:, database_id:, backup_schedule_id: + client = Google::Cloud::Spanner::Admin::Database.database_admin project_id: project_id + backup_schedule_name = "projects/#{project_id}/instances/#{instance_id}/databases/#{database_id}/backupSchedules/#{backup_schedule_id}" + retention_duration = Google::Protobuf::Duration.new seconds: 3600 * 24 + encryption_type = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig::EncryptionType::GOOGLE_DEFAULT_ENCRYPTION + encryption_config = Google::Cloud::Spanner::Admin::Database::V1::CreateBackupEncryptionConfig.new encryption_type: encryption_type + cron_spec = Google::Cloud::Spanner::Admin::Database::V1::CrontabSpec.new text: "45 10 * * *" + backup_schedule_spec = Google::Cloud::Spanner::Admin::Database::V1::BackupScheduleSpec.new cron_spec: cron_spec + + backup_schedule = Google::Cloud::Spanner::Admin::Database::V1::BackupSchedule.new( + name: backup_schedule_name, + retention_duration: retention_duration, + spec: backup_schedule_spec, + encryption_config: encryption_config + ) + field_mask = Google::Protobuf::FieldMask.new paths: ["retention_duration", "spec.cron_spec.text", "encryption_config"] + + request = Google::Cloud::Spanner::Admin::Database::V1::UpdateBackupScheduleRequest.new( + backup_schedule: backup_schedule, + update_mask: field_mask + ) + + updated_backup_schedule = client.update_backup_schedule request + puts "Updated backup schedule for #{updated_backup_schedule.name}" +end + +# [END spanner_update_backup_schedule_config] diff --git a/spanner/spec/spanner_backup_schedule_spec.rb b/spanner/spec/spanner_backup_schedule_spec.rb new file mode 100644 index 000000000..22ca8f931 --- /dev/null +++ b/spanner/spec/spanner_backup_schedule_spec.rb @@ -0,0 +1,57 @@ +# 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_relative "./spec_helper" +require_relative "../spanner_create_backup_schedule_config" +require_relative "../spanner_delete_backup_schedule_config" +require_relative "../spanner_get_backup_schedule_config" +require_relative "../spanner_list_backup_schedules_config" +require_relative "../spanner_update_backup_schedule_config" + +describe "Spanner schedule for backups:" do + example "Create backup schedule" do + capture do + spanner_create_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id + end + expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}" + end + + example "Get backup schedule" do + capture do + spanner_get_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id + end + expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}" + end + + example "List backup schedules" do + capture do + spanner_list_backup_schedules project_id: @project_id, instance_id: @instance_id, database_id: @database_id + end + expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}" + end + + example "Update backup schedules" do + capture do + spanner_update_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id + end + expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}" + end + + example "Delete backup schedule" do + capture do + spanner_delete_backup_schedule project_id: @project_id, instance_id: @instance_id, database_id: @database_id, backup_schedule_id: @backup_schedule_id + end + expect(captured_output).to include "projects/#{@project_id}/instances/#{@instance_id}/databases/#{@database_id}/backupSchedules/#{@backup_schedule_id}" + end +end diff --git a/spanner/spec/spec_helper.rb b/spanner/spec/spec_helper.rb index ea5290ad7..ea326b5e7 100644 --- a/spanner/spec/spec_helper.rb +++ b/spanner/spec/spec_helper.rb @@ -30,6 +30,7 @@ @database_id = "test_db_#{seed}" @backup_id = "test_bu_#{seed}" @copied_backup_id = "test_cbu_#{seed}" + @backup_schedule_id = "test_schedule_#{seed}" @restored_database_id = "restored_db_#{seed}" @spanner = Google::Cloud::Spanner.new project: @project_id @instance = @spanner.instance @instance_id @@ -270,4 +271,4 @@ def with_retry retries: 5 end raise end -end +end \ No newline at end of file