Exemplo n.º 1
0
    def list_database_operations(self, filter_="", page_size=None):
        """List database operations for the instance.

        :type filter_: str
        :param filter_:
            Optional. A string specifying a filter for which database 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 = ListDatabaseOperationsRequest(
            parent=self.name,
            filter=filter_,
            page_size=page_size,
        )
        page_iter = self._client.database_admin_api.list_database_operations(
            request=request, metadata=metadata)
        return map(self._item_to_operation, page_iter)
Exemplo n.º 2
0
    def test_list_database_operations_defaults(self):
        from google.api_core.operation import Operation
        from google.cloud.spanner_admin_database_v1 import CreateDatabaseMetadata
        from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
        from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest
        from google.cloud.spanner_admin_database_v1 import (
            ListDatabaseOperationsResponse,
        )
        from google.cloud.spanner_admin_database_v1 import (
            OptimizeRestoredDatabaseMetadata,
        )
        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_database_metadata = Any()
        create_database_metadata.Pack(
            CreateDatabaseMetadata.pb(CreateDatabaseMetadata(database="database"))
        )

        optimize_database_metadata = Any()
        optimize_database_metadata.Pack(
            OptimizeRestoredDatabaseMetadata.pb(
                OptimizeRestoredDatabaseMetadata(name="database")
            )
        )

        databases_pb = ListDatabaseOperationsResponse(
            operations=[
                operations_pb2.Operation(name="op1", metadata=create_database_metadata),
                operations_pb2.Operation(
                    name="op2", metadata=optimize_database_metadata
                ),
            ]
        )

        ldo_api = api._transport._wrapped_methods[
            api._transport.list_database_operations
        ] = mock.Mock(return_value=databases_pb)

        ops = instance.list_database_operations()

        expected_metadata = (
            ("google-cloud-resource-prefix", instance.name),
            ("x-goog-request-params", "parent={}".format(instance.name)),
        )
        ldo_api.assert_called_once_with(
            ListDatabaseOperationsRequest(parent=self.INSTANCE_NAME),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )
        self.assertTrue(all([type(op) == Operation for op in ops]))
Exemplo n.º 3
0
    def test_list_database_operations_w_options(self):
        from google.api_core.operation import Operation
        from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
        from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest
        from google.cloud.spanner_admin_database_v1 import (
            ListDatabaseOperationsResponse, )
        from google.cloud.spanner_admin_database_v1 import RestoreDatabaseMetadata
        from google.cloud.spanner_admin_database_v1 import RestoreSourceType
        from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlMetadata
        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)

        restore_database_metadata = Any()
        restore_database_metadata.Pack(
            RestoreDatabaseMetadata.pb(
                RestoreDatabaseMetadata(name="database",
                                        source_type=RestoreSourceType.BACKUP)))

        update_database_metadata = Any()
        update_database_metadata.Pack(
            UpdateDatabaseDdlMetadata.pb(
                UpdateDatabaseDdlMetadata(database="database",
                                          statements=["statements"])))

        databases_pb = ListDatabaseOperationsResponse(operations=[
            operations_pb2.Operation(name="op1",
                                     metadata=restore_database_metadata),
            operations_pb2.Operation(name="op2",
                                     metadata=update_database_metadata),
        ])

        ldo_api = api._transport._wrapped_methods[
            api._transport.list_database_operations] = mock.Mock(
                return_value=databases_pb)

        ops = instance.list_database_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(
            ListDatabaseOperationsRequest(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]))