From fb32c3b19c283d9d4cf78af35d33bd12f16452cf Mon Sep 17 00:00:00 2001 From: Nadya Privalova Date: Sun, 19 Jan 2014 12:45:56 +0400 Subject: [PATCH] Fix recursive_keypairs output recursive_keypairs has 'separator' argument. This method is recursive and a problem is that 'separator' argument is applied only in first iteration of the method. Besides, this method is used in flatten_metadata in API implementation. That's why it is needed not to change output from recursive_keypairs in flatten_metadata. So the path has a goal to fix incorrect recursive_keypairs impementation but not to change output of flatten_matadata in API Fixes bug 1268628 Change-Id: Ic9829342c47325c1df7b81dc4148c08a13e0d067 --- ceilometer/api/controllers/v2.py | 8 +++++++- ceilometer/utils.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index e6d622a82..8de57438f 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -510,7 +510,13 @@ def _flatten_metadata(metadata): to unicode strings. """ if metadata: - return dict((k, unicode(v)) + # After changing recursive_keypairs` output we need to keep + # flattening output unchanged. + # Example: recursive_keypairs({'a': {'b':{'c':'d'}}}, '.') + # output before: a.b:c=d + # output now: a.b.c=d + # So to keep the first variant just replace all dots except the first + return dict((k.replace('.', ':').replace(':', '.', 1), unicode(v)) for k, v in utils.recursive_keypairs(metadata, separator='.') if type(v) is not set) diff --git a/ceilometer/utils.py b/ceilometer/utils.py index c185e00a4..acfc9e0d3 100644 --- a/ceilometer/utils.py +++ b/ceilometer/utils.py @@ -30,7 +30,7 @@ def recursive_keypairs(d, separator=':'): """ for name, value in sorted(d.iteritems()): if isinstance(value, dict): - for subname, subvalue in recursive_keypairs(value): + for subname, subvalue in recursive_keypairs(value, separator): yield ('%s%s%s' % (name, separator, subname), subvalue) elif isinstance(value, (tuple, list)): # When doing a pair of JSON encode/decode operations to the tuple,