Skip to content

Commit eaae33f

Browse files
authored
chore(spanner): Add spanner_create_instance_partition sample (#1550)
* chore(spanner): Add spanner_create_instance_partition sample * Update docstring to YARD doc format
1 parent 9ceaad1 commit eaae33f

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_create_instance_partition]
16+
#
17+
# Creates an instance partition.
18+
#
19+
# @param project_id [String] The ID of the Google Cloud project.
20+
# @param instance_id [String] The ID of the Spanner instance.
21+
# @param instance_partition_id [String] The ID of the Spanner instance partition.
22+
def spanner_create_instance_partition project_id:, instance_id:, instance_partition_id:
23+
require "google/cloud/spanner/admin/instance"
24+
25+
instance_admin_client = Google::Cloud::Spanner::Admin::Instance.instance_admin
26+
27+
instance_path = instance_admin_client.instance_path \
28+
project: project_id,
29+
instance: instance_id
30+
instance_partition = {
31+
display_name: "Test Instance Partition",
32+
config: instance_admin_client.instance_config_path(project: project_id, instance_config: "nam3"),
33+
node_count: 1
34+
}
35+
36+
job = instance_admin_client.create_instance_partition \
37+
parent: instance_path,
38+
instance_partition_id: instance_partition_id,
39+
instance_partition: instance_partition
40+
41+
puts "Waiting for create instance partition operation to complete"
42+
job.wait_until_done!
43+
44+
if job.error?
45+
puts job.error
46+
else
47+
puts "Created instance partition #{instance_partition_id} on instance #{instance_id}"
48+
end
49+
end
50+
# [END spanner_create_instance_partition]
51+
52+
if $PROGRAM_NAME == __FILE__
53+
spanner_create_instance_partition project_id: ARGV.shift,
54+
instance_id: ARGV.shift,
55+
instance_partition_id: ARGV.shift
56+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_delete_instance_partition]
16+
def spanner_delete_instance_partition project_id:, instance_id:, instance_partition_id:
17+
# project_id = "Your Google Cloud project ID"
18+
# instance_id = "Your Spanner instance ID"
19+
# instance_partition_id = "Your Spanner instance partition ID"
20+
21+
require "google/cloud/spanner/admin/instance"
22+
23+
instance_admin_client = Google::Cloud::Spanner::Admin::Instance.instance_admin
24+
25+
instance_partition_path = instance_admin_client.instance_partition_path \
26+
project: project_id,
27+
instance: instance_id,
28+
instance_partition: instance_partition_id
29+
30+
instance_admin_client.delete_instance_partition name: instance_partition_path
31+
32+
puts "Deleted instance partition #{instance_partition_id}"
33+
end
34+
# [END spanner_delete_instance_partition]
35+
36+
if $PROGRAM_NAME == __FILE__
37+
spanner_delete_instance_partition project_id: ARGV.shift,
38+
instance_id: ARGV.shift,
39+
instance_partition_id: ARGV.shift
40+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require_relative "../spanner_create_instance_partition"
16+
require_relative "../spanner_delete_instance_partition"
17+
require_relative "./spec_helper"
18+
19+
describe "Spanner instance partition" do
20+
before :each do
21+
@instance_partition_ids = []
22+
end
23+
24+
after :each do
25+
@instance_partition_ids.each do |instance_partition_id|
26+
spanner_delete_instance_partition project_id: @project_id,
27+
instance_id: @instance_id,
28+
instance_partition_id: instance_partition_id
29+
end
30+
end
31+
32+
example "Create" do
33+
instance_partition_id = "ruby-test-partition-#{SecureRandom.hex(8)}"
34+
@instance_partition_ids << instance_partition_id
35+
capture do
36+
spanner_create_instance_partition project_id: @project_id,
37+
instance_id: @instance_id,
38+
instance_partition_id: instance_partition_id
39+
end
40+
expect(captured_output).to include \
41+
"Created instance partition #{instance_partition_id} on instance #{@instance_id}"
42+
end
43+
end

0 commit comments

Comments
 (0)