Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repair logging client 'list_*' methods. #1823

Merged
merged 1 commit into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions gcloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ def list_entries(self, projects=None, filter_=None, order_by=None,
if projects is None:
projects = [self.project]

resp = self.logging_api.list_entries(
resources, token = self.logging_api.list_entries(
projects=projects, filter_=filter_, order_by=order_by,
page_size=page_size, page_token=page_token)
loggers = {}
entries = [self._entry_from_resource(resource, loggers)
for resource in resp.get('entries', ())]
return entries, resp.get('nextPageToken')
for resource in resources]
return entries, token

def sink(self, name, filter_, destination):
"""Creates a sink bound to the current client.
Expand Down Expand Up @@ -205,10 +205,11 @@ def list_sinks(self, page_size=None, page_token=None):
more sinks can be retrieved with another call (pass that
value as ``page_token``).
"""
resp = self.sinks_api.list_sinks(self.project, page_size, page_token)
resources, token = self.sinks_api.list_sinks(
self.project, page_size, page_token)
sinks = [Sink.from_api_repr(resource, self)
for resource in resp.get('sinks', ())]
return sinks, resp.get('nextPageToken')
for resource in resources]
return sinks, token

def metric(self, name, filter_, description=''):
"""Creates a metric bound to the current client.
Expand Down Expand Up @@ -249,8 +250,8 @@ def list_metrics(self, page_size=None, page_token=None):
more metrics can be retrieved with another call (pass that
value as ``page_token``).
"""
resp = self.metrics_api.list_metrics(
resources, token = self.metrics_api.list_metrics(
self.project, page_size, page_token)
metrics = [Metric.from_api_repr(resource, self)
for resource in resp.get('metrics', ())]
return metrics, resp.get('nextPageToken')
for resource in resources]
return metrics, token
145 changes: 52 additions & 93 deletions gcloud/logging/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,19 @@ def test_list_entries_defaults(self):
IID = 'IID'
TEXT = 'TEXT'
TOKEN = 'TOKEN'
RETURNED = {
'entries': [{
'textPayload': TEXT,
'insertId': IID,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}],
'nextPageToken': TOKEN,
}
ENTRIES = [{
'textPayload': TEXT,
'insertId': IID,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}]
creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
api = client._logging_api = _DummyLoggingAPI()
api._list_entries_response = RETURNED
api._list_entries_response = ENTRIES, TOKEN

entries, token = client.list_entries()

Expand Down Expand Up @@ -143,28 +140,26 @@ def test_list_entries_explicit(self):
PROTO_PAYLOAD['@type'] = 'type.googleapis.com/testing.example'
TOKEN = 'TOKEN'
PAGE_SIZE = 42
RETURNED = {
'entries': [{
'jsonPayload': PAYLOAD,
'insertId': IID1,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}, {
'protoPayload': PROTO_PAYLOAD,
'insertId': IID2,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}],
}
ENTRIES = [{
'jsonPayload': PAYLOAD,
'insertId': IID1,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}, {
'protoPayload': PROTO_PAYLOAD,
'insertId': IID2,
'resource': {
'type': 'global',
},
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
}]
client = self._makeOne(self.PROJECT, credentials=_Credentials())
api = client._logging_api = _DummyLoggingAPI()
api._list_entries_response = RETURNED
api._list_entries_response = ENTRIES, None

entries, token = client.list_entries(
projects=[PROJECT1, PROJECT2], filter_=FILTER, order_by=DESCENDING,
Expand Down Expand Up @@ -216,17 +211,14 @@ def test_list_sinks_no_paging(self):
SINK_NAME = 'sink_name'
FILTER = 'logName:syslog AND severity>=ERROR'
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
RETURNED = {
'sinks': [{
'name': SINK_PATH,
'filter': FILTER,
'destination': self.DESTINATION_URI,
}],
'nextPageToken': TOKEN,
}
SINKS = [{
'name': SINK_PATH,
'filter': FILTER,
'destination': self.DESTINATION_URI,
}]
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._sinks_api = _DummySinksAPI()
api._list_sinks_response = RETURNED
api._list_sinks_response = SINKS, TOKEN

sinks, token = client.list_sinks()

Expand All @@ -249,16 +241,14 @@ def test_list_sinks_with_paging(self):
SINK_PATH = 'projects/%s/sinks/%s' % (PROJECT, SINK_NAME)
TOKEN = 'TOKEN'
PAGE_SIZE = 42
RETURNED = {
'sinks': [{
'name': SINK_PATH,
'filter': FILTER,
'destination': self.DESTINATION_URI,
}],
}
SINKS = [{
'name': SINK_PATH,
'filter': FILTER,
'destination': self.DESTINATION_URI,
}]
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._sinks_api = _DummySinksAPI()
api._list_sinks_response = RETURNED
api._list_sinks_response = SINKS, None

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

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

def test_list_sinks_missing_key(self):
PROJECT = 'PROJECT'
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._sinks_api = _DummySinksAPI()
api._list_sinks_response = {}

sinks, token = client.list_sinks()

self.assertEqual(len(sinks), 0)
self.assertEqual(token, None)
self.assertEqual(api._list_sinks_called_with,
(PROJECT, None, None))

def test_metric(self):
from gcloud.logging.metric import Metric
creds = _Credentials()
Expand All @@ -303,17 +280,14 @@ def test_list_metrics_no_paging(self):
from gcloud.logging.metric import Metric
PROJECT = 'PROJECT'
TOKEN = 'TOKEN'
RETURNED = {
'metrics': [{
'name': self.METRIC_NAME,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}],
'nextPageToken': TOKEN,
}
METRICS = [{
'name': self.METRIC_NAME,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}]
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._metrics_api = _DummyMetricsAPI()
api._list_metrics_response = RETURNED
api._list_metrics_response = METRICS, TOKEN

metrics, token = client.list_metrics()

Expand All @@ -332,16 +306,14 @@ def test_list_metrics_with_paging(self):
PROJECT = 'PROJECT'
TOKEN = 'TOKEN'
PAGE_SIZE = 42
RETURNED = {
'metrics': [{
'name': self.METRIC_NAME,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}],
}
METRICS = [{
'name': self.METRIC_NAME,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}]
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._metrics_api = _DummyMetricsAPI()
api._list_metrics_response = RETURNED
api._list_metrics_response = METRICS, None

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

def test_list_metrics_missing_key(self):
PROJECT = 'PROJECT'
client = self._makeOne(project=PROJECT, credentials=_Credentials())
api = client._metrics_api = _DummyMetricsAPI()
api._list_metrics_response = {}

metrics, token = client.list_metrics()

self.assertEqual(len(metrics), 0)
self.assertEqual(token, None)
self.assertEqual(api._list_metrics_called_with,
(PROJECT, None, None))


class _Credentials(object):

Expand Down