From 89df904c50c483b307a8c6d116c970dab449d710 Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Thu, 23 Aug 2012 16:14:43 +0100 Subject: [PATCH] heat API : make extract_user_params more generic Make api.aws.utils.extract_user_params into a more generic aws-format-list-extraction function Change-Id: If6811ea1a138ddff96542f983397852dfbd483fd Signed-off-by: Steven Hardy --- heat/api/aws/utils.py | 36 ++++++++++++++++++------------------ heat/api/v1/stacks.py | 18 +++++++++++++++++- heat/tests/test_api_aws.py | 20 +++++++++++++++----- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/heat/api/aws/utils.py b/heat/api/aws/utils.py index e3b6cd3a3a..afce0a942b 100644 --- a/heat/api/aws/utils.py +++ b/heat/api/aws/utils.py @@ -27,41 +27,41 @@ def format_response(action, response): return {'%sResponse' % action: {'%sResult' % action: response}} -def extract_user_params(params): +def extract_param_pairs(params, prefix='', keyname='', valuename=''): """ - Extract a dictionary of user input parameters for the stack + Extract a dictionary of user input parameters, from AWS style + parameter-pair encoded list - In the AWS API parameters, each user parameter appears as two key-value - pairs with keys of the form below: + In the AWS API list items appear as two key-value + pairs (passed as query parameters) with keys of the form below: - Parameters.member.1.ParameterKey - Parameters.member.1.ParameterValue + Prefix.member.1.keyname=somekey + Prefix.member.1.keyvalue=somevalue + Prefix.member.2.keyname=anotherkey + Prefix.member.2.keyvalue=somevalue - We reformat this into a normal dict here to match the heat + We reformat this into a dict here to match the heat engine API expected format - - Note this implemented outside of "create" as it will also be - used by update (and EstimateTemplateCost if appropriate..) """ # Define the AWS key format to extract - PARAM_KEYS = ( - PARAM_USER_KEY_re, - PARAM_USER_VALUE_fmt, + LIST_KEYS = ( + LIST_USER_KEY_re, + LIST_USER_VALUE_fmt, ) = ( - re.compile(r'Parameters\.member\.(.*?)\.ParameterKey$'), - 'Parameters.member.%s.ParameterValue', + re.compile(r"%s\.member\.(.*?)\.%s$" % (prefix, keyname)), + '.'.join([prefix, 'member', '%s', valuename]) ) def get_param_pairs(): for k in params: - keymatch = PARAM_USER_KEY_re.match(k) + keymatch = LIST_USER_KEY_re.match(k) if keymatch: key = params[k] - v = PARAM_USER_VALUE_fmt % keymatch.group(1) + v = LIST_USER_VALUE_fmt % keymatch.group(1) try: value = params[v] except KeyError: - logger.error('Could not apply parameter %s' % key) + logger.error('Could not extract parameter %s' % key) yield (key, value) diff --git a/heat/api/v1/stacks.py b/heat/api/v1/stacks.py index d0b06b2152..9a2e144124 100644 --- a/heat/api/v1/stacks.py +++ b/heat/api/v1/stacks.py @@ -62,6 +62,22 @@ class StackController(object): str(resp['StackId'])]) return resp + @staticmethod + def _extract_user_params(params): + """ + Extract a dictionary of user input parameters for the stack + + In the AWS API parameters, each user parameter appears as two key-value + pairs with keys of the form below: + + Parameters.member.1.ParameterKey + Parameters.member.1.ParameterValue + """ + return api_utils.extract_param_pairs(params, + prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') + def list(self, req): """ Implements ListStacks API action @@ -253,7 +269,7 @@ class StackController(object): con = req.context # Extract the stack input parameters - stack_parms = api_utils.extract_user_params(req.params) + stack_parms = self._extract_user_params(req.params) # Extract any additional arguments ("Request Parameters") create_args = extract_args(req.params) diff --git a/heat/tests/test_api_aws.py b/heat/tests/test_api_aws.py index c23407c349..40c8f085cd 100644 --- a/heat/tests/test_api_aws.py +++ b/heat/tests/test_api_aws.py @@ -41,7 +41,9 @@ class AWSCommon(unittest.TestCase): 'Parameters.member.Foo.ParameterValue': 'bar', 'Parameters.member.Blarg.ParameterKey': 'blarg', 'Parameters.member.Blarg.ParameterValue': 'wibble'} - params = api_utils.extract_user_params(p) + params = api_utils.extract_param_pairs(p, prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') self.assertEqual(len(params), 2) self.assertTrue('foo' in params) self.assertEqual(params['foo'], 'bar') @@ -53,7 +55,9 @@ class AWSCommon(unittest.TestCase): 'Parameters.member.Foo.Bar.ParameterValue': 'bar', 'Parameters.member.Foo.Baz.ParameterKey': 'blarg', 'Parameters.member.Foo.Baz.ParameterValue': 'wibble'} - params = api_utils.extract_user_params(p) + params = api_utils.extract_param_pairs(p, prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') self.assertEqual(len(params), 2) self.assertTrue('foo' in params) self.assertEqual(params['foo'], 'bar') @@ -65,7 +69,9 @@ class AWSCommon(unittest.TestCase): 'Parameters.member.Foo.Bar.ParameterValue': 'bar', 'Foo.Baz.ParameterKey': 'blarg', 'Foo.Baz.ParameterValue': 'wibble'} - params = api_utils.extract_user_params(p) + params = api_utils.extract_param_pairs(p, prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') self.assertEqual(len(params), 1) self.assertTrue('foo' in params) self.assertEqual(params['foo'], 'bar') @@ -73,13 +79,17 @@ class AWSCommon(unittest.TestCase): def test_params_extract_garbage_prefix(self): p = {'prefixParameters.member.Foo.Bar.ParameterKey': 'foo', 'Parameters.member.Foo.Bar.ParameterValue': 'bar'} - params = api_utils.extract_user_params(p) + params = api_utils.extract_param_pairs(p, prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') self.assertFalse(params) def test_params_extract_garbage_suffix(self): p = {'Parameters.member.Foo.Bar.ParameterKeysuffix': 'foo', 'Parameters.member.Foo.Bar.ParameterValue': 'bar'} - params = api_utils.extract_user_params(p) + params = api_utils.extract_param_pairs(p, prefix='Parameters', + keyname='ParameterKey', + valuename='ParameterValue') self.assertFalse(params) def test_reformat_dict_keys(self):