Skip to content

Commit

Permalink
Add 'Sink.exists' API wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Mar 11, 2016
1 parent 6f3d1a7 commit 47cf5ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gcloud/logging/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Define Logging API Sinks."""

from gcloud.exceptions import NotFound


class Sink(object):
"""Sinks represent filtered exports for log entries.
Expand Down Expand Up @@ -90,3 +92,22 @@ def create(self, client=None):
'destination': self.destination,
}
client.connection.api_request(method='PUT', path=self.path, data=data)

def exists(self, client=None):
"""API call: test for the existence of the sink via a GET request
See
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/get
:type client: :class:`gcloud.logging.client.Client` or ``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current sink.
"""
client = self._require_client(client)

try:
client.connection.api_request(method='GET', path=self.path)
except NotFound:
return False
else:
return True
27 changes: 27 additions & 0 deletions gcloud/logging/test_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ def test_create_w_alternate_client(self):
self.assertEqual(req['path'], '/%s' % FULL)
self.assertEqual(req['data'], RESOURCE)

def test_exists_miss_w_bound_client(self):
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
conn = _Connection()
CLIENT = _Client(project=self.PROJECT, connection=conn)
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI,
client=CLIENT)
self.assertFalse(sink.exists())
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % FULL)

def test_exists_hit_w_alternate_client(self):
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
conn1 = _Connection({'name': FULL})
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)
conn2 = _Connection({'name': FULL})
CLIENT2 = _Client(project=self.PROJECT, connection=conn2)
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI,
client=CLIENT1)
self.assertTrue(sink.exists(client=CLIENT2))
self.assertEqual(len(conn1._requested), 0)
self.assertEqual(len(conn2._requested), 1)
req = conn2._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % FULL)


class _Connection(object):

Expand Down

0 comments on commit 47cf5ab

Please sign in to comment.