import { AbstractSession , Headers , ImperativeError } from "@zowe/imperative" ; import { ZosmfRestClient , nozOSMFVersion } from "@zowe/core-for-zowe-sdk" ; import { WorkflowConstants , noWorkflowName , noWorkflowDefinitionFile , noSystemName , noOwner , wrongOwner } from "./WorkflowConstants" ; import { WorkflowValidator } from "./WorkflowValidator" ; import { isNullOrUndefined } from "util" ; import { ICreateWorkflow , accessT } from "./doc/ICreateWorkflow" ; import { ICreatedWorkflow } from "./doc/ICreatedWorkflow" ; import { ICreatedWorkflowLocal } from "./doc/ICreatedWorkflowLocal" ; import { IVariable } from "./doc/IVariable" ; import { Upload , Delete } from "@zowe/zos-files-for-zowe-sdk" ; import { basename } from "path" ; /**
 * Class to handle creation of zOSMF workflow instance
 */ export class CreateWorkflow { param { string } propertiesText returns { IPropertiesInput [ ] } see { IPropertiesInput } memberof public static parseProperties ( propertiesText ) { if ( propertiesText === "" ) { return [ ] ; } return propertiesText . split ( "," ) . map ( ( property ) => { const tempArray = property . split ( "=" ) ; if ( tempArray . length === 2 && tempArray [ 0 ] . length > 0 ) { return { name : tempArray [ 0 ] . trim ( ) , value : tempArray [ 1 ] . trim ( ) } ; } else { throw new ImperativeError ( { msg : `Incorrect properties format: ${ propertiesText }` } ) ; } } ) ; } param { AbstractSession } session param { string } WorkflowName param { string } WorkflowDefinitionFile param { string } systemName param { string } Owner param { string } VariableInputFile param { string } Variables param { boolean } AssignToOwner param { accessT } AccessType param { boolean } DeleteCompletedJobs param { string } zOSMFVersion returns { Promise < ICreatedWorkflow > } public static createWorkflow ( session , WorkflowName , WorkflowDefinitionFile , systemName , Owner , VariableInputFile ? , Variables ? , AssignToOwner ? , AccessType ? , DeleteCompletedJobs ? , zOSMFVersion = WorkflowConstants . ZOSMF_VERSION ) { WorkflowValidator . validateSession ( session ) ; WorkflowValidator . validateNotEmptyString ( zOSMFVersion , nozOSMFVersion . message ) ; WorkflowValidator . validateNotEmptyString ( WorkflowName , noWorkflowName . message ) ; WorkflowValidator . validateNotEmptyString ( WorkflowDefinitionFile , noWorkflowDefinitionFile . message ) ; WorkflowValidator . validateNotEmptyString ( systemName , noSystemName . message ) ; WorkflowValidator . validateNotEmptyString ( Owner , noOwner . message ) ; WorkflowValidator . validateOwner ( Owner , wrongOwner . message ) ; if ( WorkflowDefinitionFile . charAt ( 0 ) === "/" && WorkflowDefinitionFile . charAt ( 1 ) === "/" ) { WorkflowDefinitionFile = WorkflowDefinitionFile . substring ( 1 ) ; } const data = { workflowName : WorkflowName , workflowDefinitionFile : WorkflowDefinitionFile , system : systemName , owner : Owner , assignToOwner : AssignToOwner , accessType : AccessType , deleteCompletedJobs : DeleteCompletedJobs } ; if ( ! isNullOrUndefined ( VariableInputFile ) ) { if ( VariableInputFile . charAt ( 0 ) === "/" && VariableInputFile . charAt ( 1 ) === "/" ) { VariableInputFile = VariableInputFile . substring ( 1 ) ; } data . variableInputFile = VariableInputFile ; } if ( ! isNullOrUndefined ( Variables ) ) { data . variables = this . parseProperties ( Variables ) ; } if ( isNullOrUndefined ( AssignToOwner ) ) { data . assignToOwner = true ; } if ( isNullOrUndefined ( AccessType ) ) { data . accessType = "Public" ; } if ( isNullOrUndefined ( DeleteCompletedJobs ) ) { data . deleteCompletedJobs = false ; } const resourcesQuery = `${ WorkflowConstants . RESOURCE }/${ zOSMFVersion }/${ WorkflowConstants . WORKFLOW_RESOURCE }` ; return ZosmfRestClient . postExpectJSON < ICreatedWorkflow > ( session , resourcesQuery , [ Headers . APPLICATION_JSON ] , data ) ; } param { AbstractSession } session param { string } WorkflowName param { string } WorkflowDefinitionFile param { string } systemName param { string } Owner param { string } VariableInputFile param { string } Variables param { boolean } AssignToOwner param { accessT } AccessType param { boolean } DeleteCompletedJobs param { string } zOSMFVersion param { boolean } keepFiles param { string } customDir returns { Promise < ICreatedWorkflowLocal > } public static async createWorkflowLocal ( session , WorkflowName , WorkflowDefinitionFile , systemName , Owner , VariableInputFile ? , Variables ? , AssignToOwner ? , AccessType ? , DeleteCompletedJobs ? , keepFiles ? , customDir ? , zOSMFVersion = WorkflowConstants . ZOSMF_VERSION ) { WorkflowValidator . validateSession ( session ) ; WorkflowValidator . validateNotEmptyString ( zOSMFVersion , nozOSMFVersion . message ) ; WorkflowValidator . validateNotEmptyString ( WorkflowName , noWorkflowName . message ) ; WorkflowValidator . validateNotEmptyString ( WorkflowDefinitionFile , noWorkflowDefinitionFile . message ) ; WorkflowValidator . validateNotEmptyString ( systemName , noSystemName . message ) ; WorkflowValidator . validateNotEmptyString ( Owner , noOwner . message ) ; WorkflowValidator . validateOwner ( Owner , wrongOwner . message ) ; const tempDefinitionFile = CreateWorkflow . getTempFile ( session . ISession . user , WorkflowDefinitionFile , customDir ) ; await CreateWorkflow . uploadTempFile ( session , WorkflowDefinitionFile , tempDefinitionFile ) ; let tempVariableInputFile ; if ( VariableInputFile ) { tempVariableInputFile = CreateWorkflow . getTempFile ( session . ISession . user , VariableInputFile , customDir ) ; await CreateWorkflow . uploadTempFile ( session , VariableInputFile , tempVariableInputFile ) ; } const resp = await this . createWorkflow ( session , WorkflowName , tempDefinitionFile , systemName , Owner , tempVariableInputFile , Variables , AssignToOwner , AccessType , DeleteCompletedJobs , zOSMFVersion ) ; if ( ! keepFiles ) { resp . failedToDelete = [ await CreateWorkflow . deleteTempFile ( session , tempDefinitionFile ) ] ; if ( VariableInputFile ) { ! resp . failedToDelete [ 0 ] ? resp . failedToDelete = [ await CreateWorkflow . deleteTempFile ( session , tempVariableInputFile ) ] : resp . failedToDelete . push ( await CreateWorkflow . deleteTempFile ( session , tempVariableInputFile ) ) ; } } else { resp . filesKept = [ tempDefinitionFile ] ; if ( VariableInputFile ) { resp . filesKept . push ( tempVariableInputFile ) ; } } return resp ; } param { string } userId param { string } localFile param { string } customDir returns { string } public static getTempFile ( userId , localFile , customDir ? ) { let remoteFile ; if ( customDir ) { remoteFile = customDir + "/" + basename ( localFile ) ; } else { remoteFile = WorkflowConstants . tempPath + "/" + userId + Date . now ( ) . toString ( ) + basename ( localFile ) ; } return remoteFile ; } param { AbstractSession } session param { string } localFile param { string } remoteFile throws public static async uploadTempFile ( session , localFile , remoteFile ) { try { await Upload . fileToUSSFile ( session , localFile , remoteFile , true ) ; } catch ( error ) { throw new ImperativeError ( { msg : "Failed to create temporary uss file\n" + ( error . message ) + "\n" + ( error . additionalDetails ) } ) ; } } param { AbstractSession } session param { string } ussFileName public static async deleteTempFile ( session , ussFileName ) { try { let deletableLocation ; ussFileName . startsWith ( "/" ) ? deletableLocation = ussFileName . slice ( 1 ) : deletableLocation = ussFileName ; await Delete . ussFile ( session , deletableLocation ) ; } catch ( error ) { return ussFileName ; } return ; } }