Skip to content

Commit

Permalink
Merge pull request #253 from KxSystems/KXI-36572
Browse files Browse the repository at this point in the history
KXI-36572 - Connections should not be able to have the same name
  • Loading branch information
Philip-Carneiro-KX authored Feb 28, 2024
2 parents 561dc2b + 86b5147 commit a45f4d1
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export namespace ext {
export const kdbinsightsNodes: string[] = [];
export const kdbNodesWithoutAuth: string[] = [];
export const kdbNodesWithoutTls: string[] = [];
export const kdbConnectionAliasList: string[] = [];
export const maxRetryCount = 5;

export let secretSettings: AuthSettings;
Expand Down
5 changes: 4 additions & 1 deletion src/services/kdbTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
loadVariables,
loadViews,
} from "../models/serverObject";
import { getServerName } from "../utils/core";
import { getInsightsAlias, getServerAlias, getServerName } from "../utils/core";

export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: EventEmitter<
Expand Down Expand Up @@ -102,6 +102,9 @@ export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
private getMergedElements(_element?: TreeItem): TreeItem[] {
const servers = this.getChildElements(_element);
const insights = this.getInsightsChildElements();
ext.kdbConnectionAliasList.length = 0;
getServerAlias(servers.map((x) => x.details));
getInsightsAlias(insights.map((x) => x.details));
return [...servers, ...insights];
}

