Allow function 'yaql' as condition function

Allow 'yaql' in condition definition, like:

parameters:
  ServiceNames:
    type: comma_delimited_list
    default: ['neutron', 'heat']
conditions:
  contains_neutron:
    yaql:
      expression: $.data.service_names.contains('neutron')
      data:
        service_names:
          get_param: ServiceNames

Closes-Bug: #1693093

Change-Id: I83a540336c01a696780621fb2b39486a6abf0917
This commit is contained in:
huangtianhua 2017-05-24 11:33:58 +08:00
parent 8ce536f0e4
commit 77e5072621
3 changed files with 51 additions and 0 deletions

View File

@ -321,6 +321,7 @@ for the ``heat_template_version`` key:
yaql
if
We support 'yaql' as condition function in this version.
The complete list of supported condition functions is::
equals
@ -328,6 +329,7 @@ for the ``heat_template_version`` key:
not
and
or
yaql
.. _hot_spec_parameter_groups:
@ -934,10 +936,12 @@ expression
not
and
or
yaql
Note: In condition functions, you can reference a value from an input
parameter, but you cannot reference resource or its attribute. We support
referencing other conditions (by condition name) in condition functions.
We support 'yaql' as condition function in the Pike version.
An example of conditions section definition
@ -979,6 +983,12 @@ An example of conditions section definition
and:
- cd1
- cd2
cd9:
yaql:
expression: $.data.services.contains('heat')
data:
services:
get_param: ServiceNames
The example below shows how to associate condition with resources

View File

@ -607,3 +607,14 @@ class HOTemplate20170901(HOTemplate20170224):
'Fn::ResourceFacade': hot_funcs.Removed,
'Ref': hot_funcs.Removed,
}
condition_functions = {
'get_param': hot_funcs.GetParam,
'equals': hot_funcs.Equals,
'not': hot_funcs.Not,
'and': hot_funcs.And,
'or': hot_funcs.Or,
# functions added in 2017-09-01
'yaql': hot_funcs.Yaql
}

View File

@ -1299,6 +1299,36 @@ class HOTemplateTest(common.HeatTestCase):
self.assertEqual({'a': [1, 2, 3]}, resolved)
def test_yaql_as_condition(self):
hot_tpl = template_format.parse('''
heat_template_version: pike
parameters:
ServiceNames:
type: comma_delimited_list
default: ['neutron', 'heat']
''')
snippet = {
'yaql': {
'expression': '$.data.service_names.contains("neutron")',
'data': {'service_names': {'get_param': 'ServiceNames'}}}}
# when param 'ServiceNames' contains 'neutron',
# equals function resolve to true
tmpl = template.Template(hot_tpl)
stack = parser.Stack(utils.dummy_context(),
'test_condition_yaql_true', tmpl)
resolved = self.resolve_condition(snippet, tmpl, stack)
self.assertTrue(resolved)
# when param 'ServiceNames' doesn't contain 'neutron',
# equals function resolve to false
tmpl = template.Template(
hot_tpl,
env=environment.Environment(
{'ServiceNames': ['nova_network', 'heat']}))
stack = parser.Stack(utils.dummy_context(),
'test_condition_yaql_false', tmpl)
resolved = self.resolve_condition(snippet, tmpl, stack)
self.assertFalse(resolved)
def test_equals(self):
hot_tpl = template_format.parse('''
heat_template_version: 2016-10-14