Merge "Allow customized reseller_prefix in Ceilometer middleware for Swift"

This commit is contained in:
Jenkins 2014-01-10 06:47:00 +00:00 committed by Gerrit Code Review
commit 7ebb8d576b
2 changed files with 37 additions and 3 deletions

View File

@ -33,6 +33,9 @@ use = egg:ceilometer#swift
# Some optional configuration
# this allow to publish additional metadata
metadata_headers = X-TEST
# Set reseller prefix (defaults to "AUTH_" if not set)
reseller_prefix = AUTH_
"""
from __future__ import absolute_import
@ -82,6 +85,9 @@ class CeilometerMiddleware(object):
'ceilometer.transformer',
),
)
self.reseller_prefix = conf.get('reseller_prefix', 'AUTH_')
if self.reseller_prefix and self.reseller_prefix[-1] != '_':
self.reseller_prefix += '_'
def __call__(self, env, start_response):
start_response_args = [None]
@ -147,7 +153,7 @@ class CeilometerMiddleware(object):
volume=bytes_received,
user_id=env.get('HTTP_X_USER_ID'),
project_id=env.get('HTTP_X_TENANT_ID'),
resource_id=account.partition('AUTH_')[2],
resource_id=account.partition(self.reseller_prefix)[2],
timestamp=now,
resource_metadata=resource_metadata)])
@ -159,7 +165,7 @@ class CeilometerMiddleware(object):
volume=bytes_sent,
user_id=env.get('HTTP_X_USER_ID'),
project_id=env.get('HTTP_X_TENANT_ID'),
resource_id=account.partition('AUTH_')[2],
resource_id=account.partition(self.reseller_prefix)[2],
timestamp=now,
resource_metadata=resource_metadata)])
@ -173,7 +179,7 @@ class CeilometerMiddleware(object):
volume=1,
user_id=env.get('HTTP_X_USER_ID'),
project_id=env.get('HTTP_X_TENANT_ID'),
resource_id=account.partition('AUTH_')[2],
resource_id=account.partition(self.reseller_prefix)[2],
timestamp=now,
resource_metadata=resource_metadata)])

View File

@ -270,3 +270,31 @@ class TestSwiftMiddleware(test.BaseTestCase):
self.assertEqual(len(samples), 0)
self.assertEqual(resp, ["test"])
mocked_publish_sample.assert_called_once_with(mock.ANY, 0, 4)
def test_reseller_prefix(self):
# No reseller prefix set: ensure middleware uses AUTH_
app = swift_middleware.CeilometerMiddleware(FakeApp(), {})
req = webob.Request.blank('/1.0/AUTH_account/container/obj',
environ={'REQUEST_METHOD': 'GET'})
list(app(req.environ, self.start_response))
samples = self.pipeline_manager.pipelines[0].samples[0]
self.assertEqual(samples.resource_id, "account")
# Custom reseller prefix set
app = swift_middleware.CeilometerMiddleware(
FakeApp(), {'reseller_prefix': 'CUSTOM_'})
req = webob.Request.blank('/1.0/CUSTOM_account/container/obj',
environ={'REQUEST_METHOD': 'GET'})
list(app(req.environ, self.start_response))
samples = self.pipeline_manager.pipelines[0].samples[0]
self.assertEqual(samples.resource_id, "account")
def test_invalid_reseller_prefix(self):
# Custom reseller prefix set, but without trailing underscore
app = swift_middleware.CeilometerMiddleware(
FakeApp(), {'reseller_prefix': 'CUSTOM'})
req = webob.Request.blank('/1.0/CUSTOM_account/container/obj',
environ={'REQUEST_METHOD': 'GET'})
list(app(req.environ, self.start_response))
samples = self.pipeline_manager.pipelines[0].samples[0]
self.assertEqual(samples.resource_id, "account")