Beispiel #1
0
 def _validate(self, value):
     if isinstance(value, six.string_types):
         param = PARAM_REGEX.search(value)
         if not param:
             super(RefOrObject, self)._validate(value)
     else:
         super(RefOrObject, self)._validate(value)
Beispiel #2
0
 def _validate(self, value):
     if isinstance(value, str):
         param = PARAM_REGEX.search(value)
         if not param:
             super()._validate(value)
     else:
         super()._validate(value)
Beispiel #3
0
    def entity_value(self) -> Optional[str]:
        if not self.is_ref:
            return None
        value_parts = PARAM_REGEX.search(self.value)
        if value_parts:
            value_parts = value_parts.group(1)
        else:
            value_parts = self.value

        return value_parts.split(".")[-1]
Beispiel #4
0
def get_ref_or_obj(container, value):
    try:
        return container.deserialize(value)
    except (ValueError, TypeError, ValidationError):
        pass

    if not isinstance(value, six.string_types):
        raise ValidationError(
            "This field expects an {container} or a str containing a param reference."
            .format(container=container.__class__.__name__))

    param = PARAM_REGEX.search(value)
    if not param:
        raise ValidationError(
            "This field expects an {container} or a param ref inside {{  }}.".
            format(container=container.__class__.__name__))
    return value
Beispiel #5
0
def validate_image(image, allow_none=False):
    if not image:
        if allow_none:
            return
        else:
            raise ValidationError("Image is required")
    param = PARAM_REGEX.search(image)
    if param:
        return
    if " " in image:
        raise ValidationError("Invalid docker image `{}`".format(image))
    tagged_image = image.split(":")
    if len(tagged_image) > 3:
        raise ValidationError("Invalid docker image `{}`".format(image))
    if len(tagged_image) == 3 and ("/" not in tagged_image[1]
                                   or tagged_image[1].startswith("/")):
        raise ValidationError("Invalid docker image `{}`".format(image))
Beispiel #6
0
    def get_spec(self, name: str, iotype: str, is_flag: bool):
        """
        Checks if the value is param ref and validates it.

        returns: ParamSpec or None
        raises: ValidationError
        """
        if not self.is_literal:
            # value validation is the same for search and ref
            value_parts = PARAM_REGEX.search(self.value)
            if value_parts:
                value_parts = value_parts.group(1)
            else:
                value_parts = self.value

            value_parts = value_parts.split(".")
            if len(value_parts) > 3:
                raise ValidationError(
                    "Could not parse value `{}` for param `{}`.".format(
                        self.value, name))
            if len(value_parts) == 1 and value_parts[0] not in CONTEXTS:
                raise ValidationError(
                    "Received an invalid value `{}` for param `{}`. "
                    "Value must be one of `{}`".format(self.value, name,
                                                       CONTEXTS))
            # Check the case of current DAG, it should not allow to use outputs
            if len(value_parts
                   ) == 1 and self.is_dag_ref and value_parts[0] == OUTPUTS:
                raise ValidationError(
                    "Received an invalid value `{}` for param `{}`. "
                    "You can not use `{}` of current dag".format(
                        self.value, name, OUTPUTS))
            if len(value_parts
                   ) == 2 and value_parts[0] not in CONTEXTS_WITH_NESTING:
                raise ValidationError(
                    "Received an invalid value `{}` for param `{}`. "
                    "Value `{}` must be one of `{}`".format(
                        self.value, name, value_parts[0],
                        CONTEXTS_WITH_NESTING))
            if len(value_parts) == 3 and value_parts[0] != EVENTS:
                raise ValidationError(
                    "Received an invalid value `{}` for param `{}`. "
                    "Value `{}` must can only be equal to `{}`".format(
                        self.value, name, value_parts[0], EVENTS))
        if self.is_ref:
            # validate ref
            ref_parts = self.ref.split(".")
            if len(ref_parts) > 2:
                raise ValidationError(
                    "Could not parse ref `{}` for param `{}`.".format(
                        self.ref, name))
            if len(ref_parts) == 1 and ref_parts[0] != DAG:
                raise ValidationError(
                    "Could not parse ref `{}` for param `{}`.".format(
                        ref_parts[0], name))
            if len(ref_parts) == 2 and ref_parts[0] not in ENTITIES:
                raise ValidationError(
                    "Could not parse ref `{}` for param `{}`. "
                    "Ref must be one of `{}`".format(ref_parts[0], name,
                                                     ENTITIES))
            if ref_parts[0] == RUNS:
                try:
                    uuid.UUID(ref_parts[1])
                except (KeyError, ValueError):
                    raise ValidationError(
                        "Param value `{}` should reference a valid run uuid.".
                        format(ref_parts[1]))

        return ParamSpec(
            name=name,
            iotype=iotype,
            param=self,
            is_flag=is_flag,
        )
