Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
asthamohta committed Mar 25, 2022
1 parent cb1e0ca commit ac4e13b
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 119 deletions.
37 changes: 19 additions & 18 deletions google/cloud/spanner_v1/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,24 +557,25 @@ def copy_backup(
self, backup_id, source_backup, expire_time=None, encryption_config=None,
):
"""Factory to create a copy backup within this instance.
:type backup_id: str
:param backup_id: The ID of the backup copy.
:type source_backup: str
:param source_backup_id: The full path of the source backup to be copied.
:type expire_time: :class:`datetime.datetime`
:param expire_time:
Optional. The expire time that will be used when creating the copy backup.
Required if the create method needs to be called.
:type encryption_config:
:class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
or :class:`dict`
:param encryption_config:
(Optional) Encryption configuration for the backup.
If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
:rtype: :class:`~google.cloud.spanner_v1.backup.Backup`
:returns: a copy backup owned by this instance.
"""
:type backup_id: str
:param backup_id: The ID of the backup copy.
:type source_backup: str
:param source_backup_id: The full path of the source backup to be copied.
:type expire_time: :class:`datetime.datetime`
:param expire_time:
Optional. The expire time that will be used when creating the copy backup.
Required if the create method needs to be called.
:type encryption_config:
:class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
or :class:`dict`
:param encryption_config:
(Optional) Encryption configuration for the backup.
If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
:rtype: :class:`~google.cloud.spanner_v1.backup.Backup`
:returns: a copy backup owned by this instance.
"""
return Backup(
backup_id,
self,
Expand Down
7 changes: 2 additions & 5 deletions samples/samples/autocommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ def enable_autocommit_mode(instance_id, database_id):

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("instance_id", help="Your Cloud Spanner instance ID.")
parser.add_argument(
"--database-id",
help="Your Cloud Spanner database ID.",
default="example_db",
"--database-id", help="Your Cloud Spanner database ID.", default="example_db",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("enable_autocommit_mode", help=enable_autocommit_mode.__doc__)
Expand Down
2 changes: 1 addition & 1 deletion samples/samples/autocommit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def sample_name():
@RetryErrors(exception=Aborted, max_tries=2)
def test_enable_autocommit_mode(capsys, instance_id, sample_database):
# Delete table if it exists for retry attempts.
table = sample_database.table('Singers')
table = sample_database.table("Singers")
if table.exists():
op = sample_database.update_ddl(["DROP TABLE Singers"])
op.result()
Expand Down
69 changes: 49 additions & 20 deletions samples/samples/backup_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def create_backup(instance_id, database_id, backup_id, version_time):

# Create a backup
expire_time = datetime.utcnow() + timedelta(days=14)
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
backup = instance.backup(
backup_id, database=database, expire_time=expire_time, version_time=version_time
)
operation = backup.create()

# Wait for backup operation to complete.
Expand All @@ -56,7 +58,9 @@ def create_backup(instance_id, database_id, backup_id, version_time):
# [END spanner_create_backup]

# [START spanner_create_backup_with_encryption_key]
def create_backup_with_encryption_key(instance_id, database_id, backup_id, kms_key_name):
def create_backup_with_encryption_key(
instance_id, database_id, backup_id, kms_key_name
):
"""Creates a backup for a database using a Customer Managed Encryption Key (CMEK)."""
from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig

Expand All @@ -67,10 +71,15 @@ def create_backup_with_encryption_key(instance_id, database_id, backup_id, kms_k
# Create a backup
expire_time = datetime.utcnow() + timedelta(days=14)
encryption_config = {
'encryption_type': CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION,
'kms_key_name': kms_key_name,
"encryption_type": CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION,
"kms_key_name": kms_key_name,
}
backup = instance.backup(backup_id, database=database, expire_time=expire_time, encryption_config=encryption_config)
backup = instance.backup(
backup_id,
database=database,
expire_time=expire_time,
encryption_config=encryption_config,
)
operation = backup.create()

# Wait for backup operation to complete.
Expand Down Expand Up @@ -115,7 +124,7 @@ def restore_database(instance_id, new_database_id, backup_id):
restore_info.backup_info.source_database,
new_database_id,
restore_info.backup_info.backup,
restore_info.backup_info.version_time
restore_info.backup_info.version_time,
)
)

Expand All @@ -124,7 +133,9 @@ def restore_database(instance_id, new_database_id, backup_id):


# [START spanner_restore_backup_with_encryption_key]
def restore_database_with_encryption_key(instance_id, new_database_id, backup_id, kms_key_name):
def restore_database_with_encryption_key(
instance_id, new_database_id, backup_id, kms_key_name
):
"""Restores a database from a backup using a Customer Managed Encryption Key (CMEK)."""
from google.cloud.spanner_admin_database_v1 import RestoreDatabaseEncryptionConfig

Expand All @@ -134,10 +145,12 @@ def restore_database_with_encryption_key(instance_id, new_database_id, backup_id
# Start restoring an existing backup to a new database.
backup = instance.backup(backup_id)
encryption_config = {
'encryption_type': RestoreDatabaseEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION,
'kms_key_name': kms_key_name,
"encryption_type": RestoreDatabaseEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION,
"kms_key_name": kms_key_name,
}
new_database = instance.database(new_database_id, encryption_config=encryption_config)
new_database = instance.database(
new_database_id, encryption_config=encryption_config
)
operation = new_database.restore(backup)

# Wait for restore operation to complete.
Expand Down Expand Up @@ -210,7 +223,7 @@ def list_backup_operations(instance_id, database_id, backup_id):
metadata.name, metadata.database, metadata.progress.progress_percent
)
)

# List the CopyBackup operations.
filter_ = (
"(metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CopyBackupMetadata) "
Expand All @@ -221,7 +234,9 @@ def list_backup_operations(instance_id, database_id, backup_id):
metadata = op.metadata
print(
"Backup {} on source backup {}: {}% complete.".format(
metadata.name, metadata.source_backup, metadata.progress.progress_percent
metadata.name,
metadata.source_backup,
metadata.progress.progress_percent,
)
)

Expand Down Expand Up @@ -305,8 +320,11 @@ def list_backups(instance_id, database_id, backup_id):
print("All backups with pagination")
# If there are multiple pages, additional ``ListBackup``
# requests will be made as needed while iterating.
paged_backups = set()
for backup in instance.list_backups(page_size=2):
print(backup.name)
paged_backups.add(backup.name)
for backup in paged_backups:
print(backup)


# [END spanner_list_backups]
Expand Down Expand Up @@ -358,7 +376,9 @@ def update_backup(instance_id, backup_id):


# [START spanner_create_database_with_version_retention_period]
def create_database_with_version_retention_period(instance_id, database_id, retention_period):
def create_database_with_version_retention_period(
instance_id, database_id, retention_period
):
"""Creates a database with a version retention period."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
Expand All @@ -378,7 +398,7 @@ def create_database_with_version_retention_period(instance_id, database_id, rete
"ALTER DATABASE `{}`"
" SET OPTIONS (version_retention_period = '{}')".format(
database_id, retention_period
)
),
]
db = instance.database(database_id, ddl_statements)
operation = db.create()
Expand All @@ -387,12 +407,15 @@ def create_database_with_version_retention_period(instance_id, database_id, rete

db.reload()

print("Database {} created with version retention period {} and earliest version time {}".format(
db.database_id, db.version_retention_period, db.earliest_version_time
))
print(
"Database {} created with version retention period {} and earliest version time {}".format(
db.database_id, db.version_retention_period, db.earliest_version_time
)
)

db.drop()


# [END spanner_create_database_with_version_retention_period]


Expand All @@ -404,7 +427,9 @@ def copy_backup(instance_id, backup_id, source_backup_path):

# Create a backup object and wait for copy backup operation to complete.
expire_time = datetime.utcnow() + timedelta(days=14)
copy_backup = instance.copy_backup(backup_id=backup_id, source_backup=source_backup_path, expire_time=expire_time)
copy_backup = instance.copy_backup(
backup_id=backup_id, source_backup=source_backup_path, expire_time=expire_time
)
operation = copy_backup.create()

# Wait for copy backup operation to complete.
Expand All @@ -416,10 +441,14 @@ def copy_backup(instance_id, backup_id, source_backup_path):

print(
"Backup {} of size {} bytes was created at {} with version time {}".format(
copy_backup.name, copy_backup.size_bytes, copy_backup.create_time, copy_backup.version_time,
copy_backup.name,
copy_backup.size_bytes,
copy_backup.create_time,
copy_backup.version_time,
)
)


# [END spanner_copy_backup]


Expand Down
39 changes: 19 additions & 20 deletions samples/samples/backup_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,22 @@ def test_create_backup(capsys, instance_id, sample_database):
version_time = list(results)[0][0]

backup_sample.create_backup(
instance_id,
sample_database.database_id,
BACKUP_ID,
version_time,
instance_id, sample_database.database_id, BACKUP_ID, version_time,
)
out, _ = capsys.readouterr()
assert BACKUP_ID in out


@pytest.mark.dependency(name="copy_backup",depends=["create_backup"])
@pytest.mark.dependency(name="copy_backup", depends=["create_backup"])
def test_copy_backup(capsys, instance_id, spanner_client):
source_backp_path=spanner_client.project_name+'/instances/'+instance_id+'/backups/'+BACKUP_ID
backup_sample.copy_backup(
instance_id,
COPY_BACKUP_ID,
source_backp_path
source_backp_path = (
spanner_client.project_name
+ "/instances/"
+ instance_id
+ "/backups/"
+ BACKUP_ID
)
backup_sample.copy_backup(instance_id, COPY_BACKUP_ID, source_backp_path)
out, _ = capsys.readouterr()
assert COPY_BACKUP_ID in out

Expand All @@ -78,10 +77,7 @@ def test_create_backup_with_encryption_key(
capsys, instance_id, sample_database, kms_key_name,
):
backup_sample.create_backup_with_encryption_key(
instance_id,
sample_database.database_id,
CMEK_BACKUP_ID,
kms_key_name,
instance_id, sample_database.database_id, CMEK_BACKUP_ID, kms_key_name,
)
out, _ = capsys.readouterr()
assert CMEK_BACKUP_ID in out
Expand All @@ -104,7 +100,8 @@ def test_restore_database_with_encryption_key(
capsys, instance_id, sample_database, kms_key_name,
):
backup_sample.restore_database_with_encryption_key(
instance_id, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name)
instance_id, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name
)
out, _ = capsys.readouterr()
assert (sample_database.database_id + " restored to ") in out
assert (CMEK_RESTORE_DB_ID + " from backup ") in out
Expand All @@ -115,16 +112,19 @@ def test_restore_database_with_encryption_key(
@pytest.mark.dependency(depends=["create_backup", "copy_backup"])
def test_list_backup_operations(capsys, instance_id, sample_database):
backup_sample.list_backup_operations(
instance_id, sample_database.database_id, BACKUP_ID)
instance_id, sample_database.database_id, BACKUP_ID
)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
assert sample_database.database_id in out
assert COPY_BACKUP_ID in out
print(out)


@pytest.mark.dependency(depends=["create_backup"])
def test_list_backups(capsys, instance_id, sample_database, ):
@pytest.mark.dependency(name="list_backup", depends=["create_backup", "copy_backup"])
def test_list_backups(
capsys, instance_id, sample_database,
):
backup_sample.list_backups(
instance_id, sample_database.database_id, BACKUP_ID,
)
Expand All @@ -140,7 +140,7 @@ def test_update_backup(capsys, instance_id):
assert BACKUP_ID in out


@pytest.mark.dependency(depends=["create_backup","copy_backup"])
@pytest.mark.dependency(depends=["create_backup", "copy_backup", "list_backup"])
def test_delete_backup(capsys, instance_id):
backup_sample.delete_backup(instance_id, BACKUP_ID)
out, _ = capsys.readouterr()
Expand Down Expand Up @@ -173,4 +173,3 @@ def test_create_database_with_retention_period(capsys, sample_instance):
assert ("retention period " + RETENTION_PERIOD) in out
database = sample_instance.database(RETENTION_DATABASE_ID)
database.drop()

6 changes: 2 additions & 4 deletions samples/samples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def instance_config(spanner_client):

@pytest.fixture(scope="module")
def multi_region_instance_config(spanner_client):
return "{}/instanceConfigs/{}".format(
spanner_client.project_name, "nam3"
)
return "{}/instanceConfigs/{}".format(spanner_client.project_name, "nam3")


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -143,7 +141,7 @@ def multi_region_instance(
labels={
"cloud_spanner_samples": "true",
"sample_name": sample_name,
"created": str(int(time.time()))
"created": str(int(time.time())),
},
)
op = retry_429(multi_region_instance.create)()
Expand Down
8 changes: 3 additions & 5 deletions samples/samples/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ def _session_tests(

if os.path.exists("requirements-test.txt"):
if os.path.exists("constraints-test.txt"):
session.install(
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
)
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
else:
session.install("-r", "requirements-test.txt")
with open("requirements-test.txt") as rtfile:
Expand All @@ -223,9 +221,9 @@ def _session_tests(
post_install(session)

if "pytest-parallel" in packages:
concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"])
elif "pytest-xdist" in packages:
concurrent_args.extend(['-n', 'auto'])
concurrent_args.extend(["-n", "auto"])

session.run(
"pytest",
Expand Down
Loading

0 comments on commit ac4e13b

Please sign in to comment.