Skip to content

Commit

Permalink
Merge pull request #1587 from tseaver/logging-metric_update
Browse files Browse the repository at this point in the history
Add 'Metrics.update' API wrapper.
  • Loading branch information
tseaver committed Mar 14, 2016
2 parents 245e72e + 23feb30 commit 0283a36
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gcloud/logging/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ def reload(self, client=None):
data = client.connection.api_request(method='GET', path=self.path)
self.description = data.get('description', '')
self.filter_ = data['filter']

def update(self, client=None):
"""API call: update metric configuration via a PUT request
See
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics/update
: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 = {'name': self.name, 'filter': self.filter_}
if self.description:
data['description'] = self.description
client.connection.api_request(method='PUT', path=self.path, data=data)
38 changes: 38 additions & 0 deletions gcloud/logging/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,44 @@ def test_reload_w_alternate_client(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % FULL)

def test_update_w_bound_client(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
RESOURCE = {
'name': self.METRIC_NAME,
'filter': self.FILTER,
}
conn = _Connection(RESOURCE)
CLIENT = _Client(project=self.PROJECT, connection=conn)
metric = self._makeOne(self.METRIC_NAME, self.FILTER, client=CLIENT)
metric.update()
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['path'], '/%s' % FULL)
self.assertEqual(req['data'], RESOURCE)

def test_update_w_alternate_client(self):
FULL = 'projects/%s/metrics/%s' % (self.PROJECT, self.METRIC_NAME)
DESCRIPTION = 'DESCRIPTION'
RESOURCE = {
'name': self.METRIC_NAME,
'description': DESCRIPTION,
'filter': self.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,
description=DESCRIPTION)
metric.update(client=CLIENT2)
self.assertEqual(len(conn1._requested), 0)
self.assertEqual(len(conn2._requested), 1)
req = conn2._requested[0]
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['path'], '/%s' % FULL)
self.assertEqual(req['data'], RESOURCE)


class _Connection(object):

Expand Down

0 comments on commit 0283a36

Please sign in to comment.