def sanitize_for_serialization(obj: Dict[str, Any]) -> Dict[str, Any]: """Return object sanitized for serialization. May be used with a V1alpha1Workflow to sanitize it back to the original state (i.e. per manifest). """ from argo.workflows.client import ApiClient cl = ApiClient() return cl.sanitize_for_serialization(obj)
def sanitize(obj): """Return an object suitable for a JSON post. """ cl = ApiClient() try: return cl.sanitize_for_serialization(obj) except AttributeError: # This catches # AttributeError: 'V1Container' object has no attribute 'swagger_types' d_obj = obj.to_dict() return cl.sanitize_for_serialization(d_obj)
def __deserialize(cls, body: Dict[str, str], *, validate: bool) -> "Workflow": """Deserialize given object into a Workflow instance.""" wf: Union[V1alpha1Workflow, Dict[str, Any]] if validate: attr = type("Response", (), body) wf = ApiClient().deserialize(attr, cls.__model__) else: _LOGGER.warning( "Validation is turned off. This may result in missing or invalid attributes." ) wf = json.loads(body["data"]) self = cls(compile=False) if isinstance(wf, V1alpha1Workflow): self.__dict__.update( api_version=wf.api_version, kind=wf.kind, metadata=wf.metadata, spec=wf.spec, status=wf.status, # a small hack to overcome validation ) else: self.__dict__.update(**wf) self.__validated = validate return self
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"} )
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
def api() -> ApiClient: """Fake API client.""" return ApiClient()
def get_api_client(): configuration = get_configuration() # configuration.api_key['authorization'] = 'YOUR_API_KEY' # TODO verify if we need an api key api_instance = ApiClient(configuration) return api_instance