def test_create_instance_not_found(self): from google.cloud.exceptions import NotFound from google.cloud.spanner_admin_database_v1 import Backup from google.cloud.spanner_admin_database_v1 import CreateBackupRequest client = _Client() api = client.database_admin_api = self._make_database_admin_api() api.create_backup.side_effect = NotFound("testing") instance = _Instance(self.INSTANCE_NAME, client=client) timestamp = self._make_timestamp() backup = self._make_one( self.BACKUP_ID, instance, database=self.DATABASE_NAME, expire_time=timestamp ) backup_pb = Backup( database=self.DATABASE_NAME, expire_time=timestamp, ) with self.assertRaises(NotFound): backup.create() request = CreateBackupRequest( parent=self.INSTANCE_NAME, backup_id=self.BACKUP_ID, backup=backup_pb, ) api.create_backup.assert_called_once_with( request=request, metadata=[("google-cloud-resource-prefix", backup.name)], )
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 create(self): """Create this backup within its instance. :rtype: :class:`~google.api_core.operation.Operation` :returns: a future used to poll the status of the create request :raises Conflict: if the backup already exists :raises NotFound: if the instance owning the backup does not exist :raises BadRequest: if the database or expire_time values are invalid or expire_time is not set """ if not self._expire_time: raise ValueError("expire_time not set") if not self._database: raise ValueError("database not set") if (self.encryption_config and self.encryption_config.kms_key_name and self.encryption_config.encryption_type != CreateBackupEncryptionConfig.EncryptionType. CUSTOMER_MANAGED_ENCRYPTION): raise ValueError( "kms_key_name only used with CUSTOMER_MANAGED_ENCRYPTION") api = self._instance._client.database_admin_api metadata = _metadata_with_prefix(self.name) backup = BackupPB( database=self._database, expire_time=self.expire_time, version_time=self.version_time, ) request = CreateBackupRequest( parent=self._instance.name, backup_id=self.backup_id, backup=backup, encryption_config=self._encryption_config, ) future = api.create_backup( request=request, metadata=metadata, ) return future