Skip to content

Commit

Permalink
docs(samples): Add samples for Cloud Spanner Default Backup Schedules (
Browse files Browse the repository at this point in the history
…#1238)

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules
Fix field name in code samples.

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules
Fix field name in code samples.

---------

Co-authored-by: Sri Harsha CH <[email protected]>
  • Loading branch information
Sally-Ye and harshachinta authored Nov 18, 2024
1 parent 6869ed6 commit 054a186
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion samples/samples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-spanner==3.49.1
google-cloud-spanner==3.50.0
futures==3.4.0; python_version < "3"
51 changes: 51 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
default_backup_schedule_type=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,
default_backup_schedule_type=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"
Expand Down
19 changes: 19 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 054a186

Please sign in to comment.