-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy.ts
87 lines (79 loc) · 3.3 KB
/
Copy.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
/*
* 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, ImperativeExpect, Logger, Headers } from "@zowe/imperative";
import { posix } from "path";
import { ZosmfRestClient, ZosmfHeaders, IHeaderContent } from "@zowe/core-for-zowe-sdk";
import { ZosFilesConstants } from "../../constants/ZosFiles.constants";
import { ZosFilesMessages } from "../../constants/ZosFiles.messages";
import { IZosFilesResponse } from "../../doc/IZosFilesResponse";
import { IDataSet } from "../../doc/IDataSet";
import { ICopyDatasetOptions } from "./doc/ICopyDatasetOptions";
/**
* This class holds helper functions that are used to copy the contents of datasets through the
* z/OSMF APIs.
*/
export class Copy {
/**
* Copy the contents of a dataset
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {IDataSet} toDataSet - The data set to copy to
* @param {IDataSetOptions} options - Options
*
* @returns {Promise<IZosFilesResponse>} A response indicating the status of the copying
*
* @throws {ImperativeError} Data set name must be specified as a non-empty string
* @throws {Error} When the {@link ZosmfRestClient} throws an error
*
* @see https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.izua700/IZUHPINFO_API_PutDataSetMemberUtilities.htm
*/
public static async dataSet(
session: AbstractSession,
{ dsn: toDataSetName, member: toMemberName }: IDataSet,
options: ICopyDatasetOptions
): Promise<IZosFilesResponse> {
ImperativeExpect.toBeDefinedAndNonBlank(options["from-dataset"].dsn, "fromDataSetName");
ImperativeExpect.toBeDefinedAndNonBlank(toDataSetName, "toDataSetName");
const endpoint: string = posix.join(
ZosFilesConstants.RESOURCE,
ZosFilesConstants.RES_DS_FILES,
toMemberName == null ? toDataSetName : `${toDataSetName}(${toMemberName})`
);
Logger.getAppLogger().debug(`Endpoint: ${endpoint}`);
const payload: any = {
"request": "copy",
"from-dataset": {
dsn: options["from-dataset"].dsn,
member: options["from-dataset"].member
},
...options
};
delete payload.fromDataSet;
const reqHeaders: IHeaderContent[] = [
Headers.APPLICATION_JSON,
{ [Headers.CONTENT_LENGTH]: JSON.stringify(payload).length.toString() },
ZosmfHeaders.ACCEPT_ENCODING
];
if (options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
try {
await ZosmfRestClient.putExpectString(session, endpoint, reqHeaders, payload);
return {
success: true,
commandResponse: ZosFilesMessages.datasetCopiedSuccessfully.message
};
} catch (error) {
Logger.getAppLogger().error(error);
throw error;
}
}
}