def validate(self): weekdays_list = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'] err_msg = "{0} is not a valid weekday value. Accepted values: {1}" weekday_value = self.initial_value.lower()[-2:] if weekday_value not in weekdays_list: raise exceptions.DSLParsingException( 1, err_msg.format(weekday_value, weekdays_list))
def validate(self, **kwargs): """ Deployment schedules are assigned to a deployment during its creation, so they cannot cannot contain runtime properties. """ err_msg = "`workflow_parameters` cannot {0} a runtime property." \ " Please remove the `get_attribute` function{1}" if self.initial_value: if 'get_attribute' in self.initial_value: raise exceptions.DSLParsingException( 1, err_msg.format('be', '')) for key, value in self.initial_value.items(): if isinstance(value, dict) and 'get_attribute' in value: value_msg = ' from the value of {}'.format(key) raise exceptions.DSLParsingException( 1, err_msg.format('contain', value_msg))
def validate(self, **kwargs): """ A label's value cannot be a runtime property, since labels are assigned to deployment during its creation. """ type_err_msg = "The label's value must be a string or an intrinsic " \ "function. Please modify the values of {0}" get_attr_err_msg = "The label's value cannot be a runtime property. " \ "Please remove the `get_attribute` function from " \ "the values of {0}" for value in self.initial_value['values']: if not isinstance(value, (dict, text_type)): raise exceptions.DSLParsingException( 1, type_err_msg.format(self.name)) if isinstance(value, dict) and 'get_attribute' in value: raise exceptions.DSLParsingException( 1, get_attr_err_msg.format(self.name))
def validate_input_value(input_name, input_constraints, input_value): if input_constraints and functions.is_function(input_value): raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_WITH_FUNCS_AND_CONSTRAINTS, 'Input value {0}, of input {1}, cannot contain an intrinsic ' 'function and also have ' 'constraints.'.format(input_value, input_name)) for c in input_constraints: if not c.predicate(input_value): raise exceptions.ConstraintException( "Value {0} of input {1} violates constraint " "{2}.".format(input_value, input_name, c))
def test_invalid_blueprint_raises_invalid_blueprint_exception(self): deployment_id = 'dep' self._deploy_base(deployment_id, 'no_output.yaml') with patch('dsl_parser.tasks.parse_dsl') as parse_dsl_mock: parse_dsl_mock.side_effect = \ parser_exceptions.DSLParsingException('') # # It doesn't matter that we are updating the deployment with the # same blueprint, since we mocked the blueprint parsing process. self.assertRaisesRegexp(RuntimeError, 'invalid_blueprint_error', self._update, deployment_id, 'no_output.yaml')
def test_invalid_blueprint_raises_invalid_blueprint_exception(self): deployment_id = 'dep' self._deploy_base(deployment_id, 'no_output.yaml') with patch('dsl_parser.tasks.parse_dsl') as parse_dsl_mock: parse_dsl_mock.side_effect = \ parser_exceptions.DSLParsingException('') # # It doesn't matter that we are updating the deployment with the # same blueprint, since we mocked the blueprint parsing process. response = self._update(deployment_id, 'no_output.yaml') self.assertEquals(400, response.status_code) self.assertEquals('invalid_blueprint_error', response.json['error_code'])
def validate(self, **kwargs): """ A blueprint label's value cannot be an intrinsic function, as labels are assigned to a blueprint while it's uploaded, and the intrinsic functions are not yet processed. """ type_err_msg = "The blueprint label's value must be a string. " \ "Please modify the values of {0}" for value in self.initial_value['values']: if not isinstance(value, text_type): raise exceptions.DSLParsingException( 1, type_err_msg.format(self.name))
def _validate_input_value(input_name, input_constraints, input_value, type_name, value_getter): if input_constraints and utils.get_function(input_value): raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_WITH_FUNCS_AND_CONSTRAINTS, 'Input value {0}, of input {1}, cannot contain an intrinsic ' 'function and also have ' 'constraints.'.format(input_value, input_name)) if type_name in TYPES_BASED_ON_DB_ENTITIES: if not value_getter: raise exceptions.ConstraintException( 'Invalid call to validate_input_value: value_getter not set') if type_name in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT \ and not value_getter.has_deployment_id() \ and 'deployment_id' not in {c.name for c in input_constraints}: raise exceptions.ConstraintException( "Input '{0}' of type '{1}' lacks 'deployment_id' constraint.". format(input_name, type_name)) if type_name not in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT \ or ('deployment_id' not in {c.name for c in input_constraints} and value_getter.has_deployment_id()): matching_values = value_getter.get(type_name, input_value) if not any(v == input_value for v in matching_values or []): raise exceptions.ConstraintException( "Value '{0}' of '{1}' is not a valid value for data type " "'{2}'.".format(input_value, input_name, type_name)) data_based_constraints = [] for c in input_constraints: if c.data_based(type_name): data_based_constraints.append(c) continue if not c.predicate(input_value): raise exceptions.ConstraintException( "Value {0} of input {1} violates constraint " "{2}.".format(input_value, input_name, c)) if ((type_name in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT or data_based_constraints) and not predicate_many( input_value, type_name, value_getter, data_based_constraints)): raise exceptions.ConstraintException( "Value '{0}' of input '{1}' does not match any relevant entity " "or violates at least one of the constraints: {2}.".format( input_value, input_name, ", ".join(str(c) for c in data_based_constraints)))
def _set_plan_inputs(plan, inputs=None): inputs = inputs if inputs else {} # Verify inputs satisfied missing_inputs = [] for input_name, input_def in plan[INPUTS].items(): input_is_missing = False if input_name in inputs: try: str(json.dumps(inputs[input_name], ensure_ascii=False)) except UnicodeEncodeError: raise exceptions.DSLParsingInputTypeException( exceptions.ERROR_INVALID_CHARS, 'Illegal characters in input: {0}. ' 'Only valid ascii chars are supported.'.format(input_name)) else: if DEFAULT in input_def and input_def[DEFAULT] is not None: inputs[input_name] = input_def[DEFAULT] else: missing_inputs.append(input_name) input_is_missing = True # Verify inputs comply with the given constraints and also the # data_type, if mentioned input_constraints = constraints.extract_constraints(input_def) if not input_is_missing: constraints.validate_input_value( input_name, input_constraints, inputs[input_name]) try: utils.parse_value( value=inputs[input_name], type_name=input_def.get(TYPE, None), data_types=plan.get(DATA_TYPES, {}), undefined_property_error_message="Undefined property " "{1} in value of " "input {0}.", missing_property_error_message="Value of input {0} " "is missing property " "{1}.", node_name=input_name, path=[], raise_on_missing_property=True) except exceptions.DSLParsingLogicException as e: raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_VIOLATES_DATA_TYPE_SCHEMA, str(e)) if missing_inputs: raise exceptions.MissingRequiredInputError( "Required inputs {0} were not specified - expected " "inputs: {1}".format(missing_inputs, list(plan[INPUTS])) ) # Verify all inputs appear in plan not_expected = [input_name for input_name in inputs if input_name not in plan[INPUTS]] if not_expected: raise exceptions.UnknownInputError( "Unknown inputs {0} specified - " "expected inputs: {1}".format(not_expected, list(plan[INPUTS]))) plan[INPUTS] = inputs
def _set_plan_inputs(plan, inputs, auto_correct_types, values_getter): inputs = inputs if inputs else {} # Verify inputs satisfied missing_inputs = [] for input_name, input_def in plan[INPUTS].items(): input_is_missing = skip_input_validation = False if input_name in inputs: try: str(json.dumps(inputs[input_name], ensure_ascii=False)) except UnicodeEncodeError: raise exceptions.DSLParsingInputTypeException( exceptions.ERROR_INVALID_CHARS, 'Illegal characters in input: {0}. ' 'Only valid ascii chars are supported.'.format(input_name)) elif DEFAULT in input_def and input_def[DEFAULT] is not None: inputs[input_name] = input_def[DEFAULT] else: # Try to get some defaults from the data_type maybe or in other # words just try to parse the value before validating it. try: parsed_value = utils.parse_value( {}, type_name=input_def.get(TYPE, None), data_types=plan.get(DATA_TYPES, {}), undefined_property_error_message="Undefined property " "{1} in value of " "input {0}.", missing_property_error_message="Value of input {0} " "is missing property " "{1}.", node_name=input_name, path=[], raise_on_missing_property=True) except exceptions.DSLParsingException: parsed_value = None finally: if parsed_value: inputs[input_name] = parsed_value elif input_def.get('required', True): missing_inputs.append(input_name) input_is_missing = True else: skip_input_validation = True inputs[input_name] = None if skip_input_validation: continue # Verify inputs comply with the given constraints and also the # data_type, if mentioned input_constraints = constraints.extract_constraints(input_def) if not input_is_missing: if auto_correct_types: inputs[input_name] = utils.cast_to_type( inputs[input_name], input_def.get(TYPE, None)) constraints.validate_input_value(input_name, input_constraints, inputs[input_name], input_def.get(TYPE), input_def.get(ITEM_TYPE), values_getter) inputs_complete = inputs[input_name] try: inputs_complete = utils.parse_value( value=inputs[input_name], type_name=input_def.get(TYPE, None), data_types=plan.get(DATA_TYPES, {}), undefined_property_error_message="Undefined property " "{1} in value of " "input {0}.", missing_property_error_message="Value of input {0} " "is missing property " "{1}.", node_name=input_name, path=[], raise_on_missing_property=True) except exceptions.DSLParsingLogicException as e: raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_VIOLATES_DATA_TYPE_SCHEMA, str(e)) inputs[input_name] = inputs_complete if missing_inputs: raise exceptions.MissingRequiredInputError( "Required inputs {0} were not specified - expected " "inputs: {1}".format(missing_inputs, list(plan[INPUTS]))) # Verify all inputs appear in plan not_expected = [ input_name for input_name in inputs if input_name not in plan[INPUTS] ] if not_expected: raise exceptions.UnknownInputError("Unknown inputs {0} specified - " "expected inputs: {1}".format( not_expected, list(plan[INPUTS]))) plan[INPUTS] = inputs