def test_process_attributes_no_value(self):

        def get_node_instances(node_id=None):
            return [get_node_instance(node_id)]

        def get_node_instance(node_instance_id):
            return NodeInstance({
                'id': node_instance_id,
                'node_id': 'webserver',
                'runtime_properties': {}
            })

        def get_node(node_id):
            return Node({
                'id': node_id,
            })

        payload = {
            'a': {'get_attribute': ['node', 'a']},
        }

        functions.evaluate_functions(payload,
                                     {},
                                     get_node_instances,
                                     get_node_instance,
                                     get_node,
                                     None)

        self.assertIsNone(payload['a'])
    def test_evaluate_functions(self):

        payload = {
            'a': {'get_secret': 'id_a'},
            'b': {'get_secret': 'id_b'},
            'c': {'get_secret': 'id_c'},
            'd': {'get_secret': 'id_d'},
            'f': {'concat': [
                {'get_secret': 'id_a'},
                {'get_secret': 'id_b'},
                {'get_secret': 'id_c'},
                {'get_secret': 'id_d'}
            ]}
        }

        functions.evaluate_functions(payload,
                                     {},
                                     None,
                                     None,
                                     None,
                                     self._get_secret_mock)

        self.assertEqual(payload['a'], 'id_a_value')
        self.assertEqual(payload['b'], 'id_b_value')
        self.assertEqual(payload['c'], 'id_c_value')
        self.assertEqual(payload['d'], 'id_d_value')
        self.assertEqual(payload['f'], 'id_a_valueid_b_value'
                                       'id_c_valueid_d_value')
    def test_node_template_properties_simple(self):
        yaml = """
node_types:
    type:
        properties:
            property: {}
node_templates:
    node:
        type: type
        properties:
            property: { get_secret: secret }
"""
        parsed = prepare_deployment_plan(self.parse_1_3(yaml),
                                         self._get_secret_mock)
        node = self.get_node_by_name(parsed, 'node')
        self.assertEqual({'get_secret': 'secret'},
                         node['properties']['property'])

        functions.evaluate_functions(
            parsed,
            {},
            None,
            None,
            None,
            self._get_secret_mock
        )
        self.assertEqual(node['properties']['property'], 'secret_value')
    def test_evaluate_functions(self):

        def get_node_instances(node_id=None):
            return [get_node_instance(node_id)]

        def get_node_instance(node_instance_id):
            result = NodeInstance({
                'id': node_instance_id,
                'node_id': 'webserver',
                'runtime_properties': {
                }
            })
            if node_instance_id == 'node1':
                result.runtime_properties['a'] = 'a_val'
            elif node_instance_id == 'node2':
                result.runtime_properties['b'] = 'b_val'
            elif node_instance_id == 'node3':
                result.runtime_properties['c'] = 'c_val'
            elif node_instance_id == 'node4':
                result.runtime_properties['d'] = 'd_val'
            return result

        def get_node(node_id):
            return Node({
                'id': node_id,
            })

        payload = {
            'a': {'get_attribute': ['SELF', 'a']},
            'b': {'get_attribute': ['node2', 'b']},
            'c': {'get_attribute': ['SOURCE', 'c']},
            'd': {'get_attribute': ['TARGET', 'd']},
            'f': {'concat': [
                {'get_attribute': ['SELF', 'a']},
                {'get_attribute': ['node2', 'b']},
                {'get_attribute': ['SOURCE', 'c']},
                {'get_attribute': ['TARGET', 'd']}
            ]}
        }

        context = {
            'self': 'node1',
            'source': 'node3',
            'target': 'node4'
        }

        functions.evaluate_functions(payload,
                                     context,
                                     get_node_instances,
                                     get_node_instance,
                                     get_node,
                                     None)

        self.assertEqual(payload['a'], 'a_val')
        self.assertEqual(payload['b'], 'b_val')
        self.assertEqual(payload['c'], 'c_val')
        self.assertEqual(payload['d'], 'd_val')
        self.assertEqual(payload['f'], 'a_valb_valc_vald_val')
 def test_no_instances(self):
     def get_node_instances(node_id):
         return []
     payload = {'a': {'get_attribute': ['node', 'a']}}
     with testtools.testcase.ExpectedException(
             exceptions.FunctionEvaluationError,
             '.*does not exist.*'):
         functions.evaluate_functions(payload, {}, get_node_instances, None,
                                      None, None)
 def evaluate():
     payload = {'a': {'get_attribute': ['node2', 'key']}}
     context = {'self': 'node1_1'}
     functions.evaluate_functions(payload,
                                  context,
                                  get_node_instances,
                                  get_node_instance,
                                  get_node)
     return payload
 def evaluate_functions(self, payload, context):
     return dsl_functions.evaluate_functions(
         payload=payload,
         context=context,
         get_node_instances_method=self.storage.get_node_instances,
         get_node_instance_method=self.storage.get_node_instance,
         get_node_method=self.storage.get_node)
