diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index 93c8de4148..8a3764e9a5 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -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 ), ) @@ -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): @@ -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__) @@ -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": diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 6657703fd1..6938aa1cd7 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -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 @@ -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)() @@ -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" )