def test_create_sink(self):
        # Setup Expected Response
        name = "name3373707"
        destination = "destination-1429847026"
        filter_ = "filter-1274492040"
        writer_identity = "writerIdentity775638794"
        include_children = True
        expected_response = {
            "name": name,
            "destination": destination,
            "filter": filter_,
            "writer_identity": writer_identity,
            "include_children": include_children,
        }
        expected_response = logging_config_pb2.LogSink(**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 = logging_v2.ConfigServiceV2Client()

        # Setup Request
        parent = client.project_path("[PROJECT]")
        sink = {}

        response = client.create_sink(parent, sink)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = logging_config_pb2.CreateSinkRequest(parent=parent,
                                                                sink=sink)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #2
0
    def test_create_sink(self):
        # Setup Expected Response
        name = 'name3373707'
        destination = 'destination-1429847026'
        filter_ = 'filter-1274492040'
        writer_identity = 'writerIdentity775638794'
        include_children = True
        expected_response = {
            'name': name,
            'destination': destination,
            'filter': filter_,
            'writer_identity': writer_identity,
            'include_children': include_children
        }
        expected_response = logging_config_pb2.LogSink(**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 = logging_v2.ConfigServiceV2Client()

        # Setup Request
        parent = client.project_path('[PROJECT]')
        sink = {}

        response = client.create_sink(parent, sink)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = logging_config_pb2.CreateSinkRequest(parent=parent,
                                                                sink=sink)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #3
0
    def create_sink(
        self,
        parent,
        sink,
        unique_writer_identity=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Creates a sink that exports specified log entries to a destination. The
        export of newly-ingested log entries begins immediately, unless the
        sink's ``writer_identity`` is not permitted to write to the destination.
        A sink can export log entries only from the resource owning the sink.

        Example:
            >>> from google.cloud import logging_v2
            >>>
            >>> client = logging_v2.ConfigServiceV2Client()
            >>>
            >>> parent = client.project_path('[PROJECT]')
            >>>
            >>> # TODO: Initialize `sink`:
            >>> sink = {}
            >>>
            >>> response = client.create_sink(parent, sink)

        Args:
            parent (str): Required. The resource in which to create the sink:

                ::

                     "projects/[PROJECT_ID]"
                     "organizations/[ORGANIZATION_ID]"
                     "billingAccounts/[BILLING_ACCOUNT_ID]"
                     "folders/[FOLDER_ID]"

                Examples: ``"projects/my-logging-project"``,
                ``"organizations/123456789"``.
            sink (Union[dict, ~google.cloud.logging_v2.types.LogSink]): Required. The new sink, whose ``name`` parameter is a sink identifier
                that is not already in use.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.logging_v2.types.LogSink`
            unique_writer_identity (bool): Optional. Determines the kind of IAM identity returned as
                ``writer_identity`` in the new sink. If this value is omitted or set to
                false, and if the sink's parent is a project, then the value returned as
                ``writer_identity`` is the same group or service account used by Logging
                before the addition of writer identities to this API. The sink's
                destination must be in the same project as the sink itself.

                If this field is set to true, or if the sink is owned by a non-project
                resource such as an organization, then the value of ``writer_identity``
                will be a unique service account used only for exports from the new
                sink. For more information, see ``writer_identity`` in ``LogSink``.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            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.logging_v2.types.LogSink` 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 "create_sink" not in self._inner_api_calls:
            self._inner_api_calls[
                "create_sink"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.create_sink,
                    default_retry=self._method_configs["CreateSink"].retry,
                    default_timeout=self._method_configs["CreateSink"].timeout,
                    client_info=self._client_info,
                )

        request = logging_config_pb2.CreateSinkRequest(
            parent=parent,
            sink=sink,
            unique_writer_identity=unique_writer_identity)
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("parent", parent)]
        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["create_sink"](request,
                                                    retry=retry,
                                                    timeout=timeout,
                                                    metadata=metadata)