Merge "Add Boolean parameter type"

This commit is contained in:
Jenkins 2014-05-29 01:39:32 +00:00 committed by Gerrit Code Review
commit ae02823ceb
4 changed files with 41 additions and 6 deletions

View File

@ -149,7 +149,7 @@ default value defined as nested elements.
parameters:
<param name>:
type: <string | number | json | comma_delimited_list>
type: <string | number | json | comma_delimited_list | boolean>
label: <human-readable name of the parameter>
description: <description of the parameter>
default: <default value for parameter>
@ -162,7 +162,7 @@ param name
type
This attribute specifies the type of parameter. Currently supported types
are *string*, *number*, *comma_delimited_list* or *json*.
are *string*, *number*, *comma_delimited_list*, *json*, or *boolean*.
label
This *optional* attribute allows for giving a human readable name of the

View File

@ -39,9 +39,9 @@ class HOTParamSchema(parameters.Schema):
# For Parameters the type name for Schema.LIST is comma_delimited_list
# and the type name for Schema.MAP is json
TYPES = (
STRING, NUMBER, LIST, MAP,
STRING, NUMBER, LIST, MAP, BOOLEAN,
) = (
'string', 'number', 'comma_delimited_list', 'json',
'string', 'number', 'comma_delimited_list', 'json', 'boolean',
)
PARAMETER_KEYS = KEYS

View File

@ -19,6 +19,7 @@ import six
from heat.common import exception
from heat.engine import constraints as constr
from heat.openstack.common import strutils
PARAMETER_KEYS = (
@ -47,9 +48,9 @@ class Schema(constr.Schema):
# For Parameters the type name for Schema.LIST is CommaDelimitedList
# and the type name for Schema.MAP is Json
TYPES = (
STRING, NUMBER, LIST, MAP,
STRING, NUMBER, LIST, MAP, BOOLEAN,
) = (
'String', 'Number', 'CommaDelimitedList', 'Json',
'String', 'Number', 'CommaDelimitedList', 'Json', 'Boolean',
)
def __init__(self, data_type, description=None, default=None, schema=None,
@ -189,6 +190,8 @@ class Parameter(object):
ParamClass = CommaDelimitedListParam
elif schema.type == schema.MAP:
ParamClass = JsonParam
elif schema.type == schema.BOOLEAN:
ParamClass = BooleanParam
else:
raise ValueError(_('Invalid Parameter type "%s"') % schema.type)
@ -281,6 +284,20 @@ class NumberParam(Parameter):
return Schema.str_to_num(super(NumberParam, self).value())
class BooleanParam(Parameter):
'''A template parameter of type "Boolean".'''
def _validate(self, val, context):
self.schema.validate_value(self.name, val, context)
def value(self):
if self.user_value is not None:
raw_value = self.user_value
else:
raw_value = self.default()
return strutils.bool_from_string(str(raw_value), strict=True)
class StringParam(Parameter):
'''A template parameter of type "String".'''

View File

@ -298,6 +298,24 @@ class ParameterTest(testtools.TestCase):
self.new_parameter, 'p', schema, val)
self.assertIn('out of range', str(err))
def test_bool_value_true(self):
schema = {'Type': 'Boolean'}
for val in ('1', 't', 'true', 'on', 'y', 'yes', True, 1):
bo = self.new_parameter('bo', schema, val)
self.assertEqual(True, bo.value())
def test_bool_value_false(self):
schema = {'Type': 'Boolean'}
for val in ('0', 'f', 'false', 'off', 'n', 'no', False, 0):
bo = self.new_parameter('bo', schema, val)
self.assertEqual(False, bo.value())
def test_bool_value_invalid(self):
schema = {'Type': 'Boolean'}
bo = self.new_parameter('bo', schema, 'foo')
err = self.assertRaises(ValueError, bo.value)
self.assertIn("Unrecognized value 'foo'", unicode(err))
def test_missing_param(self):
'''Test missing user parameter.'''
self.assertRaises(exception.UserParameterMissing,