Esempio n. 8
0
def parse_parameters(loader, parameters, args):

    def env(func_args):
        if not isinstance(func_args, list):
            func_args = [func_args]
        return os.environ.get(*func_args)

    def func(func_args):
        kwargs = func_args.get('kwargs', {})
        for value in kwargs.values():
            if dsl_functions.parse(value) != value:
                return _RAW
        kwargs['loader'] = loader
        function = module.load_attribute(func_args['name'])
        return function(**kwargs)

    functions = {
        'env': env,
        'arg': lambda func_args: args[func_args],
        'user_config': lambda func_args: getattr(loader.user_config,
                                                 func_args),
        'loader': lambda func_args: getattr(loader, func_args),
        'func': func
    }
    for name, process in functions.items():
        dsl_functions.register(_function(process), name)
    try:
        return dsl_functions.evaluate_functions(parameters,
                                                None, None, None, None)
    finally:
        for name in functions.keys():
            dsl_functions.unregister(name)
    def test_process_attributes_properties_fallback(self):

        def get_node_instances(node_id=None):
            return [get_node_instance(node_id)]

        def get_node_instance(node_instance_id):
            return NodeInstance({
                'id': node_instance_id,
                'node_id': 'webserver',
                'runtime_properties': {}
            })

        def get_node(node_id):
            return Node({
                'id': node_id,
                'properties': {
                    'a': 'a_val',
                    'b': 'b_val',
                    'c': 'c_val',
                    'd': 'd_val',
                }
            })

        payload = {
            'a': {'get_attribute': ['SELF', 'a']},
            'b': {'get_attribute': ['node', 'b']},
            'c': {'get_attribute': ['SOURCE', 'c']},
            'd': {'get_attribute': ['TARGET', 'd']},
        }

        context = {
            'self': 'node',
            'source': 'node',
            'target': 'node'
        }

        functions.evaluate_functions(payload,
                                     context,
                                     get_node_instances,
                                     get_node_instance,
                                     get_node,
                                     None)

        self.assertEqual(payload['a'], 'a_val')
        self.assertEqual(payload['b'], 'b_val')
        self.assertEqual(payload['c'], 'c_val')
        self.assertEqual(payload['d'], 'd_val')
    def test_too_many_instances(self):
        instances = [NodeInstance({'id': '1'}), NodeInstance({'id': '2'})]

        def get_node_instances(node_id):
            return instances

        def get_node_instance(node_instance_id):
            return instances[0]

        payload = {'a': {'get_attribute': ['node', 'a']}}
        with testtools.testcase.ExpectedException(
                exceptions.FunctionEvaluationError,
                '.*unambiguously.*'):
            functions.evaluate_functions(payload, {},
                                         get_node_instances,
                                         get_node_instance,
                                         None)
def evaluate_intrinsic_functions(payload, deployment_id, context=None):
    context = context or {}
    sm = get_storage_manager()
    sm.get(Deployment, deployment_id, include=['id'])
    methods = _get_methods(deployment_id, sm)

    try:
        return functions.evaluate_functions(
            payload=payload,
            context=context,
            **methods)
    except parser_exceptions.FunctionEvaluationError, e:
        raise FunctionsEvaluationError(str(e))
