Skip to content

Commit

Permalink
Merge pull request #1862 from tseaver/1856-logging-ease_lookup_of_exi…
Browse files Browse the repository at this point in the history
…sting_sinks_metrics

Ease construction of 'Sink'/'Metric' instances to be reloaded.
  • Loading branch information
tseaver authored Jun 16, 2016
2 parents 4cf311b + 22e0987 commit 488b600
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
20 changes: 14 additions & 6 deletions gcloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,23 @@ def list_entries(self, projects=None, filter_=None, order_by=None,
for resource in resources]
return entries, token

def sink(self, name, filter_, destination):
def sink(self, name, filter_=None, destination=None):
"""Creates a sink bound to the current client.
:type name: str
:param name: the name of the sink to be constructed.
:type filter_: str
:param filter_: the advanced logs filter expression defining the
entries exported by the sink.
:param filter_: (optional) the advanced logs filter expression
defining the entries exported by the sink. If not
passed, the instance should already exist, to be
refreshed via :meth:`Sink.reload`.
:type destination: str
:param destination: destination URI for the entries exported by
the sink.
the sink. If not passed, the instance should
already exist, to be refreshed via
:meth:`Sink.reload`.
:rtype: :class:`gcloud.logging.sink.Sink`
:returns: Sink created with the current client.
Expand Down Expand Up @@ -211,18 +215,22 @@ def list_sinks(self, page_size=None, page_token=None):
for resource in resources]
return sinks, token

def metric(self, name, filter_, description=''):
def metric(self, name, filter_=None, description=''):
"""Creates a metric bound to the current client.
:type name: str
:param name: the name of the metric to be constructed.
:type filter_: str
:param filter_: the advanced logs filter expression defining the
entries tracked by the metric.
entries tracked by the metric. If not
passed, the instance should already exist, to be
refreshed via :meth:`Metric.reload`.
:type description: str
:param description: the description of the metric to be constructed.
If not passed, the instance should already exist,
to be refreshed via :meth:`Metric.reload`.
:rtype: :class:`gcloud.logging.metric.Metric`
:returns: Metric created with the current client.
Expand Down
7 changes: 4 additions & 3 deletions gcloud/logging/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ class Metric(object):
:type filter_: string
:param filter_: the advanced logs filter expression defining the entries
tracked by the metric.
tracked by the metric. If not passed, the instance should
already exist, to be refreshed via :meth:`reload`.
:type client: :class:`gcloud.logging.client.Client`
:param client: A client which holds credentials and project configuration
for the metric (which requires a project).
:type description: string
:param description: an optional description of the metric
:param description: an optional description of the metric.
"""
def __init__(self, name, filter_, client, description=''):
def __init__(self, name, filter_=None, client=None, description=''):
self.name = name
self._client = client
self.filter_ = filter_
Expand Down
7 changes: 5 additions & 2 deletions gcloud/logging/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ class Sink(object):
:type filter_: string
:param filter_: the advanced logs filter expression defining the entries
exported by the sink.
exported by the sink. If not passed, the instance should
already exist, to be refreshed via :meth:`reload`.
:type destination: string
:param destination: destination URI for the entries exported by the sink.
If not passed, the instance should already exist, to
be refreshed via :meth:`reload`.
:type client: :class:`gcloud.logging.client.Client`
:param client: A client which holds credentials and project configuration
for the sink (which requires a project).
"""
def __init__(self, name, filter_, destination, client):
def __init__(self, name, filter_=None, destination=None, client=None):
self.name = name
self.filter_ = filter_
self.destination = destination
Expand Down
29 changes: 27 additions & 2 deletions gcloud/logging/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,19 @@ def test_list_entries_explicit(self):
api._list_entries_called_with,
([PROJECT1, PROJECT2], FILTER, DESCENDING, PAGE_SIZE, TOKEN))

def test_sink(self):
def test_sink_defaults(self):
from gcloud.logging.sink import Sink
creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
sink = client.sink(self.SINK_NAME)
self.assertTrue(isinstance(sink, Sink))
self.assertEqual(sink.name, self.SINK_NAME)
self.assertEqual(sink.filter_, None)
self.assertEqual(sink.destination, None)
self.assertTrue(sink.client is client)
self.assertEqual(sink.project, self.PROJECT)

def test_sink_explicit(self):
from gcloud.logging.sink import Sink
creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
Expand Down Expand Up @@ -260,7 +272,20 @@ def test_list_sinks_with_paging(self):
self.assertEqual(api._list_sinks_called_with,
(PROJECT, PAGE_SIZE, TOKEN))

def test_metric(self):
def test_metric_defaults(self):
from gcloud.logging.metric import Metric
creds = _Credentials()

client_obj = self._makeOne(project=self.PROJECT, credentials=creds)
metric = client_obj.metric(self.METRIC_NAME)
self.assertTrue(isinstance(metric, Metric))
self.assertEqual(metric.name, self.METRIC_NAME)
self.assertEqual(metric.filter_, None)
self.assertEqual(metric.description, '')
self.assertTrue(metric.client is client_obj)
self.assertEqual(metric.project, self.PROJECT)

def test_metric_explicit(self):
from gcloud.logging.metric import Metric
creds = _Credentials()

Expand Down
4 changes: 2 additions & 2 deletions gcloud/logging/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def _makeOne(self, *args, **kw):
def test_ctor_defaults(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
client = _Client(self.PROJECT)
metric = self._makeOne(self.METRIC_NAME, self.FILTER, client=client)
metric = self._makeOne(self.METRIC_NAME, client=client)
self.assertEqual(metric.name, self.METRIC_NAME)
self.assertEqual(metric.filter_, self.FILTER)
self.assertEqual(metric.filter_, None)
self.assertEqual(metric.description, '')
self.assertTrue(metric.client is client)
self.assertEqual(metric.project, self.PROJECT)
Expand Down
14 changes: 13 additions & 1 deletion gcloud/logging/test_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ def _getTargetClass(self):
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_ctor(self):
def test_ctor_defaults(self):
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
client = _Client(self.PROJECT)
sink = self._makeOne(self.SINK_NAME, client=client)
self.assertEqual(sink.name, self.SINK_NAME)
self.assertEqual(sink.filter_, None)
self.assertEqual(sink.destination, None)
self.assertTrue(sink.client is client)
self.assertEqual(sink.project, self.PROJECT)
self.assertEqual(sink.full_name, FULL)
self.assertEqual(sink.path, '/%s' % (FULL,))

def test_ctor_explicit(self):
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
client = _Client(self.PROJECT)
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI,
Expand Down

0 comments on commit 488b600

Please sign in to comment.