Skip to content
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
34 changes: 34 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import uuid

import pytest
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
from google.api_core import exceptions
from google.cloud import spanner
from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect
Expand Down Expand Up @@ -82,6 +83,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 +195,33 @@ 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):
spanner_client = spanner.Client()
operation = spanner_client.instance_admin_api.create_instance(
Copy link
Contributor

@harshachinta harshachinta Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow one of the 2 approaches,

  1. We can re-use existing instance that is already created in
    def test_create_instance_explicit(spanner_client, create_instance_id):
    .

This will prevent instance creation time when running tests and also reduce the no of instances that get created.
This test can be marked as dependent to the instance creation test like

@pytest.mark.dependency(name="create_instance_config")
.

  1. If above one is complicated, then can we directly use snippets.create_instance to create an instance instead of rewriting the create logic here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented Approach 2. The challenge with reusing an existing instance is that there are several restrictions around instance partitions. Notably:

  • An instance partition cannot have the same instance config as the parent instance
  • Several Spanner features do not work in instances where the user has created one or more instance partitions.

parent=spanner_client.project_name,
instance_id=instance_partition_instance_id,
instance=spanner_instance_admin.Instance(
config="{}/instanceConfigs/regional-us-central1".format(
spanner_client.project_name
),
display_name="Instance partitions test.",
node_count=1,
labels={
"cloud_spanner_samples": "true",
"created": str(int(time.time())),
},
),
)
operation.result(240)
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
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