Utilizing the unique tree node ID sent by the call, my custom command interacts with the JSON data saved in the global extension storage to retrieve necessary information and locate the corresponding node. Additional details required for the command are also extracted from the JSON.
While there may be alternative methods, this is the approach I have implemented successfully:
export function pingAgent(node: vscode.TreeItem, context: vscode.ExtensionContext) {
if (processNumber < 9999) {
processNumber++;
}
else {
processNumber = 1;
}
let procNumString: string = strPrefix + processNumber.toString().padStart(4, "0");
let strMessage: string = "";
let responseMessage: string | undefined;
let responseMessageError: string | undefined;
let responseMessageBase: string | undefined;
let ctmInfrastructureCache: string = context.globalState.get('ctmInfrastructureCache');
let tree: json.Node = json.parseTree(ctmInfrastructureCache);
let nodeKey: string | undefined = "";
// this is the node # within the TreeView
let offsetTemp: number = Number(node.toString());
// this is the node # with additional information, relative to the initial node #
let offset: number = Number(offsetTemp) + 10;
// Get parent
const pathTemp = json.getLocation(ctmInfrastructureCache, offsetTemp).path;
const valueNodeTemp = json.findNodeAtLocation(tree, pathTemp);
let ctmResourceType = getResourceType(valueNodeTemp);
// Get CTM node name
const path = json.getLocation(ctmInfrastructureCache, offset).path;
const valueNode = json.findNodeAtLocation(tree, path);
const nodeid = valueNode.value.toString();
if (ctmResourceType === "ctm.agent") {
let ctmDatacenterName = getDatacenterName(valueNodeTemp);
try {
nodeKey = valueNode.parent.children[0].value;
} catch (error) {
nodeKey = null;
}
strMessage = "Awaiting Agent Status of: '" + nodeid + "' managed by: " + ctmDatacenterName;
OutputUtils.getOutputChannel().appendLine(procNumString + ' Message : ' + strMessage);
let jsonStrA = CtmTools.cmdPingAgent(ctmDatacenterName, nodeid);
let jsonStrB = json.parse(jsonStrA.replace(/\n/g, ''));
try {
responseMessageBase = jsonStrB["message"].toString();
// eslint-disable-next-line no-empty
} catch { }
try {
responseMessageError = jsonStrB["errors"][0]["message"];
// eslint-disable-next-line no-empty
} catch { }
if (responseMessageBase) {
responseMessage = responseMessageBase;
} else if (responseMessageError) {
responseMessage = responseMessageError;
}
let reStrA = "unavailable";
let reStrB = "available";
let reStrC = "Failed";
if (responseMessage.includes(reStrA)) {
strMessage = 'Status : ' + responseMessage;
MessageUtils.showWarningMessage(strMessage);
} else if (responseMessage.includes(reStrB)) {
strMessage = 'Status : ' + responseMessage;
MessageUtils.showInfoMessage(strMessage);
} else if (responseMessage.includes(reStrC)) {
strMessage = 'Status : ' + responseMessage;
MessageUtils.showErrorMessage(strMessage);
} else {
strMessage = 'Status : ' + responseMessage;
MessageUtils.showWarningMessage(strMessage);
}
OutputUtils.getOutputChannel().appendLine(procNumString + ' Message : ' + responseMessage);
}
}
Warm regards