From e9be3584fcdc90f428314cee5db219b292677489 Mon Sep 17 00:00:00 2001 From: Andrew Twydell Date: Wed, 18 Dec 2024 18:45:46 +0000 Subject: [PATCH] refactor profileManagement, not complete Signed-off-by: Andrew Twydell --- .editorconfig | 2 +- .../CICSCombinedProgramTree.ts | 45 +- packages/vsce/src/utils/profileManagement.ts | 436 ++++++++---------- 3 files changed, 231 insertions(+), 252 deletions(-) diff --git a/.editorconfig b/.editorconfig index 2536d66b..5760be58 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,7 +3,7 @@ root = true [*] indent_style = space -indent_size = 4 +indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true diff --git a/packages/vsce/src/trees/CICSCombinedTrees/CICSCombinedProgramTree.ts b/packages/vsce/src/trees/CICSCombinedTrees/CICSCombinedProgramTree.ts index 4417cd73..7a874a5e 100644 --- a/packages/vsce/src/trees/CICSCombinedTrees/CICSCombinedProgramTree.ts +++ b/packages/vsce/src/trees/CICSCombinedTrees/CICSCombinedProgramTree.ts @@ -9,18 +9,19 @@ * */ -import { TreeItemCollapsibleState, TreeItem, window, ProgressLocation, workspace } from "vscode"; -import { CICSPlexTree } from "../CICSPlexTree"; -import { CICSProgramTreeItem } from "../treeItems/CICSProgramTreeItem"; -import { CICSRegionTree } from "../CICSRegionTree"; -import { CICSTree } from "../CICSTree"; -import { ProfileManagement } from "../../utils/profileManagement"; -import { ViewMore } from "../treeItems/utils/ViewMore"; import { CicsCmciConstants } from "@zowe/cics-for-zowe-sdk"; +import { imperative } from "@zowe/zowe-explorer-api"; +import { ProgressLocation, TreeItem, TreeItemCollapsibleState, window, workspace } from "vscode"; import { toEscapedCriteriaString } from "../../utils/filterUtils"; +import { ProfileManagement } from "../../utils/profileManagement"; +import { getIconPathInResources } from "../../utils/profileUtils"; +import { CICSPlexTree } from "../CICSPlexTree"; import { CICSRegionsContainer } from "../CICSRegionsContainer"; +import { CICSRegionTree } from "../CICSRegionTree"; +import { CICSTree } from "../CICSTree"; +import { CICSProgramTreeItem } from "../treeItems/CICSProgramTreeItem"; import { TextTreeItem } from "../treeItems/utils/TextTreeItem"; -import { getIconPathInResources } from "../../utils/profileUtils"; +import { ViewMore } from "../treeItems/utils/ViewMore"; export class CICSCombinedProgramTree extends TreeItem { children: (CICSProgramTreeItem | ViewMore)[] | [TextTreeItem] | null; @@ -66,8 +67,8 @@ export class CICSCombinedProgramTree extends TreeItem { this.getParent().getGroupName() ); if (cacheTokenInfo) { - const recordsCount = cacheTokenInfo.recordCount; - if (parseInt(recordsCount, 10)) { + const recordsCount = parseInt(cacheTokenInfo.recordCount, 10); + if (recordsCount) { let allPrograms; if (recordsCount <= this.incrementCount) { allPrograms = await ProfileManagement.getCachedResources( @@ -75,7 +76,7 @@ export class CICSCombinedProgramTree extends TreeItem { cacheTokenInfo.cacheToken, this.constant, 1, - parseInt(recordsCount, 10) + recordsCount ); } else { allPrograms = await ProfileManagement.getCachedResources( @@ -85,7 +86,7 @@ export class CICSCombinedProgramTree extends TreeItem { 1, this.incrementCount ); - count = parseInt(recordsCount); + count = recordsCount; } this.addProgramsUtil([], allPrograms, count); this.iconPath = getIconPathInResources("folder-open-dark.svg", "folder-open-light.svg"); @@ -99,12 +100,20 @@ export class CICSCombinedProgramTree extends TreeItem { } } } catch (error) { - window.showErrorMessage( - `Something went wrong when fetching programs - ${JSON.stringify(error, Object.getOwnPropertyNames(error)).replace( - /(\\n\t|\\n|\\t)/gm, - " " - )}` - ); + if (error instanceof imperative.ImperativeError && error.mDetails.msg.includes("NOTAVAILABLE")) { + this.children = []; + this.iconPath = getIconPathInResources("folder-open-dark.svg", "folder-open-light.svg"); + tree._onDidChangeTreeData.fire(undefined); + window.showInformationMessage(`No programs found`); + this.label = `All Programs${this.activeFilter ? ` (${this.activeFilter}) ` : " "}[${recordsCount}]`; + } else { + window.showErrorMessage( + `Something went wrong when fetching programs - ${JSON.stringify(error, Object.getOwnPropertyNames(error)).replace( + /(\\n\t|\\n|\\t)/gm, + " " + )}` + ); + } } } ); diff --git a/packages/vsce/src/utils/profileManagement.ts b/packages/vsce/src/utils/profileManagement.ts index bd0164c3..e08ebd74 100644 --- a/packages/vsce/src/utils/profileManagement.ts +++ b/packages/vsce/src/utils/profileManagement.ts @@ -73,275 +73,245 @@ export class ProfileManagement { return JSON.parse(xml2json(data, { compact: true, spaces: 4 })); } - /** - * Populates the info - * @param profile - * @returns Array of type InfoLoaded - */ - public static async getPlexInfo(profile: imperative.IProfileLoaded): Promise { - - const session = new Session({ - protocol: profile.profile.protocol, - hostname: profile.profile.host, - port: profile.profile.port, + public static getSessionFromProfile(profile: imperative.IProfile): Session { + return new Session({ + protocol: profile.protocol, + hostname: profile.host, + port: profile.port, type: "basic", - user: profile.profile.user, - password: profile.profile.password, - rejectUnauthorized: profile.profile && 'rejectUnauthorized' in profile.profile ? profile.profile.rejectUnauthorized : true, + user: profile.user, + password: profile.password, + rejectUnauthorized: 'rejectUnauthorized' in profile ? profile.rejectUnauthorized : true, }); + } - const infoLoaded: InfoLoaded[] = []; + public static async regionIsGroup(session: Session, profile: imperative.IProfile): Promise { - if (profile.profile.cicsPlex) { - if (profile.profile.regionName) { - /** - * Both Supplied, no searching required - Only load 1 region - */ - let checkIfSystemGroup: ICMCIApiResponse; - try { - checkIfSystemGroup = await getResource(session, { - name: "CICSRegionGroup", - cicsPlex: profile.profile.cicsPlex, - regionName: profile.profile.regionName, - criteria: `GROUP=${profile.profile.regionName}`, - }); - } catch (error) { - if (error instanceof imperative.ImperativeError) { - if (!error.mDetails.msg.toUpperCase().includes("NODATA")) { - throw error; - } - } + let checkIfSystemGroup: ICMCIApiResponse; + try { + checkIfSystemGroup = await getResource(session, { + name: "CICSRegionGroup", + cicsPlex: profile.cicsPlex, + regionName: profile.regionName, + criteria: `GROUP=${profile.regionName}`, + }); + } catch (error) { + if (error instanceof imperative.ImperativeError) { + if (!error.mDetails.msg.toUpperCase().includes("NODATA")) { + throw error; } + } + } + + return checkIfSystemGroup?.response.resultsummary.recordcount !== "0"; + } - if ( - checkIfSystemGroup && - checkIfSystemGroup.response.resultsummary && - checkIfSystemGroup.response.resultsummary.recordcount !== "0" - ) { - // CICSGroup - const singleGroupResponse = await getResource(session, { - name: "CICSManagedRegion", - cicsPlex: profile.profile.cicsPlex, - regionName: profile.profile.regionName, - }); - infoLoaded.push({ - plexname: profile.profile.cicsPlex, - regions: toArray(singleGroupResponse.response.records.cicsmanagedregion), - group: true, - }); - } else { - // Region - const singleRegionResponse = await getResource(session, { - name: "CICSManagedRegion", - cicsPlex: profile.profile.cicsPlex, - regionName: profile.profile.regionName, - }); - if (singleRegionResponse.response.records && singleRegionResponse.response.records.cicsmanagedregion) { - infoLoaded.push({ - plexname: profile.profile.cicsPlex, - regions: toArray(singleRegionResponse.response.records.cicsmanagedregion), - group: false, - }); - } else { - window.showErrorMessage( - `Cannot find region ${profile.profile.regionName} in plex ${profile.profile.cicsPlex} for profile ${profile.name}` - ); - throw new Error("Region Not Found"); - } + public static async isPlex(session: Session): Promise { + try { + const { response } = await getResource(session, { + name: "CICSCICSPlex", + queryParams: { + summonly: true, + nodiscard: true, } - } else { - /** - * Plex given - must search for regions - */ - const allRegionResponse = await getResource(session, { - name: "CICSManagedRegion", - cicsPlex: profile.profile.cicsPlex, - }); - if (allRegionResponse.response.records && allRegionResponse.response.records.cicsmanagedregion) { - infoLoaded.push({ - plexname: profile.profile.cicsPlex, - regions: toArray(allRegionResponse.response.records.cicsmanagedregion), - group: false, - }); - } else { - window.showErrorMessage(`Cannot find plex ${profile.profile.cicsPlex} for profile ${profile.name}`); - throw new Error("Plex Not Found"); + }); + return response.resultsummary.api_response1_alt === "OK" ? + response.resultsummary.cachetoken : null; + } catch (error) { + if (error instanceof imperative.RestClientError) { + if (`${error.mDetails.errorCode}` === "404") { + return null; } } + throw error; + } + } + + public static async regionPlexProvided(session: Session, profile: imperative.IProfile): Promise { + + const infoLoaded: InfoLoaded[] = []; + + const { response } = await getResource(session, { + name: "CICSManagedRegion", + cicsPlex: profile.cicsPlex, + regionName: profile.regionName, + }); + + if (response.records?.cicsmanagedregion) { + infoLoaded.push({ + plexname: profile.cicsPlex, + regions: toArray(response.records.cicsmanagedregion), + group: await this.regionIsGroup(session, profile), + }); + } else { + window.showErrorMessage( + `Cannot find region ${profile.regionName} in plex ${profile.cicsPlex} for profile ${profile.name}` + ); + throw new Error("Region Not Found"); + } + + return infoLoaded; + } + + public static async plexProvided(session: Session, profile: imperative.IProfile): Promise { + + const infoLoaded: InfoLoaded[] = []; + + const { response } = await getResource(session, { + name: "CICSManagedRegion", + cicsPlex: profile.cicsPlex, + }); + + if (response.records?.cicsmanagedregion) { + infoLoaded.push({ + plexname: profile.cicsPlex, + regions: toArray(response.records.cicsmanagedregion), + group: false, + }); + } else { + window.showErrorMessage(`Cannot find plex ${profile.cicsPlex} for profile ${profile.name}`); + throw new Error("Plex Not Found"); + } + + return infoLoaded; + } + + public static async regionProvided(session: Session, profile: imperative.IProfile): Promise { + + const infoLoaded: InfoLoaded[] = []; + + const { response } = await getResource(session, { + name: "CICSRegion", + regionName: profile.regionName, + }); + + if (response.records.cicsregion) { + infoLoaded.push({ + plexname: null, + regions: toArray(response.records.cicsregion), + group: false, + }); } else { - if (profile.profile.regionName) { - /** - * Region but no plex - Single region system, use that - */ - const singleRegionResponse = await getResource(session, { - name: "CICSRegion", - regionName: profile.profile.regionName, + window.showErrorMessage(`Cannot find region ${profile.regionName}`); + throw new Error("Region Not Found"); + } + + return infoLoaded; + } + + public static async noneProvided(session: Session): Promise { + + const infoLoaded: InfoLoaded[] = []; + + const isPlex = await this.isPlex(session); + if (isPlex) { + const { response } = await getCache(session, { cacheToken: isPlex, nodiscard: false }); + // const uniqueReturnedPlexes = testIfPlexResponse.response.records.cicscicsplex.filter( + // (plex: any, index: number) => index === + // testIfPlexResponse.response.records.cicscicsplex.findIndex((found: any) => + // found.plexname === plex.plexname)); + for (const plex of response.records.cicscicsplex || []) { + infoLoaded.push({ + plexname: plex.plexname, + regions: [], + group: false, }); - if (singleRegionResponse.response.records && singleRegionResponse.response.records.cicsregion) { - infoLoaded.push({ - plexname: null, - regions: toArray(singleRegionResponse.response.records.cicsregion), - group: false, - }); - } else { - window.showErrorMessage(`Cannot find region ${profile.profile.regionName} for profile ${profile.name}`); - throw new Error("Region Not Found"); - } - } else { - /** - * Nothing given - Test if plex and find all info - */ - try { - const testIfPlexResponse = await getResource(session, { - name: "CICSCICSPlex", - }); - if (testIfPlexResponse.response.resultsummary.api_response1_alt === "OK") { - // Plex - if (testIfPlexResponse.response.records && testIfPlexResponse.response.records.cicscicsplex) { - const uniqueReturnedPlexes = testIfPlexResponse.response.records.cicscicsplex.filter( - (plex: any, index: number) => index === testIfPlexResponse.response.records.cicscicsplex.findIndex((found: any) => found.plexname === plex.plexname) - ); - for (const plex of uniqueReturnedPlexes) { - try { - // Regions are empty because we only load Plex when session is expanded - infoLoaded.push({ - plexname: plex.plexname, - regions: [], - group: false, - }); - } catch (error) { - console.log(error); - } - } - } - } else { - // Not Plex - const singleRegion = await getResource(session, { - name: "CICSRegion", - }); - infoLoaded.push({ - plexname: null, - regions: toArray(singleRegion.response.records.cicsregion), - group: false, - }); - } - } catch (error) { - // Not Plex - Could be error - try { - const singleRegion = await getResource(session, { - name: "CICSRegion", - }); - infoLoaded.push({ - plexname: null, - regions: toArray(singleRegion.response.records.cicsregion), - group: false, - }); - } catch (e2) { - throw e2; - } - } } + } else { + // TODO: Error checking!! + const singleRegion = await getResource(session, { + name: "CICSRegion", + }); + infoLoaded.push({ + plexname: null, + regions: toArray(singleRegion.response.records.cicsregion), + group: false, + }); } + return infoLoaded; } + /** + * Populates the info + * @param profile + * @returns Array of type InfoLoaded + */ + public static async getPlexInfo(profile: imperative.IProfileLoaded): Promise { + + const session = this.getSessionFromProfile(profile.profile); + + if (profile.profile.cicsPlex && profile.profile.regionName) { + return this.regionPlexProvided(session, profile.profile); + } else if (profile.profile.cicsPlex) { + return this.plexProvided(session, profile.profile); + } else if (profile.profile.regionName) { + return this.regionProvided(session, profile.profile); + } else { + return this.noneProvided(session); + } + } + /** * Return all the regions in a given plex */ - public static async getRegionInfoInPlex(plex: CICSPlexTree) { + public static async getRegionInfoInPlex(plex: CICSPlexTree): Promise { try { - const profile = plex.getProfile(); - - const session = new Session({ - protocol: profile.profile.protocol, - hostname: profile.profile.host, - port: profile.profile.port, - type: "basic", - user: profile.profile.user, - password: profile.profile.password, - rejectUnauthorized: profile.profile && 'rejectUnauthorized' in profile.profile ? profile.profile.rejectUnauthorized : true, - }); - - const regionResponse = await getResource(session, { + const session = this.getSessionFromProfile(plex.getProfile().profile); + const { response } = await getResource(session, { name: "CICSManagedRegion", cicsPlex: plex.getPlexName(), }); - if (regionResponse.response.resultsummary.api_response1_alt === "OK") { - if (regionResponse.response.records && regionResponse.response.records.cicsmanagedregion) { - return regionResponse.response.records.cicsmanagedregion; - } + if (response.resultsummary.api_response1_alt === "OK") { + return toArray(response.records.cicsmanagedregion); } } catch (error) { - console.log(error); - if (error instanceof imperative.ImperativeError) { - if (error.mDetails.msg.includes("NOTAVAILABLE")) { - window.showErrorMessage(`No regions found for plex ${plex.getPlexName()} with profile ${plex.getParent().label}`); - throw new Error("No regions found"); - } + if (error instanceof imperative.ImperativeError && !error.mDetails.msg.includes("NOTAVAILABLE")) { + window.showErrorMessage(`Error retrieving ManagedRegions for plex ${plex.getPlexName()} with profile ${plex.getParent().label} - ${error}`); } - window.showErrorMessage(`Error retrieving ManagedRegions for plex ${plex.getPlexName()} with profile ${plex.getParent().label}`, error.message); - throw new Error("Error retrieving ManagedRegions"); } } - public static async generateCacheToken(profile: imperative.IProfileLoaded, plexName: string, resourceName: string, criteria?: string, group?: string) { - try { - const session = new Session({ - protocol: profile.profile.protocol, - hostname: profile.profile.host, - port: profile.profile.port, - type: "basic", - user: profile.profile.user, - password: profile.profile.password, - rejectUnauthorized: profile.profile && 'rejectUnauthorized' in profile.profile ? profile.profile.rejectUnauthorized : true, - }); - const allProgramsResponse = await getResource(session, { - name: resourceName, - cicsPlex: plexName, - ...group ? { regionName: group } : {}, - queryParams: { - summonly: true, - nodiscard: true, - } - }); - if (allProgramsResponse.response.resultsummary.api_response1_alt === "OK") { - if (allProgramsResponse.response && allProgramsResponse.response.resultsummary) { - const resultsSummary = allProgramsResponse.response.resultsummary; - return { cacheToken: resultsSummary.cachetoken, recordCount: resultsSummary.recordcount }; - } + public static async generateCacheToken( + profile: imperative.IProfileLoaded, + plexName: string, + resourceName: string, + criteria?: string, + group?: string + ) { + const session = this.getSessionFromProfile(profile.profile); + const allProgramsResponse = await getResource(session, { + name: resourceName, + cicsPlex: plexName, + ...group ? { regionName: group } : {}, + criteria: criteria, + queryParams: { + summonly: true, + nodiscard: true, + overrideWarningCount: true, + } + }); + if (allProgramsResponse.response.resultsummary.api_response1_alt === "OK") { + if (allProgramsResponse.response && allProgramsResponse.response.resultsummary) { + const resultsSummary = allProgramsResponse.response.resultsummary; + return { cacheToken: resultsSummary.cachetoken, recordCount: resultsSummary.recordcount }; } - } catch (error) { - console.log(error); - throw error; } } public static async getCachedResources(profile: imperative.IProfileLoaded, cacheToken: string, resourceName: string, start = 1, increment = 800) { - try { - const session = new Session({ - protocol: profile.profile.protocol, - hostname: profile.profile.host, - port: profile.profile.port, - type: "basic", - user: profile.profile.user, - password: profile.profile.password, - rejectUnauthorized: profile.profile && 'rejectUnauthorized' in profile.profile ? profile.profile.rejectUnauthorized : true, - }); - const allItemsresponse = await getCache(session, { - cacheToken, - startIndex: start, - count: increment, - }); + const session = this.getSessionFromProfile(profile.profile); + const allItemsresponse = await getCache(session, { + cacheToken, + startIndex: start, + count: increment, + }); - if (allItemsresponse.response.resultsummary.api_response1_alt === "OK") { - if (allItemsresponse.response && allItemsresponse.response.records && allItemsresponse.response.records[resourceName.toLowerCase()]) { - const recordAttributes = allItemsresponse.response.records[resourceName.toLowerCase()]; - return toArray(recordAttributes); - } + if (allItemsresponse.response.resultsummary.api_response1_alt === "OK") { + if (allItemsresponse.response && allItemsresponse.response.records && allItemsresponse.response.records[resourceName.toLowerCase()]) { + const recordAttributes = allItemsresponse.response.records[resourceName.toLowerCase()]; + return toArray(recordAttributes); } - } catch (error) { - console.log(error); - throw error; } } }