Beispiel #7
0
def get_param(name, value, iotype, is_flag):
    """
    Checks if the value is param ref and validates it.

    returns: ParamSpec or None
    raises: ValidationError
    """
    if not isinstance(value, six.string_types):
        return ParamSpec(
            name=name,
            iotype=iotype,
            value=value,
            entity=None,
            entity_ref=None,
            entity_value=None,
            is_flag=is_flag,
        )

    param = PARAM_REGEX.search(value)
    if not param:
        return ParamSpec(
            name=name,
            iotype=iotype,
            value=value,
            entity=None,
            entity_ref=None,
            entity_value=None,
            is_flag=is_flag,
        )

    param = param.group(1)
    param_parts = param.split(".")
    if len(param_parts) < 3 or len(param_parts) > 4:
        raise ValidationError(
            "Could not parse value `{}` for param `{}`.".format(value, name)
        )
    if param_parts[0] not in ENTITIES:
        raise ValidationError(
            "Could not parse value `{}` for param `{}`.".format(value, name)
        )

    def process_pipeline_param():
        if param_parts[0] != DAG or param_parts[1] != INPUTS:
            raise ValidationError(
                "Param `{}` value `{}` is not valid, "
                "it should follow a format "
                "`pipeline.outputs.name`.".format(name, value)
            )
        return param_parts[0], DAG_ENTITY_REF, param_parts[-1]

    def process_op_param():
        if param_parts[0] == OPS:
            if param_parts[2] not in {INPUTS, OUTPUTS}:
                raise ValidationError(
                    "Param `{}` value `{}` is not valid, "
                    "it should follow a format "
                    "`ops.entity-id.[inputs/outputs].name`.".format(name, value)
                )
        elif param_parts[0] == RUNS:
            try:
                uuid.UUID(param_parts[1])
            except (KeyError, ValueError):
                raise ValidationError(
                    "Param value `{}` should reference a valid uuid.".format(
                        value, param_parts[1]
                    )
                )

            if param_parts[2] != OUTPUTS:
                raise ValidationError(
                    "Param `{}` value `{}` is not valid, "
                    "it should follow a format "
                    "`runs.uuid.outputs.name`.".format(name, value)
                )
        else:
            raise ValidationError(
                "Param `{}` value `{}` is not valid, "
                "it should follow a format "
                "`entity.entity-id.[inputs/outputs].name`.".format(name, value)
            )

        return param_parts[0], param_parts[1], param_parts[-1]

    if len(param_parts) == 3:
        entity, entity_ref, entity_value = process_pipeline_param()
    elif len(param_parts) == 4:
        entity, entity_ref, entity_value = process_op_param()
    else:
        raise ValidationError("Param `{}` value `{}` is not valid.".format(name, value))

    return ParamSpec(
        name=name,
        iotype=iotype,
        value=param,
        entity=entity,
        entity_ref=entity_ref,
        entity_value=entity_value,
        is_flag=is_flag,
    )