Ejemplo n.º 1
0
 def _validate_nodefilter_filter(self, node_filter, cap_label=''):
     valid = True
     if cap_label:
         name = 'capability "%s" on nodefilter on template "%s"' % (
             cap_label, self.name)
     else:
         name = 'nodefilter on template "%s"' % self.name
     if not isinstance(node_filter, dict):
         ExceptionCollector.appendException(
             TypeMismatchError(what=name, type='dict'))
         return False
     if 'properties' in node_filter:
         propfilters = node_filter['properties']
         if not isinstance(propfilters, list):
             ExceptionCollector.appendException(
                 TypeMismatchError(what='"properties" of %s' % name,
                                   type='list'))
             return False
         for filter in propfilters:
             if not isinstance(filter, dict):
                 ExceptionCollector.appendException(
                     TypeMismatchError(what='filter in %s' % name,
                                       type='dict'))
                 valid = False
                 continue
             if len(filter) != 1:
                 msg = _(
                     'Invalid %s: only one condition allow per filter condition'
                 ) % name
                 ExceptionCollector.appendException(
                     ValidationError(message=msg))
                 valid = False
                 continue
             # XXX validate filter condition
     return valid
Ejemplo n.º 2
0
    def _validate_requirements(self):
        type_requires = self.type_definition.get_all_requirements()
        allowed_reqs = ["template"]
        if type_requires:
            for treq in type_requires:
                for key, value in treq.items():
                    allowed_reqs.append(key)
                    if isinstance(value, dict):
                        for key in value:
                            allowed_reqs.append(key)

        requires = self.type_definition.get_value(self.REQUIREMENTS,
                                                  self.entity_tpl)
        if requires:
            if not isinstance(requires, list):
                ExceptionCollector.appendException(
                    TypeMismatchError(what='"requirements" of template "%s"' %
                                      self.name,
                                      type='list'))
            else:
                for req in requires:
                    for r1, value in req.items():
                        if isinstance(value, dict):
                            self._validate_requirements_keys(value)
                            self._validate_requirements_properties(value)
                            allowed_reqs.append(r1)
                    self._common_validate_field(req, allowed_reqs,
                                                'requirements')
Ejemplo n.º 3
0
    def __init__(self, name, values):
        self.name = name
        self.tpl = values
        # TOSCA 1.0 backwards compatibility:
        if isinstance(self.tpl, six.string_types):
            tpl = dict(url=self.tpl)
        else:
            tpl = self.tpl
        if isinstance(tpl, dict):
            if URL not in tpl.keys():
                ExceptionCollector.appendException(
                    MissingRequiredFieldError(what=_('repository "%s"') %
                                              self.name,
                                              required='url'))
            for key, value in tpl.items():
                if key not in SECTIONS:
                    ExceptionCollector.appendException(
                        UnknownFieldError(what=_('repository "%s"') % name,
                                          field=key))
                setattr(self, key, value)

            self.validate()
            self.hostname = urlparse(self.url).hostname
        else:
            ExceptionCollector.appendException(
                TypeMismatchError(what=_('repository "%s"') % self.name,
                                  type="dict"))
Ejemplo n.º 4
0
    def _validate_requirements(self):
        type_requires = self.type_definition.get_all_requirements()
        allowed_reqs = ["template"]
        if type_requires:
            for treq in type_requires:
                for key, value in treq.items():
                    allowed_reqs.append(key)
                    if isinstance(value, dict):
                        for key in value:
                            allowed_reqs.append(key)

        requires = self.requirements

        if requires:
            if not isinstance(requires, list):
                ExceptionCollector.appendException(
                    TypeMismatchError(what='"requirements" of template "%s"' %
                                      self.name,
                                      type='list'))
            else:
                for req in requires:
                    if not isinstance(req, dict):
                        ExceptionCollector.appendException(
                            TypeMismatchError(
                                what='a "requirement" in template "%s"' %
                                self.name,
                                type='dict'))
                        continue
                    if len(req) != 1:
                        what = 'requirement "%s" in template "%s"' % (
                            req, self.name)
                        ExceptionCollector.appendException(
                            InvalidPropertyValueError(what=what))
                        continue

                    for r1, value in req.items():
                        if isinstance(value, dict):
                            self._validate_requirements_keys(value)
                            self._validate_requirements_properties(value)
                            node_filter = value.get('node_filter')
                            if node_filter:
                                self._validate_nodefilter(node_filter)
                        elif not isinstance(value, str):
                            msg = 'bad value "%s" for requirement "%s" in template "%s"' % (
                                value, r1, self.name)
                            ExceptionCollector.appendException(
                                ValidationError(message=msg))
