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 <shardy@redhat.com>
This commit is contained in:
Steven Hardy 2012-08-23 16:14:43 +01:00
parent 00527198c8
commit 89df904c50
3 changed files with 50 additions and 24 deletions

View File

@ -27,41 +27,41 @@ def format_response(action, response):
return {'%sResponse' % action: {'%sResult' % 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 In the AWS API list items appear as two key-value
pairs with keys of the form below: pairs (passed as query parameters) with keys of the form below:
Parameters.member.1.ParameterKey Prefix.member.1.keyname=somekey
Parameters.member.1.ParameterValue 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 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 # Define the AWS key format to extract
PARAM_KEYS = ( LIST_KEYS = (
PARAM_USER_KEY_re, LIST_USER_KEY_re,
PARAM_USER_VALUE_fmt, LIST_USER_VALUE_fmt,
) = ( ) = (
re.compile(r'Parameters\.member\.(.*?)\.ParameterKey$'), re.compile(r"%s\.member\.(.*?)\.%s$" % (prefix, keyname)),
'Parameters.member.%s.ParameterValue', '.'.join([prefix, 'member', '%s', valuename])
) )
def get_param_pairs(): def get_param_pairs():
for k in params: for k in params:
keymatch = PARAM_USER_KEY_re.match(k) keymatch = LIST_USER_KEY_re.match(k)
if keymatch: if keymatch:
key = params[k] key = params[k]
v = PARAM_USER_VALUE_fmt % keymatch.group(1) v = LIST_USER_VALUE_fmt % keymatch.group(1)
try: try:
value = params[v] value = params[v]
except KeyError: except KeyError:
logger.error('Could not apply parameter %s' % key) logger.error('Could not extract parameter %s' % key)
yield (key, value) yield (key, value)

View File

@ -62,6 +62,22 @@ class StackController(object):
str(resp['StackId'])]) str(resp['StackId'])])
return resp 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): def list(self, req):
""" """
Implements ListStacks API action Implements ListStacks API action
@ -253,7 +269,7 @@ class StackController(object):
con = req.context con = req.context
# Extract the stack input parameters # 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") # Extract any additional arguments ("Request Parameters")
create_args = extract_args(req.params) create_args = extract_args(req.params)

View File

@ -41,7 +41,9 @@ class AWSCommon(unittest.TestCase):
'Parameters.member.Foo.ParameterValue': 'bar', 'Parameters.member.Foo.ParameterValue': 'bar',
'Parameters.member.Blarg.ParameterKey': 'blarg', 'Parameters.member.Blarg.ParameterKey': 'blarg',
'Parameters.member.Blarg.ParameterValue': 'wibble'} '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.assertEqual(len(params), 2)
self.assertTrue('foo' in params) self.assertTrue('foo' in params)
self.assertEqual(params['foo'], 'bar') self.assertEqual(params['foo'], 'bar')
@ -53,7 +55,9 @@ class AWSCommon(unittest.TestCase):
'Parameters.member.Foo.Bar.ParameterValue': 'bar', 'Parameters.member.Foo.Bar.ParameterValue': 'bar',
'Parameters.member.Foo.Baz.ParameterKey': 'blarg', 'Parameters.member.Foo.Baz.ParameterKey': 'blarg',
'Parameters.member.Foo.Baz.ParameterValue': 'wibble'} '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.assertEqual(len(params), 2)
self.assertTrue('foo' in params) self.assertTrue('foo' in params)
self.assertEqual(params['foo'], 'bar') self.assertEqual(params['foo'], 'bar')
@ -65,7 +69,9 @@ class AWSCommon(unittest.TestCase):
'Parameters.member.Foo.Bar.ParameterValue': 'bar', 'Parameters.member.Foo.Bar.ParameterValue': 'bar',
'Foo.Baz.ParameterKey': 'blarg', 'Foo.Baz.ParameterKey': 'blarg',
'Foo.Baz.ParameterValue': 'wibble'} '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.assertEqual(len(params), 1)
self.assertTrue('foo' in params) self.assertTrue('foo' in params)
self.assertEqual(params['foo'], 'bar') self.assertEqual(params['foo'], 'bar')
@ -73,13 +79,17 @@ class AWSCommon(unittest.TestCase):
def test_params_extract_garbage_prefix(self): def test_params_extract_garbage_prefix(self):
p = {'prefixParameters.member.Foo.Bar.ParameterKey': 'foo', p = {'prefixParameters.member.Foo.Bar.ParameterKey': 'foo',
'Parameters.member.Foo.Bar.ParameterValue': 'bar'} '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) self.assertFalse(params)
def test_params_extract_garbage_suffix(self): def test_params_extract_garbage_suffix(self):
p = {'Parameters.member.Foo.Bar.ParameterKeysuffix': 'foo', p = {'Parameters.member.Foo.Bar.ParameterKeysuffix': 'foo',
'Parameters.member.Foo.Bar.ParameterValue': 'bar'} '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) self.assertFalse(params)
def test_reformat_dict_keys(self): def test_reformat_dict_keys(self):