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

Add 'Metrics.reload' API wrapper. #1586

Merged
merged 1 commit into from
Mar 13, 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
15 changes: 15 additions & 0 deletions gcloud/logging/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,18 @@ def exists(self, client=None):
return False
else:
return True

def reload(self, client=None):
"""API call: sync local metric configuration via a GET request

See
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics/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 metric.
"""
client = self._require_client(client)
data = client.connection.api_request(method='GET', path=self.path)
self.description = data.get('description', '')
self.filter_ = data['filter']
45 changes: 44 additions & 1 deletion gcloud/logging/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_exists_miss_w_bound_client(self):

def test_exists_hit_w_alternate_client(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
conn1 = _Connection({'name': FULL})
conn1 = _Connection()
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)
conn2 = _Connection({'name': FULL})
CLIENT2 = _Client(project=self.PROJECT, connection=conn2)
Expand All @@ -118,6 +118,49 @@ def test_exists_hit_w_alternate_client(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % FULL)

def test_reload_w_bound_client(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
DESCRIPTION = 'DESCRIPTION'
NEW_FILTER = 'logName:syslog AND severity>=INFO'
RESOURCE = {
'name': self.METRIC_NAME,
'filter': NEW_FILTER,
}
conn = _Connection(RESOURCE)
CLIENT = _Client(project=self.PROJECT, connection=conn)
metric = self._makeOne(self.METRIC_NAME, self.FILTER, client=CLIENT,
description=DESCRIPTION)
metric.reload()
self.assertEqual(metric.filter_, NEW_FILTER)
self.assertEqual(metric.description, '')
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % FULL)

def test_reload_w_alternate_client(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
DESCRIPTION = 'DESCRIPTION'
NEW_FILTER = 'logName:syslog AND severity>=INFO'
RESOURCE = {
'name': self.METRIC_NAME,
'description': DESCRIPTION,
'filter': NEW_FILTER,
}
conn1 = _Connection()
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)
conn2 = _Connection(RESOURCE)
CLIENT2 = _Client(project=self.PROJECT, connection=conn2)
metric = self._makeOne(self.METRIC_NAME, self.FILTER, client=CLIENT1)
metric.reload(client=CLIENT2)
self.assertEqual(metric.filter_, NEW_FILTER)
self.assertEqual(metric.description, DESCRIPTION)
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