Skip to content

Commit

Permalink
Merge pull request #420 from Philip-Carneiro/KXI-52687
Browse files Browse the repository at this point in the history
KXI-52687 - Export connections
  • Loading branch information
Philip-Carneiro-KX authored Sep 5, 2024
2 parents 73cb64c + 16339d9 commit 0a2e240
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 55 deletions.
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@
}
},
"commands": [
{
"category": "KX",
"command": "kdb.connections.export.all",
"title": "Export connections"
},
{
"category": "KX",
"command": "kdb.connections.export.single",
"title": "Export connection"
},
{
"category": "KX",
"command": "kdb.connections.import",
"title": "Import connections"
},
{
"category": "KX",
"command": "kdb.refreshServerObjects",
Expand Down Expand Up @@ -701,6 +716,16 @@
"command": "kdb.resultsPanel.export.csv",
"when": "view == kdb-results",
"group": "resultsPanel"
},
{
"command": "kdb.connections.export.all",
"when": "view == kdb-servers",
"group": "inline"
},
{
"command": "kdb.connections.import",
"when": "view == kdb-servers",
"group": "inline"
}
],
"view/item/context": [
Expand Down Expand Up @@ -749,6 +774,11 @@
"when": "view == kdb-servers && viewItem in kdb.rootNodes",
"group": "connection@5"
},
{
"command": "kdb.connections.export.single",
"when": "view == kdb-servers && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)",
"group": "connection@6"
},
{
"command": "kdb.startLocalProcess",
"when": "view == kdb-servers && viewItem in kdb.local && viewItem not in kdb.running && viewItem in kdb.rootNodes",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import {
writeQueryResultsToConsole,
writeQueryResultsToView,
} from "./serverCommand";
import { ServerType } from "../models/server";
import { Telemetry } from "../utils/telemetryClient";
import { LocalConnection } from "../classes/localConnection";
import { ConnectionManagementService } from "../services/connectionManagerService";
import { InsightsConnection } from "../classes/insightsConnection";
import { kdbOutputLog, offerConnectAction } from "../utils/core";
import { ServerType } from "../models/connectionsModels";

export async function addDataSource(): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/installTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
onboardingInput,
onboardingWorkflow,
} from "../models/items/onboarding";
import { Server } from "../models/server";
import { KdbNode } from "../services/kdbTreeProvider";
import {
addLocalConnectionContexts,
Expand All @@ -64,6 +63,7 @@ import { executeCommand } from "../utils/cpUtils";
import { openUrl } from "../utils/openUrl";
import { Telemetry } from "../utils/telemetryClient";
import { validateServerPort } from "../validators/kdbValidator";
import { Server } from "../models/connectionsModels";

export async function installTools(): Promise<void> {
let file: Uri[] | undefined;
Expand Down
50 changes: 48 additions & 2 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import {
import { ext } from "../extensionVariables";
import { DataSourceFiles } from "../models/dataSource";
import { ExecutionTypes } from "../models/execution";
import { InsightDetails, Insights } from "../models/insights";
import { queryConstants } from "../models/queryResult";
import { ScratchpadResult } from "../models/scratchpadResult";
import { Server, ServerDetails, ServerType } from "../models/server";
import { ServerObject } from "../models/serverObject";
import { DataSourcesPanel } from "../panels/datasource";
import {
Expand Down Expand Up @@ -69,6 +67,14 @@ import { ConnectionManagementService } from "../services/connectionManagerServic
import { InsightsConnection } from "../classes/insightsConnection";
import { MetaContentProvider } from "../services/metaContentProvider";
import { handleLabelsConnMap, removeConnFromLabels } from "../utils/connLabel";
import {
InsightDetails,
Insights,
Server,
ServerDetails,
ServerType,
} from "../models/connectionsModels";
import * as fs from "fs";

export async function addNewConnection(): Promise<void> {
NewConnectionPannel.close();
Expand Down Expand Up @@ -875,6 +881,46 @@ export async function openMeta(node: MetaObjectPayloadNode | InsightsMetaNode) {
}
}

export async function exportConnections(connLabel?: string) {
const connMngService = new ConnectionManagementService();
const doc = connMngService.exportConnection(connLabel);
if (doc && doc !== "") {
const formattedDoc = JSON.stringify(JSON.parse(doc), null, 2);
const uri = await window.showSaveDialog({
saveLabel: "Save Exported Connections",
filters: {
"JSON Files": ["json"],
"All Files": ["*"],
},
});
if (uri) {
fs.writeFile(uri.fsPath, formattedDoc, (err) => {
if (err) {
kdbOutputLog(
`[EXPORT CONNECTIONS] Error saving file: ${err.message}`,
"ERROR",
);
window.showErrorMessage(`Error saving file: ${err.message}`);
} else {
workspace.openTextDocument(uri).then((document) => {
window.showTextDocument(document, { preview: false });
});
}
});
} else {
kdbOutputLog(
"[EXPORT CONNECTIONS] Save operation was cancelled by the user",
"INFO",
);
}
} else {
kdbOutputLog(
"[EXPORT CONNECTIONS] No connections found to be exported",
"ERROR",
);
}
}

export function writeQueryResultsToConsole(
result: string | string[],
query: string,
Expand Down
21 changes: 19 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
editInsightsConnection,
editKdbConnection,
enableTLS,
exportConnections,
openMeta,
refreshGetMeta,
removeConnection,
Expand All @@ -59,9 +60,7 @@ import {
import { showInstallationDetails } from "./commands/walkthroughCommand";
import { ext } from "./extensionVariables";
import { ExecutionTypes } from "./models/execution";
import { InsightDetails, Insights } from "./models/insights";
import { QueryResult } from "./models/queryResult";
import { Server, ServerDetails } from "./models/server";
import {
InsightsMetaNode,
InsightsNode,
Expand Down Expand Up @@ -114,6 +113,12 @@ import {
setLabelColor,
} from "./utils/connLabel";
import { activateTextDocument } from "./utils/workspace";
import {
InsightDetails,
Insights,
Server,
ServerDetails,
} from "./models/connectionsModels";

let client: LanguageClient;

Expand Down Expand Up @@ -267,6 +272,18 @@ export async function activate(context: ExtensionContext) {
await disconnect(connLabel);
},
),
commands.registerCommand("kdb.connections.export.all", () => {
exportConnections();
}),
commands.registerCommand(
"kdb.connections.export.single",
async (viewItem: KdbNode | InsightsNode) => {
exportConnections(viewItem.label);
},
),
commands.registerCommand("kdb.connections.import", () => {
window.showInformationMessage("Import Connections command executed");
}),
commands.registerCommand(
"kdb.open.meta",
async (viewItem: InsightsMetaNode | MetaObjectPayloadNode) => {
Expand Down
21 changes: 21 additions & 0 deletions src/models/server.ts → src/models/connectionsModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* specific language governing permissions and limitations under the License.
*/

//TODO: start to migrate all connections models to here

export enum ServerType {
INSIGHTS,
KDB,
Expand All @@ -31,3 +33,22 @@ export interface ServerDetails {
export interface Server {
[name: string]: ServerDetails;
}

export interface InsightDetails {
alias: string;
server: string;
auth: boolean;
realm?: string;
insecure?: boolean;
}

export interface Insights {
[name: string]: InsightDetails;
}

export interface ExportedConnections {
connections: {
Insights: InsightDetails[];
KDB: ServerDetails[];
};
}
24 changes: 0 additions & 24 deletions src/models/insights.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/models/queryHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* specific language governing permissions and limitations under the License.
*/

import { ServerType } from "./connectionsModels";
import { DataSourceFiles, DataSourceTypes } from "./dataSource";
import { ServerType } from "./server";

export interface QueryHistory {
executorName: string;
Expand Down
41 changes: 39 additions & 2 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import {
updateInsights,
updateServers,
} from "../utils/core";
import { Insights } from "../models/insights";
import { Server } from "../models/server";
import { refreshDataSourcesPanel } from "../utils/dataSource";
import { MetaInfoType } from "../models/meta";
import { retrieveConnLabelsNames } from "../utils/connLabel";
import {
ExportedConnections,
Insights,
Server,
} from "../models/connectionsModels";

export class ConnectionManagementService {
public retrieveConnection(
Expand Down Expand Up @@ -403,4 +406,38 @@ export class ConnectionManagementService {

return connection.returnMetaObject(metaType);
}

public exportConnection(connLabel?: string): string {
const exportedContent: ExportedConnections = {
connections: {
Insights: [],
KDB: [],
},
};
if (connLabel) {
const connection = this.retrieveConnection(connLabel);
if (!connection) {
return "";
}
if (connection instanceof KdbNode) {
connection.details.auth = false;
exportedContent.connections.KDB.push(connection.details);
} else {
exportedContent.connections.Insights.push(connection.details);
}
} else {
ext.connectionsList.forEach((connection) => {
if (connection instanceof KdbNode) {
connection.details.auth = false;
exportedContent.connections.KDB.push(connection.details);
} else {
exportedContent.connections.Insights.push(connection.details);
}
});
}
return exportedContent.connections.Insights.length === 0 &&
exportedContent.connections.KDB.length === 0
? ""
: JSON.stringify(exportedContent, null, 2);
}
}
8 changes: 6 additions & 2 deletions src/services/kdbTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import {
commands,
} from "vscode";
import { ext } from "../extensionVariables";
import { InsightDetails, Insights } from "../models/insights";
import { Server, ServerDetails } from "../models/server";
import {
loadDictionaries,
loadFunctions,
Expand All @@ -49,6 +47,12 @@ import {
retrieveConnLabelsNames,
} from "../utils/connLabel";
import { Labels } from "../models/labels";
import {
InsightDetails,
Insights,
Server,
ServerDetails,
} from "../models/connectionsModels";

export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: EventEmitter<
Expand Down
19 changes: 11 additions & 8 deletions src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import * as semver from "semver";
import { commands, ConfigurationTarget, Uri, window, workspace } from "vscode";
import { installTools } from "../commands/installTools";
import { ext } from "../extensionVariables";
import { InsightDetails, Insights } from "../models/insights";
import { QueryResult } from "../models/queryResult";
import { Server, ServerDetails } from "../models/server";
import { tryExecuteCommand } from "./cpUtils";
import { showRegistrationNotification } from "./registration";
import { Telemetry } from "./telemetryClient";
import {
InsightDetails,
Insights,
Server,
ServerDetails,
} from "../models/connectionsModels";

export function log(childProcess: ChildProcess): void {
kdbOutputLog(`Process ${childProcess.pid} started`, "INFO");
Expand Down Expand Up @@ -312,26 +316,25 @@ export function kdbOutputLog(message: string, type: string): void {
const dateNow = new Date().toLocaleDateString();
const timeNow = new Date().toLocaleTimeString();
ext.outputChannel.appendLine(`[${dateNow} ${timeNow}] [${type}] ${message}`);
if (type === "ERROR") {
window.showErrorMessage(
`Error occured, check kdb output channel for details.`,
);
}
}

export function tokenUndefinedError(connLabel: string): void {
kdbOutputLog(
`Error retrieving access token for Insights connection named: ${connLabel}`,
"ERROR",
);
window.showErrorMessage(
`Error retrieving access token for Insights connection named: ${connLabel}`,
);
}

export function invalidUsernameJWT(connLabel: string): void {
kdbOutputLog(
`JWT did not contain a valid preferred username for Insights connection: ${connLabel}`,
"ERROR",
);
window.showErrorMessage(
`JWT did not contain a valid preferred username for Insights connection: ${connLabel}`,
);
}

/* istanbul ignore next */
Expand Down
Loading

0 comments on commit 0a2e240

Please sign in to comment.