diff --git a/spanner/spanner_create_instance_partition.rb b/spanner/spanner_create_instance_partition.rb new file mode 100644 index 000000000..3f1742e7a --- /dev/null +++ b/spanner/spanner_create_instance_partition.rb @@ -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 diff --git a/spanner/spanner_delete_instance_partition.rb b/spanner/spanner_delete_instance_partition.rb new file mode 100644 index 000000000..7e7597972 --- /dev/null +++ b/spanner/spanner_delete_instance_partition.rb @@ -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 diff --git a/spanner/spec/spanner_create_instance_partition_spec.rb b/spanner/spec/spanner_create_instance_partition_spec.rb new file mode 100644 index 000000000..0ff511e32 --- /dev/null +++ b/spanner/spec/spanner_create_instance_partition_spec.rb @@ -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 diff --git a/spanner/spec/spanner_delete_instance_partition_spec.rb b/spanner/spec/spanner_delete_instance_partition_spec.rb new file mode 100644 index 000000000..fbd01a255 --- /dev/null +++ b/spanner/spec/spanner_delete_instance_partition_spec.rb @@ -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 diff --git a/spanner/spec/spec_helper.rb b/spanner/spec/spec_helper.rb index ea5290ad7..6aca94f30 100644 --- a/spanner/spec/spec_helper.rb +++ b/spanner/spec/spec_helper.rb @@ -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", @@ -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