Esempio n. 12
0
    def test_process_attribute_relationship_ambiguity_resolution(self):

        node_instances = [{
            'id':
            'node1_1',
            'node_id':
            'node1',
            'relationships': [{
                'target_name': 'node2',
                'target_id': 'node2_1'
            }]
        }, {
            'id': 'node2_1',
            'node_id': 'node2',
            'runtime_properties': {
                'key': 'value1'
            }
        }, {
            'id': 'node2_2',
            'node_id': 'node2',
            'runtime_properties': {
                'key': 'value2'
            }
        }]

        nodes = [{'id': 'node1'}, {'id': 'node2'}]
        storage = self._mock_evaluation_storage(node_instances=node_instances,
                                                nodes=nodes)

        payload = {'a': {'get_attribute': ['node2', 'key']}}
        context = {'self': 'node1_1'}

        result = functions.evaluate_functions(payload.copy(), context, storage)
        self.assertEqual(result['a'], 'value1')

        # sanity
        node_instances[0]['relationships'] = []
        storage = self._mock_evaluation_storage(node_instances=node_instances,
                                                nodes=nodes)
        self.assertRaises(exceptions.FunctionEvaluationError,
                          functions.evaluate_functions, payload.copy(),
                          context, storage)
    def evaluate_functions(self, deployment_id, context, payload):
        self.get_deployment(deployment_id, include=['id'])

        def get_node_instances(node_id=None):
            return self.sm.get_node_instances(deployment_id, node_id)

        def get_node_instance(node_instance_id):
            return self.sm.get_node_instance(node_instance_id)

        def get_node(node_id):
            return self.sm.get_node(deployment_id, node_id)

        try:
            return functions.evaluate_functions(
                payload=payload,
                context=context,
                get_node_instances_method=get_node_instances,
                get_node_instance_method=get_node_instance,
                get_node_method=get_node)
        except parser_exceptions.FunctionEvaluationError, e:
            raise manager_exceptions.FunctionsEvaluationError(str(e))
    def evaluate_functions(self, deployment_id, context, payload):
        self.get_deployment(deployment_id, include=['id'])

        def get_node_instances(node_id=None):
            filters = self.create_filters_dict(deployment_id=deployment_id,
                                               node_id=node_id)
            return self.sm.get_node_instances(filters=filters).items

        def get_node_instance(node_instance_id):
            return self.sm.get_node_instance(node_instance_id)

        def get_node(node_id):
            return self.sm.get_node(deployment_id, node_id)

        try:
            return functions.evaluate_functions(
                payload=payload,
                context=context,
                get_node_instances_method=get_node_instances,
                get_node_instance_method=get_node_instance,
                get_node_method=get_node)
        except parser_exceptions.FunctionEvaluationError, e:
            raise manager_exceptions.FunctionsEvaluationError(str(e))
Esempio n. 15
0
    def test_get_attributes_dict_no_node_instances(self):
        node_instances = [
            {
                'id': 'badnode_0hn011',
                'node_id': 'badnode',
                'runtime_properties': {
                    'sing': 'forget'
                },
            },
        ]
        nodes = [
            {
                'id': 'badnode'
            },
            {
                'id': 'goodnode'
            },
        ]
        storage = self.mock_evaluation_storage(node_instances, nodes)
        payload = {'a': {'get_attributes_dict': ['goodnode', 'sing', 'eat']}}
        result = functions.evaluate_functions(payload, {}, storage)

        attributes_list = result['a']
        assert attributes_list == {}
Esempio n. 16
0
 def test_no_instances(self):
     payload = {'a': {'get_attribute': ['node', 'a']}}
     with testtools.testcase.ExpectedException(
             exceptions.FunctionEvaluationError, '.*has no instances.*'):
         functions.evaluate_functions(payload, {},
                                      self.mock_evaluation_storage())
Esempio n. 17
0
 def test_missing_source_ref(self):
     payload = {'a': {'get_attribute': ['SOURCE', 'a']}}
     with testtools.testcase.ExpectedException(
             exceptions.FunctionEvaluationError, '.*SOURCE is missing.*'):
         functions.evaluate_functions(payload, {},
                                      self.mock_evaluation_storage())
