Skip to content

Commit

Permalink
feat(spanner): add samples for instance partitions (#1168)
Browse files Browse the repository at this point in the history
* feat(spanner): add samples for instance partitions

* PR feedback

---------

Co-authored-by: Sri Harsha CH <[email protected]>
  • Loading branch information
varuncnaik and harshachinta authored Aug 6, 2024
1 parent 1a77146 commit 55f83dc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
18 changes: 18 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 55f83dc

Please sign in to comment.