Expand Down
68 changes: 41 additions & 27 deletions src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as semver from "semver";
import { commands, ConfigurationTarget, Uri, window, workspace } from "vscode";
import { installTools } from "../commands/installTools";
import { ext } from "../extensionVariables";
import { Insights } from "../models/insights";
import { InsightDetails, Insights } from "../models/insights";
import { QueryResult } from "../models/queryResult";
import { Server, ServerDetails } from "../models/server";
import { tryExecuteCommand } from "./cpUtils";
Expand All @@ -39,14 +39,14 @@ export async function checkOpenSslInstalled(): Promise<string | null> {
undefined,
"openSsl",
log,
"version"
"version",
);
if (result.code === 0) {
const matcher = /(\d+.\d+.\d+)/;
const installedVersion = result.cmdOutput.match(matcher);

ext.outputChannel.appendLine(
`Detected version ${installedVersion} of OpenSSL installed.`
`Detected version ${installedVersion} of OpenSSL installed.`,
);

return semver.clean(installedVersion ? installedVersion[1] : "");
Expand All @@ -71,62 +71,62 @@ export function initializeLocalServers(servers: Server): void {
}

export async function addLocalConnectionStatus(
serverName: string
serverName: string,
): Promise<void> {
ext.localConnectionStatus.push(serverName);
await commands.executeCommand(
"setContext",
"kdb.running",
ext.localConnectionStatus
ext.localConnectionStatus,
);
}

export async function removeLocalConnectionStatus(
serverName: string
serverName: string,
): Promise<void> {
const result = ext.localConnectionStatus.filter(
(connection) => connection !== serverName
(connection) => connection !== serverName,
);
ext.localConnectionStatus = result;
await commands.executeCommand(
"setContext",
"kdb.running",
ext.localConnectionStatus
ext.localConnectionStatus,
);
}

export async function addLocalConnectionContexts(
serverName: string
serverName: string,
): Promise<void> {
ext.localConnectionContexts.push(serverName);
await commands.executeCommand(
"setContext",
"kdb.local",
ext.localConnectionContexts
ext.localConnectionContexts,
);
}

export async function removeLocalConnectionContext(
serverName: string
serverName: string,
): Promise<void> {
const result = ext.localConnectionContexts.filter(
(connection) => connection !== serverName
(connection) => connection !== serverName,
);
ext.localConnectionContexts = result;
await commands.executeCommand(
"setContext",
"kdb.local",
ext.localConnectionContexts
ext.localConnectionContexts,
);
}

export function saveLocalProcessObj(
childProcess: ChildProcess,
args: string[]
args: string[],
): void {
window.showInformationMessage("q process started successfully!");
ext.outputChannel.appendLine(
`Child process id ${childProcess.pid!} saved in cache.`
`Child process id ${childProcess.pid!} saved in cache.`,
);
ext.localProcessObjects[args[2]] = childProcess;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ export function getPlatformFolder(platform: string): string | undefined {
}

export async function getWorkspaceFolder(
corePath: string
corePath: string,
): Promise<string | undefined> {
if (await pathExists(join(corePath, "q"))) {
return corePath;
Expand All @@ -181,7 +181,7 @@ export function getHideDetailedConsoleQueryOutput(): void {
.update(
"kdb.hideDetailedConsoleQueryOutput",
true,
ConfigurationTarget.Global
ConfigurationTarget.Global,
);
ext.hideDetailedConsoleQueryOutput = true;
} else {
Expand All @@ -203,15 +203,15 @@ export function setOutputWordWrapper(): void {
.update(
"[Log]",
{ "editor.wordWrap": "off" },
ConfigurationTarget.Global
ConfigurationTarget.Global,
);
}
}

export function getInsights(): Insights | undefined {
const configuration = workspace.getConfiguration();
const insights = configuration.get<Insights>(
"kdb.insightsEnterpriseConnections"
"kdb.insightsEnterpriseConnections",
);

return insights && Object.keys(insights).length > 0
Expand All @@ -231,7 +231,7 @@ export async function updateInsights(insights: Insights): Promise<void> {
.update(
"kdb.insightsEnterpriseConnections",
insights,
ConfigurationTarget.Global
ConfigurationTarget.Global,
);
}

Expand All @@ -241,6 +241,20 @@ export function getServerName(server: ServerDetails): string {
: `${server.serverName}:${server.serverPort}`;
}

export function getServerAlias(serverList: ServerDetails[]): void {
serverList.forEach(({ serverAlias }) => {
if (serverAlias) {
ext.kdbConnectionAliasList.push(serverAlias);
}
});
}

export function getInsightsAlias(insightsList: InsightDetails[]): void {
insightsList.forEach((x) => {
ext.kdbConnectionAliasList.push(x.alias);
});
}

export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand All @@ -254,7 +268,7 @@ export async function checkLocalInstall(): Promise<void> {
}
await writeFile(
join(__dirname, "qinstall.md"),
`# q runtime installed location: \n### ${env.QHOME}`
`# q runtime installed location: \n### ${env.QHOME}`,
);

// persist the QHOME to global settings
Expand All @@ -271,7 +285,7 @@ export async function checkLocalInstall(): Promise<void> {
.get<boolean>("kdb.hideInstallationNotification");
if (!hideNotification) {
window.showInformationMessage(
`Installation of q found here: ${env.QHOME}`
`Installation of q found here: ${env.QHOME}`,
);
}

Expand All @@ -281,7 +295,7 @@ export async function checkLocalInstall(): Promise<void> {
.update(
"kdb.hideInstallationNotification",
true,
ConfigurationTarget.Global
ConfigurationTarget.Global,
);

return;
Expand All @@ -294,7 +308,7 @@ export async function checkLocalInstall(): Promise<void> {
.showInformationMessage(
"Local q installation not found!",
"Install new instance",
"Cancel"
"Cancel",
)
.then(async (installResult) => {
if (installResult === "Install new instance") {
Expand All @@ -307,7 +321,7 @@ export async function checkLocalInstall(): Promise<void> {

export async function convertBase64License(
encodedLicense: string,
tempDir: string = tmpdir()
tempDir: string = tmpdir(),
): Promise<Uri> {
const decodedLicense = Buffer.from(encodedLicense, "base64");
await writeFile(join(tempDir, "kc.lic"), decodedLicense);
Expand Down Expand Up @@ -364,7 +378,7 @@ export function formatTable(headers_: any, rows_: any, opts: any) {
});
return acc;
},
[]
[],
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -400,7 +414,7 @@ export function formatTable(headers_: any, rows_: any, opts: any) {
return acc;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
headers_.map((x: any) => x.length)
headers_.map((x: any) => x.length),
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
8 changes: 8 additions & 0 deletions src/validators/kdbValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* specific language governing permissions and limitations under the License.
*/

import { ext } from "../extensionVariables";
import { validateUtils } from "../utils/validateUtils";

export function validateServerAlias(
Expand All @@ -19,6 +20,9 @@ export function validateServerAlias(
): string | undefined {
// server alias is not required, but should be validated if entered
if (input !== undefined && input !== "") {
if (isAliasInUse(input)) {
return "Server Name is already in use. Please use a different name.";
}
if (input[0] === " ") {
return "Input value cannot start with a space.";
}
Expand Down Expand Up @@ -96,3 +100,7 @@ export function validateTls(input: string | undefined): string | undefined {
}
return undefined;
}

export function isAliasInUse(alias: string): boolean {
return ext.kdbConnectionAliasList.includes(alias);
}
46 changes: 45 additions & 1 deletion test/suite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ext } from "../../src/extensionVariables";
import * as QTable from "../../src/ipc/QTable";
import { CancellationEvent } from "../../src/models/cancellationEvent";
import { QueryResultType } from "../../src/models/queryResult";
import { ServerType } from "../../src/models/server";
import { ServerDetails, ServerType } from "../../src/models/server";
import { InsightsNode, KdbNode } from "../../src/services/kdbTreeProvider";
import { QueryHistoryProvider } from "../../src/services/queryHistoryProvider";
import { KdbResultsViewProvider } from "../../src/services/resultsPanelProvider";
Expand Down Expand Up @@ -47,6 +47,7 @@ import {
DTimestampClass,
} from "../../src/ipc/cClasses";
import { DataSourceTypes } from "../../src/models/dataSource";
import { InsightDetails } from "../../src/models/insights";

interface ITestItem extends vscode.QuickPickItem {
id: number;
Expand Down Expand Up @@ -106,6 +107,49 @@ describe("Utils", () => {
getConfigurationStub.restore();
});

describe("server alias", () => {
beforeEach(() => {
ext.kdbConnectionAliasList.length = 0;
});

afterEach(() => {
ext.kdbConnectionAliasList.length = 0;
});

it("should add insights alias to the list getInsightsAlias", () => {
const insightsDetail: InsightDetails = {
alias: "test",
server: "test",
auth: true,
};
coreUtils.getInsightsAlias([insightsDetail]);
assert.strictEqual(ext.kdbConnectionAliasList.length, 1);
});

it("should add alias only from kdb server that have alias using getServerAlias", () => {
const serverList: ServerDetails[] = [
{
serverName: "test",
serverAlias: "test",
serverPort: "5001",
managed: false,
auth: false,
tls: false,
},
{
serverName: "test2",
serverAlias: undefined,
serverPort: "5001",
managed: false,
auth: false,
tls: false,
},
];
coreUtils.getServerAlias(serverList);
assert.strictEqual(ext.kdbConnectionAliasList.length, 1);
});
});

it("should update configuration and set hideDetailedConsoleQueryOutput to true when setting is undefined", async () => {
getConfigurationStub.returns({
get: sinon.stub().returns(undefined),
Expand Down
Loading

0 comments on commit a45f4d1

Please sign in to comment.