def evaluate(self, handler): if isinstance(self.secret_id, list): secret = handler.get_secret(self.secret_id[0]) try: value = json.loads(secret) except ValueError as err: raise exceptions.FunctionEvaluationError( 'Could not parse secret "{secret_name}" as json. ' 'Error was: {err}'.format( secret_name=self.secret_id[0], err=err, ) ) if len(self.secret_id) > 1: for i in self.secret_id[1:]: try: value = value[i] except (KeyError, TypeError, IndexError) as err: raise exceptions.FunctionEvaluationError( 'Could not find "{key}" in nested lookup on ' 'secret "{secret}". Error was: {err}'.format( key=i, secret=self.secret_id[0], err=err, ) ) return value else: return handler.get_secret(self.secret_id)
def _resolve_node_instance_by_name(self, storage): node_id = self.node_name node_instances = storage.get_node_instances(node_id) if len(node_instances) == 0: raise exceptions.FunctionEvaluationError( self.name, 'Node {0} has no instances.'.format(self.node_name)) if len(node_instances) == 1: return node_instances[0] node_instance = self._try_resolve_node_instance_by_relationship( storage=storage, node_instances=node_instances) if node_instance: return node_instance node_instance = self._try_resolve_node_instance_by_scaling_group( storage=storage, node_instances=node_instances) if node_instance: return node_instance raise exceptions.FunctionEvaluationError( self.name, 'More than one node instance found for node "{0}". Cannot ' 'resolve a node instance unambiguously.'.format(self.node_name))
def evaluate(self, handler): if isinstance(self.input_value, list): if any(is_function(x) for x in self.input_value): raise exceptions.FunctionEvaluationError( '{0}: found an unresolved argument: {1}'.format( self.name, self.input_value)) return self._get_input_attribute( handler.get_input(self.input_value[0])) if is_function(self.input_value): raise exceptions.FunctionEvaluationError( '{0}: found an unresolved argument: {1}'.format( self.name, self.input_value)) return handler.get_input(self.input_value)
def _get_input_attribute(self, root): value = root for index, attr in enumerate(self.input_value[1:]): if isinstance(value, dict): if attr not in value: raise exceptions.InputEvaluationError( "Input attribute '{0}' of '{1}', " "doesn't exist.".format( attr, _convert_attribute_list_to_python_syntax_string( self.input_value[:index + 1]))) value = value[attr] elif isinstance(value, list): try: value = value[attr] except TypeError: raise exceptions.InputEvaluationError( "Item in index {0} in the get_input arguments list " "'{1}' is expected to be an int but got {2}.".format( index, self.input_value, type(attr).__name__)) except IndexError: raise exceptions.InputEvaluationError( "List size of '{0}' is {1} but index {2} is " "retrieved.".format( _convert_attribute_list_to_python_syntax_string( self.input_value[:index + 1]), len(value), attr)) else: raise exceptions.FunctionEvaluationError( self.name, "Object {0} has no attribute {1}".format( _convert_attribute_list_to_python_syntax_string( self.input_value[:index + 1]), attr)) return value
def evaluate(self, handler): if is_function(self.node_name) or \ any(is_function(x) for x in self.property_path): raise exceptions.FunctionEvaluationError( '{0}: found an unresolved argument in path: {1}, {2}'.format( self.name, self.node_name, self.property_path)) return self._get_property_value(self.get_node_template(handler))
def get_nested_attribute_value_of_capability(root, path): value = root for index, attr in enumerate(path[2:]): if isinstance(value, dict): if attr not in value: raise exceptions.FunctionEvaluationError( "Attribute '{0}' doesn't exist in '{1}' " "in deployment '{2}'.".format( attr, _convert_attribute_list_to_python_syntax_string( path[1:index + 2]), path[0] ) ) value = value[attr] elif isinstance(value, list): try: value = value[attr] except TypeError: raise exceptions.FunctionEvaluationError( "Item in index {0} in the get_capability arguments " "list [{1}] is expected to be an int " "but got {2}.".format( index + 2, ', '.join('{0}'.format(item) for item in path), type(attr).__name__)) except IndexError: raise exceptions.FunctionEvaluationError( "List size of '{0}' is {1}, in " "deployment '{2}', but index {3} is " "retrieved.".format( _convert_attribute_list_to_python_syntax_string( path[1:index + 2]), len(value), path[0], attr)) else: raise exceptions.FunctionEvaluationError( "Object {0}, in capability '{1}' in deployment '{2}', " "has no attribute {3}".format( _convert_attribute_list_to_python_syntax_string( path[2:index + 2]), path[1], path[0], attr)) return {'value': value}
def _validate_ref(self, ref, ref_name): if not ref: raise exceptions.FunctionEvaluationError( self.name, '{0} is missing in request context in {1} for ' 'attribute {2}'.format(ref_name, self.path, self.attribute_path))
def validate(self, plan): if plan.version.definitions_version < (1, 1): raise exceptions.FunctionEvaluationError( 'Using {0} requires using dsl version 1_1 or ' 'greater, but found: {1} in {2}.' .format(self.name, plan.version, self.path)) if self.scope not in [scan.NODE_TEMPLATE_SCOPE, scan.NODE_TEMPLATE_RELATIONSHIP_SCOPE, scan.OUTPUTS_SCOPE]: raise ValueError('{0} cannot be used in {1}.' .format(self.name, self.path))
def handler(dict_, k, v, scope, context, path): func = parse(v, scope=scope, context=context, path=path) if isinstance(func, GetAttribute): attributes = [] if 'node_instances' not in ctx: ctx['node_instances'] = get_node_instances_method() for instance in ctx['node_instances']: if instance.node_id == func.node_name: attributes.append( instance.runtime_properties.get(func.attribute_name) if instance.runtime_properties else None) if len(attributes) == 1: dict_[k] = attributes[0] elif len(attributes) == 0: raise exceptions.FunctionEvaluationError( GET_ATTRIBUTE_FUNCTION, 'Node specified in function does not exist: {0}.'.format( func.node_name)) else: raise exceptions.FunctionEvaluationError( GET_ATTRIBUTE_FUNCTION, 'Multi instances of node "{0}" are not supported by ' 'function.'.format(func.node_name))