def sink_update(self, project, sink_name, filter_, destination): """API call: update a sink resource. :type project: str :param project: ID of the project containing the sink. :type sink_name: str :param sink_name: the name of the sink :type filter_: str :param filter_: the advanced logs filter expression defining the entries exported by the sink. :type destination: str :param destination: destination URI for the entries exported by the sink. :rtype: dict :returns: The sink object returned from the API (converted from a protobuf to a dictionary). """ options = None path = 'projects/%s/sinks/%s' % (project, sink_name) sink_pb = LogSink(name=path, filter=filter_, destination=destination) try: sink_pb = self._gax_api.update_sink(path, sink_pb, options=options) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: raise NotFound(path) raise # NOTE: LogSink message type does not have an ``Any`` field # so `MessageToDict`` can safely be used. return MessageToDict(sink_pb)
def sink_create(self, project, sink_name, filter_, destination): """API call: create a sink resource. See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create :type project: str :param project: ID of the project in which to create the sink. :type sink_name: str :param sink_name: the name of the sink :type filter_: str :param filter_: the advanced logs filter expression defining the entries exported by the sink. :type destination: str :param destination: destination URI for the entries exported by the sink. """ options = None parent = 'projects/%s' % (project, ) sink_pb = LogSink(name=sink_name, filter=filter_, destination=destination) try: self._gax_api.create_sink(parent, sink_pb, options=options) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/sinks/%s' % (project, sink_name) raise Conflict(path) raise
def test_list_sinks_w_paging(self): from google.cloud.proto.logging.v2.logging_config_pb2 import LogSink from google.cloud._testing import _GAXPageIterator from google.cloud.logging.sink import Sink TOKEN = 'TOKEN' PAGE_SIZE = 42 sink_pb = LogSink(name=self.SINK_PATH, destination=self.DESTINATION_URI, filter=self.FILTER) response = _GAXPageIterator([sink_pb]) gax_api = _GAXSinksAPI(_list_sinks_response=response) client = object() api = self._make_one(gax_api, client) iterator = api.list_sinks( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) sinks = list(iterator) token = iterator.next_page_token # First check the token. self.assertIsNone(token) # Then check the sinks returned. self.assertEqual(len(sinks), 1) sink = sinks[0] self.assertIsInstance(sink, Sink) self.assertEqual(sink.name, self.SINK_PATH) self.assertEqual(sink.filter_, self.FILTER) self.assertEqual(sink.destination, self.DESTINATION_URI) self.assertIs(sink.client, client) project, page_size, options = gax_api._list_sinks_called_with self.assertEqual(project, self.PROJECT_PATH) self.assertEqual(page_size, PAGE_SIZE) self.assertEqual(options.page_token, TOKEN)
def sink_create(self, project, sink_name, filter_, destination, unique_writer_identity=False): """API call: create a sink resource. See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create :type project: str :param project: ID of the project in which to create the sink. :type sink_name: str :param sink_name: the name of the sink :type filter_: str :param filter_: the advanced logs filter expression defining the entries exported by the sink. :type destination: str :param destination: destination URI for the entries exported by the sink. :type unique_writer_identity: bool :param unique_writer_identity: (Optional) determines the kind of IAM identity returned as writer_identity in the new sink. :rtype: dict :returns: The sink resource returned from the API (converted from a protobuf to a dictionary). """ options = None parent = 'projects/%s' % (project, ) sink_pb = LogSink(name=sink_name, filter=filter_, destination=destination) try: created_pb = self._gax_api.create_sink( parent, sink_pb, unique_writer_identity=unique_writer_identity, options=options, ) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/sinks/%s' % (project, sink_name) raise Conflict(path) raise return MessageToDict(created_pb)
def test_sink_update_hit(self): from google.cloud.proto.logging.v2.logging_config_pb2 import LogSink response = LogSink(name=self.SINK_NAME, destination=self.DESTINATION_URI, filter=self.FILTER) gax_api = _GAXSinksAPI(_update_sink_response=response) api = self._make_one(gax_api, None) api.sink_update( self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI) sink_name, sink, options = ( gax_api._update_sink_called_with) self.assertEqual(sink_name, self.SINK_PATH) self.assertIsInstance(sink, LogSink) self.assertEqual(sink.name, self.SINK_PATH) self.assertEqual(sink.filter, self.FILTER) self.assertEqual(sink.destination, self.DESTINATION_URI) self.assertIsNone(options)
def test_sink_get_hit(self): from google.cloud.proto.logging.v2.logging_config_pb2 import LogSink RESPONSE = { 'name': self.SINK_PATH, 'filter': self.FILTER, 'destination': self.DESTINATION_URI, } sink_pb = LogSink(name=self.SINK_PATH, destination=self.DESTINATION_URI, filter=self.FILTER) gax_api = _GAXSinksAPI(_get_sink_response=sink_pb) api = self._make_one(gax_api, None) response = api.sink_get(self.PROJECT, self.SINK_NAME) self.assertEqual(response, RESPONSE) sink_name, options = gax_api._get_sink_called_with self.assertEqual(sink_name, self.SINK_PATH) self.assertIsNone(options)