示例#1
0
        def validate_schema(schema):
            if isinstance(schema, (dict, elements.Dict)):
                if not isinstance(value, dict):
                    raise exceptions.DSLParsingFormatException(
                        1, _expected_type_message(value, dict))
                for key in value:
                    if not isinstance(key, text_type):
                        raise exceptions.DSLParsingFormatException(
                            1, "Dict keys must be strings but"
                            " found '{0}' of type '{1}'".format(
                                key, _py_type_to_user_type(type(key))))

            if strict and isinstance(schema, dict):
                for key in value:
                    if key not in schema:
                        ex = exceptions.DSLParsingFormatException(
                            1, "'{0}' is not in schema. "
                            "Valid schema values: {1}".format(
                                key, list(schema)))
                        for child_element in element.children():
                            if child_element.name == key:
                                ex.element = child_element
                                break
                        raise ex

            if (isinstance(schema, elements.List)
                    and not isinstance(value, list)):
                raise exceptions.DSLParsingFormatException(
                    1, _expected_type_message(value, list))

            if (isinstance(schema, elements.Leaf)
                    and not isinstance(value, schema.type)):
                raise exceptions.DSLParsingFormatException(
                    1, _expected_type_message(value, schema.type))
示例#2
0
 def validate(self, **kwargs):
     imports = [i.value for i in self.children()]
     imports_set = set()
     for _import in imports:
         if _import in imports_set:
             raise exceptions.DSLParsingFormatException(
                 2, 'Duplicate imports')
         imports_set.add(_import)
示例#3
0
 def ancestors_iter(self, element):
     current_element = element
     while True:
         predecessors = self._element_tree.predecessors(current_element)
         if not predecessors:
             return
         if len(predecessors) > 1:
             raise exceptions.DSLParsingFormatException(
                 1, 'More than 1 parent found for {0}'.format(element))
         current_element = predecessors[0]
         yield current_element
示例#4
0
 def validate(self):
     if len(self.children()) < 1:
         raise exceptions.DSLParsingFormatException(
             1, "At least one member should be specified")
 def validate(self):
     if self.initial_value < 0:
         raise exceptions.DSLParsingFormatException(
             1, 'deploy instances must be a non-negative number')
示例#6
0
def load_yaml(raw_yaml, error_message, filename=None):
    try:
        return yaml_loader.load(raw_yaml, filename)
    except yaml.parser.ParserError as ex:
        raise exceptions.DSLParsingFormatException(
            -1, '{0}: Illegal yaml; {1}'.format(error_message, ex))
示例#7
0
    def _extract_element_requirements(element):
        context = element.context
        required_args = {}
        for required_type, requirements in element.requires.items():
            requirements = [
                Requirement(r) if isinstance(r, text_type) else r
                for r in requirements
            ]
            if not requirements:
                # only set required type as a logical dependency
                pass
            elif required_type == 'inputs':
                for input in requirements:
                    if input.name not in context.inputs and input.required:
                        raise exceptions.DSLParsingFormatException(
                            1, "Missing required input '{0}'. "
                            "Existing inputs: ".format(input.name))
                    required_args[input.name] = context.inputs.get(input.name)
            else:
                if required_type == 'self':
                    required_type = type(element)

                if (len(requirements) == 1
                        and requirements[0].predicate == sibling_predicate):
                    # Similar to the other siblings predicate check above,
                    # doing this saves a massive amount of time on larger
                    # blueprints by avoiding n**2 time complexity.
                    required_type_elements = [
                        child for child in element.parent().children()
                        if isinstance(child, required_type)
                    ]
                else:
                    required_type_elements = (
                        context.element_type_to_elements.get(
                            required_type, []))

                for requirement in requirements:
                    result = []
                    for required_element in required_type_elements:
                        if requirement.predicate and not requirement.predicate(
                                element, required_element):
                            continue
                        if requirement.parsed:
                            result.append(required_element.value)
                        else:
                            if (requirement.name
                                    not in required_element.provided):
                                provided = list(required_element.provided)
                                if requirement.required:
                                    raise exceptions.DSLParsingFormatException(
                                        1, "Required value '{0}' is not "
                                        "provided by '{1}'. Provided values "
                                        "are: {2}".format(
                                            requirement.name,
                                            required_element.name, provided))
                                else:
                                    continue
                            result.append(
                                required_element.provided[requirement.name])

                    if len(result) != 1 and not requirement.multiple_results:
                        if requirement.required:
                            raise exceptions.DSLParsingFormatException(
                                1, "Expected exactly one result for "
                                "requirement '{0}' but found {1}".format(
                                    requirement.name,
                                    'none' if not result else result))
                        elif not result:
                            result = [None]
                        else:
                            raise ValueError('Illegal state')

                    if not requirement.multiple_results:
                        result = result[0]
                    required_args[requirement.name] = result

        return required_args