Esempio n. 18
0
    def _test_process_attribute_scaling_group_ambiguity_resolution(
            self, context, index):
        node_instances = [{
            'id': 'node1_1',
            'node_id': 'node1',
            'scaling_groups': [{
                'name': 'g1',
                'id': 'g1_1'
            }]
        }, {
            'id':
            'node2_1',
            'node_id':
            'node2',
            'scaling_groups': [{
                'name': 'g2',
                'id': 'g2_1'
            }],
            'relationships': [{
                'target_name': 'node1',
                'target_id': 'node1_1'
            }]
        }, {
            'id':
            'node3_1',
            'node_id':
            'node3',
            'scaling_groups': [{
                'name': 'g3',
                'id': 'g3_1'
            }],
            'relationships': [{
                'target_name': 'node2',
                'target_id': 'node2_1'
            }]
        }, {
            'id': 'node4_1',
            'node_id': 'node4',
            'scaling_groups': [{
                'name': 'g1',
                'id': 'g1_1'
            }]
        }, {
            'id':
            'node5_1',
            'node_id':
            'node5',
            'scaling_groups': [{
                'name': 'g5',
                'id': 'g5_1'
            }],
            'relationships': [{
                'target_name': 'node4',
                'target_id': 'node4_1'
            }]
        }, {
            'id':
            'node6_1',
            'node_id':
            'node6',
            'scaling_groups': [{
                'name': 'g6',
                'id': 'g6_1'
            }],
            'relationships': [{
                'target_name': 'node5',
                'target_id': 'node5_1'
            }],
            'runtime_properties': {
                'key': 'value6_1'
            }
        }, {
            'id': 'node1_2',
            'node_id': 'node1',
            'scaling_groups': [{
                'name': 'g1',
                'id': 'g1_2'
            }]
        }, {
            'id':
            'node2_2',
            'node_id':
            'node2',
            'scaling_groups': [{
                'name': 'g2',
                'id': 'g2_2'
            }],
            'relationships': [{
                'target_name': 'node1',
                'target_id': 'node1_2'
            }]
        }, {
            'id':
            'node3_2',
            'node_id':
            'node3',
            'scaling_groups': [{
                'name': 'g3',
                'id': 'g3_2'
            }],
            'relationships': [{
                'target_name': 'node2',
                'target_id': 'node2_2'
            }],
        }, {
            'id': 'node4_2',
            'node_id': 'node4',
            'scaling_groups': [{
                'name': 'g1',
                'id': 'g1_2'
            }]
        }, {
            'id':
            'node5_2',
            'node_id':
            'node5',
            'scaling_groups': [{
                'name': 'g5',
                'id': 'g5_2'
            }],
            'relationships': [{
                'target_name': 'node4',
                'target_id': 'node4_2'
            }]
        }, {
            'id':
            'node6_2',
            'node_id':
            'node6',
            'scaling_groups': [{
                'name': 'g6',
                'id': 'g6_2'
            }],
            'relationships': [{
                'target_name': 'node5',
                'target_id': 'node5_2'
            }],
            'runtime_properties': {
                'key': 'value6_2'
            }
        }, {
            'id': 'stub',
            'node_id': 'stub'
        }]

        nodes_by_id = {}
        for node_instance in node_instances:
            nodes_by_id[node_instance['node_id']] = {
                'id':
                node_instance['node_id'],
                'relationships': [{
                    'target_id':
                    r['target_name'],
                    'type_hierarchy': [constants.CONTAINED_IN_REL_TYPE]
                } for r in node_instance.get('relationships', [])],
            }
        storage = self.mock_evaluation_storage(node_instances=node_instances,
                                               nodes=list(
                                                   nodes_by_id.values()))
        payload = {'a': {'get_attribute': ['node6', 'key']}}
        functions.evaluate_functions(payload, context, storage)

        self.assertEqual(payload['a'], 'value6_{0}'.format(index))
Esempio n. 19
0
 def test_missing_target_ref(self):
     payload = {'a': {'get_attribute': ['TARGET', 'a']}}
     with testtools.testcase.ExpectedException(
             exceptions.FunctionEvaluationError, '.*TARGET is missing.*'):
         functions.evaluate_functions(payload, {}, None, None, None, None)
