-
Notifications
You must be signed in to change notification settings - Fork 85
/
Delete.ts
210 lines (184 loc) · 9.09 KB
/
Delete.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
/*
* 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, ImperativeError, ImperativeExpect, Logger } from "@zowe/imperative";
import { posix } from "path";
import { ZosmfRestClient, IHeaderContent, ZosmfHeaders } from "@zowe/core-for-zowe-sdk";
import { ZosFilesConstants } from "../../constants/ZosFiles.constants";
import { ZosFilesMessages } from "../../constants/ZosFiles.messages";
import { IZosFilesResponse } from "../../doc/IZosFilesResponse";
import { Invoke } from "../invoke";
import { IDeleteDatasetOptions } from "./doc/IDeleteDatasetOptions";
import { IDeleteVsamOptions } from "./doc/IDeleteVsamOptions";
import { IDeleteVsamResponse } from "./doc/IDeleteVsamResponse";
import { ZosFilesUtils } from "../../utils/ZosFilesUtils";
import { IZosFilesOptions } from "../../doc/IZosFilesOptions";
/**
* This class holds helper functions that are used to delete files through the
* z/OSMF APIs.
*/
export class Delete {
/**
* Deletes a non-VSAM data set
*
* @param {AbstractSession} session z/OSMF connection info
* @param {string} dataSetName The name of the data set to delete
* @param {IDeleteDatasetOptions} [options={}] Additional options for deletion of the data set
*
* @returns {Promise<IZosFilesResponse>} A response indicating the status of the deletion
*
* @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.3.0/com.ibm.zos.v2r3.izua700/IZUHPINFO_API_DeleteDataSet.htm
*/
public static async dataSet(session: AbstractSession,
dataSetName: string,
options: IDeleteDatasetOptions = {}): 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);
if (options.volume) {
endpoint = posix.join(endpoint, `-(${options.volume})`);
}
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options && options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
endpoint = posix.join(endpoint, dataSetName);
Logger.getAppLogger().debug(`Endpoint: ${endpoint}`);
// Since there is a bug with the deleteExpectJSON (doesn't handle 204 no content) we will be using the expect
// string api since that doesn't seem to complain.
await ZosmfRestClient.deleteExpectString(session, endpoint, reqHeaders);
return {
success : true,
commandResponse: ZosFilesMessages.datasetDeletedSuccessfully.message
};
} catch (error) {
Logger.getAppLogger().error(error);
throw error;
}
}
/**
* Deletes a VSAM data set
*
* @param {AbstractSession} session z/OSMF connection info
* @param {string} dataSetName The name of the VSAM data set to delete
* @param {IDeleteVsamOptions} [options={}] Additional options for deletion of the data set
*
* @returns {Promise<IDeleteVsamResponse>} A response indicating the status of the deletion
*
* @throws {ImperativeError} Data set name must be specified as a non-empty string
*
* @see https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.idai200/delet.htm
*/
public static async vsam(session: AbstractSession,
dataSetName: string,
options: IDeleteVsamOptions = {}): Promise<IDeleteVsamResponse> {
// required
ImperativeExpect.toNotBeNullOrUndefined(dataSetName, ZosFilesMessages.missingDatasetName.message);
ImperativeExpect.toNotBeEqual(dataSetName, "", ZosFilesMessages.missingDatasetName.message);
// Create the control statements
const amsControlStatements = [
"DELETE -",
`${dataSetName} -`,
"CLUSTER -",
`${options.erase ? "ERASE" : "NOERASE"} -`,
options.purge ? "PURGE" : "NOPURGE"
];
try {
const response: IZosFilesResponse = await Invoke.ams(session, amsControlStatements, {responseTimeout: options.responseTimeout});
return {
success : true,
commandResponse: ZosFilesMessages.datasetDeletedSuccessfully.message,
apiResponse : response
};
} catch (error) {
Logger.getAppLogger().error(error);
throw error;
}
}
/**
* Deletes a USS file or directory
*
* @param {AbstractSession} session z/OSMF connection info
* @param {string} fileName The name of the file or directory to delete
* @param {boolean} recursive The indicator to delete directory recursively
* @returns {Promise<IDeleteVsamResponse>} A response indicating the status of the deletion
*
* @throws {ImperativeError} Data set name must be specified as a non-empty string
*
* @see https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.izua700/IZUHPINFO_API_DeleteUnixFile.htm
*/
public static async ussFile(session: AbstractSession,
fileName: string,
recursive?: boolean,
options?: IZosFilesOptions): Promise<IZosFilesResponse> {
// required
ImperativeExpect.toNotBeNullOrUndefined(fileName, ZosFilesMessages.missingUSSFileName.message);
ImperativeExpect.toNotBeEqual(fileName, "", ZosFilesMessages.missingUSSFileName.message);
// Format the endpoint to send the request to
let endpoint = posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_USS_FILES);
fileName = posix.normalize(fileName);
fileName = ZosFilesUtils.formatUnixFilepath(fileName);
fileName = encodeURIComponent(fileName);
endpoint = posix.join(endpoint, fileName);
Logger.getAppLogger().debug(`Endpoint: ${endpoint}`);
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (recursive && recursive === true) {
reqHeaders.push({"X-IBM-Option": "recursive"});
}
if (options && options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
try {
await ZosmfRestClient.deleteExpectString(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: ZosFilesMessages.ussFileDeletedSuccessfully.message
};
} catch (error) {
Logger.getAppLogger().error(error);
throw error;
}
}
/**
* Deletes a z/OS file system
*
* @param {AbstractSession} session z/OSMF connection info
* @param {string} fileSystemName The name of the ZFS to delete
*
* @throws {ImperativeError} File system name must be specified as a non-empty string
*
* @see https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.izua700/IZUHPINFO_API_DeleteUnixzFsFilesystem.htm
*/
public static async zfs(session: AbstractSession,
fileSystemName: string,
options?: IZosFilesOptions): Promise<IZosFilesResponse> {
// required
ImperativeExpect.toNotBeNullOrUndefined(fileSystemName, ZosFilesMessages.missingFileSystemName.message);
ImperativeExpect.toNotBeEqual(fileSystemName, "", ZosFilesMessages.missingFileSystemName.message);
// Format the endpoint to send the request to
const endpoint = ZosFilesConstants.RESOURCE + ZosFilesConstants.RES_ZFS_FILES + "/" + fileSystemName;
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options && options.responseTimeout != null) {
reqHeaders.push({[ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: options.responseTimeout.toString()});
}
const data = await ZosmfRestClient.deleteExpectString(session, endpoint, reqHeaders);
return {
success: true,
commandResponse: ZosFilesMessages.zfsDeletedSuccessfully.message,
apiResponse: data
};
}
}