Skip to content

Commit 488b600

Browse files
authored
Merge pull request #1862 from tseaver/1856-logging-ease_lookup_of_existing_sinks_metrics
Ease construction of 'Sink'/'Metric' instances to be reloaded.
2 parents 4cf311b + 22e0987 commit 488b600

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

gcloud/logging/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,23 @@ def list_entries(self, projects=None, filter_=None, order_by=None,
165165
for resource in resources]
166166
return entries, token
167167

168-
def sink(self, name, filter_, destination):
168+
def sink(self, name, filter_=None, destination=None):
169169
"""Creates a sink bound to the current client.
170170
171171
:type name: str
172172
:param name: the name of the sink to be constructed.
173173
174174
:type filter_: str
175-
:param filter_: the advanced logs filter expression defining the
176-
entries exported by the sink.
175+
:param filter_: (optional) the advanced logs filter expression
176+
defining the entries exported by the sink. If not
177+
passed, the instance should already exist, to be
178+
refreshed via :meth:`Sink.reload`.
177179
178180
:type destination: str
179181
:param destination: destination URI for the entries exported by
180-
the sink.
182+
the sink. If not passed, the instance should
183+
already exist, to be refreshed via
184+
:meth:`Sink.reload`.
181185
182186
:rtype: :class:`gcloud.logging.sink.Sink`
183187
:returns: Sink created with the current client.
@@ -211,18 +215,22 @@ def list_sinks(self, page_size=None, page_token=None):
211215
for resource in resources]
212216
return sinks, token
213217

214-
def metric(self, name, filter_, description=''):
218+
def metric(self, name, filter_=None, description=''):
215219
"""Creates a metric bound to the current client.
216220
217221
:type name: str
218222
:param name: the name of the metric to be constructed.
219223
220224
:type filter_: str
221225
:param filter_: the advanced logs filter expression defining the
222-
entries tracked by the metric.
226+
entries tracked by the metric. If not
227+
passed, the instance should already exist, to be
228+
refreshed via :meth:`Metric.reload`.
223229
224230
:type description: str
225231
:param description: the description of the metric to be constructed.
232+
If not passed, the instance should already exist,
233+
to be refreshed via :meth:`Metric.reload`.
226234
227235
:rtype: :class:`gcloud.logging.metric.Metric`
228236
:returns: Metric created with the current client.

gcloud/logging/metric.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ class Metric(object):
2828
2929
:type filter_: string
3030
:param filter_: the advanced logs filter expression defining the entries
31-
tracked by the metric.
31+
tracked by the metric. If not passed, the instance should
32+
already exist, to be refreshed via :meth:`reload`.
3233
3334
:type client: :class:`gcloud.logging.client.Client`
3435
:param client: A client which holds credentials and project configuration
3536
for the metric (which requires a project).
3637
3738
:type description: string
38-
:param description: an optional description of the metric
39+
:param description: an optional description of the metric.
3940
"""
40-
def __init__(self, name, filter_, client, description=''):
41+
def __init__(self, name, filter_=None, client=None, description=''):
4142
self.name = name
4243
self._client = client
4344
self.filter_ = filter_

gcloud/logging/sink.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ class Sink(object):
2828
2929
:type filter_: string
3030
:param filter_: the advanced logs filter expression defining the entries
31-
exported by the sink.
31+
exported by the sink. If not passed, the instance should
32+
already exist, to be refreshed via :meth:`reload`.
3233
3334
:type destination: string
3435
:param destination: destination URI for the entries exported by the sink.
36+
If not passed, the instance should already exist, to
37+
be refreshed via :meth:`reload`.
3538
3639
:type client: :class:`gcloud.logging.client.Client`
3740
:param client: A client which holds credentials and project configuration
3841
for the sink (which requires a project).
3942
"""
40-
def __init__(self, name, filter_, destination, client):
43+
def __init__(self, name, filter_=None, destination=None, client=None):
4144
self.name = name
4245
self.filter_ = filter_
4346
self.destination = destination