Esempio n. 20
0
    def test_get_attributes_dict_relationship(self):
        node_instances = [
            {
                'id': 'acre_4m4d34',
                'node_id': 'acre',
                'runtime_properties': {},
            },
            {
                'id': 'acre_8r00k5',
                'node_id': 'acre',
                'runtime_properties': {
                    'thing': 'hello'
                },
            },
            {
                'id': 'mandel_aa50f7',
                'node_id': 'mandel',
                'runtime_properties': {
                    'thing': 'wave'
                },
            },
            {
                'id': 'mandel_c311aa',
                'node_id': 'mandel',
                'runtime_properties': {},
            },
        ]
        nodes = [
            {
                'id': 'acre',
                'properties': {
                    'thing': 'say'
                },
            },
            {
                'id': 'mandel',
                'properties': {
                    'thing': 'goodbye'
                },
            },
        ]
        storage = self.mock_evaluation_storage(node_instances, nodes)

        payload = {
            'src': {
                'get_attributes_dict': ['SOURCE', 'thing']
            },
            'trg': {
                'get_attributes_dict': ['TARGET', 'thing']
            },
        }

        context = {
            'source': 'acre',
            'target': 'mandel',
        }

        result = functions.evaluate_functions(payload, context, storage)

        assert result['src'] == {
            'acre_4m4d34': {
                'thing': 'say'
            },
            'acre_8r00k5': {
                'thing': 'hello'
            },
        }
        assert result['trg'] == {
            'mandel_aa50f7': {
                'thing': 'wave'
            },
            'mandel_c311aa': {
                'thing': 'goodbye'
            },
        }
    def _test_process_attribute_scaling_group_ambiguity_resolution(
            self, context, index):

        node_instances = {
            'node1_1': {
                'node_id': 'node1',
                'scaling_groups': [{'name': 'g1', 'id': 'g1_1'}]
            },

            'node2_1': {
                'node_id': 'node2',
                'scaling_groups': [{'name': 'g2', 'id': 'g2_1'}],
                'relationships': [{
                    'target_name': 'node1',
                    'target_id': 'node1_1'}]
            },

            'node3_1': {
                'node_id': 'node3',
                'scaling_groups': [{'name': 'g3', 'id': 'g3_1'}],
                'relationships': [{
                    'target_name': 'node2',
                    'target_id': 'node2_1'}]
            },

            'node4_1': {
                'node_id': 'node4',
                'scaling_groups': [{'name': 'g1', 'id': 'g1_1'}]
            },

            'node5_1': {
                'node_id': 'node5',
                'scaling_groups': [{'name': 'g5', 'id': 'g5_1'}],
                'relationships': [{
                    'target_name': 'node4',
                    'target_id': 'node4_1'}]
            },

            'node6_1': {
                'node_id': 'node6',
                'scaling_groups': [{'name': 'g6', 'id': 'g6_1'}],
                'relationships': [{
                    'target_name': 'node5',
                    'target_id': 'node5_1'}],
                'runtime_properties': {
                    'key': 'value6_1'
                }
            },

            'node1_2': {
                'node_id': 'node1',
                'scaling_groups': [{'name': 'g1', 'id': 'g1_2'}]
            },

            'node2_2': {
                'node_id': 'node2',
                'scaling_groups': [{'name': 'g2', 'id': 'g2_2'}],
                'relationships': [{
                    'target_name': 'node1',
                    'target_id': 'node1_2'}]
            },

            'node3_2': {
                'node_id': 'node3',
                'scaling_groups': [{'name': 'g3', 'id': 'g3_2'}],
                'relationships': [{
                    'target_name': 'node2',
                    'target_id': 'node2_2'}],
            },

            'node4_2': {
                'node_id': 'node4',
                'scaling_groups': [{'name': 'g1', 'id': 'g1_2'}]
            },

            'node5_2': {
                'node_id': 'node5',
                'scaling_groups': [{'name': 'g5', 'id': 'g5_2'}],
                'relationships': [{
                    'target_name': 'node4',
                    'target_id': 'node4_2'}]
            },

            'node6_2': {
                'node_id': 'node6',
                'scaling_groups': [{'name': 'g6', 'id': 'g6_2'}],
                'relationships': [{
                    'target_name': 'node5',
                    'target_id': 'node5_2'}],
                'runtime_properties': {
                    'key': 'value6_2'
                }
            },
            'stub': {'node_id': 'stub'}
        }

        node_instances = dict(
            (node_instance_id, NodeInstance(node_instance))
            for node_instance_id, node_instance in node_instances.items())

        node_to_node_instances = collections.defaultdict(list)
        for node_instance_id, node_instance in node_instances.items():
            node_instance['id'] = node_instance_id
            node_to_node_instances[node_instance.node_id].append(node_instance)

        nodes = {}
        for node_instance in node_instances.values():
            nodes[node_instance.node_id] = Node({
                'relationships': [
                    {'target_id': r['target_name'],
                     'type_hierarchy': [constants.CONTAINED_IN_REL_TYPE]}
                    for r in node_instance.get('relationships', [])],
            })

        def get_node_instances(node_id):
            return node_to_node_instances[node_id]

        def get_node_instance(node_instance_id):
            return node_instances[node_instance_id]

        def get_node(node_id):
            return nodes[node_id]

        payload = {'a': {'get_attribute': ['node6', 'key']}}
        functions.evaluate_functions(payload,
                                     context,
                                     get_node_instances,
                                     get_node_instance,
                                     get_node,
                                     None)

        self.assertEqual(payload['a'], 'value6_{0}'.format(index))
