import { AbstractSession , Headers , ImperativeError , ImperativeExpect , Logger , RestClient } from "@zowe/imperative" ; import { JobsConstants } from "./JobsConstants" ; import { ZosmfRestClient } from "@zowe/core-for-zowe-sdk" ; import { ICommonJobParms , IGetJobsParms , IJob , IJobFile } from "./" ; export class export class GetJobs { static param { AbstractSession } session returns { Promise < IJob [ ] > } memberof public static getJobs ( session ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJobs()" ) ; return GetJobs . getJobsCommon ( session ) ; } static param { AbstractSession } session param { string } prefix returns { Promise < IJob [ ] > } memberof public static async getJobsByPrefix ( session , prefix ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJobsByPrefix()" ) ; ImperativeExpect . toBeDefinedAndNonBlank ( prefix , "prefix" ) ; return GetJobs . getJobsCommon ( session , { prefix } ) ; } static param { AbstractSession } session param { string } owner returns { Promise < IJob [ ] > } memberof public static async getJobsByOwner ( session , owner ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJobsByOwner()" ) ; ImperativeExpect . toBeDefinedAndNonBlank ( owner , "owner" ) ; return GetJobs . getJobsCommon ( session , { owner } ) ; } static param { AbstractSession } session param { string } owner param { string } prefix returns { Promise < IJob [ ] > } memberof public static async getJobsByOwnerAndPrefix ( session , owner , prefix ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJobsByOwnerAndPrefix()" ) ; ImperativeExpect . toBeDefinedAndNonBlank ( owner , "owner" ) ; ImperativeExpect . toBeDefinedAndNonBlank ( prefix , "prefix" ) ; return GetJobs . getJobsCommon ( session , { owner , prefix } ) ; } static param { AbstractSession } session param { string } jobid returns { Promise < IJob > } memberof public static async getJob ( session , jobid ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJob()" ) ; ImperativeExpect . toBeDefinedAndNonBlank ( jobid , "jobid" ) ; ImperativeExpect . toNotBeNullOrUndefined ( session , "Required session must be defined" ) ; const jobs = await GetJobs . getJobsCommon ( session , { jobid , owner : "*" } ) ; const errorMessagePrefix = "Obtaining job info for a single job id " + jobid + " on " + session . ISession . hostname + ":" + session . ISession . port + " failed: " ; if ( jobs . length === 0 ) { throw new ImperativeError ( { msg : errorMessagePrefix + "Job not found" } ) ; } if ( jobs . length > 1 ) { throw new ImperativeError ( { msg : errorMessagePrefix + "Expected 1 job returned but received " + jobs . length } ) ; } return jobs [ 0 ] ; } static param { AbstractSession } session param { IGetJobsParms } parms returns { Promise < IJob [ ] > } memberof public static async getJobsCommon ( session , parms ? ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJobsCommon()" ) ; ImperativeExpect . toNotBeNullOrUndefined ( session , "Required session must be defined" ) ; let query = JobsConstants . QUERY_ID ; if ( parms ) { if ( parms . owner ) { query += ( JobsConstants . QUERY_OWNER + parms . owner ) ; } if ( parms . prefix ) { if ( parms . prefix !== JobsConstants . DEFAULT_PREFIX ) { if ( RestClient . hasQueryString ( query ) ) { query += JobsConstants . COMBO_ID ; } query += JobsConstants . QUERY_PREFIX + parms . prefix ; } } if ( parms . maxJobs ) { if ( parms . maxJobs !== JobsConstants . DEFAULT_MAX_JOBS ) { if ( RestClient . hasQueryString ( query ) ) { query += JobsConstants . COMBO_ID ; } query += ( JobsConstants . QUERY_MAX_JOBS + parms . maxJobs ) ; } } if ( parms . jobid ) { if ( RestClient . hasQueryString ( query ) ) { query += JobsConstants . COMBO_ID ; } query += ( JobsConstants . QUERY_JOBID + parms . jobid ) ; } } let resource = JobsConstants . RESOURCE ; resource += ( query === JobsConstants . QUERY_ID ) ? "" : query ; Logger . getAppLogger ( ) . info ( "GetJobs.getJobsCommon() resource: " + resource ) ; return ZosmfRestClient . getExpectJSON < IJob [ ] > ( session , resource ) ; } static param { AbstractSession } session param { string } jobname param { string } jobid returns { Promise < IJob > } memberof public static async getStatus ( session , jobname , jobid ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getStatus()" ) ; return GetJobs . getStatusCommon ( session , { jobname , jobid } ) ; } static param { AbstractSession } session param { IJob } job returns { Promise < IJob > } memberof public static async getStatusForJob ( session , job ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getStatusForJob()" ) ; return GetJobs . getStatusCommon ( session , { jobname : job . jobname , jobid : job . jobid } ) ; } static param { AbstractSession } session param { ICommonJobParms } parms returns { Promise < IJob > } memberof public static async getStatusCommon ( session , parms ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getStatusCommon()" ) ; ImperativeExpect . keysToBeDefinedAndNonBlank ( parms , [ "jobname" , "jobid" ] ) ; const parameters = "/" + parms . jobname + "/" + parms . jobid ; Logger . getAppLogger ( ) . info ( "GetJobs.getStatusCommon() parameters: " + parameters ) ; return ZosmfRestClient . getExpectJSON < IJob > ( session , JobsConstants . RESOURCE + parameters ) ; } static param { AbstractSession } session param { string } jobname param { string } jobid returns { Promise < IJobFile [ ] > } memberof public static getSpoolFiles ( session , jobname , jobid ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolFiles()" ) ; return GetJobs . getSpoolFilesCommon ( session , { jobname , jobid } ) ; } static param { AbstractSession } session param { IJob } job returns { Promise < IJobFile [ ] > } memberof public static getSpoolFilesForJob ( session , job ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolFilesForJob()" ) ; return GetJobs . getSpoolFilesCommon ( session , { jobname : job . jobname , jobid : job . jobid } ) ; } static param { AbstractSession } session param { ICommonJobParms } parms returns { Promise < IJobFile [ ] > } memberof public static async getSpoolFilesCommon ( session , parms ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolFilesCommon()" ) ; ImperativeExpect . keysToBeDefinedAndNonBlank ( parms , [ "jobname" , "jobid" ] ) ; const parameters = "/" + parms . jobname + "/" + parms . jobid + JobsConstants . RESOURCE_SPOOL_FILES ; Logger . getAppLogger ( ) . info ( "GetJobs.getSpoolFilesCommon() parameters: " + parameters ) ; return ZosmfRestClient . getExpectJSON < IJobFile [ ] > ( session , JobsConstants . RESOURCE + parameters ) ; } static param { AbstractSession } session param { string } jobname param { string } jobid returns { Promise < IJob > } memberof public static async getJcl ( session , jobname , jobid ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJcl()" ) ; return GetJobs . getJclCommon ( session , { jobname , jobid } ) ; } static param { AbstractSession } session param { IJob } job returns { Promise < string > } memberof public static async getJclForJob ( session , job ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJclForJob()" ) ; return GetJobs . getJclCommon ( session , { jobname : job . jobname , jobid : job . jobid } ) ; } static param { AbstractSession } session param { ICommonJobParms } parms returns { Promise < string > } memberof public static async getJclCommon ( session , parms ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getJclCommon()" ) ; ImperativeExpect . keysToBeDefinedAndNonBlank ( parms , [ "jobname" , "jobid" ] ) ; const parameters = "/" + parms . jobname + "/" + parms . jobid + JobsConstants . RESOURCE_SPOOL_FILES + JobsConstants . RESOURCE_JCL_CONTENT + JobsConstants . RESOURCE_SPOOL_CONTENT ; Logger . getAppLogger ( ) . info ( "GetJobs.getJclCommon() parameters: " + parameters ) ; return ZosmfRestClient . getExpectString ( session , JobsConstants . RESOURCE + parameters ) ; } static param { AbstractSession } session param jobFile returns { Promise < string > } memberof public static getSpoolContent ( session , jobFile ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolContent()" ) ; return GetJobs . getSpoolContentCommon ( session , jobFile ) ; } static param { AbstractSession } session param jobname param jobid param spoolId returns { Promise < string > } memberof public static async getSpoolContentById ( session , jobname , jobid , spoolId ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolContentById()" ) ; ImperativeExpect . toNotBeNullOrUndefined ( jobname , "Required parameter jobname must be defined" ) ; ImperativeExpect . toNotBeNullOrUndefined ( jobid , "Required parameter jobid must be defined" ) ; ImperativeExpect . toNotBeNullOrUndefined ( spoolId , "Required parameter spoolId must be defined" ) ; const parameters = "/" + jobname + "/" + jobid + JobsConstants . RESOURCE_SPOOL_FILES + "/" + spoolId + JobsConstants . RESOURCE_SPOOL_CONTENT ; Logger . getAppLogger ( ) . info ( "GetJobs.getSpoolContentById() parameters: " + parameters ) ; return ZosmfRestClient . getExpectString ( session , JobsConstants . RESOURCE + parameters , [ Headers . TEXT_PLAIN_UTF8 ] ) ; } static param { AbstractSession } session param jobFile returns { Promise < string > } memberof public static async getSpoolContentCommon ( session , jobFile ) { Logger . getAppLogger ( ) . trace ( "GetJobs.getSpoolContentCommon()" ) ; ImperativeExpect . toNotBeNullOrUndefined ( jobFile , "Required job file object must be defined" ) ; const parameters = "/" + jobFile . jobname + "/" + jobFile . jobid + JobsConstants . RESOURCE_SPOOL_FILES + "/" + jobFile . id + JobsConstants . RESOURCE_SPOOL_CONTENT ; Logger . getAppLogger ( ) . info ( "GetJobs.getSpoolContentCommon() parameters: " + parameters ) ; return ZosmfRestClient . getExpectString ( session , JobsConstants . RESOURCE + parameters , [ Headers . TEXT_PLAIN_UTF8 ] ) ; } }