Skip to content

chore(spanner): Add spanner_create_instance_partition sample #1550

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 2 commits 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
57 changes: 57 additions & 0 deletions spanner/spanner_create_instance_partition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START spanner_create_instance_partition]
require "google/cloud/spanner"
require "google/cloud/spanner/admin/instance"

def spanner_create_instance_partition project_id:, instance_id:, instance_partition_id:
# project_id = "The Google Cloud project ID"
# instance_id = "The Spanner instance ID"
# instance_partition_id = "The instance partition ID"

instance_admin_client = Google::Cloud::Spanner::Admin::Instance.instance_admin
instance_path = instance_admin_client.instance_path project: project_id, instance: instance_id
instance_config_path = instance_admin_client.instance_config_path \
project: project_id, instance_config: "nam3"
instance_partition_path = instance_admin_client.instance_partition_path \
project: project_id, instance: instance_id, instance_partition: instance_partition_id

instance_partition = {
config: instance_config_path,
display_name: "test_create_instance_partition",
node_count: 1
}
request = {
parent: instance_path,
instance_partition_id: instance_partition_id,
instance_partition: instance_partition
}
job = instance_admin_client.create_instance_partition request

puts "Waiting for create instance partition to complete"

job.wait_until_done!

if job.error?
puts job.error
else
puts "Created instance partition #{instance_partition_id}"
end
end
# [END spanner_create_instance_partition]

if $PROGRAM_NAME == __FILE__
spanner_create_instance_partition project_id: ARGV.shift, instance_id: ARGV.shift, instance_partition_id: ARGV.shift
end
30 changes: 30 additions & 0 deletions spanner/spanner_delete_instance_partition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START spanner_delete_instance_partition]
require "google/cloud/spanner"
require "google/cloud/spanner/admin/instance"

def spanner_delete_instance_partition instance_partition_id:
# instance_partition_id = "The instance partition's full name, e.g projects/{project}/instances/{instance}/instancePartitions/custom_nam11"

instance_admin_client = Google::Cloud::Spanner::Admin::Instance.instance_admin
instance_admin_client.delete_instance_partition name: instance_partition_id
puts "Deleted instance partition #{instance_partition_id}"
end
# [END spanner_delete_instance_partition]

if $PROGRAM_NAME == __FILE__
spanner_delete_instance_partition instance_partition_id: ARGV.shift
end
42 changes: 42 additions & 0 deletions spanner/spec/spanner_create_instance_partition_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../spanner_create_instance_partition"
require_relative "../spanner_delete_instance_partition"
require_relative "./spec_helper"

describe "Spanner custom instance partition" do
after :each do
@created_instance_partitions_ids.each do |instance_partition_id|
name = instance_partition_path @instance_id, instance_partition_id
spanner_delete_instance_partition instance_partition_id: name
end
@created_instance_partitions_ids.clear
end

example "Create" do

instance_partition_id = "custom-ruby-samples-instance-partition-#{SecureRandom.hex(8)}"
@created_instance_partitions_ids << instance_partition_id

capture do
spanner_create_instance_partition project_id: @project_id,
instance_id: @instance_id,
instance_partition_id: instance_partition_id
end
expect(captured_output).to include(
"Created instance partition #{instance_partition_id}"
)
end
end
35 changes: 35 additions & 0 deletions spanner/spec/spanner_delete_instance_partition_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../spanner_create_instance_partition"
require_relative "../spanner_delete_instance_partition"
require_relative "./spec_helper"

describe "Spanner custom instance partition" do
example "Delete" do
instance_partition_id = "custom-ruby-samples-instance-partition-#{SecureRandom.hex(8)}"
spanner_create_instance_partition project_id: @project_id,
instance_id: @instance_id,
instance_partition_id: instance_partition_id

name = instance_partition_path @instance_id, instance_partition_id

capture do
spanner_delete_instance_partition instance_partition_id: name
end
expect(captured_output).to include(
"Deleted instance partition #{name}"
)
end
end
8 changes: 8 additions & 0 deletions spanner/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@mr_instance = @spanner.instance @mr_instance_id
@created_instance_ids = []
@created_instance_config_ids = []
@created_instance_partitions_ids = []
# A list of KMS key names to be used with CMEK
@kms_key_names = [
"projects/#{@project_id}/locations/us-central1/keyRings/spanner-test-keyring/cryptoKeys/spanner-test-cmek",
Expand Down Expand Up @@ -114,6 +115,13 @@ def instance_config_path instance_config_id
project: @project_id, instance_config: instance_config_id
end

def instance_partition_path instance_id, instance_partition_id
instance_admin_client.instance_partition_path \
project: @project_id,
instance: instance_id,
instance_partition: instance_partition_id
end

def instance_path instance_id
instance_admin_client.instance_path \
project: @project_id, instance: instance_id
Expand Down