示例#1
0
        def _compile(obj: Any):
            if hasattr(obj, "__model__"):
                if obj.model is not None:
                    # prevents referenced templates from being compiled again
                    return obj.model

                args: Dict[str, Any] = {}
                props: Dict[str, Any] = getattr(obj.fget, "__props__", {})

                arguments: V1alpha1Arguments = props.get("arguments")
                if arguments:
                    for artifact in getattr(arguments, "artifacts", []) or []:
                        if hasattr(artifact, "to_dict"):
                            artifact = V1alpha1Artifact(**artifact.to_dict())
                        else:
                            artifact = V1alpha1Artifact(**artifact)
                        args[underscore(artifact.name)] = artifact

                    for param in getattr(arguments, "parameters", []) or []:
                        if hasattr(param, "to_dict"):
                            param = V1alpha1Parameter(**param.to_dict())
                        else:
                            param = V1alpha1Parameter(**param)
                        args[underscore(param.name)] = param

                return obj.__get__(self).__call__(**args)
            if isinstance(obj, list):
                return list(map(_compile, obj))
            if hasattr(obj, "attribute_map"):
                for attr in obj.attribute_map:
                    value: Any = _compile(getattr(obj, attr))
                    setattr(obj, attr, value)

            return obj
示例#2
0
    def test_submit(self, api: ApiClient, wf: Workflow) -> None:
        """Test `Workflow.submit` method."""
        fake_workflow_name = "test"
        flexmock(WorkflowServiceApi).should_receive("create_workflow").and_return(
            fake_workflow_name
        )

        # submit w/o parameters
        workflow_name: str = wf.submit(client=ApiClient(), namespace="test")

        assert isinstance(workflow_name, str)
        assert workflow_name == "test"

        # submit w/ parameters
        with pytest.raises(AttributeError) as exc:
            # Expected ValueError due to undefined parameter
            workflow_name: str = wf.submit(
                client=api, namespace="test", parameters={"param": "test"}
            )

        wf.spec.arguments = V1alpha1Arguments(
            parameters=[V1alpha1Parameter(name="param")]
        )
        workflow_result: str = wf.submit(
            client=api, namespace="test", parameters={"param": "test"}
        )
示例#3
0
    def test_submit(self, api: V1alpha1Api, wf: Workflow) -> None:
        """Test `Workflow.submit` method."""
        fake_workflow_name = "test"
        flexmock(V1alpha1Api).should_receive("create_namespaced_workflow").and_return(
            fake_workflow_name
        )

        # submit w/o parameters
        workflow_name: str = wf.submit(client=V1alpha1Api(), namespace="test")

        assert isinstance(workflow_name, str)
        assert workflow_name == "test"

        # submit w/ parameters
        with pytest.raises(AttributeError) as exc:
            # Expected ValueError due to undefined parameter
            workflow_name: str = wf.submit(
                client=api, namespace="test", parameters={"param": "test"}
            )

        wf.spec.arguments = V1alpha1Arguments(
            parameters=[V1alpha1Parameter(name="param")]
        )
        workflow_result: str = wf.submit(
            client=api, namespace="test", parameters={"param": "test"}
        )

        assert isinstance(workflow_result, V1alpha1Workflow)
        assert isinstance(workflow_result.metadata.name, str)
        assert len(workflow_result.spec.arguments.parameters) == 1
        assert workflow_result.spec.arguments.parameters[0].name == 'param'
        assert workflow_result.spec.arguments.parameters[0].value == 'test'
        assert workflow_result.metadata.name == "test"
示例#4
0
    def submit(
        self,
        client: ApiClient,
        namespace: str,
        *,
        parameters: Optional[Dict[str, str]] = None,
    ) -> V1alpha1CronWorkflow:
        """Submit an Argo CronWorkflow to a given namespace.

        :returns: V1alpha1CronWorkflow, submitted CronWorkflow
        """
        parameters = parameters or {}

        new_parameters: List[V1alpha1Parameter] = []
        for name, value in parameters.items():
            param = V1alpha1Parameter(name=name, value=value)
            new_parameters.append(param)

        if getattr(self.spec.workflow_spec, "arguments"):
            for p in getattr(self.spec.workflow_spec.arguments, "parameters",
                             []):
                if p.name in parameters:
                    continue  # overridden
                elif not getattr(p, "value"):
                    default = getattr(p, "default")
                    if default is not None:
                        p.value = default
                    else:
                        raise Exception(
                            f"Missing required workflow parameter {p.name}")

                new_parameters.append(p)

            self.spec.arguments.parameters = new_parameters
        elif parameters:
            raise AttributeError(
                "The CronWorkflow doesn't take any parameters.")

        body: Dict[str, Any]
        if not getattr(self, "validated", True):
            _LOGGER.debug("The CronWorkflow has not been previously validated."
                          "Sanitizing for serialization.")
            body = camelize(self.to_dict())
        else:
            body = client.sanitize_for_serialization(self)

        service = CronWorkflowServiceApi(api_client=client)
        # submit the workflow
        created: V1alpha1CronWorkflow = service.create_cron_workflow(
            namespace, V1alpha1CreateCronWorkflowRequest(cron_workflow=body))

        # return the computed CronWorkflow
        return created
示例#5
0
    def submit(
        self,
        client: client.V1alpha1Api,
        namespace: str,
        *,
        parameters: Optional[Dict[str, str]] = None,
    ) -> str:
        """Submit an Argo Workflow to a given namespace.

        :returns: str, Workflow name
        """
        parameters = parameters or {}

        new_parameters: List[V1alpha1Parameter] = []
        for name, value in parameters.items():
            param = V1alpha1Parameter(name=name, value=value)
            new_parameters.append(param)

        if hasattr(self.spec, "arguments"):
            for p in getattr(self.spec.arguments, "parameters", []):
                if p.name in parameters:
                    continue  # overridden
                elif not getattr(p, "value"):
                    default = getattr(p, "default")
                    if default is not None:
                        p.value = default
                    else:
                        raise Exception(
                            f"Missing required workflow parameter {p.name}")

                new_parameters.append(p)

            self.spec.arguments.parameters = new_parameters

        body: Dict[str, Any]
        if not getattr(self, "validated", True):
            _LOGGER.debug("The Workflow has not been previously validated."
                          "Sanitizing for serialization.")
            body = camelize(self.to_dict())
        else:
            body = client.api_client.sanitize_for_serialization(self)

        # submit the workflow
        created: models.V1alpha1Workflow = client.create_namespaced_workflow(
            namespace, body)

        # return the computed Workflow ID
        return self.name