From 1d64d5d45e29701a3d6d3b25536fe0e30e43ffc7 Mon Sep 17 00:00:00 2001 From: aayushimalik Date: Tue, 21 Mar 2023 11:09:51 +0000 Subject: [PATCH 1/3] feat: drop database protection sample and sample test --- samples/samples/snippets.py | 37 ++++++++++++++++++++++++++++++++ samples/samples/snippets_test.py | 9 ++++++++ 2 files changed, 46 insertions(+) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index a447121010..a3ffbbd160 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -196,6 +196,43 @@ def create_database(instance_id, database_id): # [END spanner_create_database] +# [START spanner_update_database_drop_protection] +def update_database_drop_protection(instance_id, database_id): + """Updates the drop protection setting for a database by first enabling and then disabling it.""" + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + db = instance.database(database_id) + db.drop_protection_enabled = True + + operation = db.update() + + print("Waiting for operation to complete...") + operation.result(OPERATION_TIMEOUT_SECONDS) + + print( + "Drop protection for database {} updated to {}".format( + db.name, db.drop_protection_enabled + ) + ) + + db.drop_protection_enabled = False + + operation = db.update() + + print("Waiting for operation to complete...") + operation.result(OPERATION_TIMEOUT_SECONDS) + + print( + "Drop protection for database {} updated to {}".format( + db.name, db.drop_protection_enabled + ) + ) + + +# [END spanner_update_database_drop_protection] + + # [START spanner_create_database_with_encryption_key] def create_database_with_encryption_key(instance_id, database_id, kms_key_name): """Creates a database with tables using a Customer Managed Encryption Key (CMEK).""" diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 6d5822e37b..e5b95f64c7 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -154,6 +154,15 @@ def test_create_instance_with_processing_units(capsys, lci_instance_id): retry_429(instance.delete)() +def test_update_database_drop_protection(capsys, instance_id, sample_database): + snippets.update_database_drop_protection( + instance_id, sample_database.database_id + ) + out, _ = capsys.readouterr() + assert "updated to True" in out + assert "updated to False" in out + + def test_create_database_with_encryption_config( capsys, instance_id, cmek_database_id, kms_key_name ): From ee653cd0d11a91e3efa0a3ab83e9a3092618b13d Mon Sep 17 00:00:00 2001 From: aayushimalik Date: Tue, 4 Apr 2023 08:42:33 +0000 Subject: [PATCH 2/3] Make sample changes follow client lib spec --- samples/samples/snippets.py | 30 +++++++----------------------- samples/samples/snippets_test.py | 12 ++++++++---- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index a3ffbbd160..ee202f502d 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -196,9 +196,9 @@ def create_database(instance_id, database_id): # [END spanner_create_database] -# [START spanner_update_database_drop_protection] -def update_database_drop_protection(instance_id, database_id): - """Updates the drop protection setting for a database by first enabling and then disabling it.""" +# [START spanner_update_database] +def update_database(instance_id, database_id): + """Updates the drop protection setting for a database.""" spanner_client = spanner.Client() instance = spanner_client.instance(instance_id) @@ -207,30 +207,14 @@ def update_database_drop_protection(instance_id, database_id): operation = db.update() - print("Waiting for operation to complete...") - operation.result(OPERATION_TIMEOUT_SECONDS) - - print( - "Drop protection for database {} updated to {}".format( - db.name, db.drop_protection_enabled - ) - ) - - db.drop_protection_enabled = False - - operation = db.update() - - print("Waiting for operation to complete...") + print("Waiting for update operation for {} to complete...".format( + db.database_id)) operation.result(OPERATION_TIMEOUT_SECONDS) - print( - "Drop protection for database {} updated to {}".format( - db.name, db.drop_protection_enabled - ) - ) + print("Updated database {}.".format(db.database_id)) -# [END spanner_update_database_drop_protection] +# [END spanner_update_database] # [START spanner_create_database_with_encryption_key] diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index e5b95f64c7..3bf23b6bff 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -154,13 +154,17 @@ def test_create_instance_with_processing_units(capsys, lci_instance_id): retry_429(instance.delete)() -def test_update_database_drop_protection(capsys, instance_id, sample_database): - snippets.update_database_drop_protection( +def test_update_database(capsys, instance_id, sample_database): + snippets.update_database( instance_id, sample_database.database_id ) out, _ = capsys.readouterr() - assert "updated to True" in out - assert "updated to False" in out + assert "Updated database {}.".format(sample_database.database_id) in out + + # Cleanup + sample_database.drop_protection_enabled = False + op = sample_database.update() + op.result() def test_create_database_with_encryption_config( From 545cd8c839d8b7d7d72636e5b3a7e7f4eaa6837b Mon Sep 17 00:00:00 2001 From: aayushimalik Date: Thu, 13 Apr 2023 05:38:46 +0000 Subject: [PATCH 3/3] Update field name `drop_protection_enabled` to `enable_drop_protection`. --- samples/samples/snippets.py | 8 ++++---- samples/samples/snippets_test.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index ee202f502d..57590551ad 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -203,15 +203,15 @@ def update_database(instance_id, database_id): instance = spanner_client.instance(instance_id) db = instance.database(database_id) - db.drop_protection_enabled = True + db.enable_drop_protection = True - operation = db.update() + operation = db.update(["enable_drop_protection"]) print("Waiting for update operation for {} to complete...".format( - db.database_id)) + db.name)) operation.result(OPERATION_TIMEOUT_SECONDS) - print("Updated database {}.".format(db.database_id)) + print("Updated database {}.".format(db.name)) # [END spanner_update_database] diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 3bf23b6bff..b8e1e093a1 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -159,11 +159,11 @@ def test_update_database(capsys, instance_id, sample_database): instance_id, sample_database.database_id ) out, _ = capsys.readouterr() - assert "Updated database {}.".format(sample_database.database_id) in out + assert "Updated database {}.".format(sample_database.name) in out # Cleanup - sample_database.drop_protection_enabled = False - op = sample_database.update() + sample_database.enable_drop_protection = False + op = sample_database.update(["enable_drop_protection"]) op.result()