Skip to content

Add samples for Spanner Backup Schedule Apis #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 52 additions & 39 deletions spanner/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions spanner/spanner_create_backup_schedule_config.rb
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions spanner/spanner_delete_backup_schedule_config.rb
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions spanner/spanner_get_backup_schedule_config.rb
Original file line number Diff line number Diff line change
@@ -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]
38 changes: 38 additions & 0 deletions spanner/spanner_list_backup_schedules_config.rb
Original file line number Diff line number Diff line change
@@ -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]
53 changes: 53 additions & 0 deletions spanner/spanner_update_backup_schedule_config.rb
Original file line number Diff line number Diff line change
@@ -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]
Loading