def __init__( self, backup_id, instance, database="", expire_time=None, version_time=None, encryption_config=None, source_backup=None, ): self.backup_id = backup_id self._instance = instance self._database = database self._source_backup = source_backup self._expire_time = expire_time self._create_time = None self._version_time = version_time self._size_bytes = None self._state = None self._referencing_databases = None self._encryption_info = None self._max_expire_time = None self._referencing_backups = None if type(encryption_config) == dict: if source_backup: self._encryption_config = CopyBackupEncryptionConfig( **encryption_config) else: self._encryption_config = CreateBackupEncryptionConfig( **encryption_config) else: self._encryption_config = encryption_config
def test_backup_factory_explicit(self): import datetime from google.cloud._helpers import UTC from google.cloud.spanner_v1.backup import Backup from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig client = _Client(self.PROJECT) instance = self._make_one(self.INSTANCE_ID, client, self.CONFIG_NAME) BACKUP_ID = "backup-id" DATABASE_NAME = "database-name" timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) encryption_config = CreateBackupEncryptionConfig( encryption_type=CreateBackupEncryptionConfig.EncryptionType. CUSTOMER_MANAGED_ENCRYPTION, kms_key_name="kms_key_name", ) backup = instance.backup( BACKUP_ID, database=DATABASE_NAME, expire_time=timestamp, encryption_config=encryption_config, ) self.assertIsInstance(backup, Backup) self.assertEqual(backup.backup_id, BACKUP_ID) self.assertIs(backup._instance, instance) self.assertEqual(backup._database, DATABASE_NAME) self.assertIs(backup._expire_time, timestamp) self.assertEqual(backup._encryption_config, encryption_config)
def test_ctor_non_defaults(self): from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig instance = _Instance(self.INSTANCE_NAME) timestamp = self._make_timestamp() encryption_config = CreateBackupEncryptionConfig( encryption_type=CreateBackupEncryptionConfig.EncryptionType. CUSTOMER_MANAGED_ENCRYPTION, kms_key_name="key_name", ) backup = self._make_one( self.BACKUP_ID, instance, database=self.DATABASE_NAME, expire_time=timestamp, encryption_config=encryption_config, ) self.assertEqual(backup.backup_id, self.BACKUP_ID) self.assertIs(backup._instance, instance) self.assertEqual(backup._database, self.DATABASE_NAME) self.assertIsNotNone(backup._expire_time) self.assertIs(backup._expire_time, timestamp) self.assertEqual(backup.encryption_config, encryption_config)
def test_encryption_config_property(self): from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig instance = _Instance(self.INSTANCE_NAME) backup = self._make_one(self.BACKUP_ID, instance) expected = backup._encryption_config = CreateBackupEncryptionConfig( encryption_type=CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION, kms_key_name="kms_key_name", ) self.assertEqual(backup._encryption_config, expected)
def test_create_success(self): from google.cloud.spanner_admin_database_v1 import Backup from google.cloud.spanner_admin_database_v1 import CreateBackupRequest from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig from datetime import datetime from datetime import timedelta from pytz import UTC op_future = object() client = _Client() api = client.database_admin_api = self._make_database_admin_api() api.create_backup.return_value = op_future instance = _Instance(self.INSTANCE_NAME, client=client) version_timestamp = datetime.utcnow() - timedelta(minutes=5) version_timestamp = version_timestamp.replace(tzinfo=UTC) expire_timestamp = self._make_timestamp() encryption_config = {"encryption_type": 3, "kms_key_name": "key_name"} backup = self._make_one( self.BACKUP_ID, instance, database=self.DATABASE_NAME, expire_time=expire_timestamp, version_time=version_timestamp, encryption_config=encryption_config, ) backup_pb = Backup( database=self.DATABASE_NAME, expire_time=expire_timestamp, version_time=version_timestamp, ) future = backup.create() self.assertIs(future, op_future) expected_encryption_config = CreateBackupEncryptionConfig( **encryption_config) request = CreateBackupRequest( parent=self.INSTANCE_NAME, backup_id=self.BACKUP_ID, backup=backup_pb, encryption_config=expected_encryption_config, ) api.create_backup.assert_called_once_with( request=request, metadata=[("google-cloud-resource-prefix", backup.name)], )
def test_ctor_w_encryption_config_dict(self): from google.cloud.spanner_admin_database_v1 import CreateBackupEncryptionConfig instance = _Instance(self.INSTANCE_NAME) timestamp = self._make_timestamp() encryption_config = {"encryption_type": 3, "kms_key_name": "key_name"} backup = self._make_one( self.BACKUP_ID, instance, database=self.DATABASE_NAME, expire_time=timestamp, encryption_config=encryption_config, ) expected_encryption_config = CreateBackupEncryptionConfig(**encryption_config) self.assertEqual(backup.backup_id, self.BACKUP_ID) self.assertIs(backup._instance, instance) self.assertEqual(backup._database, self.DATABASE_NAME) self.assertIsNotNone(backup._expire_time) self.assertIs(backup._expire_time, timestamp) self.assertEqual(backup._encryption_config, expected_encryption_config)
def test_backup_workflow( shared_instance, shared_database, database_version_time, backups_to_delete, databases_to_delete, ): from google.cloud.spanner_admin_database_v1 import ( CreateBackupEncryptionConfig, EncryptionConfig, EncryptionInfo, RestoreDatabaseEncryptionConfig, ) backup_id = _helpers.unique_id("backup_id", separator="_") expire_time = datetime.datetime.now( datetime.timezone.utc) + datetime.timedelta(days=3) encryption_enum = CreateBackupEncryptionConfig.EncryptionType encryption_config = CreateBackupEncryptionConfig( encryption_type=encryption_enum.GOOGLE_DEFAULT_ENCRYPTION, ) # Create backup. backup = shared_instance.backup( backup_id, database=shared_database, expire_time=expire_time, version_time=database_version_time, encryption_config=encryption_config, ) operation = backup.create() backups_to_delete.append(backup) # Check metadata. metadata = operation.metadata assert backup.name == metadata.name assert shared_database.name == metadata.database operation.result() # blocks indefinitely # Check backup object. backup.reload() assert shared_database.name == backup._database assert expire_time == backup.expire_time assert backup.create_time is not None assert database_version_time == backup.version_time assert backup.size_bytes is not None assert backup.state is not None assert (EncryptionInfo.Type.GOOGLE_DEFAULT_ENCRYPTION == backup.encryption_info.encryption_type) # Update with valid argument. valid_expire_time = datetime.datetime.now( datetime.timezone.utc) + datetime.timedelta(days=7) backup.update_expire_time(valid_expire_time) assert valid_expire_time == backup.expire_time # Restore database to same instance. restored_id = _helpers.unique_id("restored_db", separator="_") encryption_config = RestoreDatabaseEncryptionConfig( encryption_type=RestoreDatabaseEncryptionConfig.EncryptionType. GOOGLE_DEFAULT_ENCRYPTION, ) database = shared_instance.database( restored_id, encryption_config=encryption_config, ) databases_to_delete.append(database) operation = database.restore(source=backup) restored_db = operation.result() # blocks indefinitely assert database_version_time == restored_db.restore_info.backup_info.version_time metadata = operation.metadata assert database_version_time == metadata.backup_info.version_time database.reload() expected_encryption_config = EncryptionConfig() assert expected_encryption_config == database.encryption_config database.drop() backup.delete() assert not backup.exists()