diff --git a/src/classes/insightsConnection.ts b/src/classes/insightsConnection.ts index f957ee18..3d0a5250 100644 --- a/src/classes/insightsConnection.ts +++ b/src/classes/insightsConnection.ts @@ -30,12 +30,16 @@ import { kdbOutputLog, tokenUndefinedError, } from "../utils/core"; +import { InsightsConfig, InsightsEndpoints } from "../models/config"; export class InsightsConnection { public connected: boolean; public connLabel: string; public node: InsightsNode; public meta?: MetaObject; + public config?: InsightsConfig; + public insightsVersion?: string; + public connEndpoints?: InsightsEndpoints; constructor(connLabel: string, node: InsightsNode) { this.connected = false; @@ -52,6 +56,7 @@ export class InsightsConnection { ).then(async (token) => { this.connected = token ? true : false; if (token) { + await this.getConfig(); await this.getMeta(); } }); @@ -98,6 +103,97 @@ export class InsightsConnection { return undefined; } + public async getConfig() { + if (this.connected) { + const configUrl = new url.URL( + ext.insightsAuthUrls.configURL, + this.node.details.server, + ); + const token = await getCurrentToken( + this.node.details.server, + this.node.details.alias, + this.node.details.realm || "insights", + ); + + if (token === undefined) { + tokenUndefinedError(this.connLabel); + return undefined; + } + + const options = { + headers: { Authorization: `Bearer ${token.accessToken}` }, + }; + + const configResponse = await axios.get(configUrl.toString(), options); + this.config = configResponse.data; + this.getInsightsVersion(); + this.defineEndpoints(); + } + } + + public getInsightsVersion() { + const match = this.config?.version.match(/-\d+(\.\d+){2}(-|$)/); + const version = match ? match[0].replace(/-/g, "") : null; + if (version) { + const [major, minor, _path] = version.split("."); + this.insightsVersion = `${major}.${minor}`; + } + } + + public defineEndpoints() { + if (this.insightsVersion) { + switch (this.insightsVersion) { + case "1.11": + this.connEndpoints = { + scratchpad: { + scratchpad: "scratchpad-manager/api/v1/execute/display", + import: "scratchpad-manager/api/v1/execute/import/data", + importSql: "scratchpad-manager/api/v1/execute/import/sql", + importQsql: "scratchpad-manager/api/v1/execute/import/qsql", + reset: "scratchpad-manager/api/v1/execute/reset", + }, + serviceGateway: { + meta: "servicegateway/meta", + data: "servicegateway/data", + sql: "servicegateway/qe/sql", + qsql: "servicegateway/qe/qsql", + }, + }; + break; + default: + this.connEndpoints = { + scratchpad: { + scratchpad: "servicebroker/scratchpad/display", + import: "servicebroker/scratchpad/import/data", + importSql: "servicebroker/scratchpad/import/sql", + importQsql: "servicebroker/scratchpad/import/qsql", + reset: "servicebroker/scratchpad/reset", + }, + serviceGateway: { + meta: "servicegateway/meta", + data: "servicegateway/data", + sql: "servicegateway/qe/sql", + qsql: "servicegateway/qe/qsql", + }, + }; + break; + } + } + } + + public retrieveEndpoints( + parentKey: "scratchpad" | "serviceGateway", + childKey: string, + ): string | undefined { + if (this.connEndpoints) { + const parent = this.connEndpoints[parentKey]; + if (parent) { + return parent[childKey as keyof typeof parent]; + } + return undefined; + } + } + public async getDataInsights( targetUrl: string, body: string, diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 5192b5d1..83de1c43 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -133,6 +133,7 @@ export namespace ext { revoke: "auth/realms/insights/protocol/openid-connect/revoke", tokenURL: "auth/realms/insights/protocol/openid-connect/token", scratchpadURL: "servicebroker/scratchpad/display", + configURL: "kxicontroller/config", }; export const insightsScratchpadUrls = { diff --git a/src/models/config.ts b/src/models/config.ts new file mode 100644 index 00000000..76d05ee0 --- /dev/null +++ b/src/models/config.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 1998-2023 Kx Systems Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +export type InsightsConfig = { + description?: string; + encryptionInFlight?: boolean; + installSize?: any; + restricted?: boolean; + storage?: any; + version: string; +}; + +export type InsightsEndpoints = { + scratchpad: { + scratchpad: string; + import: string; + importSql: string; + importQsql: string; + reset: string; + }; + serviceGateway: { + meta: string; + data: string; + sql: string; + qsql: string; + }; +}; diff --git a/src/services/connectionManagerService.ts b/src/services/connectionManagerService.ts index e1973811..0f2ffc51 100644 --- a/src/services/connectionManagerService.ts +++ b/src/services/connectionManagerService.ts @@ -95,8 +95,8 @@ export class ConnectionManagementService { } if (conn) { kdbOutputLog( - `[CONNECTION] Connection established successfully to: ${connLabel}`, - "INFO", + `Connection established successfully to: ${connLabel}`, + "CONNECTION", ); Telemetry.sendEvent("Connection.Connected.QProcess"); @@ -115,6 +115,14 @@ export class ConnectionManagementService { await insightsConn.connect(); if (insightsConn.connected) { Telemetry.sendEvent("Connection.Connected.Insights"); + kdbOutputLog( + `Connection established successfully to: ${connLabel}`, + "CONNECTION", + ); + kdbOutputLog( + `${connLabel} connection insights version: ${insightsConn.insightsVersion}`, + "CONNECTION", + ); ext.connectedConnectionList.push(insightsConn); this.isConnectedBehaviour(connection); } else {