From 33355eaf68c9fbd7731028f8a76438ea434d2919 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Tue, 25 Aug 2015 11:00:13 +0200 Subject: [PATCH] gnocchi: quote the resource_id in url Some resource_id looks like "812c9916-83f7-460b-8b27-ab5a2ba98275/foobar". This change uses urllib.quote to encode them when they are used into a url. Related-bug: #1480346 Change-Id: If19b0aec5f89e031b886b7f1e2233c0a88d1b4b1 --- ceilometer/dispatcher/gnocchi_client.py | 9 ++++++--- ceilometer/tests/unit/dispatcher/test_gnocchi.py | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ceilometer/dispatcher/gnocchi_client.py b/ceilometer/dispatcher/gnocchi_client.py index f3335080..a298e4e2 100644 --- a/ceilometer/dispatcher/gnocchi_client.py +++ b/ceilometer/dispatcher/gnocchi_client.py @@ -19,6 +19,7 @@ import json from oslo_log import log import requests import retrying +from six.moves.urllib import parse as urlparse from ceilometer.i18n import _ from ceilometer import keystone_client @@ -104,7 +105,8 @@ class Client(object): measure_attributes): r = self._session.post("%s/v1/resource/%s/%s/metric/%s/measures" % (self._gnocchi_url, resource_type, - resource_id, metric_name), + urlparse.quote(resource_id, safe=""), + metric_name), headers=self._get_headers(), data=json.dumps(measure_attributes)) @@ -155,7 +157,8 @@ class Client(object): resource_extra): r = self._session.patch( "%s/v1/resource/%s/%s" - % (self._gnocchi_url, resource_type, resource_id), + % (self._gnocchi_url, resource_type, + urlparse.quote(resource_id, safe="")), headers=self._get_headers(), data=json.dumps(resource_extra)) @@ -175,7 +178,7 @@ class Client(object): params = {metric_name: archive_policy} r = self._session.post("%s/v1/resource/%s/%s/metric" % (self._gnocchi_url, resource_type, - resource_id), + urlparse.quote(resource_id, safe="")), headers=self._get_headers(), data=json.dumps(params)) if r.status_code == 409: diff --git a/ceilometer/tests/unit/dispatcher/test_gnocchi.py b/ceilometer/tests/unit/dispatcher/test_gnocchi.py index 7c10f168..22b68415 100644 --- a/ceilometer/tests/unit/dispatcher/test_gnocchi.py +++ b/ceilometer/tests/unit/dispatcher/test_gnocchi.py @@ -337,7 +337,7 @@ class DispatcherWorkflowTest(base.BaseTestCase, group="dispatcher_gnocchi" ) - self.sample['resource_id'] = str(uuid.uuid4()) + self.sample['resource_id'] = str(uuid.uuid4()) + "/foobar" @mock.patch('ceilometer.dispatcher.gnocchi.LOG') @mock.patch('ceilometer.dispatcher.gnocchi_client.LOG') @@ -348,7 +348,9 @@ class DispatcherWorkflowTest(base.BaseTestCase, base_url = self.dispatcher.conf.dispatcher_gnocchi.url url_params = { 'url': urlparse.urljoin(base_url, '/v1/resource'), - 'resource_id': self.sample['resource_id'], + # NOTE(sileht): we don't use urlparse.quote here + # to ensure / is converted in %2F + 'resource_id': self.sample['resource_id'].replace("/", "%2F"), 'resource_type': self.resource_type, 'metric_name': self.sample['counter_name'] }