diff --git a/gcloud/logging/metric.py b/gcloud/logging/metric.py index c4a358a93abb..cc8d4d783329 100644 --- a/gcloud/logging/metric.py +++ b/gcloud/logging/metric.py @@ -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) diff --git a/gcloud/logging/test_metric.py b/gcloud/logging/test_metric.py index a491d4e0c845..a592176a7084 100644 --- a/gcloud/logging/test_metric.py +++ b/gcloud/logging/test_metric.py @@ -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):