From 6984a7793883164af0e28c3e707048e989670e9d Mon Sep 17 00:00:00 2001 From: Jason Dunsmore Date: Wed, 7 May 2014 15:40:53 -0500 Subject: [PATCH] Add Boolean parameter type Add a boolean parameter type to allow passing in boolean values via parameters. blueprint boolean-parameter-type Change-Id: I693d0aa5bdc54bb57b8363633f1b41c59ccae7d3 --- doc/source/template_guide/hot_spec.rst | 4 ++-- heat/engine/hot/parameters.py | 4 ++-- heat/engine/parameters.py | 21 +++++++++++++++++++-- heat/tests/test_parameters.py | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/doc/source/template_guide/hot_spec.rst b/doc/source/template_guide/hot_spec.rst index 4a7b1c8036..d92aafa6f7 100644 --- a/doc/source/template_guide/hot_spec.rst +++ b/doc/source/template_guide/hot_spec.rst @@ -149,7 +149,7 @@ default value defined as nested elements. parameters: : - type: + type: label: description: default: @@ -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 diff --git a/heat/engine/hot/parameters.py b/heat/engine/hot/parameters.py index e7bf5a5b90..35dfd6ffa4 100644 --- a/heat/engine/hot/parameters.py +++ b/heat/engine/hot/parameters.py @@ -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 diff --git a/heat/engine/parameters.py b/heat/engine/parameters.py index fa0de3c6ce..dd7d28611a 100644 --- a/heat/engine/parameters.py +++ b/heat/engine/parameters.py @@ -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".''' diff --git a/heat/tests/test_parameters.py b/heat/tests/test_parameters.py index 5f4e1b41ea..b6dfc98ec3 100644 --- a/heat/tests/test_parameters.py +++ b/heat/tests/test_parameters.py @@ -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,