From: Steve Baker Date: Wed, 20 Mar 2013 21:42:28 +0000 (+1300) Subject: Tolerate missing keys in reformat_dict_keys X-Git-Tag: 2014.1~745^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=a924df95b53de114898e185d70ff2d98ae3b7a87;p=openstack-build%2Fheat-build.git Tolerate missing keys in reformat_dict_keys For validate, some keys are optional but still need mapping if they are there. Helps bug #1157537 Change-Id: Ib29cd99979a84892a98117c607651ea4c352e3d1 --- diff --git a/heat/api/aws/utils.py b/heat/api/aws/utils.py index dd43e793..9b3a6ecb 100644 --- a/heat/api/aws/utils.py +++ b/heat/api/aws/utils.py @@ -111,4 +111,5 @@ def reformat_dict_keys(keymap={}, inputdict={}): ''' Utility function for mapping one dict format to another ''' - return dict([(outk, inputdict[ink]) for ink, outk in keymap.items()]) + return dict([(outk, inputdict[ink]) for ink, outk in keymap.items() + if ink in inputdict]) diff --git a/heat/tests/test_api_aws.py b/heat/tests/test_api_aws.py index 920454b7..507b4dcb 100644 --- a/heat/tests/test_api_aws.py +++ b/heat/tests/test_api_aws.py @@ -181,6 +181,13 @@ class AWSCommon(unittest.TestCase): result = api_utils.reformat_dict_keys(keymap, data) self.assertEqual(result, expected) + def test_reformat_dict_keys_missing(self): + keymap = {"foo": "bar", "foo2": "bar2"} + data = {"foo": 123} + expected = {"bar": 123} + result = api_utils.reformat_dict_keys(keymap, data) + self.assertEqual(result, expected) + def setUp(self): print "setup complete"