Beispiel #1
0
 def _validate_string(value,
                      name,
                      min_length=0,
                      max_length=None,
                      pattern=None):
     if value is None:
         if min_length > 0:
             raise exc.InvalidArtifactTypeDefinition(
                 message=_("%(attribute)s is required"), attribute=name)
         else:
             return
     if not isinstance(value, six.string_types):
         raise exc.InvalidArtifactTypeDefinition(
             message=_("%(attribute)s have to be string"), attribute=name)
     if max_length and len(value) > max_length:
         raise exc.InvalidArtifactTypeDefinition(
             message=_("%(attribute)s may not be longer than %(length)i"),
             attribute=name,
             length=max_length)
     if min_length and len(value) < min_length:
         raise exc.InvalidArtifactTypeDefinition(
             message=_("%(attribute)s may not be shorter than %(length)i"),
             attribute=name,
             length=min_length)
     if pattern and not re.match(pattern, value):
         raise exc.InvalidArtifactTypeDefinition(
             message=_("%(attribute)s should match pattern %(pattern)s"),
             attribute=name,
             pattern=pattern.pattern)
Beispiel #2
0
    def __init__(self, type_name, type_display_name, type_version,
                 type_description, endpoint):
        """Initializes the Artifact Type metadata

        :param type_name: name of the artifact type
        :param type_display_name: display name of the artifact type
        :param type_version:  version of the artifact type
        :param type_description: description of the artifact type
        :param endpoint: REST API URI suffix to call the artifacts of this type
        """

        self.attributes = ArtifactAttributes()

        # These are going to be defined by third-party plugin
        # developers, so we need to do some validations on these values and
        # raise InvalidArtifactTypeDefinition if they are violated
        self.type_name = type_name
        self.type_display_name = type_display_name or type_name
        self.type_version = type_version or '1.0'
        self.type_description = type_description
        self.endpoint = endpoint or type_name.lower()

        self._validate_string(self.type_name,
                              'Type name',
                              min_length=1,
                              max_length=255)
        self._validate_string(self.type_display_name,
                              'Type display name',
                              max_length=255)
        self._validate_string(self.type_description, 'Type description')
        self._validate_string(self.endpoint, 'endpoint', min_length=1)
        try:
            semantic_version.Version(self.type_version, partial=True)
        except ValueError:
            raise exc.InvalidArtifactTypeDefinition(
                message=_("Type version has to be a valid semver string"))