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
This commit is contained in:
Mehdi Abaakouk 2015-08-25 11:00:13 +02:00
parent fcf5b4e7e2
commit 33355eaf68
2 changed files with 10 additions and 5 deletions

View File

@ -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:

View File

@ -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']
}