示例#1
0
    def list_backup_operations(self, filter_="", page_size=None):
        """List backup operations for the instance.

        :type filter_: str
        :param filter_:
            Optional. A string specifying a filter for which backup operations
            to list.

        :type page_size: int
        :param page_size:
            Optional. The maximum number of operations in each page of results
            from this request. Non-positive values are ignored. Defaults to a
            sensible value set by the API.

        :rtype: :class:`~google.api_core.page_iterator.Iterator`
        :returns:
            Iterator of :class:`~google.api_core.operation.Operation`
            resources within the current instance.
        """
        metadata = _metadata_with_prefix(self.name)
        request = ListBackupOperationsRequest(
            parent=self.name,
            filter=filter_,
            page_size=page_size,
        )
        page_iter = self._client.database_admin_api.list_backup_operations(
            request=request, metadata=metadata)
        return map(self._item_to_operation, page_iter)
    def test_list_backup_operations_w_options(self):
        from google.api_core.operation import Operation
        from google.cloud.spanner_admin_database_v1 import CreateBackupMetadata
        from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
        from google.cloud.spanner_admin_database_v1 import ListBackupOperationsRequest
        from google.cloud.spanner_admin_database_v1 import ListBackupOperationsResponse
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any

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

        create_backup_metadata = Any()
        create_backup_metadata.Pack(
            CreateBackupMetadata.pb(
                CreateBackupMetadata(name="backup", database="database")
            )
        )

        operations_pb = ListBackupOperationsResponse(
            operations=[
                operations_pb2.Operation(name="op1", metadata=create_backup_metadata)
            ]
        )

        ldo_api = api._transport._wrapped_methods[
            api._transport.list_backup_operations
        ] = mock.Mock(return_value=operations_pb)

        ops = instance.list_backup_operations(filter_="filter", page_size=10)

        expected_metadata = (
            ("google-cloud-resource-prefix", instance.name),
            ("x-goog-request-params", "parent={}".format(instance.name)),
        )
        ldo_api.assert_called_once_with(
            ListBackupOperationsRequest(
                parent=self.INSTANCE_NAME, filter="filter", page_size=10
            ),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )
        self.assertTrue(all([type(op) == Operation for op in ops]))