Skip to content

Commit d4ff022

Browse files
committed
Merge pull request #1823 from tseaver/1820-logging-repair_client_list_methods
Repair logging client 'list_*' methods.
2 parents 88daa08 + 82245d5 commit d4ff022

File tree

2 files changed

+62
-102
lines changed

2 files changed

+62
-102
lines changed

gcloud/logging/client.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ def list_entries(self, projects=None, filter_=None, order_by=None,
157157
if projects is None:
158158
projects = [self.project]
159159

160-
resp = self.logging_api.list_entries(
160+
resources, token = self.logging_api.list_entries(
161161
projects=projects, filter_=filter_, order_by=order_by,
162162
page_size=page_size, page_token=page_token)
163163
loggers = {}
164164
entries = [self._entry_from_resource(resource, loggers)
165-
for resource in resp.get('entries', ())]
166-
return entries, resp.get('nextPageToken')
165+
for resource in resources]
166+
return entries, token
167167

168168
def sink(self, name, filter_, destination):
169169
"""Creates a sink bound to the current client.
@@ -205,10 +205,11 @@ def list_sinks(self, page_size=None, page_token=None):
205205
more sinks can be retrieved with another call (pass that
206206
value as ``page_token``).
207207
"""
208-
resp = self.sinks_api.list_sinks(self.project, page_size, page_token)
208+
resources, token = self.sinks_api.list_sinks(
209+
self.project, page_size, page_token)
209210
sinks = [Sink.from_api_repr(resource, self)
210-
for resource in resp.get('sinks', ())]
211-
return sinks, resp.get('nextPageToken')
211+
for resource in resources]
212+
return sinks, token
212213

213214
def metric(self, name, filter_, description=''):
214215
"""Creates a metric bound to the current client.
@@ -249,8 +250,8 @@ def list_metrics(self, page_size=None, page_token=None):
249250
more metrics can be retrieved with another call (pass that
250251
value as ``page_token``).
251252
"""
252-
resp = self.metrics_api.list_metrics(
253+
resources, token = self.metrics_api.list_metrics(
253254
self.project, page_size, page_token)
254255
metrics = [Metric.from_api_repr(resource, self)
255-
for resource in resp.get('metrics', ())]
256-
return metrics, resp.get('nextPageToken')
256+
for resource in resources]
257+
return metrics, token

gcloud/logging/test_client.py

Lines changed: 52 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,19 @@ def test_list_entries_defaults(self):
9494
IID = 'IID'
9595
TEXT = 'TEXT'
9696
TOKEN = 'TOKEN'
97-
RETURNED = {
98-
'entries': [{
99-
'textPayload': TEXT,
100-
'insertId': IID,
101-
'resource': {
102-
'type': 'global',
103-
},
104-
'logName': 'projects/%s/logs/%s' % (
105-
self.PROJECT, self.LOGGER_NAME),
106-
}],
107-
'nextPageToken': TOKEN,
108-
}
97+
ENTRIES = [{
98+
'textPayload': TEXT,
99+
'insertId': IID,
100+
'resource': {
101+
'type': 'global',
102+
},
103+
'logName': 'projects/%s/logs/%s' % (
104+
self.PROJECT, self.LOGGER_NAME),
105+
}]
109106
creds = _Credentials()
110107
client = self._makeOne(project=self.PROJECT, credentials=creds)
111108
api = client._logging_api = _DummyLoggingAPI()
112-
api._list_entries_response = RETURNED
109+
api._list_entries_response = ENTRIES, TOKEN
113110

114111
entries, token = client.list_entries()
115112

@@ -143,28 +140,26 @@ def test_list_entries_explicit(self):
143140
PROTO_PAYLOAD['@type'] = 'type.googleapis.com/testing.example'
144141
TOKEN = 'TOKEN'
145142
PAGE_SIZE = 42
146-
RETURNED = {
147-
'entries': [{
148-
'jsonPayload': PAYLOAD,
149-
'insertId': IID1,
150-
'resource': {
151-
'type': 'global',
152-
},
153-
'logName': 'projects/%s/logs/%s' % (
154-
self.PROJECT, self.LOGGER_NAME),
155-
}, {
156-
'protoPayload': PROTO_PAYLOAD,
157-
'insertId': IID2,
158-
'resource': {
159-
'type': 'global',
160-
},
161-
'logName': 'projects/%s/logs/%s' % (
162-
self.PROJECT, self.LOGGER_NAME),
163-
}],
164-
}
143+
ENTRIES = [{
144+
'jsonPayload': PAYLOAD,
145+
'insertId': IID1,
146+
'resource': {
147+
'type': 'global',
148+
},
149+
'logName': 'projects/%s/logs/%s' % (
150+
self.PROJECT, self.LOGGER_NAME),
151+
}, {
152+
'protoPayload': PROTO_PAYLOAD,
153+
'insertId': IID2,
154+
'resource': {
155+
'type': 'global',
156+
},
157+
'logName': 'projects/%s/logs/%s' % (
158+
self.PROJECT, self.LOGGER_NAME),
159+
}]
165160
client = self._makeOne(self.PROJECT, credentials=_Credentials())
166161
api = client._logging_api = _DummyLoggingAPI()
167-
api._list_entries_response = RETURNED
162+
api._list_entries_response = ENTRIES, None
168163

