-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCICSTree.ts
555 lines (531 loc) · 23.6 KB
/
CICSTree.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* 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 { FilterDescriptor } from "../utils/filterUtils";
import { findSelectedNodes } from "../utils/commandUtils";
import { getResource } from "@zowe/cics-for-zowe-sdk";
import { Gui, imperative, FileManagement, ZoweVsCodeExtension } from "@zowe/zowe-explorer-api";
import {
commands,
Event,
EventEmitter,
ProgressLocation,
ProviderResult,
TreeDataProvider,
TreeItem,
window,
TreeView,
QuickPickItem,
l10n,
QuickPickOptions,
} from "vscode";
import constants from "../utils/constants";
import { PersistentStorage } from "../utils/PersistentStorage";
import { InfoLoaded, ProfileManagement } from "../utils/profileManagement";
import { missingSessionParameters, promptCredentials } from "../utils/profileUtils";
import { getIconFilePathFromName } from "../utils/iconUtils";
import { openConfigFile } from "../utils/workspaceUtils";
import { CICSPlexTree } from "./CICSPlexTree";
import { CICSRegionTree } from "./CICSRegionTree";
import { CICSSessionTree } from "./CICSSessionTree";
export class CICSTree implements TreeDataProvider<CICSSessionTree> {
loadedProfiles: CICSSessionTree[] = [];
constructor() {
this.loadStoredProfileNames();
}
public getLoadedProfiles() {
return this.loadedProfiles;
}
public async refreshLoadedProfiles() {
this.clearLoadedProfiles();
await this.loadStoredProfileNames();
commands.executeCommand("workbench.actions.treeView.cics-view.collapseAll");
}
public clearLoadedProfiles() {
this.loadedProfiles = [];
this._onDidChangeTreeData.fire(undefined);
}
/**
* Searches profiles stored in persistent storage, retrieves information for that profile from
* ZE's PorfilesCache API and then creates CICSSessionTrees with this information and adds
* these as children to the CICSTree (TreeDataProvider)
*/
public async loadStoredProfileNames() {
const persistentStorage = new PersistentStorage("zowe.cics.persistent");
await ProfileManagement.profilesCacheRefresh();
// Retrieve previously added profiles from persistent storage
for (const profilename of persistentStorage.getLoadedCICSProfile()) {
try {
const profileToLoad = await ProfileManagement.getProfilesCache().loadNamedProfile(profilename, "cics");
// avoid accidental repeats
if (!this.loadedProfiles.filter((sessionTree) => sessionTree.label === profilename).length) {
const newSessionTree = new CICSSessionTree(profileToLoad);
this.loadedProfiles.push(newSessionTree);
}
} catch {
continue;
}
}
this._onDidChangeTreeData.fire(undefined);
}
/**
* Provides user with user actions and allows them to manage the selected profile
* @param treeview CICSTree View
* *@param node current selected node
*/
async manageProfile(treeview: TreeView<any>, node: any) {
const allSelectedNodes = findSelectedNodes(treeview, CICSSessionTree, node);
if (!allSelectedNodes || !allSelectedNodes.length) {
window.showErrorMessage("No profile selected to manage");
return;
}
try {
const configInstance = await ProfileManagement.getConfigInstance();
if (configInstance.getTeamConfig().exists) {
const currentProfile = await ProfileManagement.getProfilesCache().getProfileFromConfig(allSelectedNodes[allSelectedNodes.length - 1].label);
const deleteProfile: QuickPickItem = {
label: `$(trash) ${l10n.t(`Delete Profile${allSelectedNodes.length > 1 ? "s" : ""}`)}`,
description: l10n.t(`Delete the selected Profile${allSelectedNodes.length > 1 ? "s" : ""}`),
};
const hideProfile: QuickPickItem = {
label: `$(eye-closed) ${l10n.t(`Hide Profile${allSelectedNodes.length > 1 ? "s" : ""}`)}`,
description: l10n.t(`Hide the selected Profile${allSelectedNodes.length > 1 ? "s" : ""}`),
};
const editProfile: QuickPickItem = {
label: `$(pencil) ${l10n.t(`Edit Profile${allSelectedNodes.length > 1 ? "s" : ""}`)}`,
description: l10n.t(`Update the selected Profile${allSelectedNodes.length > 1 ? "s" : ""}`),
};
const quickpick = Gui.createQuickPick();
const addProfilePlaceholder = "Choose user action for selected profiles";
quickpick.items = [editProfile, hideProfile, deleteProfile];
quickpick.placeholder = addProfilePlaceholder;
quickpick.ignoreFocusOut = true;
quickpick.show();
const choice = await Gui.resolveQuickPick(quickpick);
quickpick.hide();
const debugMsg = l10n.t(`Profile selection has been cancelled.`);
if (!choice) {
Gui.showMessage(debugMsg);
return;
} else if (choice === hideProfile) {
this.hideZoweConfigFile(allSelectedNodes);
return;
} else if (choice === editProfile) {
for (const sessionTree of allSelectedNodes) {
await this.updateSession(sessionTree, configInstance);
}
} else {
const filePath = currentProfile.profLoc.osLoc[0];
await openConfigFile(filePath);
return;
}
}
} catch (error) {
window.showErrorMessage(
`Something went wrong while managing the profile - ${JSON.stringify(error, Object.getOwnPropertyNames(error)).replace(
/(\\n\t|\\n|\\t)/gm,
" ",
)}`,
);
}
}
/**
*
* Provides user with prompts and allows them to add a profile after clicking the '+' button
*/
async addProfile() {
try {
//const allCICSProfileNames = await ProfileManagement.getProfilesCache().getNamesForType('cics');
const configInstance = await ProfileManagement.getConfigInstance();
const profileInfo = await ProfileManagement.getProfilesCache().getProfileInfo();
const allCICSProfiles = profileInfo.getAllProfiles("cics");
// const allCICSProfiles = await ProfileManagement.getProfilesCache().getProfiles('cics');
const allCICSProfileNames: string[] = allCICSProfiles ? (allCICSProfiles.map((profile) => profile.profName) as unknown as [string]) : [];
// No cics profiles needed beforhand for team config method
if (configInstance.getTeamConfig().exists || allCICSProfileNames.length > 0) {
const createNewConfig = "Create a New Team Configuration File";
const editConfig = "Edit Team Configuration File";
const configPick = new FilterDescriptor("\uFF0B " + createNewConfig);
const configEdit = new FilterDescriptor("\u270F " + editConfig);
const items: QuickPickItem[] = [];
const profAllAttrs = profileInfo.getAllProfiles();
allCICSProfileNames
.filter((name) => {
for (const loadedProfile of this.loadedProfiles) {
if (loadedProfile.label === name) {
return false;
}
}
return true;
})
.map((profileName) => {
const osLocInfo = profileInfo.getOsLocInfo(profAllAttrs.find((p) => p.profName === profileName));
items.push(new FilterDescriptor(this.getProfileIcon(osLocInfo)[0] + " " + profileName));
return { label: profileName };
});
const quickpick = Gui.createQuickPick();
const addProfilePlaceholder = l10n.t(`Choose "Create new..." to define or select a profile to add to the CICS tree`);
quickpick.items = [configPick, configEdit, ...items];
quickpick.placeholder = addProfilePlaceholder;
quickpick.ignoreFocusOut = true;
quickpick.show();
const choice = await Gui.resolveQuickPick(quickpick);
quickpick.hide();
const debugMsg = l10n.t(`Profile selection has been cancelled.`);
if (!choice) {
Gui.showMessage(debugMsg);
return;
} else if (choice === configPick) {
commands.executeCommand("zowe.all.config.init");
return;
} else if (choice === configEdit) {
await this.editZoweConfigFile();
return;
} else {
await this.loadExistingProfile(choice.label);
return;
}
} else {
// Create New Profile Form should appear
commands.executeCommand("zowe.all.config.init");
}
} catch (error) {
window.showErrorMessage(JSON.stringify(error, Object.getOwnPropertyNames(error)).replace(/(\\n\t|\\n|\\t)/gm, " "));
}
}
/**
*
* @param profile
* @param position number that's passed in when updating or expanding profile - needed
* to replace position of current CICSSessionTree.
* @param sessionTree current CICSSessionTree only passed in if expanding a profile
*/
async loadProfile(profile?: imperative.IProfileLoaded, position?: number | undefined, sessionTree?: CICSSessionTree) {
const persistentStorage = new PersistentStorage("zowe.cics.persistent");
await persistentStorage.addLoadedCICSProfile(profile.name);
let newSessionTree: CICSSessionTree;
window.withProgress(
{
title: "Load profile",
location: ProgressLocation.Notification,
cancellable: true,
},
async (progress, token) => {
token.onCancellationRequested(() => {});
progress.report({
message: `Loading ${profile.name}`,
});
try {
const configInstance = await ProfileManagement.getConfigInstance();
if (configInstance.getTeamConfig().exists) {
let missingParamters = missingSessionParameters(profile.profile);
if (missingParamters.length) {
const userPass = ["user", "password"];
if (missingParamters.includes(userPass[0]) || missingParamters.includes(userPass[1])) {
const updatedProfile = await promptCredentials(profile.name, true);
if (!updatedProfile) {
return;
}
profile = updatedProfile;
// Remove "user" and "password" from missing params array
missingParamters = missingParamters.filter((param) => userPass.indexOf(param) === -1);
}
if (missingParamters.length) {
window.showInformationMessage(
`The following fields are missing from ${profile.name}: ${missingParamters.join(", ")}. Please update them in your config file.`,
);
return;
}
// If profile is expanded and it previously had 401 error code
} else if (sessionTree && sessionTree.getIsUnauthorized()) {
const updatedProfile = await promptCredentials(profile.name, true);
if (!updatedProfile) {
return;
}
profile = updatedProfile;
}
}
const plexInfo: InfoLoaded[] = await ProfileManagement.getPlexInfo(profile);
// Initialise session tree
newSessionTree = new CICSSessionTree(profile, getIconFilePathFromName("profile"));
// For each InfoLoaded object - happens if there are multiple plexes
for (const item of plexInfo) {
// No plex
if (item.plexname === null) {
const session = new imperative.Session({
type: "basic",
hostname: profile.profile.host,
port: Number(profile.profile.port),
user: profile.profile.user,
password: profile.profile.password,
rejectUnauthorized: profile.profile.rejectUnauthorized,
protocol: profile.profile.protocol,
});
const regionsObtained = await getResource(session, {
name: "CICSRegion",
regionName: item.regions[0].applid,
});
// 200 OK received
newSessionTree.setAuthorized();
const newRegionTree = new CICSRegionTree(
item.regions[0].applid,
regionsObtained.response.records.cicsregion,
newSessionTree,
undefined,
newSessionTree,
);
newSessionTree.addRegion(newRegionTree);
} else {
if (item.group) {
const newPlexTree = new CICSPlexTree(item.plexname, profile, newSessionTree, profile.profile.regionName);
newPlexTree.setLabel(`${item.plexname} - ${profile.profile.regionName}`);
newSessionTree.addPlex(newPlexTree);
} else {
//Plex
const newPlexTree = new CICSPlexTree(item.plexname, profile, newSessionTree);
newSessionTree.addPlex(newPlexTree);
}
}
}
// If method was called when expanding profile
if (sessionTree) {
this.loadedProfiles.splice(position, 1, newSessionTree);
}
// If method was called when updating profile
else if (position || position === 0) {
this.loadedProfiles.splice(position, 0, newSessionTree);
} else {
this.loadedProfiles.push(newSessionTree);
}
this._onDidChangeTreeData.fire(undefined);
} catch (error) {
// Change session tree icon to disconnected upon error
newSessionTree = new CICSSessionTree(profile, getIconFilePathFromName("profile-disconnected"));
// If method was called when expanding profile
if (sessionTree) {
this.loadedProfiles.splice(position, 1, newSessionTree);
}
// If method was called when updating profile
else if (position || position === 0) {
this.loadedProfiles.splice(position, 0, newSessionTree);
} else {
this.loadedProfiles.push(newSessionTree);
}
this._onDidChangeTreeData.fire(undefined);
if (typeof error === "object") {
if ("code" in error) {
switch (error.code) {
case "ETIMEDOUT":
window.showErrorMessage(`Error: connect ETIMEDOUT ${profile.profile.host}:${profile.profile.port} (${profile.name})`);
break;
case "ENOTFOUND":
window.showErrorMessage(`Error: getaddrinfo ENOTFOUND ${profile.profile.host}:${profile.profile.port} (${profile.name})`);
break;
case "ECONNRESET":
window.showErrorMessage(`Error: socket hang up ${profile.profile.host}:${profile.profile.port} (${profile.name})`);
break;
case "EPROTO":
window.showErrorMessage(`Error: write EPROTO ${profile.profile.host}:${profile.profile.port} (${profile.name})`);
break;
case "DEPTH_ZERO_SELF_SIGNED_CERT":
case "SELF_SIGNED_CERT_IN_CHAIN":
case "ERR_TLS_CERT_ALTNAME_INVALID":
case "CERT_HAS_EXPIRED":
// If re-expanding a profile that has an expired certificate
if (sessionTree) {
const decision = await window.showInformationMessage(
`Warning: Your connection is not private (${error.code}) - ` +
`would you still like to proceed to ${profile.profile.host} (unsafe)?`,
...["Yes", "No"],
);
if (decision) {
if (decision === "Yes") {
const configInstance = await ProfileManagement.getConfigInstance();
let updatedProfile;
if (configInstance.getTeamConfig().exists) {
const upd = { profileName: profile.name, profileType: "cics" };
// const configInstance = await ProfileManagement.getConfigInstance();
// flip rejectUnauthorized to false
await configInstance.updateProperty({ ...upd, property: "rejectUnauthorized", value: false });
updatedProfile = await ProfileManagement.getProfilesCache().getLoadedProfConfig(profile.name);
} else {
await ProfileManagement.profilesCacheRefresh();
updatedProfile = await ProfileManagement.getProfilesCache().loadNamedProfile(profile.name, "cics");
}
await this.removeSession(sessionTree, updatedProfile, position);
}
}
}
break;
default:
window.showErrorMessage(
`Error: An error has occurred ${profile.profile.host}:${profile.profile.port} (${profile.name}) - ${JSON.stringify(
error,
Object.getOwnPropertyNames(error),
).replace(/(\\n\t|\\n|\\t)/gm, " ")}`,
);
}
} else if ("response" in error) {
if (error.response !== "undefined" && error.response.status) {
switch (error.response.status) {
case constants.HTTP_ERROR_UNAUTHORIZED:
window.showErrorMessage(`Error: Request failed with status code 401 for Profile '${profile.name}'`);
// set the unauthorized flag to true for reprompting of credentials.
newSessionTree.setUnauthorized();
// Replace old profile tree with new disconnected profile tree item
this.loadedProfiles.splice(position, 1, newSessionTree);
break;
case constants.HTTP_ERROR_NOT_FOUND:
window.showErrorMessage(`Error: Request failed with status code 404 for Profile '${profile.name}' - Not Found`);
break;
case constants.HTTP_ERROR_SERVER_ERROR:
window.showErrorMessage(`Error: Request failed with status code 500 for Profile '${profile.name}'`);
break;
default:
window.showErrorMessage(`Error: Request failed with status code ${error.response.status} for Profile '${profile.name}'`);
}
} else {
window.showErrorMessage(
`Error: An error has occurred ${profile.profile.host}:${profile.profile.port} (${profile.name}) - ${JSON.stringify(
error,
Object.getOwnPropertyNames(error),
).replace(/(\\n\t|\\n|\\t)/gm, " ")}`,
);
}
}
}
}
},
);
}
/**
* Allows user to edit a configuration file based on global or project level context
*/
async editZoweConfigFile() {
let rootPath = FileManagement.getZoweDir();
const workspaceDir = ZoweVsCodeExtension.workspaceRoot;
const choice = await this.getConfigLocationPrompt("edit");
if (choice === "global") {
await openConfigFile(rootPath + "/zowe.config.json");
} else if (choice === "project") {
rootPath = workspaceDir.uri.fsPath;
await openConfigFile(rootPath + "/zowe.config.json");
} else {
return;
}
}
/**
* Allows user to load an existing file from persistance area instead of creating a new profile.
* @param label name of the selected profile
*/
async loadExistingProfile(label: string) {
label = label.split(/ (.*)/)[1];
const profileToLoad = await ProfileManagement.getProfilesCache().getLoadedProfConfig(label);
const newSessionTree = new CICSSessionTree(profileToLoad);
this.loadedProfiles.push(newSessionTree);
const persistentStorage = new PersistentStorage("zowe.cics.persistent");
await persistentStorage.addLoadedCICSProfile(label);
this._onDidChangeTreeData.fire(undefined);
}
/**
* Method for profile configuration that provides UI for user to hide a selected profile.
* @param allSelectedNodes array of selected nodes
*/
async hideZoweConfigFile(allSelectedNodes: any[]) {
for (const index in allSelectedNodes) {
try {
const currentNode = allSelectedNodes[parseInt(index)];
await this.removeSession(currentNode);
} catch (error) {
window.showErrorMessage(
`Something went wrong when hiding the profile - ${JSON.stringify(error, Object.getOwnPropertyNames(error)).replace(
/(\\n\t|\\n|\\t)/gm,
" ",
)}`,
);
}
}
}
async removeSession(session: CICSSessionTree, profile?: imperative.IProfileLoaded, position?: number) {
const persistentStorage = new PersistentStorage("zowe.cics.persistent");
await persistentStorage.removeLoadedCICSProfile(session.label.toString());
this.loadedProfiles = this.loadedProfiles.filter((p) => p.profile.name !== session.label?.toString());
if (profile && position !== undefined) {
await this.loadProfile(profile, position);
}
this._onDidChangeTreeData.fire(undefined);
}
/**
* Update profile functionality for profile configuration
* @param session CICSSessions Tree
*/
async updateSession(session: CICSSessionTree, configInstance: imperative.ProfileInfo) {
await ProfileManagement.profilesCacheRefresh();
const profileCache = await ProfileManagement.getProfilesCache();
const profileToUpdate = profileCache.loadNamedProfile(session.label?.toString()!, "cics");
const currentProfile = await profileCache.getProfileFromConfig(profileToUpdate.name);
const teamConfigFilePath = configInstance.getTeamConfig().opts.homeDir + "/zowe.config.json";
const filePath = currentProfile?.profLoc.osLoc?.[0] ?? teamConfigFilePath;
await openConfigFile(filePath);
}
/**
* Method for profile configuration that returns the context of a configuration file.
* @param action string create or edit
*/
private async getConfigLocationPrompt(action: string): Promise<string> {
let placeHolderText: string;
if (action === "create") {
placeHolderText = l10n.t("Select the location where the config file will be initialized");
} else {
placeHolderText = l10n.t("Select the location of the config file to edit");
}
const quickPickOptions: QuickPickOptions = {
placeHolder: placeHolderText,
ignoreFocusOut: true,
canPickMany: false,
};
const globalText = l10n.t("Global: in the Zowe home directory");
const projectText = l10n.t("Project: in the current working directory");
const location = await Gui.showQuickPick([globalText, projectText], quickPickOptions);
// call check for existing and prompt here
switch (location) {
case globalText:
return "global";
case projectText:
return "project";
}
}
/**
* Method that returns the icon based on global or local context.
* @param osLocInfo physical location of of profile on OS
*/
private getProfileIcon(osLocInfo: imperative.IProfLocOsLoc[]): string[] {
const ret: string[] = [];
for (const loc of osLocInfo ?? []) {
if (loc.global) {
ret.push("$(home)");
} else {
ret.push("$(folder)");
}
}
return ret;
}
getTreeItem(element: CICSSessionTree): TreeItem | Thenable<TreeItem> {
return element;
}
getChildren(element?: CICSSessionTree): ProviderResult<any[]> {
return element === undefined ? this.loadedProfiles : element.children;
}
getParent(element: any): ProviderResult<any> {
element.getParent();
}
public _onDidChangeTreeData: EventEmitter<any | undefined> = new EventEmitter<any | undefined>();
readonly onDidChangeTreeData: Event<any | undefined> = this._onDidChangeTreeData.event;
}