Esempio n. 22
0
 def evaluate_functions(self, payload, context):
     return dsl_functions.evaluate_functions(payload=payload,
                                             context=context,
                                             storage=self.storage)
Esempio n. 23
0
 def evaluate():
     payload = {'a': {'get_attribute': ['node2', 'key']}}
     context = {'self': 'node1_1'}
     functions.evaluate_functions(payload, context, get_node_instances,
                                  get_node_instance, get_node, None)
     return payload
Esempio n. 24
0
    def _test_process_attribute_scaling_group_ambiguity_resolution(
            self, context, index):

        node_instances = {
            'node1_1': {
                'node_id': 'node1',
                'scaling_groups': [{
                    'name': 'g1',
                    'id': 'g1_1'
                }]
            },
            'node2_1': {
                'node_id': 'node2',
                'scaling_groups': [{
                    'name': 'g2',
                    'id': 'g2_1'
                }],
                'relationships': [{
                    'target_name': 'node1',
                    'target_id': 'node1_1'
                }]
            },
            'node3_1': {
                'node_id': 'node3',
                'scaling_groups': [{
                    'name': 'g3',
                    'id': 'g3_1'
                }],
                'relationships': [{
                    'target_name': 'node2',
                    'target_id': 'node2_1'
                }]
            },
            'node4_1': {
                'node_id': 'node4',
                'scaling_groups': [{
                    'name': 'g1',
                    'id': 'g1_1'
                }]
            },
            'node5_1': {
                'node_id': 'node5',
                'scaling_groups': [{
                    'name': 'g5',
                    'id': 'g5_1'
                }],
                'relationships': [{
                    'target_name': 'node4',
                    'target_id': 'node4_1'
                }]
            },
            'node6_1': {
                'node_id': 'node6',
                'scaling_groups': [{
                    'name': 'g6',
                    'id': 'g6_1'
                }],
                'relationships': [{
                    'target_name': 'node5',
                    'target_id': 'node5_1'
                }],
                'runtime_properties': {
                    'key': 'value6_1'
                }
            },
            'node1_2': {
                'node_id': 'node1',
                'scaling_groups': [{
                    'name': 'g1',
                    'id': 'g1_2'
                }]
            },
            'node2_2': {
                'node_id': 'node2',
                'scaling_groups': [{
                    'name': 'g2',
                    'id': 'g2_2'
                }],
                'relationships': [{
                    'target_name': 'node1',
                    'target_id': 'node1_2'
                }]
            },
            'node3_2': {
                'node_id':
                'node3',
                'scaling_groups': [{
                    'name': 'g3',
                    'id': 'g3_2'
                }],
                'relationships': [{
                    'target_name': 'node2',
                    'target_id': 'node2_2'
                }],
            },
            'node4_2': {
                'node_id': 'node4',
                'scaling_groups': [{
                    'name': 'g1',
                    'id': 'g1_2'
                }]
            },
            'node5_2': {
                'node_id': 'node5',
                'scaling_groups': [{
                    'name': 'g5',
                    'id': 'g5_2'
                }],
                'relationships': [{
                    'target_name': 'node4',
                    'target_id': 'node4_2'
                }]
            },
            'node6_2': {
                'node_id': 'node6',
                'scaling_groups': [{
                    'name': 'g6',
                    'id': 'g6_2'
                }],
                'relationships': [{
                    'target_name': 'node5',
                    'target_id': 'node5_2'
                }],
                'runtime_properties': {
                    'key': 'value6_2'
                }
            },
            'stub': {
                'node_id': 'stub'
            }
        }

        node_instances = dict(
            (node_instance_id, NodeInstance(node_instance))
            for node_instance_id, node_instance in node_instances.items())

        node_to_node_instances = collections.defaultdict(list)
        for node_instance_id, node_instance in node_instances.items():
            node_instance['id'] = node_instance_id
            node_to_node_instances[node_instance.node_id].append(node_instance)

        nodes = {}
        for node_instance in node_instances.values():
            nodes[node_instance.node_id] = Node({
                'relationships': [{
                    'target_id':
                    r['target_name'],
                    'type_hierarchy': [constants.CONTAINED_IN_REL_TYPE]
                } for r in node_instance.get('relationships', [])],
            })

        def get_node_instances(node_id):
            return node_to_node_instances[node_id]

        def get_node_instance(node_instance_id):
            return node_instances[node_instance_id]

        def get_node(node_id):
            return nodes[node_id]

        payload = {'a': {'get_attribute': ['node6', 'key']}}
        functions.evaluate_functions(payload, context, get_node_instances,
                                     get_node_instance, get_node, None)

        self.assertEqual(payload['a'], 'value6_{0}'.format(index))
