import { AbstractSession , Headers , IHeaderContent , ImperativeError , ImperativeExpect , Logger , TextUtils } from "@zowe/imperative" ; import { isNullOrUndefined } from "util" ; import { ZosmfHeaders , ZosmfRestClient } from "@zowe/core-for-zowe-sdk" ; import { ZosFilesConstants } from "../../constants/ZosFiles.constants" ; import { ZosFilesMessages } from "../../constants/ZosFiles.messages" ; import { IZosFilesResponse } from "../../doc/IZosFilesResponse" ; import { CreateDefaults } from "./Create.defaults" ; import { CreateDataSetTypeEnum } from "./CreateDataSetType.enum" ; import { ICreateDataSetOptions } from "./doc/ICreateDataSetOptions" ; import { Invoke } from "../invoke" ; import { ICreateVsamOptions } from "./doc/ICreateVsamOptions" ; import { ICreateZfsOptions } from "./doc/ICreateZfsOptions" ; import * as path from "path" ; import { IZosFilesOptions } from "../../doc/IZosFilesOptions" ; /**
 * Class to handle creation of data sets
 */ export class Create { param { AbstractSession } session param { CreateDataSetTypeEnum } cmdType param { string } dataSetName param { Partial < ICreateDataSetOptions > } options returns { Promise < IZosFilesResponse > } public static async dataSet ( session , cmdType , dataSetName , options ? ) { let validCmdType = true ; let tempOptions = ! isNullOrUndefined ( options ) ? JSON . parse ( JSON . stringify ( options ) ) : { } ; ImperativeExpect . toNotBeNullOrUndefined ( cmdType , ZosFilesMessages . missingDatasetType . message ) ; ImperativeExpect . toNotBeNullOrUndefined ( dataSetName , ZosFilesMessages . missingDatasetName . message ) ; switch ( cmdType ) { case CreateDataSetTypeEnum . DATA_SET_PARTITIONED : tempOptions = { ... CreateDefaults . DATA_SET . PARTITIONED , ... tempOptions } ; break ; case CreateDataSetTypeEnum . DATA_SET_SEQUENTIAL : tempOptions = { ... CreateDefaults . DATA_SET . SEQUENTIAL , ... tempOptions } ; break ; case CreateDataSetTypeEnum . DATA_SET_BINARY : tempOptions = { ... CreateDefaults . DATA_SET . BINARY , ... tempOptions } ; break ; case CreateDataSetTypeEnum . DATA_SET_C : tempOptions = { ... CreateDefaults . DATA_SET . C , ... tempOptions } ; break ; case CreateDataSetTypeEnum . DATA_SET_CLASSIC : tempOptions = { ... CreateDefaults . DATA_SET . CLASSIC , ... tempOptions } ; break ; case CreateDataSetTypeEnum . DATA_SET_BLANK : tempOptions = { ... CreateDefaults . DATA_SET . BLANK , ... tempOptions } ; break ; default : validCmdType = false ; break ; } if ( ! validCmdType ) { throw new ImperativeError ( { msg : ZosFilesMessages . unsupportedDatasetType . message } ) ; } else { if ( ! isNullOrUndefined ( tempOptions . size ) ) { const tAlcunit = tempOptions . size . toString ( ) . match ( /[a-zA-Z]+/g ) ; if ( ! isNullOrUndefined ( tAlcunit ) ) { tempOptions . alcunit = tAlcunit . join ( "" ) . toUpperCase ( ) ; } const tPrimary = tempOptions . size . toString ( ) . match ( /[0-9]+/g ) ; if ( ! isNullOrUndefined ( tPrimary ) ) { tempOptions . primary = + ( tPrimary . join ( "" ) ) ; if ( isNullOrUndefined ( tempOptions . secondary ) ) { const TEN_PERCENT = 0.10 ; tempOptions . secondary = Math . round ( tempOptions . primary * TEN_PERCENT ) ; } } } else { if ( isNullOrUndefined ( tempOptions . secondary ) ) { if ( cmdType !== CreateDataSetTypeEnum . DATA_SET_BINARY ) { tempOptions . secondary = 1 ; } else { tempOptions . secondary = 10 ; } } } delete tempOptions . size ; let response = "" ; if ( ! isNullOrUndefined ( tempOptions . showAttributes ) ) { if ( tempOptions . showAttributes ) { delete tempOptions . showAttributes ; response = TextUtils . prettyJson ( tempOptions ) ; } else { delete tempOptions . showAttributes ; } } const endpoint = ZosFilesConstants . RESOURCE + ZosFilesConstants . RES_DS_FILES + "/" + dataSetName ; const headers = [ ZosmfHeaders . ACCEPT_ENCODING ] ; if ( options && options . responseTimeout != null ) { headers . push ( { [ ZosmfHeaders . X_IBM_RESPONSE_TIMEOUT ] : options . responseTimeout . toString ( ) } ) ; } Create . dataSetValidateOptions ( tempOptions ) ; await ZosmfRestClient . postExpectString ( session , endpoint , headers , JSON . stringify ( tempOptions ) ) ; return { success : true , commandResponse : response + ZosFilesMessages . dataSetCreatedSuccessfully . message } ; } } public static async dataSetLike ( session , dataSetName , likeDataSetName , options ? ) { ImperativeExpect . toNotBeNullOrUndefined ( dataSetName , ZosFilesMessages . missingDatasetName . message ) ; ImperativeExpect . toNotBeNullOrUndefined ( likeDataSetName , ZosFilesMessages . missingDatasetLikeName . message ) ; const tempOptions = JSON . parse ( JSON . stringify ( { like : likeDataSetName , ... ( options || { } ) } ) ) ; Create . dataSetValidateOptions ( tempOptions ) ; const endpoint = ZosFilesConstants . RESOURCE + ZosFilesConstants . RES_DS_FILES + "/" + dataSetName ; await ZosmfRestClient . postExpectString ( session , endpoint , [ ZosmfHeaders . ACCEPT_ENCODING ] , JSON . stringify ( tempOptions ) ) ; return { success : true , commandResponse : ZosFilesMessages . dataSetCreatedSuccessfully . message } ; } static param options public static dataSetValidateOptions ( options ) { ImperativeExpect . toNotBeNullOrUndefined ( options , ZosFilesMessages . missingFilesCreateOptions . message ) ; const tempOptions = options ; for ( const option in tempOptions ) { if ( Object . prototype . hasOwnProperty . call ( tempOptions , option ) ) { switch ( option ) { case "alcunit" : if ( isNullOrUndefined ( tempOptions . alcunit ) ) { tempOptions . alcunit = "TRK" ; } switch ( tempOptions . alcunit . toUpperCase ( ) ) { case "CYL" : case "TRK" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidAlcunitOption . message + tempOptions . alcunit } ) ; } break ; case "avgblk" : break ; case "blksize" : if ( isNullOrUndefined ( tempOptions . blksize ) ) { tempOptions . blksize = tempOptions . lrecl ; } break ; case "lrecl" : ImperativeExpect . toNotBeNullOrUndefined ( tempOptions . lrecl , ZosFilesMessages . missingRecordLength . message ) ; break ; case "dirblk" : if ( tempOptions . dirblk !== 0 && tempOptions . dsorg === "PS" ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidPSDsorgDirblkCombination . message } ) ; } if ( tempOptions . dirblk === 0 && tempOptions . dsorg === "PO" ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidPODsorgDirblkCombination . message } ) ; } break ; case "dsntype" : { const type = tempOptions . dsntype . toUpperCase ( ) ; const availableTypes = [ "BASIC" , "EXTPREF" , "EXTREQ" , "HFS" , "LARGE" , "PDS" , "LIBRARY" , "PIPE" ] ; if ( availableTypes . indexOf ( type ) === - 1 ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidDsntypeOption . message + tempOptions . dsntype } ) ; } break ; } case "dsorg" : switch ( tempOptions . dsorg . toUpperCase ( ) ) { case "PO" : case "PS" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidDsorgOption . message + tempOptions . dsorg } ) ; } break ; case "primary" : ImperativeExpect . toNotBeNullOrUndefined ( tempOptions . primary , ZosFilesMessages . missingPrimary . message ) ; if ( tempOptions . primary > ZosFilesConstants . MAX_ALLOC_QUANTITY ) { throw new ImperativeError ( { msg : ZosFilesMessages . maximumAllocationQuantityExceeded . message + " for 'primary'." } ) ; } break ; case "secondary" : if ( isNullOrUndefined ( tempOptions . secondary ) ) { tempOptions . secondary = 0 ; } if ( tempOptions . secondary > ZosFilesConstants . MAX_ALLOC_QUANTITY ) { throw new ImperativeError ( { msg : ZosFilesMessages . maximumAllocationQuantityExceeded . message + " for 'secondary'." } ) ; } break ; case "recfm" : if ( isNullOrUndefined ( tempOptions . recfm ) ) { tempOptions . recfm = "F" ; } switch ( tempOptions . recfm . toUpperCase ( ) ) { case "F" : case "FB" : case "V" : case "VB" : case "U" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidRecfmOption . message + tempOptions . recfm } ) ; } break ; case "mgntclass" : case "storclass" : case "dataclass" : break ; case "unit" : case "volser" : case "responseTimeout" : case "like" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidFilesCreateOption . message + option } ) ; } } } } param { AbstractSession } session param { string } dataSetName param { Partial < ICreateVsamOptions > } options example returns { Promise < IZosFilesResponse > } public static async vsam ( session , dataSetName , options ? ) { ImperativeExpect . toNotBeNullOrUndefined ( dataSetName , ZosFilesMessages . missingDatasetName . message ) ; const idcamsOptions = this . vsamConvertToIdcamsOptions ( options ) ; let attribText = "" ; if ( ! isNullOrUndefined ( idcamsOptions . showAttributes ) ) { if ( idcamsOptions . showAttributes ) { delete idcamsOptions . showAttributes ; attribText = ZosFilesMessages . attributeTitle . message + TextUtils . prettyJson ( idcamsOptions ) ; } else { delete idcamsOptions . showAttributes ; } } let respTimeout ; if ( options ) { respTimeout = options . responseTimeout ; } try { this . vsamValidateOptions ( idcamsOptions ) ; const idcamsCmds = this . vsamFormIdcamsCreateCmd ( dataSetName , idcamsOptions ) ; Logger . getAppLogger ( ) . debug ( "Invoking this IDCAMS command:\n" + idcamsCmds . join ( "\n" ) ) ; const idcamsResponse = await Invoke . ams ( session , idcamsCmds , { responseTimeout : respTimeout } ) ; return { success : true , commandResponse : attribText + ZosFilesMessages . dataSetCreatedSuccessfully . message , apiResponse : idcamsResponse } ; } catch ( error ) { const impErr = new ImperativeError ( { msg : attribText + error . mDetails . msg , causeErrors : error . mDetails . causeErrors , additionalDetails : error . mDetails . additionalDetails } ) ; Logger . getAppLogger ( ) . error ( impErr . toString ( ) ) ; throw impErr ; } } param { AbstractSession } session param { string } ussPath param { string } type param { string } mode returns { Promise < IZosFilesResponse > } public static async uss ( session , ussPath , type , mode ? , options ? ) { ImperativeExpect . toNotBeNullOrUndefined ( type , ZosFilesMessages . missingRequestType . message ) ; ImperativeExpect . toNotBeEqual ( type , "" , ZosFilesMessages . missingRequestType . message ) ; ussPath = path . posix . normalize ( ussPath ) ; ussPath = ussPath . charAt ( 0 ) === "/" ? ussPath . substring ( 1 ) : ussPath ; ussPath = encodeURIComponent ( ussPath ) ; const parameters = `${ ZosFilesConstants . RESOURCE }${ ZosFilesConstants . RES_USS_FILES }/${ ussPath }` ; const headers = [ Headers . APPLICATION_JSON , ZosmfHeaders . ACCEPT_ENCODING ] ; if ( options && options . responseTimeout != null ) { headers . push ( { [ ZosmfHeaders . X_IBM_RESPONSE_TIMEOUT ] : options . responseTimeout . toString ( ) } ) ; } let payload = { type } ; if ( mode ) { payload = { ... payload , ... { mode } } ; } const data = await ZosmfRestClient . postExpectString ( session , parameters , headers , payload ) ; return { success : true , commandResponse : ZosFilesMessages . ussCreatedSuccessfully . message , apiResponse : data } ; } public static async zfs ( session , fileSystemName , options ? ) { ImperativeExpect . toNotBeNullOrUndefined ( fileSystemName , ZosFilesMessages . missingFileSystemName . message ) ; const tempOptions = ! isNullOrUndefined ( options ) ? JSON . parse ( JSON . stringify ( options ) ) : { } ; let endpoint = ZosFilesConstants . RESOURCE + ZosFilesConstants . RES_ZFS_FILES + "/" + fileSystemName ; this . zfsValidateOptions ( tempOptions ) ; tempOptions . JSONversion = 1 ; if ( ! isNullOrUndefined ( tempOptions . timeout ) ) { endpoint += `?timeout=${ tempOptions . timeout }` ; delete tempOptions . timeout ; } const jsonContent = JSON . stringify ( tempOptions ) ; const headers = [ { "Content-Length" : jsonContent . length } , ZosmfHeaders . ACCEPT_ENCODING ] ; const data = await ZosmfRestClient . postExpectString ( session , endpoint , headers , jsonContent ) ; return { success : true , commandResponse : ZosFilesMessages . zfsCreatedSuccessfully . message , apiResponse : data } ; } param { ICreateVsamOptions } cliOptions returns { ICreateVsamOptions } private static vsamConvertToIdcamsOptions ( cliOptions ) { let idcamsOptions = isNullOrUndefined ( cliOptions ) ? { } : JSON . parse ( JSON . stringify ( cliOptions ) ) ; let matchArray ; if ( idcamsOptions . size ) { idcamsOptions . size = idcamsOptions . size . toUpperCase ( ) ; matchArray = idcamsOptions . size . match ( /[A-Z]+/g ) ; if ( matchArray ) { idcamsOptions . alcunit = matchArray [ 0 ] ; } matchArray = idcamsOptions . size . match ( /[0-9]+/g ) ; if ( matchArray ) { idcamsOptions . primary = matchArray [ 0 ] ; } delete idcamsOptions . size ; } idcamsOptions = { ... CreateDefaults . VSAM , ... idcamsOptions } ; if ( isNullOrUndefined ( idcamsOptions . secondary ) ) { const tenPercent = 0.10 ; idcamsOptions . secondary = Math . round ( idcamsOptions . primary * tenPercent ) ; } return idcamsOptions ; } param { string } dataSetName param options returns { string } private static vsamFormIdcamsCreateCmd ( dataSetName , options ) { return [ "DEFINE CLUSTER -\n" + "(" + "NAME('" + dataSetName . toUpperCase ( ) + "') -\n" + options . dsorg . toUpperCase ( ) + " -\n" + options . alcunit . toUpperCase ( ) + "(" + options . primary + " " + options . secondary + ")" + " -\n" + ( options . retainTo ? "TO(" + options . retainTo + ") -\n" : "" ) + ( options . retainFor ? "FOR(" + options . retainFor + ") -\n" : "" ) + ( options . volumes ? "VOLUMES(" + options . volumes . toUpperCase ( ) + ") -\n" : "" ) + ( options . storclass ? "STORAGECLASS(" + options . storclass + ") -\n" : "" ) + ( options . mgntclass ? "MANAGEMENTCLASS(" + options . mgntclass + ") -\n" : "" ) + ( options . dataclass ? "DATACLASS(" + options . dataclass + ") -\n" : "" ) + ")" ] ; } param options private static vsamValidateOptions ( options ) { ImperativeExpect . toNotBeNullOrUndefined ( options , ZosFilesMessages . missingFilesCreateOptions . message ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . dsorg , ZosFilesMessages . missingVsamOption . message + "dsorg" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . alcunit , ZosFilesMessages . missingVsamOption . message + "alcunit" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . primary , ZosFilesMessages . missingVsamOption . message + "primary" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . secondary , ZosFilesMessages . missingVsamOption . message + "secondary" ) ; for ( const option in options ) { if ( Object . prototype . hasOwnProperty . call ( options , option ) ) { switch ( option ) { case "dsorg" : if ( ! ZosFilesConstants . VSAM_DSORG_CHOICES . includes ( options . dsorg . toUpperCase ( ) ) ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidDsorgOption . message + options . dsorg } ) ; } break ; case "alcunit" : if ( ! ZosFilesConstants . VSAM_ALCUNIT_CHOICES . includes ( options . alcunit . toUpperCase ( ) ) ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidAlcunitOption . message + options . alcunit } ) ; } break ; case "primary" : case "secondary" : if ( options [ option ] > ZosFilesConstants . MAX_ALLOC_QUANTITY ) { throw new ImperativeError ( { msg : ZosFilesMessages . maximumAllocationQuantityExceeded . message + " " + ZosFilesMessages . commonFor . message + " '" + option + "' " + ZosFilesMessages . commonWithValue . message + " = " + options [ option ] + "." } ) ; } break ; case "retainFor" : if ( options [ option ] < ZosFilesConstants . MIN_RETAIN_DAYS || options [ option ] > ZosFilesConstants . MAX_RETAIN_DAYS ) { throw new ImperativeError ( { msg : TextUtils . formatMessage ( ZosFilesMessages . valueOutOfBounds . message , { optionName : option , value : options [ option ] , minValue : ZosFilesConstants . MIN_RETAIN_DAYS , maxValue : ZosFilesConstants . MAX_RETAIN_DAYS } ) } ) ; } break ; case "retainTo" : case "volumes" : case "storclass" : case "mgntclass" : case "dataclass" : case "responseTimeout" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidFilesCreateOption . message + option } ) ; } } } } param options private static zfsValidateOptions ( options ) { ImperativeExpect . toNotBeNullOrUndefined ( options , ZosFilesMessages . missingFilesCreateOptions . message ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . perms , ZosFilesMessages . missingZfsOption . message + "perms" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . cylsPri , ZosFilesMessages . missingZfsOption . message + "cyls-pri" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . cylsSec , ZosFilesMessages . missingZfsOption . message + "cyls-sec" ) ; ImperativeExpect . toNotBeNullOrUndefined ( options . timeout , ZosFilesMessages . missingZfsOption . message + "timeout" ) ; for ( const option in options ) { if ( Object . prototype . hasOwnProperty . call ( options , option ) ) { switch ( option ) { case "perms" : { const maxPerm = 777 ; if ( ( options . perms < 0 ) || ( options . perms > maxPerm ) ) { throw new ImperativeError ( { msg : ZosFilesMessages . invalidPermsOption . message + options . perms } ) ; } break ; } case "cylsPri" : case "cylsSec" : if ( options [ option ] > ZosFilesConstants . MAX_ALLOC_QUANTITY ) { throw new ImperativeError ( { msg : ZosFilesMessages . maximumAllocationQuantityExceeded . message + " " + ZosFilesMessages . commonFor . message + " '" + option + "' " + ZosFilesMessages . commonWithValue . message + " = " + options [ option ] + "." } ) ; } break ; case "owner" : case "group" : case "storclass" : case "mgntclass" : case "dataclass" : case "volumes" : case "timeout" : case "responseTimeout" : break ; default : throw new ImperativeError ( { msg : ZosFilesMessages . invalidFilesCreateOption . message + option } ) ; } } } } }