Skip to content

Commit

Permalink
Merge pull request #384 from KxSystems/KXI-50032
Browse files Browse the repository at this point in the history
KXI-50032 - edit connection
  • Loading branch information
Philip-Carneiro-KX authored Jul 30, 2024
2 parents 032c031 + 1607b47 commit 940b81e
Show file tree
Hide file tree
Showing 12 changed files with 1,116 additions and 231 deletions.
22 changes: 13 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "kdb",
"description": "IDE support for kdb product suite",
"publisher": "KX",
"version": "1.6.1",
"version": "1.7.0",
"engines": {
"vscode": "^1.86.0"
},
Expand Down Expand Up @@ -35,6 +35,9 @@
"onCommand:kdb.newConnection.createNewInsightConnection",
"onCommand:kdb.newConnection.createNewConnection",
"onCommand:kdb.newConnection.createNewBundledConnection",
"onCommand:kdb.newConnection.editInsightsConnection",
"onCommand:kdb.newConnection.editMyQConnection",
"onCommand:kdb.newConnection.editBundledConnection",
"onView:kdb-datasources-explorer",
"onTerminalProfile:kdb.q-terminal",
"onLanguage:python"
Expand Down Expand Up @@ -253,6 +256,11 @@
"title": "New connection...",
"icon": "$(add)"
},
{
"category": "KX",
"command": "kdb.editConnection",
"title": "Edit connection"
},
{
"category": "KX",
"command": "kdb.removeConnection",
Expand Down Expand Up @@ -561,6 +569,10 @@
"command": "kdb.addConnection",
"when": "false"
},
{
"command": "kdb.editConnection",
"when": "false"
},
{
"command": "kdb.removeConnection",
"when": "false"
Expand Down Expand Up @@ -665,6 +677,11 @@
"when": "view == kdb-servers && viewItem not in kdb.connected && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)",
"group": "connection@1"
},
{
"command": "kdb.editConnection",
"when": "view == kdb-servers",
"group": "connection@4"
},
{
"command": "kdb.active.connection",
"when": "view == kdb-servers && viewItem in kdb.connected && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes) && viewItem not in kdb.connected.active",
Expand Down
211 changes: 209 additions & 2 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ import { InsightsConnection } from "../classes/insightsConnection";
import { MetaContentProvider } from "../services/metaContentProvider";

export async function addNewConnection(): Promise<void> {
NewConnectionPannel.close();
NewConnectionPannel.render(ext.context.extensionUri);
}

export async function editConnection(viewItem: KdbNode | InsightsNode) {
NewConnectionPannel.close();
NewConnectionPannel.render(ext.context.extensionUri, viewItem);
}

export async function addInsightsConnection(insightsData: InsightDetails) {
const aliasValidation = validateServerAlias(insightsData.alias, false);
if (aliasValidation) {
Expand Down Expand Up @@ -131,6 +137,88 @@ export async function addInsightsConnection(insightsData: InsightDetails) {
}
}

/* istanbul ignore next */
export async function editInsightsConnection(
insightsData: InsightDetails,
oldAlias: string,
) {
const aliasValidation =
oldAlias === insightsData.alias
? undefined
: validateServerAlias(insightsData.alias, false);
if (aliasValidation) {
window.showErrorMessage(aliasValidation);
return;
}
await disconnect(oldAlias);
if (insightsData.alias === undefined || insightsData.alias === "") {
const host = new url.URL(insightsData.server);
insightsData.alias = host.host;
}
const insights: Insights | undefined = getInsights();
if (insights) {
const oldInsights = insights[getKeyForServerName(oldAlias)];
const newAliasExists =
oldAlias !== insightsData.alias
? insights[getKeyForServerName(insightsData.alias)]
: undefined;
if (newAliasExists) {
await window.showErrorMessage(
`Insights instance named ${insightsData.alias} already exists.`,
);
return;
} else {
if (!oldInsights) {
await window.showErrorMessage(
`Insights instance named ${oldAlias} does not exist.`,
);
return;
} else {
const oldKey = getKeyForServerName(oldAlias);
const newKey = insightsData.alias;
if (insights[oldKey] && oldAlias !== insightsData.alias) {
const uInsights = Object.keys(insights).filter((insight) => {
return insight !== oldKey;
});
const updatedInsights: Insights = {};
uInsights.forEach((insight) => {
updatedInsights[insight] = insights[insight];
});

updatedInsights[newKey] = {
auth: true,
alias: insightsData.alias,
server: insightsData.server,
realm: insightsData.realm,
insecure: insightsData.insecure,
};

await updateInsights(updatedInsights);
} else {
insights[oldKey] = {
auth: true,
alias: insightsData.alias,
server: insightsData.server,
realm: insightsData.realm,
insecure: insightsData.insecure,
};
await updateInsights(insights);
}

const newInsights = getInsights();
if (newInsights != undefined) {
ext.serverProvider.refreshInsights(newInsights);
Telemetry.sendEvent("Connection.Edited.Insights");
}
window.showInformationMessage(
`Edited Insights connection: ${insightsData.alias}`,
);
NewConnectionPannel.close();
}
}
}
}

// Not possible to test secrets
/* istanbul ignore next */
export async function addAuthConnection(
Expand Down Expand Up @@ -167,6 +255,16 @@ export async function addAuthConnection(
}
}

// Not possible to test secrets
/* istanbul ignore next */
function removeAuthConnection(serverKey: string) {
if (ext.secretSettings.storeAuthData.hasOwnProperty(serverKey)) {
delete (ext.secretSettings.storeAuthData as { [key: string]: any })[
serverKey
];
}
}

export async function enableTLS(serverKey: string): Promise<void> {
const servers: Server | undefined = getServers();

Expand Down Expand Up @@ -237,7 +335,7 @@ export async function addKdbConnection(
serverName: kdbData.serverName,
serverPort: kdbData.serverPort,
serverAlias: kdbData.serverAlias,
managed: kdbData.serverAlias === "local" ? true : false,
managed: kdbData.serverAlias === "local",
tls: kdbData.tls,
},
};
Expand All @@ -250,7 +348,7 @@ export async function addKdbConnection(
serverName: kdbData.serverName,
serverPort: kdbData.serverPort,
serverAlias: kdbData.serverAlias,
managed: kdbData.serverAlias === "local" ? true : false,
managed: kdbData.serverAlias === "local",
tls: kdbData.tls,
};
if (servers[key].managed) {
Expand All @@ -274,6 +372,115 @@ export async function addKdbConnection(
}
}

/* istanbul ignore next */
export async function editKdbConnection(
kdbData: ServerDetails,
oldAlias: string,
isLocal?: boolean,
editAuth?: boolean,
) {
const aliasValidation =
oldAlias === kdbData.serverAlias
? undefined
: validateServerAlias(kdbData.serverAlias, isLocal!);
const hostnameValidation = validateServerName(kdbData.serverName);
const portValidation = validateServerPort(kdbData.serverPort);
if (aliasValidation) {
window.showErrorMessage(aliasValidation);
return;
}
if (hostnameValidation) {
window.showErrorMessage(hostnameValidation);
return;
}
if (portValidation) {
window.showErrorMessage(portValidation);
return;
}
await disconnect(oldAlias);
let servers: Server | undefined = getServers();

if (servers) {
const oldServer = servers[getKeyForServerName(oldAlias)];
const newAliasExists =
oldAlias !== kdbData.serverAlias
? servers[getKeyForServerName(kdbData.serverAlias)]
: undefined;
if (newAliasExists) {
await window.showErrorMessage(
`KDB instance named ${kdbData.serverAlias} already exists.`,
);
return;
} else {
if (!oldServer) {
await window.showErrorMessage(
`KDB instance named ${oldAlias} does not exist.`,
);
return;
} else {
const oldKey = getKeyForServerName(oldAlias);
const newKey = kdbData.serverAlias;
const removedAuth =
editAuth && (kdbData.username === "" || kdbData.password === "");
if (servers[oldKey] && oldAlias !== kdbData.serverAlias) {
const uServers = Object.keys(servers).filter((server) => {
return server !== oldKey;
});
const updatedServers: Server = {};
uServers.forEach((server) => {
updatedServers[server] = servers[server];
});

updatedServers[newKey] = {
auth: removedAuth ? false : kdbData.auth,
serverName: kdbData.serverName,
serverPort: kdbData.serverPort,
serverAlias: kdbData.serverAlias,
managed: kdbData.serverAlias === "local",
tls: kdbData.tls,
};

await updateServers(updatedServers);
} else {
servers[oldKey] = {
auth: removedAuth ? false : kdbData.auth,
serverName: kdbData.serverName,
serverPort: kdbData.serverPort,
serverAlias: kdbData.serverAlias,
managed: kdbData.serverAlias === "local",
tls: kdbData.tls,
};

await updateServers(servers);
}
const newServers = getServers();
if (newServers != undefined) {
ext.serverProvider.refresh(newServers);
Telemetry.sendEvent("Connection.Edited.KDB");
}
window.showInformationMessage(
`Edited KDB connection: ${kdbData.serverAlias}`,
);
if (oldKey !== newKey) {
removeAuthConnection(oldKey);
if (kdbData.auth) {
addAuthConnection(newKey, kdbData.username!, kdbData.password!);
}
} else {
if (editAuth && !removedAuth) {
addAuthConnection(newKey, kdbData.username!, kdbData.password!);
}
if (editAuth && removedAuth) {
removeAuthConnection(newKey);
}
}

NewConnectionPannel.close();
}
}
}
}

export async function removeConnection(viewItem: KdbNode | InsightsNode) {
const connMngService = new ConnectionManagementService();
await connMngService.removeConnection(viewItem);
Expand Down
Loading

0 comments on commit 940b81e

Please sign in to comment.