Esempio n. 25
0
 def test_get_deeply_nested_secret(self):
     payload = {
         'a': {'get_secret': ['big_secret', 'something', 0, 'like', 1]},
     }
     functions.evaluate_functions(payload, {}, self.mock_storage)
     self.assertEqual(payload['a'], 'that')
Esempio n. 26
0
    def test_evaluate_functions(self):
        def get_node_instances(node_id=None):
            return [get_node_instance(node_id)]

        def get_node_instance(node_instance_id):
            result = NodeInstance({
                'id': node_instance_id,
                'node_id': 'webserver',
                'runtime_properties': {}
            })
            if node_instance_id == 'node1':
                result.runtime_properties['a'] = 'a_val'
            elif node_instance_id == 'node2':
                result.runtime_properties['b'] = 'b_val'
            elif node_instance_id == 'node3':
                result.runtime_properties['c'] = 'c_val'
            elif node_instance_id == 'node4':
                result.runtime_properties['d'] = 'd_val'
            return result

        def get_node(node_id):
            return Node({
                'id': node_id,
            })

        payload = {
            'a': {
                'get_attribute': ['SELF', 'a']
            },
            'b': {
                'get_attribute': ['node2', 'b']
            },
            'c': {
                'get_attribute': ['SOURCE', 'c']
            },
            'd': {
                'get_attribute': ['TARGET', 'd']
            },
            'f': {
                'concat': [{
                    'get_attribute': ['SELF', 'a']
                }, {
                    'get_attribute': ['node2', 'b']
                }, {
                    'get_attribute': ['SOURCE', 'c']
                }, {
                    'get_attribute': ['TARGET', 'd']
                }]
            }
        }

        context = {'self': 'node1', 'source': 'node3', 'target': 'node4'}

        functions.evaluate_functions(payload, context, get_node_instances,
                                     get_node_instance, get_node, None)

        self.assertEqual(payload['a'], 'a_val')
        self.assertEqual(payload['b'], 'b_val')
        self.assertEqual(payload['c'], 'c_val')
        self.assertEqual(payload['d'], 'd_val')
        self.assertEqual(payload['f'], 'a_valb_valc_vald_val')
