-
Notifications
You must be signed in to change notification settings - Fork 85
/
List.ts
426 lines (377 loc) · 18.3 KB
/
List.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { AbstractSession, IHeaderContent, ImperativeError, ImperativeExpect, Logger, TaskProgress } from "@zowe/imperative";
import { posix } from "path";
import * as util from "util";
import { ZosmfRestClient, ZosmfHeaders, asyncPool } from "@zowe/core-for-zowe-sdk";
import { ZosFilesConstants } from "../../constants/ZosFiles.constants";
import { ZosFilesMessages } from "../../constants/ZosFiles.messages";
import { IZosFilesResponse } from "../../doc/IZosFilesResponse";
import { IListOptions } from "./doc/IListOptions";
import { IUSSListOptions } from "./doc/IUSSListOptions";
import { IFsOptions } from "./doc/IFsOptions";
import { IZosmfListResponse } from "./doc/IZosmfListResponse";
import { IDsmListOptions } from "./doc/IDsmListOptions";
/**
* This class holds helper functions that are used to list data sets and its members through the z/OS MF APIs
*/
export class List {
/**
* Retrieve all members from a PDS
*
* @param {AbstractSession} session - z/OS MF connection info
* @param {string} dataSetName - contains the data set name
* @param {IListOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API
*
* @throws {ImperativeError} data set name must be set
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*
* @see https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.izua700/IZUHPINFO_API_GetListDataSetMembers.htm
*/
public static async allMembers(session: AbstractSession, dataSetName: string, options: IListOptions = {}): Promise<IZosFilesResponse> {
// required
ImperativeExpect.toNotBeNullOrUndefined(dataSetName, ZosFilesMessages.missingDatasetName.message);
ImperativeExpect.toNotBeEqual(dataSetName, "", ZosFilesMessages.missingDatasetName.message);
try {
// Format the endpoint to send the request to
let endpoint = posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_DS_FILES, dataSetName, ZosFilesConstants.RES_DS_MEMBERS);
if (options.pattern) {
endpoint += `?pattern=${options.pattern}`;
}
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options.attributes) {
reqHeaders.push(ZosmfHeaders.X_IBM_ATTRIBUTES_BASE);
}
if (options.maxLength) {
reqHeaders.push({"X-IBM-Max-Items": `${options.maxLength}`});
} else {
reqHeaders.push(ZosmfHeaders.X_IBM_MAX_ITEMS);
}
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
this.log.debug(`Endpoint: ${endpoint}`);
const response: any = await ZosmfRestClient.getExpectJSON(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: null,
apiResponse: response
};
} catch (error) {
this.log.error(error);
throw error;
}
}
/**
* Retrieve all members from a data set name
*
* @param {AbstractSession} session - z/OS MF connection info
* @param {string} dataSetName - contains the data set name
* @param {IListOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API
*
* @throws {ImperativeError} data set name must be set
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*/
public static async dataSet(session: AbstractSession, dataSetName: string, options: IListOptions = {}): Promise<IZosFilesResponse> {
ImperativeExpect.toNotBeNullOrUndefined(dataSetName, ZosFilesMessages.missingDatasetName.message);
ImperativeExpect.toNotBeEqual(dataSetName, "", ZosFilesMessages.missingDatasetName.message);
try {
let endpoint = posix.join(ZosFilesConstants.RESOURCE,
`${ZosFilesConstants.RES_DS_FILES}?${ZosFilesConstants.RES_DS_LEVEL}=${dataSetName}`);
if (options.volume) {
endpoint = `${endpoint}&volser=${options.volume}`;
}
if (options.start) {
endpoint = `${endpoint}&start=${options.start}`;
}
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options.attributes) {
reqHeaders.push(ZosmfHeaders.X_IBM_ATTRIBUTES_BASE);
}
if (options.maxLength) {
reqHeaders.push({"X-IBM-Max-Items": `${options.maxLength}`});
} else {
reqHeaders.push(ZosmfHeaders.X_IBM_MAX_ITEMS);
}
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
// Migrated recall options
if (options.recall) {
switch (options.recall.toLowerCase()) {
case "wait":
reqHeaders.push(ZosmfHeaders.X_IBM_MIGRATED_RECALL_WAIT);
break;
case "nowait":
reqHeaders.push(ZosmfHeaders.X_IBM_MIGRATED_RECALL_NO_WAIT);
break;
case "error":
reqHeaders.push(ZosmfHeaders.X_IBM_MIGRATED_RECALL_ERROR);
break;
}
}
this.log.debug(`Endpoint: ${endpoint}`);
const response: any = await ZosmfRestClient.getExpectJSON(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: null,
apiResponse: response
};
} catch (error) {
this.log.error(error);
throw error;
}
}
/**
* Retrieve a list of all files from a path name
*
* @param {AbstractSession} session - z/OS MF connection info
* @param {string} path - contains the uss path name
* @param {IUSSListOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API
*
* @throws {ImperativeError} data set name must be set
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*/
public static async fileList(session: AbstractSession, path: string, options: IUSSListOptions = {}): Promise<IZosFilesResponse> {
ImperativeExpect.toNotBeNullOrUndefined(path, ZosFilesMessages.missingUSSFileName.message);
ImperativeExpect.toNotBeEqual(path.trim(), "", ZosFilesMessages.missingUSSFileName.message);
// Error out if someone tries to use a second table parameter without specifying a first table parameter
if (options.depth || options.filesys != null || options.symlinks != null){
if (!(options.group || options.user || options.name || options.size || options.mtime || options.perm || options.type)) {
throw new ImperativeError({msg: ZosFilesMessages.missingRequiredTableParameters.message});
}
}
// Remove a trailing slash from the path, if one exists
// Do not remove if requesting the root directory
if (path.trim().length > 1 && path.endsWith("/")) { path = path.slice(0, -1); }
try {
let endpoint = posix.join(ZosFilesConstants.RESOURCE,
`${ZosFilesConstants.RES_USS_FILES}?${ZosFilesConstants.RES_PATH}=${encodeURIComponent(path)}`);
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options.maxLength) {
reqHeaders.push({"X-IBM-Max-Items": `${options.maxLength}`});
} else {
reqHeaders.push(ZosmfHeaders.X_IBM_MAX_ITEMS);
}
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
// Start modifying the endpoint with the query parameters that were passed in
if (options.group) { endpoint += `&${ZosFilesConstants.RES_GROUP}=${encodeURIComponent(options.group)}`; }
if (options.user) { endpoint += `&${ZosFilesConstants.RES_USER}=${encodeURIComponent(options.user)}`; }
if (options.name) { endpoint += `&${ZosFilesConstants.RES_NAME}=${encodeURIComponent(options.name)}`; }
if (options.size) { endpoint += `&${ZosFilesConstants.RES_SIZE}=${encodeURIComponent(options.size)}`; }
if (options.mtime) { endpoint += `&${ZosFilesConstants.RES_MTIME}=${encodeURIComponent(options.mtime)}`; }
if (options.perm) { endpoint += `&${ZosFilesConstants.RES_PERM}=${encodeURIComponent(options.perm)}`; }
if (options.type) { endpoint += `&${ZosFilesConstants.RES_TYPE}=${encodeURIComponent(options.type)}`; }
if (options.depth) { endpoint += `&${ZosFilesConstants.RES_DEPTH}=${encodeURIComponent(options.depth)}`; }
if (options.filesys != null) {
if (options.filesys === true) { endpoint += `&${ZosFilesConstants.RES_FILESYS}=all`; }
else { endpoint += `&${ZosFilesConstants.RES_FILESYS}=same`; }
}
if (options.symlinks != null) {
if (options.symlinks === true) { endpoint += `&${ZosFilesConstants.RES_SYMLINKS}=report`; }
else { endpoint += `&${ZosFilesConstants.RES_SYMLINKS}=follow`; }
}
this.log.debug(`Endpoint: ${endpoint}`);
const response: any = await ZosmfRestClient.getExpectJSON(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: null,
apiResponse: response
};
} catch (error) {
this.log.error(error);
throw error;
}
}
/**
* Retrieve zfs files
*
* @param {AbstractSession} session - z/OS MF connection info
* @param {IZfsOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API
*
* @throws {ImperativeError} data set name must be set
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*/
public static async fs(session: AbstractSession, options: IFsOptions = {}): Promise<IZosFilesResponse> {
try {
let endpoint = posix.join(ZosFilesConstants.RESOURCE,
`${ZosFilesConstants.RES_MFS}`);
if (options.fsname) {
endpoint = posix.join(endpoint, `?${ZosFilesConstants.RES_FSNAME}=${encodeURIComponent(options.fsname)}`);
}
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
// if (options.path) {
// reqHeaders.push(ZosmfHeaders.X_IBM_ATTRIBUTES_BASE);
// }
if (options.maxLength) {
reqHeaders.push({"X-IBM-Max-Items": `${options.maxLength}`});
} else {
reqHeaders.push(ZosmfHeaders.X_IBM_MAX_ITEMS);
}
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
this.log.debug(`Endpoint: ${endpoint}`);
const response: any = await ZosmfRestClient.getExpectJSON(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: null,
apiResponse: response
};
} catch (error) {
this.log.error(error);
throw error;
}
}
/**
* Retrieve zfs files if indicated path
*
* @param {AbstractSession} session - z/OS MF connection info
* @param {IZfsOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<IZosFilesResponse>} A response indicating the outcome of the API
*
* @throws {ImperativeError} data set name must be set
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*/
public static async fsWithPath(session: AbstractSession, options: IFsOptions = {}): Promise<IZosFilesResponse> {
try {
let endpoint = posix.join(ZosFilesConstants.RESOURCE,
`${ZosFilesConstants.RES_MFS}`);
if (options.path) {
endpoint = posix.join(endpoint, `?${ZosFilesConstants.RES_PATH}=${encodeURIComponent(options.path)}`);
}
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options.maxLength) {
reqHeaders.push({"X-IBM-Max-Items": `${options.maxLength}`});
} else {
reqHeaders.push(ZosmfHeaders.X_IBM_MAX_ITEMS);
}
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
this.log.debug(`Endpoint: ${endpoint}`);
const response: any = await ZosmfRestClient.getExpectJSON(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: null,
apiResponse: response
};
} catch (error) {
this.log.error(error);
throw error;
}
}
/**
* List data sets that match a DSLEVEL pattern
* @param {AbstractSession} session z/OSMF connection info
* @param {string[]} patterns Data set patterns to include
* @param {IDsmListOptions} options Contains options for the z/OSMF request
* @returns {Promise<IZosFilesResponse>} List of z/OSMF list responses for each data set
*
* @example
* ```typescript
*
* // List all "PS" and "PO" datasets that match the pattern "USER.**.DATASET"
* await List.dataSetsMatchingPattern(session, "USER.**.DATASET");
* ```
*/
public static async dataSetsMatchingPattern(session: AbstractSession, patterns: string[],
options: IDsmListOptions = {}): Promise<IZosFilesResponse> {
// Pattern is required to be non-empty
ImperativeExpect.toNotBeNullOrUndefined(patterns, ZosFilesMessages.missingPatterns.message);
patterns = patterns.filter(Boolean);
ImperativeExpect.toNotBeEqual(patterns.length, 0, ZosFilesMessages.missingPatterns.message);
const zosmfResponses: IZosmfListResponse[] = [];
// Get names of all data sets
for (const pattern of patterns) {
let response: any;
try {
response = await List.dataSet(session, pattern, { attributes: true });
} catch (err) {
// Listing data sets with attributes may fail sometimes, for
// example if a TSO prompt is triggered. If that happens, we
// try first to list them all without attributes, and then fetch
// the attributes for each data set one by one. When an error
// is thrown we record it on the response object. This is a slow
// process but better than throwing an error.
response = await List.dataSet(session, pattern);
let listsInitiated = 0;
const createListPromise = (dataSetObj: any) => {
if (options.task != null) {
options.task.percentComplete = Math.floor(TaskProgress.ONE_HUNDRED_PERCENT *
(listsInitiated / response.apiResponse.items.length));
listsInitiated++;
}
return List.dataSet(session, dataSetObj.dsname, { attributes: true }).then(
(tempResponse) => {
Object.assign(dataSetObj, tempResponse.apiResponse.items[0]);
},
(tempErr) => {
Object.assign(dataSetObj, { error: tempErr });
}
);
};
const maxConcurrentRequests = options.maxConcurrentRequests == null ? 1 : options.maxConcurrentRequests;
if (maxConcurrentRequests === 0) {
await Promise.all(response.apiResponse.items.map(createListPromise));
} else {
await asyncPool(maxConcurrentRequests, response.apiResponse.items, createListPromise);
}
}
zosmfResponses.push(...response.apiResponse.items);
}
// Check if data sets matching pattern found
if (zosmfResponses.length === 0) {
return {
success: false,
commandResponse: ZosFilesMessages.noDataSetsMatchingPattern.message,
apiResponse: []
};
}
// Exclude names of data sets
for (const pattern of (options.excludePatterns || [])) {
const response = await List.dataSet(session, pattern);
response.apiResponse.items.forEach((dataSetObj: IZosmfListResponse) => {
const responseIndex = zosmfResponses.findIndex(response => response.dsname === dataSetObj.dsname);
if (responseIndex !== -1) {
zosmfResponses.splice(responseIndex, 1);
}
});
}
// Check if exclude pattern has left any data sets in the list
if (zosmfResponses.length === 0) {
return {
success: false,
commandResponse: ZosFilesMessages.noDataSetsInList.message,
apiResponse: []
};
}
return {
success: true,
commandResponse: util.format(ZosFilesMessages.dataSetsMatchedPattern.message, zosmfResponses.length),
apiResponse: zosmfResponses
};
}
private static get log() {
return Logger.getAppLogger();
}
}