169164
entries, token = client.list_entries(
170165
projects=[PROJECT1, PROJECT2], filter_=FILTER, order_by=DESCENDING,
@@ -216,17 +211,14 @@ def test_list_sinks_no_paging(self):
216211
SINK_NAME = 'sink_name'
217212
FILTER = 'logName:syslog AND severity>=ERROR'
218213
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
219-
RETURNED = {
220-
'sinks': [{
221-
'name': SINK_PATH,
222-
'filter': FILTER,
223-
'destination': self.DESTINATION_URI,
224-
}],
225-
'nextPageToken': TOKEN,
226-
}
214+
SINKS = [{
215+
'name': SINK_PATH,
216+
'filter': FILTER,
217+
'destination': self.DESTINATION_URI,
218+
}]
227219
client = self._makeOne(project=PROJECT, credentials=_Credentials())
228220
api = client._sinks_api = _DummySinksAPI()
229-
api._list_sinks_response = RETURNED
221+
api._list_sinks_response = SINKS, TOKEN
230222

231223
sinks, token = client.list_sinks()
232224

@@ -249,16 +241,14 @@ def test_list_sinks_with_paging(self):
249241
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
250242
TOKEN = 'TOKEN'
251243
PAGE_SIZE = 42
252-
RETURNED = {
253-
'sinks': [{
254-
'name': SINK_PATH,
255-
'filter': FILTER,
256-
'destination': self.DESTINATION_URI,
257-
}],
258-
}
244+
SINKS = [{
245+
'name': SINK_PATH,
246+
'filter': FILTER,
247+
'destination': self.DESTINATION_URI,
248+
}]
259249
client = self._makeOne(project=PROJECT, credentials=_Credentials())
260250
api = client._sinks_api = _DummySinksAPI()
261-
api._list_sinks_response = RETURNED
251+
api._list_sinks_response = SINKS, None
262252

263253
sinks, token = client.list_sinks(PAGE_SIZE, TOKEN)
264254

@@ -272,19 +262,6 @@ def test_list_sinks_with_paging(self):
272262
self.assertEqual(api._list_sinks_called_with,
273263
(PROJECT, PAGE_SIZE, TOKEN))
274264

275-
def test_list_sinks_missing_key(self):
276-
PROJECT = 'PROJECT'
277-
client = self._makeOne(project=PROJECT, credentials=_Credentials())
278-
api = client._sinks_api = _DummySinksAPI()
279-
api._list_sinks_response = {}
280-
281-
sinks, token = client.list_sinks()
282-
283-
self.assertEqual(len(sinks), 0)
284-
self.assertEqual(token, None)
285-
self.assertEqual(api._list_sinks_called_with,
286-
(PROJECT, None, None))
287-
288265
def test_metric(self):
289266
from gcloud.logging.metric import Metric
290267
creds = _Credentials()
@@ -303,17 +280,14 @@ def test_list_metrics_no_paging(self):
303280
from gcloud.logging.metric import Metric
304281
PROJECT = 'PROJECT'
305282
TOKEN = 'TOKEN'
306-
RETURNED = {
307-
'metrics': [{
308-
'name': self.METRIC_NAME,
309-
'filter': self.FILTER,
310-
'description': self.DESCRIPTION,
311-
}],
312-
'nextPageToken': TOKEN,
313-
}
283+
METRICS = [{
284+
'name': self.METRIC_NAME,
285+
'filter': self.FILTER,
286+
'description': self.DESCRIPTION,
287+
}]
314288
client = self._makeOne(project=PROJECT, credentials=_Credentials())
315289
api = client._metrics_api = _DummyMetricsAPI()
316-
api._list_metrics_response = RETURNED
290+
api._list_metrics_response = METRICS, TOKEN
317291

318292
metrics, token = client.list_metrics()
319293

@@ -332,16 +306,14 @@ def test_list_metrics_with_paging(self):
332306
PROJECT = 'PROJECT'
333307
TOKEN = 'TOKEN'
334308
PAGE_SIZE = 42
335-
RETURNED = {
336-
'metrics': [{
337-
'name': self.METRIC_NAME,
338-
'filter': self.FILTER,
339-
'description': self.DESCRIPTION,
340-
}],
341-
}
309+
METRICS = [{
310+
'name': self.METRIC_NAME,
311+
'filter': self.FILTER,
312+
'description': self.DESCRIPTION,
313+
}]
342314
client = self._makeOne(project=PROJECT, credentials=_Credentials())
343315
api = client._metrics_api = _DummyMetricsAPI()
344-
api._list_metrics_response = RETURNED
316+
api._list_metrics_response = METRICS, None
345317

346318
# Execute request.
347319
metrics, token = client.list_metrics(PAGE_SIZE, TOKEN)
@@ -356,19 +328,6 @@ def test_list_metrics_with_paging(self):
356328
self.assertEqual(api._list_metrics_called_with,
357329
(PROJECT, PAGE_SIZE, TOKEN))
358330

359-
def test_list_metrics_missing_key(self):
360-
PROJECT = 'PROJECT'
361-
client = self._makeOne(project=PROJECT, credentials=_Credentials())
362-
api = client._metrics_api = _DummyMetricsAPI()
363-
api._list_metrics_response = {}
364-
365-
metrics, token = client.list_metrics()
366-
367-
self.assertEqual(len(metrics), 0)
368-
self.assertEqual(token, None)
369-
self.assertEqual(api._list_metrics_called_with,
370-
(PROJECT, None, None))
371-
372331

373332
class _Credentials(object):
374333

0 commit comments

Comments
 (0)