def handler(dict_, k, v, scope, context, path): func = functions.parse(v, scope=scope, context=context, path=path) evaluated_value = v while isinstance(func, functions.Function): if isinstance(func, functions.GetAttribute): dict_[k] = func.raw return evaluated_value = func.evaluate(plan) func = functions.parse(evaluated_value, scope=scope, context=context, path=path) dict_[k] = evaluated_value
def validate_inputs_types(inputs, inputs_schema): for input_key, _input in inputs_schema.iteritems(): input_type = _input.get('type') if input_type is None: # no type defined - no validation continue input_val = inputs[input_key] if functions.parse(input_val) != input_val: # intrinsic function - not validated at the moment continue if input_type == 'integer': if isinstance(input_val, (int, long)) and not \ isinstance(input_val, bool): continue elif input_type == 'float': if isinstance(input_val, (int, float, long)) and not \ isinstance(input_val, bool): continue elif input_type == 'boolean': if isinstance(input_val, bool): continue elif input_type == 'string': continue else: raise DSLParsingLogicException( 80, "Unexpected type defined in inputs schema " "for input '{0}' - unknown type is {1}".format( input_key, input_type)) raise DSLParsingLogicException( 50, "Input type validation failed: Input '{0}' type " "is '{1}', yet it was assigned with the value '{2}'".format( input_key, input_type, input_val))
def _validate_properties_types(properties, properties_schema): for prop_key, prop in properties_schema.iteritems(): prop_type = prop.get('type') if prop_type is None: continue prop_val = properties[prop_key] if functions.parse(prop_val) != prop_val: # intrinsic function - not validated at the moment continue if prop_type == 'integer': if isinstance(prop_val, (int, long)) and not isinstance( prop_val, bool): continue elif prop_type == 'float': if isinstance(prop_val, (int, float, long)) and not isinstance( prop_val, bool): continue elif prop_type == 'boolean': if isinstance(prop_val, bool): continue elif prop_type == 'string': continue else: raise RuntimeError( "Unexpected type defined in property schema for property '{0}'" " - unknown type is '{1}'".format(prop_key, prop_type)) raise DSLParsingLogicException( 50, "Property type validation failed: Property '{0}' type " "is '{1}', yet it was assigned with the value '{2}'" .format(prop_key, prop_type, prop_val))
def validate_inputs_types(inputs, inputs_schema): for input_key, _input in inputs_schema.items(): input_type = _input.get('type') if input_type is None: # no type defined - no validation continue input_val = inputs[input_key] if functions.parse(input_val) != input_val: # intrinsic function - not validated at the moment continue if input_type == 'integer': if isinstance(input_val, int) and not \ isinstance(input_val, bool): continue elif input_type == 'float': if isinstance(input_val, (int, float)) and not \ isinstance(input_val, bool): continue elif input_type == 'boolean': if isinstance(input_val, bool): continue elif input_type == 'string': continue else: raise DSLParsingLogicException( 80, "Unexpected type defined in inputs schema " "for input '{0}' - unknown type is {1}" .format(input_key, input_type)) raise DSLParsingLogicException( 50, "Input type validation failed: Input '{0}' type " "is '{1}', yet it was assigned with the value '{2}'" .format(input_key, input_type, input_val))
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)
def parse_value(value, type_name, data_types, undefined_property_error_message, missing_property_error_message, node_name, path, derived_value=None, raise_on_missing_property=True): if type_name is None: return value if functions.parse(value) != value: # intrinsic function - not validated at the moment return value if type_name == 'integer': if isinstance(value, (int, long)) and not isinstance(value, bool): return value elif type_name == 'float': if isinstance(value, (int, float, long)) and not isinstance(value, bool): return value elif type_name == 'boolean': if isinstance(value, bool): return value elif type_name == 'string': return value elif type_name in data_types: if isinstance(value, dict): data_schema = data_types[type_name]['properties'] flattened_data_schema = flatten_schema(data_schema) if isinstance(derived_value, dict): flattened_data_schema.update(derived_value) undef_msg = undefined_property_error_message return _merge_flattened_schema_and_instance_properties( instance_properties=value, schema_properties=data_schema, flattened_schema_properties=flattened_data_schema, data_types=data_types, undefined_property_error_message=undef_msg, missing_property_error_message=missing_property_error_message, node_name=node_name, path=path, raise_on_missing_property=raise_on_missing_property) else: raise RuntimeError( "Unexpected type defined in property schema for property '{0}'" " - unknown type is '{1}'".format(_property_description(path), type_name)) raise DSLParsingLogicException( exceptions.ERROR_VALUE_DOES_NOT_MATCH_TYPE, "Property type validation failed in '{0}': property " "'{1}' type is '{2}', yet it was assigned with the " "value '{3}'".format(node_name, _property_description(path), type_name, value))
def validate(self): value = self.initial_value if isinstance(value, dict): function = functions.parse(value) if not isinstance(function, functions.Function): raise exceptions.DSLParsingLogicException( exceptions.ERROR_INVALID_DICT_VALUE, '{0} should be a valid intrinsic function or a value.' .format(self.name)) return True return False
def validate(self): value = self.initial_value if isinstance(value, dict): function = functions.parse(value) if not isinstance(function, functions.Function): raise exceptions.DSLParsingLogicException( exceptions.ERROR_INVALID_DICT_VALUE, '{0} should be a valid intrinsic function or a value.'. format(self.name)) return True return False
def test_valid_get_secret(self): yaml = """ node_types: webserver_type: {} node_templates: webserver: type: webserver_type outputs: port: description: p0 value: { get_secret: secret_key } """ parsed = self.parse(yaml) outputs = parsed['outputs'] func = functions.parse(outputs['port']['value']) self.assertTrue(isinstance(func, functions.GetSecret)) self.assertEqual('secret_key', func.secret_id) prepared = prepare_deployment_plan(parsed, self._get_secret_mock) self.assertEqual(parsed['outputs'], prepared['outputs'])
def test_valid_get_attribute(self): yaml = """ node_types: webserver_type: {} node_templates: webserver: type: webserver_type outputs: port: description: p0 value: { get_attribute: [ webserver, port ] } """ parsed = self.parse(yaml) outputs = parsed['outputs'] func = functions.parse(outputs['port']['value']) self.assertTrue(isinstance(func, functions.GetAttribute)) self.assertEqual('webserver', func.node_name) self.assertEqual('port', func.attribute_path[0]) prepared = prepare_deployment_plan(parsed) self.assertEqual(parsed['outputs'], prepared['outputs'])
def evaluate(self, handler): if functions.parse(self.arg) != self.arg: return self.raw return str(self.arg).upper()
def parse_value( value, type_name, data_types, undefined_property_error_message, missing_property_error_message, node_name, path, derived_value=None, raise_on_missing_property=True): if type_name is None: return value if functions.parse(value) != value: # intrinsic function - not validated at the moment return value if type_name == 'integer': if isinstance(value, int) and not isinstance( value, bool): return value elif type_name == 'float': if isinstance(value, (int, float)) and not isinstance( value, bool): return value elif type_name == 'boolean': if isinstance(value, bool): return value elif type_name == 'string': return value elif type_name in data_types: if isinstance(value, dict): data_schema = data_types[type_name]['properties'] flattened_data_schema = flatten_schema(data_schema) if isinstance(derived_value, dict): flattened_data_schema.update(derived_value) undef_msg = undefined_property_error_message return _merge_flattened_schema_and_instance_properties( instance_properties=value, schema_properties=data_schema, flattened_schema_properties=flattened_data_schema, data_types=data_types, undefined_property_error_message=undef_msg, missing_property_error_message=missing_property_error_message, node_name=node_name, path=path, raise_on_missing_property=raise_on_missing_property) else: raise RuntimeError( "Unexpected type defined in property schema for property '{0}'" " - unknown type is '{1}'".format( _property_description(path), type_name)) raise DSLParsingLogicException( exceptions.ERROR_VALUE_DOES_NOT_MATCH_TYPE, "Property type validation failed in '{0}': property " "'{1}' type is '{2}', yet it was assigned with the " "value '{3}'".format( node_name, _property_description(path), type_name, value))
def evaluate(self, plan): if functions.parse(self.arg) != self.arg: return self.raw return str(self.arg).upper()