Merge "Move recursive_keypairs into utils"

This commit is contained in:
Jenkins 2013-05-21 21:53:16 +00:00 committed by Gerrit Code Review
commit 0f57d7573c
4 changed files with 55 additions and 33 deletions

View File

@ -28,6 +28,7 @@ from oslo.config import cfg
from ceilometer.openstack.common import log
from ceilometer.openstack.common import rpc
from ceilometer import publisher
from ceilometer import utils
LOG = log.getLogger(__name__)
@ -55,28 +56,11 @@ def register_opts(config):
register_opts(cfg.CONF)
def recursive_keypairs(d):
"""Generator that produces sequence of keypairs for nested dictionaries.
"""
for name, value in sorted(d.iteritems()):
if isinstance(value, dict):
for subname, subvalue in recursive_keypairs(value):
yield ('%s:%s' % (name, subname), subvalue)
elif isinstance(value, (tuple, list)):
# When doing a pair of JSON encode/decode operations to the tuple,
# the tuple would become list. So we have to generate the value as
# list here.
yield name, list(map(lambda x: unicode(x).encode('utf-8'),
value))
else:
yield name, value
def compute_signature(message, secret):
"""Return the signature for a message dictionary.
"""
digest_maker = hmac.new(secret, '', hashlib.sha256)
for name, value in recursive_keypairs(message):
for name, value in utils.recursive_keypairs(message):
if name == 'message_signature':
# Skip any existing signature value, which would not have
# been part of the original message.

View File

@ -42,3 +42,20 @@ def read_cached_file(filename, cache_info, reload_func=None):
if reload_func:
reload_func(cache_info['data'])
return cache_info['data']
def recursive_keypairs(d):
"""Generator that produces sequence of keypairs for nested dictionaries.
"""
for name, value in sorted(d.iteritems()):
if isinstance(value, dict):
for subname, subvalue in recursive_keypairs(value):
yield ('%s:%s' % (name, subname), subvalue)
elif isinstance(value, (tuple, list)):
# When doing a pair of JSON encode/decode operations to the tuple,
# the tuple would become list. So we have to generate the value as
# list here.
yield name, list(map(lambda x: unicode(x).encode('utf-8'),
value))
else:
yield name, value

View File

@ -86,21 +86,6 @@ def test_verify_signature_incorrect():
assert not meter.verify_signature(data, 'not-so-secret')
def test_recursive_keypairs():
data = {'a': 'A',
'b': 'B',
'nested': {'a': 'A',
'b': 'B',
},
}
pairs = list(meter.recursive_keypairs(data))
assert pairs == [('a', 'A'),
('b', 'B'),
('nested:a', 'A'),
('nested:b', 'B'),
]
def test_verify_signature_nested():
data = {'a': 'A',
'b': 'B',

36
tests/test_utils.py Normal file
View File

@ -0,0 +1,36 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Tests for ceilometer/utils.py
"""
from ceilometer import utils
def test_recursive_keypairs():
data = {'a': 'A',
'b': 'B',
'nested': {'a': 'A',
'b': 'B',
},
}
pairs = list(utils.recursive_keypairs(data))
assert pairs == [('a', 'A'),
('b', 'B'),
('nested:a', 'A'),
('nested:b', 'B'),
]