From 55f83dc5f776d436b30da6056a9cdcad3971ce39 Mon Sep 17 00:00:00 2001 From: Varun Naik Date: Tue, 6 Aug 2024 01:00:48 -0700 Subject: [PATCH] feat(spanner): add samples for instance partitions (#1168) * feat(spanner): add samples for instance partitions * PR feedback --------- Co-authored-by: Sri Harsha CH <57220027+harshachinta@users.noreply.github.com> --- samples/samples/snippets.py | 30 ++++++++++++++++++++++++++++++ samples/samples/snippets_test.py | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index e7c76685d3..93c8de4148 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -158,6 +158,36 @@ def list_instance_config(): # [END spanner_list_instance_configs] +# [START spanner_create_instance_partition] +def create_instance_partition(instance_id, instance_partition_id): + """Creates an instance partition.""" + from google.cloud.spanner_admin_instance_v1.types import \ + spanner_instance_admin + + spanner_client = spanner.Client() + instance_admin_api = spanner_client.instance_admin_api + + config_name = "{}/instanceConfigs/nam3".format(spanner_client.project_name) + + operation = spanner_client.instance_admin_api.create_instance_partition( + parent=instance_admin_api.instance_path(spanner_client.project, instance_id), + instance_partition_id=instance_partition_id, + instance_partition=spanner_instance_admin.InstancePartition( + config=config_name, + display_name="Test instance partition", + node_count=1, + ), + ) + + print("Waiting for operation to complete...") + operation.result(OPERATION_TIMEOUT_SECONDS) + + print("Created instance partition {}".format(instance_partition_id)) + + +# [END spanner_create_instance_partition] + + # [START spanner_list_databases] def list_databases(instance_id): """Lists databases and their leader options.""" diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 909305a65a..6657703fd1 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -82,6 +82,12 @@ def lci_instance_id(): return f"lci-instance-{uuid.uuid4().hex[:10]}" +@pytest.fixture(scope="module") +def instance_partition_instance_id(): + """Id for the instance that tests instance partitions.""" + return f"instance-partition-test-{uuid.uuid4().hex[:10]}" + + @pytest.fixture(scope="module") def database_id(): return f"test-db-{uuid.uuid4().hex[:10]}" @@ -188,6 +194,18 @@ def test_create_instance_with_autoscaling_config(capsys, lci_instance_id): retry_429(instance.delete)() +def test_create_instance_partition(capsys, instance_partition_instance_id): + snippets.create_instance(instance_partition_instance_id) + retry_429(snippets.create_instance_partition)( + instance_partition_instance_id, "my-instance-partition" + ) + out, _ = capsys.readouterr() + assert "Created instance partition my-instance-partition" in out + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_partition_instance_id) + retry_429(instance.delete)() + + def test_update_database(capsys, instance_id, sample_database): snippets.update_database(instance_id, sample_database.database_id) out, _ = capsys.readouterr()