From 4854c35d3f77947a42d07e0fe997341396f1eab0 Mon Sep 17 00:00:00 2001 From: Sally Ye Date: Wed, 13 Nov 2024 07:34:37 -0800 Subject: [PATCH 1/3] chore(samples): Add samples for Cloud Spanner Default Backup Schedules --- samples/samples/requirements.txt | 2 +- samples/samples/snippets.py | 51 ++++++++++++++++++++++++++++++++ samples/samples/snippets_test.py | 19 ++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/samples/samples/requirements.txt b/samples/samples/requirements.txt index 5a108d39ef..4009a0a00b 100644 --- a/samples/samples/requirements.txt +++ b/samples/samples/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-spanner==3.49.1 +google-cloud-spanner==3.50.0 futures==3.4.0; python_version < "3" diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index c958a66822..a36c78f59e 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -3222,6 +3222,57 @@ def create_instance_with_autoscaling_config(instance_id): # [END spanner_create_instance_with_autoscaling_config] +# [START spanner_create_instance_without_default_backup_schedule] +def create_instance_without_default_backup_schedules(instance_id): + spanner_client = spanner.Client() + config_name = "{}/instanceConfigs/regional-me-central2".format( + spanner_client.project_name + ) + + operation = spanner_client.instance_admin_api.create_instance( + parent=spanner_client.project_name, + instance_id=instance_id, + instance=spanner_instance_admin.Instance( + config=config_name, + display_name="This is a display name.", + node_count=1, + edition=spanner_instance_admin.Instance.DefaultBackupScheduleType.NONE, # Optional + ), + ) + + print("Waiting for operation to complete...") + operation.result(OPERATION_TIMEOUT_SECONDS) + + print("Created instance {} without default backup schedules".format(instance_id)) + + +# [END spanner_create_instance_without_default_backup_schedule] + + +# [START spanner_update_instance_default_backup_schedule_type] +def update_instance_default_backup_schedule_type(instance_id): + 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, + edition=spanner_instance_admin.Instance.DefaultBackupScheduleType.AUTOMATIC, # Optional + ), + field_mask=field_mask_pb2.FieldMask( + paths=["default_backup_schedule_type"] + ), + ) + + print("Waiting for operation to complete...") + operation.result(OPERATION_TIMEOUT_SECONDS) + + print("Updated instance {} to have default backup schedules".format(instance_id)) + +# [END spanner_update_instance_default_backup_schedule_type] + + def add_proto_type_columns(instance_id, database_id): # [START spanner_add_proto_type_columns] # instance_id = "your-spanner-instance" diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index ba3c0bbfe7..87fa7a43a2 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -197,6 +197,25 @@ def test_create_instance_with_autoscaling_config(capsys, lci_instance_id): retry_429(instance.delete)() +def test_create_and_update_instance_default_backup_schedule_type(capsys, lci_instance_id): + retry_429(snippets.create_instance_without_default_backup_schedules)( + lci_instance_id, + ) + create_out, _ = capsys.readouterr() + assert lci_instance_id in create_out + assert "without default backup schedules" in create_out + + retry_429(snippets.update_instance_default_backup_schedule_type)( + lci_instance_id, + ) + update_out, _ = capsys.readouterr() + assert lci_instance_id in update_out + assert "to have default backup schedules" in update_out + spanner_client = spanner.Client() + instance = spanner_client.instance(lci_instance_id) + retry_429(instance.delete)() + + def test_create_instance_partition(capsys, 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. From 1a875bc55db4a97beb28ee45382072f1a3cb5809 Mon Sep 17 00:00:00 2001 From: Sally Ye Date: Wed, 13 Nov 2024 07:34:37 -0800 Subject: [PATCH 2/3] chore(samples): Add samples for Cloud Spanner Default Backup Schedules Fix field name in code samples. --- samples/samples/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index a36c78f59e..65fb478810 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -3236,7 +3236,7 @@ def create_instance_without_default_backup_schedules(instance_id): config=config_name, display_name="This is a display name.", node_count=1, - edition=spanner_instance_admin.Instance.DefaultBackupScheduleType.NONE, # Optional + default_backup_schedule_type=spanner_instance_admin.Instance.DefaultBackupScheduleType.NONE, # Optional ), ) @@ -3258,7 +3258,7 @@ def update_instance_default_backup_schedule_type(instance_id): operation = spanner_client.instance_admin_api.update_instance( instance=spanner_instance_admin.Instance( name=name, - edition=spanner_instance_admin.Instance.DefaultBackupScheduleType.AUTOMATIC, # Optional + default_backup_schedule_type=spanner_instance_admin.Instance.DefaultBackupScheduleType.AUTOMATIC, # Optional ), field_mask=field_mask_pb2.FieldMask( paths=["default_backup_schedule_type"] From 5d9f3374a55834de7ba525c5773827d40d05713f Mon Sep 17 00:00:00 2001 From: Sally Ye Date: Wed, 13 Nov 2024 07:34:37 -0800 Subject: [PATCH 3/3] chore(samples): Add samples for Cloud Spanner Default Backup Schedules Fix field name in code samples. --- samples/samples/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index 65fb478810..6650ebe88d 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -3238,7 +3238,7 @@ def create_instance_without_default_backup_schedules(instance_id): node_count=1, default_backup_schedule_type=spanner_instance_admin.Instance.DefaultBackupScheduleType.NONE, # Optional ), - ) + ) print("Waiting for operation to complete...") operation.result(OPERATION_TIMEOUT_SECONDS)