示例#8
0
    def _validate_element_schema(element, strict):
        value = element.initial_value
        if element.required and value is None:
            raise exceptions.DSLParsingFormatException(
                1, "'{0}' key is required but it is currently missing".format(
                    element.name))

        def validate_schema(schema):
            if isinstance(schema, (dict, elements.Dict)):
                if not isinstance(value, dict):
                    raise exceptions.DSLParsingFormatException(
                        1, _expected_type_message(value, dict))
                for key in value:
                    if not isinstance(key, text_type):
                        raise exceptions.DSLParsingFormatException(
                            1, "Dict keys must be strings but"
                            " found '{0}' of type '{1}'".format(
                                key, _py_type_to_user_type(type(key))))

            if strict and isinstance(schema, dict):
                for key in value:
                    if key not in schema:
                        ex = exceptions.DSLParsingFormatException(
                            1, "'{0}' is not in schema. "
                            "Valid schema values: {1}".format(
                                key, list(schema)))
                        for child_element in element.children():
                            if child_element.name == key:
                                ex.element = child_element
                                break
                        raise ex

            if (isinstance(schema, elements.List)
                    and not isinstance(value, list)):
                raise exceptions.DSLParsingFormatException(
                    1, _expected_type_message(value, list))

            if (isinstance(schema, elements.Leaf)
                    and not isinstance(value, schema.type)):
                raise exceptions.DSLParsingFormatException(
                    1, _expected_type_message(value, schema.type))

        if value is not None:
            if isinstance(element.schema, list):
                validated = False
                last_error = None
                for schema_item in element.schema:
                    try:
                        validate_schema(schema_item)
                    except exceptions.DSLParsingFormatException as e:
                        last_error = e
                    else:
                        validated = True
                        break
                if not validated:
                    if not last_error:
                        raise ValueError('Illegal state should have been '
                                         'identified by schema API validation')
                    else:
                        raise last_error
            else:
                validate_schema(element.schema)
示例#9
0
    def _extract_element_requirements(element):
        context = element.context
        required_args = {}
        for required_type, requirements in element.requires.items():
            requirements = [Requirement(r) if isinstance(r, basestring)
                            else r for r in requirements]
            if not requirements:
                # only set required type as a logical dependency
                pass
            elif required_type == 'inputs':
                for input in requirements:
                    if input.name not in context.inputs and input.required:
                        raise exceptions.DSLParsingFormatException(
                            1, "Missing required input '{0}'. "
                               "Existing inputs: "
                               .format(input.name, context.inputs.keys()))
                    required_args[input.name] = context.inputs.get(input.name)
            else:
                if required_type == 'self':
                    required_type = type(element)
                required_type_elements = context.element_type_to_elements.get(
                    required_type, [])
                for requirement in requirements:
                    result = []
                    for required_element in required_type_elements:
                        if requirement.predicate and not requirement.predicate(
                                element, required_element):
                            continue
                        if requirement.parsed:
                            result.append(required_element.value)
                        else:
                            if (requirement.name not in
                                    required_element.provided):
                                provided = required_element.provided.keys()
                                if requirement.required:
                                    raise exceptions.DSLParsingFormatException(
                                        1,
                                        "Required value '{0}' is not "
                                        "provided by '{1}'. Provided values "
                                        "are: {2}"
                                        .format(requirement.name,
                                                required_element.name,
                                                provided))
                                else:
                                    continue
                            result.append(required_element.provided[
                                requirement.name])

                    if len(result) != 1 and not requirement.multiple_results:
                        if requirement.required:
                            raise exceptions.DSLParsingFormatException(
                                1, "Expected exactly one result for "
                                   "requirement '{0}' but found {1}"
                                   .format(requirement.name,
                                           'none' if not result else result))
                        elif not result:
                            result = [None]
                        else:
                            raise ValueError('Illegal state')

                    if not requirement.multiple_results:
                        result = result[0]
                    required_args[requirement.name] = result

        return required_args