Example #1
0
    def test_export_documents(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        done = True
        expected_response = {"name": name_2, "done": done}
        expected_response = operations_pb2.Operation(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_admin_v1.FirestoreAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[DATABASE]")

        response = client.export_documents(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_admin_pb2.ExportDocumentsRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_export_documents(self):
        # Setup Expected Response
        output_uri_prefix = "outputUriPrefix124746435"
        expected_response = {"output_uri_prefix": output_uri_prefix}
        expected_response = operation_pb2.ExportDocumentsResponse(
            **expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_export_documents", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_admin_v1.FirestoreAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[DATABASE]")

        response = client.export_documents(name)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = firestore_admin_pb2.ExportDocumentsRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def export_documents(
        self,
        name,
        collection_ids=None,
        output_uri_prefix=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Exports a copy of all or a subset of documents from Google Cloud Firestore
        to another storage system, such as Google Cloud Storage. Recent updates to
        documents may not be reflected in the export. The export occurs in the
        background and its progress can be monitored and managed via the
        Operation resource that is created. The output of an export may only be
        used once the associated operation is done. If an export operation is
        cancelled before completion it may leave partial data behind in Google
        Cloud Storage.

        Example:
            >>> from google.cloud import firestore_admin_v1
            >>>
            >>> client = firestore_admin_v1.FirestoreAdminClient()
            >>>
            >>> name = client.database_path('[PROJECT]', '[DATABASE]')
            >>>
            >>> response = client.export_documents(name)

        Args:
            name (str): Required. Database to export. Should be of the form:
                ``projects/{project_id}/databases/{database_id}``.
            collection_ids (list[str]): Which collection ids to export. Unspecified means all collections.
            output_uri_prefix (str): The output URI. Currently only supports Google Cloud Storage URIs of the
                form: ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where ``BUCKET_NAME`` is
                the name of the Google Cloud Storage bucket and ``NAMESPACE_PATH`` is an
                optional Google Cloud Storage namespace path. When choosing a name, be
                sure to consider Google Cloud Storage naming guidelines:
                https://cloud.google.com/storage/docs/naming. If the URI is a bucket
                (without a namespace path), a prefix will be generated based on the
                start time.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will
                be retried using a default configuration.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.firestore_admin_v1.types.Operation` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "export_documents" not in self._inner_api_calls:
            self._inner_api_calls[
                "export_documents"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.export_documents,
                    default_retry=self._method_configs["ExportDocuments"].
                    retry,
                    default_timeout=self._method_configs["ExportDocuments"].
                    timeout,
                    client_info=self._client_info,
                )

        request = firestore_admin_pb2.ExportDocumentsRequest(
            name=name,
            collection_ids=collection_ids,
            output_uri_prefix=output_uri_prefix,
        )
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("name", name)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["export_documents"](request,
                                                         retry=retry,
                                                         timeout=timeout,
                                                         metadata=metadata)