gcloud/logging/test_client.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,19 @@ def test_list_entries_explicit(self):
192192
api._list_entries_called_with,
193193
([PROJECT1, PROJECT2], FILTER, DESCENDING, PAGE_SIZE, TOKEN))
194194

195-
def test_sink(self):
195+
def test_sink_defaults(self):
196+
from gcloud.logging.sink import Sink
197+
creds = _Credentials()
198+
client = self._makeOne(project=self.PROJECT, credentials=creds)
199+
sink = client.sink(self.SINK_NAME)
200+
self.assertTrue(isinstance(sink, Sink))
201+
self.assertEqual(sink.name, self.SINK_NAME)
202+
self.assertEqual(sink.filter_, None)
203+
self.assertEqual(sink.destination, None)
204+
self.assertTrue(sink.client is client)
205+
self.assertEqual(sink.project, self.PROJECT)
206+
207+
def test_sink_explicit(self):
196208
from gcloud.logging.sink import Sink
197209
creds = _Credentials()
198210
client = self._makeOne(project=self.PROJECT, credentials=creds)
@@ -260,7 +272,20 @@ def test_list_sinks_with_paging(self):
260272
self.assertEqual(api._list_sinks_called_with,
261273
(PROJECT, PAGE_SIZE, TOKEN))
262274

263-
def test_metric(self):
275+
def test_metric_defaults(self):
276+
from gcloud.logging.metric import Metric
277+
creds = _Credentials()
278+
279+
client_obj = self._makeOne(project=self.PROJECT, credentials=creds)
280+
metric = client_obj.metric(self.METRIC_NAME)
281+
self.assertTrue(isinstance(metric, Metric))
282+
self.assertEqual(metric.name, self.METRIC_NAME)
283+
self.assertEqual(metric.filter_, None)
284+
self.assertEqual(metric.description, '')
285+
self.assertTrue(metric.client is client_obj)
286+
self.assertEqual(metric.project, self.PROJECT)
287+
288+
def test_metric_explicit(self):
264289
from gcloud.logging.metric import Metric
265290
creds = _Credentials()
266291

gcloud/logging/test_metric.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def _makeOne(self, *args, **kw):
3232
def test_ctor_defaults(self):
3333
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
3434
client = _Client(self.PROJECT)
35-
metric = self._makeOne(self.METRIC_NAME, self.FILTER, client=client)
35+
metric = self._makeOne(self.METRIC_NAME, client=client)
3636
self.assertEqual(metric.name, self.METRIC_NAME)
37-
self.assertEqual(metric.filter_, self.FILTER)
37+
self.assertEqual(metric.filter_, None)
3838
self.assertEqual(metric.description, '')
3939
self.assertTrue(metric.client is client)
4040
self.assertEqual(metric.project, self.PROJECT)

gcloud/logging/test_sink.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ def _getTargetClass(self):
2929
def _makeOne(self, *args, **kw):
3030
return self._getTargetClass()(*args, **kw)
3131

32-
def test_ctor(self):
32+
def test_ctor_defaults(self):
33+
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
34+
client = _Client(self.PROJECT)
35+
sink = self._makeOne(self.SINK_NAME, client=client)
36+
self.assertEqual(sink.name, self.SINK_NAME)
37+
self.assertEqual(sink.filter_, None)
38+
self.assertEqual(sink.destination, None)
39+
self.assertTrue(sink.client is client)
40+
self.assertEqual(sink.project, self.PROJECT)
41+
self.assertEqual(sink.full_name, FULL)
42+
self.assertEqual(sink.path, '/%s' % (FULL,))
43+
44+
def test_ctor_explicit(self):
3345
FULL = 'projects/%s/sinks/%s' % (self.PROJECT, self.SINK_NAME)
3446
client = _Client(self.PROJECT)
3547
sink = self._makeOne(self.SINK_NAME, self.FILTER, self.DESTINATION_URI,

0 commit comments

Comments
 (0)