Пример #1
0
    def sink_update(
        self,
        sink_name,
        filter_,
        destination,
        *,
        unique_writer_identity=False,
    ):
        """Update a sink resource.

        Args:
            sink_name (str): Required. The resource name of the sink,
                including the parent resource and the sink identifier:

            ::

                "projects/[PROJECT_ID]/sinks/[SINK_ID]"
                "organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
                "billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
                "folders/[FOLDER_ID]/sinks/[SINK_ID]"
            filter_ (str): The advanced logs filter expression defining the
                entries exported by the sink.
            destination (str): destination URI for the entries exported by
                the sink.
            unique_writer_identity (Optional[bool]): determines the kind of
                IAM identity returned as writer_identity in the new sink.


        Returns:
            dict: The sink resource returned from the API (converted from a
                  protobuf to a dictionary).
        """
        name = sink_name.split("/")[-1]  # parse name out of full resoure name
        sink_pb = LogSink(
            name=name,
            filter=filter_,
            destination=destination,
        )

        request = UpdateSinkRequest(
            sink_name=sink_name,
            sink=sink_pb,
            unique_writer_identity=unique_writer_identity,
        )
        sink_pb = self._gapic_api.update_sink(request=request)
        # NOTE: LogSink message type does not have an ``Any`` field
        #       so `MessageToDict`` can safely be used.
        return MessageToDict(
            LogSink.pb(sink_pb),
            preserving_proto_field_name=False,
            including_default_value_fields=False,
        )
Пример #2
0
    def sink_get(self, sink_name):
        """Retrieve a sink resource.

        Args:
            sink_name (str): The resource name of the sink,
                including the parent resource and the sink identifier:

            ::

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

        Returns:
            dict: The sink object returned from the API (converted from a
                protobuf to a dictionary).
        """
        sink_pb = self._gapic_api.get_sink(sink_name=sink_name)
        # NOTE: LogSink message type does not have an ``Any`` field
        #       so `MessageToDict`` can safely be used.
        return MessageToDict(
            LogSink.pb(sink_pb),
            preserving_proto_field_name=False,
            including_default_value_fields=False,
        )
Пример #3
0
    def test_list_sinks(self):
        client = self.make_sinks_api()

        sink_msg = LogSink(name=self.SINK_NAME,
                           destination=self.DESTINATION_URI,
                           filter=FILTER)
        with mock.patch.object(type(client._gapic_api.transport.list_sinks),
                               "__call__") as call:
            call.return_value = logging_v2.types.ListSinksResponse(
                sinks=[sink_msg])

            result = client.list_sinks(self.PARENT_PATH, )

        sinks = list(result)

        # Check the response
        assert len(sinks) == 1
        sink = sinks[0]
        assert isinstance(sink, google.cloud.logging_v2.sink.Sink)
        assert sink.name == self.SINK_NAME
        assert sink.destination == self.DESTINATION_URI
        assert sink.filter_ == FILTER

        # Check the request
        call.assert_called_once()
        request = call.call_args.args[0]
        assert request.parent == self.PARENT_PATH
Пример #4
0
    def sink_create(self,
                    parent,
                    sink_name,
                    filter_,
                    destination,
                    *,
                    unique_writer_identity=False):
        """Create a sink resource.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create

        Args:
            parent(str): The resource in which to create the sink,
                including the parent resource and the sink identifier:

            ::

                "projects/[PROJECT_ID]"
                "organizations/[ORGANIZATION_ID]"
                "billingAccounts/[BILLING_ACCOUNT_ID]"
                "folders/[FOLDER_ID]".
            sink_name (str): The name of the sink.
            filter_ (str): The advanced logs filter expression defining the
                entries exported by the sink.
            destination (str): Destination URI for the entries exported by
                the sink.
            unique_writer_identity (Optional[bool]):  determines the kind of
                IAM identity returned as writer_identity in the new sink.

        Returns:
            dict: The sink resource returned from the API (converted from a
                protobuf to a dictionary).
        """
        sink_pb = LogSink(name=sink_name,
                          filter=filter_,
                          destination=destination)
        request = CreateSinkRequest(
            parent=parent,
            sink=sink_pb,
            unique_writer_identity=unique_writer_identity)
        created_pb = self._gapic_api.create_sink(request=request)
        return MessageToDict(
            LogSink.pb(created_pb),
            preserving_proto_field_name=False,
            including_default_value_fields=False,
        )
Пример #5
0
    def test_list_sinks_with_max_results(self):
        client = self.make_sinks_api()
        sink_msg = LogSink(name=self.SINK_NAME,
                           destination=self.DESTINATION_URI,
                           filter=FILTER)

        with mock.patch.object(type(client._gapic_api.transport.list_sinks),
                               "__call__") as call:
            call.return_value = logging_v2.types.ListSinksResponse(
                sinks=[sink_msg, sink_msg])
            result = client.list_sinks(self.PARENT_PATH,
                                       page_size=42,
                                       page_token="token",
                                       max_results=1)
        # Check the request
        call.assert_called_once()
        assert len(list(result)) == 1
Пример #6
0
 def sinks_pager(page_iter):
     for page in page_iter:
         # Convert the GAPIC sink type into the handwritten `Sink` type
         yield Sink.from_api_repr(LogSink.to_dict(page),
                                  client=self._client)