Ejemplo n.º 5
0
    def _validate_nodefilter(self, node_filter):
        valid = True
        if not self._validate_nodefilter_filter(node_filter):
            return False

        capfilters = node_filter.get('capabilities')
        if capfilters:
            if not isinstance(capfilters, list):
                ExceptionCollector.appendException(
                    TypeMismatchError(
                        what='"capabilities" of nodefilter in template "%s"' %
                        self.name,
                        type='list'))
                return False
            for capfilter in capfilters:
                if not isinstance(capfilter, dict):
                    ExceptionCollector.appendException(
                        TypeMismatchError(
                            what=
                            'capabilities list item on nodefilter in template "%s"'
                            % self.name,
                            type='dict'))
                    valid = False
                    continue
                if len(capfilter) != 1:
                    msg = _(
                        'Invalid nodefilter on template "%s": only one capability name per list item'
                    ) % self.name
                    ExceptionCollector.appendException(
                        ValidationError(message=msg))
                    valid = False
                    continue
                name, filter = list(capfilter.items())[0]
                if not self._validate_nodefilter_filter(filter, name):
                    valid = False
        return valid
Ejemplo n.º 6
0
 def _validate_properties(self):
     properties = self.type_definition.get_value(self.PROPERTIES,
                                                 self.entity_tpl)
     if isinstance(properties, list):
         properties = {p["title"]: p.get('value') for p in properties}
     if not properties:
         properties = {}
     if not isinstance(properties, dict):
         ExceptionCollector.appendException(
             TypeMismatchError(what='"properties" of template "%s"' %
                               self.name,
                               type='dict'))
         return {}
     self._common_validate_properties(self.type_definition, properties,
                                      self.additionalProperties)
     return properties
Ejemplo n.º 7
0
    def validate(self):
        '''Validate the value by the definition of the datatype.'''

        # A datatype can not have both 'type' and 'properties' definitions.
        # If the datatype has 'type' definition
        if self.datatype.value_type:
            self.value = DataEntity.validate_datatype(self.datatype.value_type,
                                                      self.value, None,
                                                      self.custom_def)
            schema = Schema(self.property_name, self.datatype.defs)
            for constraint in schema.constraints:
                constraint.validate(self.value)
        # If the datatype has 'properties' definition
        else:
            if not isinstance(self.value, dict):
                ExceptionCollector.appendException(
                    TypeMismatchError(what=self.value,
                                      type=self.datatype.type))
            allowed_props = []
            required_props = []
            default_props = {}
            if self.schema:
                allowed_props = self.schema.keys()
                for name, prop_def in self.schema.items():
                    if prop_def.required:
                        required_props.append(name)
                    if prop_def.default:
                        default_props[name] = prop_def.default

            # check allowed field
            for value_key in list(self.value.keys()):
                if value_key not in allowed_props:
                    ExceptionCollector.appendException(
                        UnknownFieldError(what=(_('Data value of type "%s"') %
                                                self.datatype.type),
                                          field=value_key))

            # check default field
            for def_key, def_value in list(default_props.items()):
                if def_key not in list(self.value.keys()):
                    self.value[def_key] = def_value

            # check missing field
            missingprop = []
            for req_key in required_props:
                if req_key not in list(self.value.keys()):
                    missingprop.append(req_key)
            if missingprop:
                ExceptionCollector.appendException(
                    MissingRequiredFieldError(
                        what=(_('Data value of type "%s"') %
                              self.datatype.type),
                        required=missingprop))

            # check every field
            for name, value in list(self.value.items()):
                schema_name = self._find_schema(name)
                if not schema_name:
                    continue
                prop_schema = Schema(name, schema_name)
                # check if field value meets type defined
                DataEntity.validate_datatype(prop_schema.type, value,
                                             prop_schema.entry_schema,
                                             self.custom_def)
                # check if field value meets constraints defined
                if prop_schema.constraints:
                    for constraint in prop_schema.constraints:
                        if isinstance(value, list):
                            for val in value:
                                constraint.validate(val)
                        else:
                            constraint.validate(value)

        return self.value