Skip to content

Commit

Permalink
chore(samples): add sample for spanner edition (#1196)
Browse files Browse the repository at this point in the history
* chore(samples): add sample for spanner edition

* refactor

* refactor editions samples

* refactor test

---------

Co-authored-by: Sri Harsha CH <[email protected]>
  • Loading branch information
alkatrivedi and harshachinta committed Sep 17, 2024
1 parent 03b3e86 commit 2415049
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
33 changes: 33 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def create_instance(instance_id):
"sample_name": "snippets-create_instance-explicit",
"created": str(int(time.time())),
},
edition=spanner_instance_admin.Instance.Edition.STANDARD, # Optional
),
)

Expand All @@ -73,6 +74,35 @@ def create_instance(instance_id):

# [END spanner_create_instance]

# [START spanner_update_instance]
def update_instance(instance_id):
"""Updates an instance."""
from google.cloud.spanner_admin_instance_v1.types import \
spanner_instance_admin

spanner_client = spanner.Client()

name = "{}/instances/{}".format(spanner_client.project_name, instance_id)

operation = spanner_client.instance_admin_api.update_instance(
instance=spanner_instance_admin.Instance(
name=name,
labels={
"sample_name": "snippets-update_instance-explicit",
},
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, # Optional
),
field_mask=field_mask_pb2.FieldMask(paths=["labels", "edition"]),
)

print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)

print("Updated instance {}".format(instance_id))


# [END spanner_update_instance]


# [START spanner_create_instance_with_processing_units]
def create_instance_with_processing_units(instance_id, processing_units):
Expand Down Expand Up @@ -3421,6 +3451,7 @@ def query_data_with_proto_types_parameter(instance_id, database_id):

subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("create_instance", help=create_instance.__doc__)
subparsers.add_parser("update_instance", help=update_instance.__doc__)
subparsers.add_parser("create_database", help=create_database.__doc__)
subparsers.add_parser("insert_data", help=insert_data.__doc__)
subparsers.add_parser("batch_write", help=batch_write.__doc__)
Expand Down Expand Up @@ -3571,6 +3602,8 @@ def query_data_with_proto_types_parameter(instance_id, database_id):

if args.command == "create_instance":
create_instance(args.instance_id)
if args.command == "update_instance":
update_instance(args.instance_id)
elif args.command == "create_database":
create_database(args.instance_id, args.database_id)
elif args.command == "insert_data":
Expand Down
11 changes: 8 additions & 3 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import time
import uuid

import pytest
from google.api_core import exceptions
from google.cloud import spanner
from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect
import pytest
from test_utils.retry import RetryErrors

import snippets
Expand Down Expand Up @@ -152,10 +152,13 @@ def base_instance_config_id(spanner_client):
return "{}/instanceConfigs/{}".format(spanner_client.project_name, "nam7")


def test_create_instance_explicit(spanner_client, create_instance_id):
def test_create_and_update_instance_explicit(spanner_client, create_instance_id):
# Rather than re-use 'sample_isntance', we create a new instance, to
# ensure that the 'create_instance' snippet is tested.
retry_429(snippets.create_instance)(create_instance_id)
# Rather than re-use 'sample_isntance', we are using created instance, to
# ensure that the 'update_instance' snippet is tested.
retry_429(snippets.update_instance)(create_instance_id)
instance = spanner_client.instance(create_instance_id)
retry_429(instance.delete)()

Expand Down Expand Up @@ -195,7 +198,9 @@ def test_create_instance_with_autoscaling_config(capsys, lci_instance_id):


def test_create_instance_partition(capsys, instance_partition_instance_id):
snippets.create_instance(instance_partition_instance_id)
# Unable to use create_instance since it has editions set where partitions are unsupported.
# The minimal requirement for editions is ENTERPRISE_PLUS for the paritions to get supported.
snippets.create_instance_with_processing_units(instance_partition_instance_id, 1000)
retry_429(snippets.create_instance_partition)(
instance_partition_instance_id, "my-instance-partition"
)
Expand Down

0 comments on commit 2415049

Please sign in to comment.