示例#1
0
    def test_list_backups_defaults(self):
        from google.cloud.spanner_admin_database_v1 import Backup as BackupPB
        from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
        from google.cloud.spanner_admin_database_v1 import ListBackupsRequest
        from google.cloud.spanner_admin_database_v1 import ListBackupsResponse

        api = DatabaseAdminClient(credentials=mock.Mock())
        client = _Client(self.PROJECT)
        client.database_admin_api = api
        instance = self._make_one(self.INSTANCE_ID, client)

        backups_pb = ListBackupsResponse(backups=[
            BackupPB(name=instance.name + "/backups/op1"),
            BackupPB(name=instance.name + "/backups/op2"),
            BackupPB(name=instance.name + "/backups/op3"),
        ])

        lbo_api = api._transport._wrapped_methods[
            api._transport.list_backups] = mock.Mock(return_value=backups_pb)

        backups = instance.list_backups()

        for backup in backups:
            self.assertIsInstance(backup, BackupPB)

        expected_metadata = (
            ("google-cloud-resource-prefix", instance.name),
            ("x-goog-request-params", "parent={}".format(instance.name)),
        )
        lbo_api.assert_called_once_with(
            ListBackupsRequest(parent=self.INSTANCE_NAME),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )
示例#2
0
    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")
        api = self._instance._client.database_admin_api
        metadata = _metadata_with_prefix(self.name)
        backup = BackupPB(
            database=self._database,
            expire_time=self.expire_time,
        )

        future = api.create_backup(
            parent=self._instance.name,
            backup_id=self.backup_id,
            backup=backup,
            metadata=metadata,
        )
        return future
示例#3
0
    def update_expire_time(self, new_expire_time):
        """Update the expire time of this backup.

        :type new_expire_time: :class:`datetime.datetime`
        :param new_expire_time: the new expire time timestamp
        """
        api = self._instance._client.database_admin_api
        metadata = _metadata_with_prefix(self.name)
        backup_update = BackupPB(
            name=self.name,
            expire_time=new_expire_time,
        )
        update_mask = {"paths": ["expire_time"]}
        api.update_backup(backup=backup_update,
                          update_mask=update_mask,
                          metadata=metadata)
        self._expire_time = new_expire_time
示例#4
0
    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