Esempio n. 27
0
    def test_evaluate_functions(self):
        def _node_instance(node_id, runtime_proerties):
            return {
                'id': node_id,
                'node_id': node_id,
                'runtime_properties': runtime_proerties
            }

        node_instances = [
            _node_instance('node1', {
                'a': 'a_val',
                'node_id': 'node1'
            }),
            _node_instance('node2', {
                'b': 'b_val',
                'node_id': 'node2'
            }),
            _node_instance('node3', {
                'c': 'c_val',
                'node_id': 'node3'
            }),
            _node_instance('node4', {
                'd': 'd_val',
                'node_id': 'node4'
            }),
        ]
        nodes = [
            {
                'id': 'node1'
            },
            {
                'id': 'node2'
            },
            {
                'id': 'node3'
            },
            {
                'id': 'node4'
            },
        ]

        storage = self.mock_evaluation_storage(node_instances=node_instances,
                                               nodes=nodes)
        payload = {
            'a': {
                'get_attribute': ['SELF', 'a']
            },
            'b': {
                'get_attribute': ['node2', 'b']
            },
            'c': {
                'get_attribute': ['SOURCE', 'c']
            },
            'd': {
                'get_attribute': ['TARGET', 'd']
            },
            'f': {
                'concat': [{
                    'get_attribute': ['SELF', 'a']
                }, {
                    'get_attribute': ['node2', 'b']
                }, {
                    'get_attribute': ['SOURCE', 'c']
                }, {
                    'get_attribute': ['TARGET', 'd']
                }]
            }
        }

        context = {'self': 'node1', 'source': 'node3', 'target': 'node4'}

        functions.evaluate_functions(payload, context, storage)

        self.assertEqual(payload['a'], 'a_val')
        self.assertEqual(payload['b'], 'b_val')
        self.assertEqual(payload['c'], 'c_val')
        self.assertEqual(payload['d'], 'd_val')
        self.assertEqual(payload['f'], 'a_valb_valc_vald_val')
 def test_missing_target_ref(self):
     payload = {'a': {'get_attribute': ['TARGET', 'a']}}
     with testtools.testcase.ExpectedException(
             exceptions.FunctionEvaluationError,
             '.*TARGET is missing.*'):
         functions.evaluate_functions(payload, {}, None, None, None, None)
Esempio n. 29
0
    def test_get_attributes_dict_one_attribute(self):
        node_instances = [
            {
                'id': 'goodnode_r1ck00',
                'node_id': 'goodnode',
                'runtime_properties': {
                    'sing': 'never',
                    'eat': 'dromekage'
                },
            },
            {
                'id': 'badnode_0hn011',
                'node_id': 'badnode',
                'runtime_properties': {
                    'sing': 'forget',
                    'eat': 'apple'
                },
            },
            {
                'id': 'goodnode_45713y',
                'node_id': 'goodnode',
                'runtime_properties': {
                    'sing': 'gonna',
                    'eat': 'chip'
                },
            },
            {
                'id': 'goodnode_r0113d',
                'node_id': 'goodnode',
                'runtime_properties': {
                    'eat': 'biscuit'
                },
            },
            {
                'id': 'goodnode_s1n613',
                'node_id': 'goodnode',
                'runtime_properties': {
                    'sing': 'you'
                },
            },
            {
                'id': 'goodnode_41b2m5',
                'node_id': 'goodnode',
                'runtime_properties': {
                    'sing': 'up',
                    'eat': 'cheesecake'
                },
            },
        ]
        nodes = [
            {
                'id': 'badnode'
            },
            {
                'id': 'goodnode',
                'properties': {
                    'sing': 'give',
                    'eat': 'marshmallow'
                }
            },
        ]
        storage = self.mock_evaluation_storage(node_instances=node_instances,
                                               nodes=nodes)

        payload = {'a': {'get_attributes_dict': ['goodnode', 'sing']}}
        context = {}
        result = functions.evaluate_functions(payload.copy(), context, storage)

        attributes_list = result['a']
        assert attributes_list == {
            'goodnode_r1ck00': {
                'sing': 'never'
            },
            'goodnode_45713y': {
                'sing': 'gonna'
            },
            'goodnode_r0113d': {
                'sing': 'give'
            },
            'goodnode_s1n613': {
                'sing': 'you'
            },
            'goodnode_41b2m5': {
                'sing': 'up'
            },
        }