def get_nodes(self, evaluate_functions=False): nodes = copy.deepcopy(list(self._nodes.values())) if not evaluate_functions: return nodes for node in nodes: dsl_functions.evaluate_node_functions(node, self) return nodes
def get_node(self, node_id, evaluate_functions=False): node = self._nodes.get(node_id) if node is None: raise KeyError('Node {0} does not exist'.format(node_id)) node = copy.deepcopy(node) if evaluate_functions: dsl_functions.evaluate_node_functions(node, self) return node
def test_get_property_another_node_runtime(self): yaml = """ node_types: vm_type: properties: a: { type: string } c: { type: string } node_templates: vm1: type: vm_type properties: a: {get_property: [vm2, c]} c: secret1 vm2: type: vm_type properties: a: xxx c: secret2 """ plan = prepare_deployment_plan( self.parse(yaml), runtime_only_evaluation=True) plan_node = next(n for n in plan['nodes'] if n['name'] == 'vm1') self.assertEqual(plan_node['properties']['a'], {'get_property': ['vm2', 'c']}) node = functions.evaluate_node_functions( plan_node, self._mock_evaluation_storage(nodes=plan['nodes'])) self.assertEqual(node['properties']['a'], 'secret2')
def test_get_property_from_get_secret_runtime(self): yaml = """ node_types: vm_type: properties: secret1_value: { type: string } b: { type: string } node_templates: vm1: type: vm_type properties: secret1_value: secret b: {get_property: [SELF, {get_secret: secret1} ]} """ storage = self._mock_evaluation_storage() plan = prepare_deployment_plan( self.parse(yaml), get_secret_method=storage.get_secret, runtime_only_evaluation=True) self.assertEqual( {'get_property': ['SELF', {'get_secret': 'secret1'}]}, plan['nodes'][0]['properties']['b']) evaluated = functions.evaluate_node_functions( plan['nodes'][0], storage ) self.assertEqual('secret', evaluated['properties']['b'])
def test_get_property_from_get_input_runtime(self): yaml = """ inputs: dict_input: {} node_types: vm_type: properties: a: { type: string } b: { type: string } node_templates: vm1: type: vm_type properties: a: {get_input: dict_input} b: {get_property: [SELF, a, key]} """ inputs = {'dict_input': {'key': 'secret'}} plan = prepare_deployment_plan( self.parse(yaml), inputs=inputs, runtime_only_evaluation=True) # with runtime-only-evaluation, the property isn't evaluated at # prepare_deployment_plan time self.assertEqual( {'get_property': ['SELF', 'a', 'key']}, plan['nodes'][0]['properties']['b']) evaluated = functions.evaluate_node_functions( plan['nodes'][0], self._mock_evaluation_storage(inputs=inputs) ) self.assertEqual('secret', evaluated['properties']['b'])
def evaluate_node(node): # dsl-parser uses name in plans, while the db storage uses id :( node['name'] = node['id'] deployment_id = node['deployment_id'] sm = get_storage_manager() sm.get(Deployment, deployment_id, include=['id']) storage = FunctionEvaluationStorage(deployment_id, sm) try: return functions.evaluate_node_functions(node, storage) except parser_exceptions.FunctionEvaluationError as e: raise FunctionsEvaluationError(str(e))
def test_get_property_runtime(self): yaml = """ node_types: vm_type: properties: a: { type: string } c: { type: string } node_templates: vm1: type: vm_type properties: a: {get_property: [SELF, c]} c: secret """ plan = prepare_deployment_plan( self.parse(yaml), runtime_only_evaluation=True) self.assertEqual(plan['nodes'][0]['properties']['a'], {'get_property': ['SELF', 'c']}) node = functions.evaluate_node_functions(plan['nodes'][0], None) self.assertEqual(node['properties']['a'], 'secret')