From 60676243fa217c1bc2e007a7043feb3fcf3ed3cc Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Wed, 11 Dec 2024 12:08:56 -0300 Subject: [PATCH] Validate results gotten from sema4ai-data.dataserver.start. --- sema4ai/vscode-client/src/dataExtension.ts | 69 ++++++++++++++++++- .../vscode-client/src/robo/actionPackage.ts | 12 +--- .../src/robo/dataSourceHandling.ts | 11 ++- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/sema4ai/vscode-client/src/dataExtension.ts b/sema4ai/vscode-client/src/dataExtension.ts index 60bb0778..08540a15 100644 --- a/sema4ai/vscode-client/src/dataExtension.ts +++ b/sema4ai/vscode-client/src/dataExtension.ts @@ -1,5 +1,6 @@ import { commands, extensions, window } from "vscode"; -import { logError } from "./channel"; +import { logError, OUTPUT_CHANNEL } from "./channel"; +import { DataServerConfig } from "./robo/actionPackage"; const DATA_EXTENSION_ID = "sema4ai.sema4ai-data-access"; export const DATA_SERVER_START_COMMAND_ID = "sema4ai-data.dataserver.start"; @@ -85,3 +86,69 @@ export async function verifyDataExtensionIsInstalled( return false; } + +function failWithErrorMessage(dataServerInfo: DataServerConfig, errorMessage: string) { + OUTPUT_CHANNEL.appendLine( + `${errorMessage} (obtained from the ${DATA_SERVER_START_COMMAND_ID} command). Full data server info: ` + + JSON.stringify(dataServerInfo, null, 4) + ); + window.showErrorMessage(errorMessage); +} + +export async function startDataServerAndGetInfo(): Promise { + const dataServerInfo = (await commands.executeCommand(DATA_SERVER_START_COMMAND_ID, { + "showUIMessages": false, + })) as DataServerConfig | undefined; + if (dataServerInfo) { + if (!dataServerInfo.isRunning) { + failWithErrorMessage( + dataServerInfo, + "After starting the data server, isRunning still returning false in provided data server config." + ); + return undefined; + } + + // Let's validate that the data server info has the correct structure (check each field one by one and + // show error if it's not correct) + if (!dataServerInfo.api) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api' field"); + return undefined; + } + + if (!dataServerInfo.api.http) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.http' field"); + return undefined; + } + + if (!dataServerInfo.api.http.host) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.http.host' field"); + return undefined; + } + + if (!dataServerInfo.api.http.port) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.http.port' field"); + return undefined; + } + + if (!dataServerInfo.api.mysql) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.mysql' field"); + return undefined; + } + + if (!dataServerInfo.api.mysql.host) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.mysql.host' field"); + return undefined; + } + + if (!dataServerInfo.api.mysql.port) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'api.mysql.port' field"); + return undefined; + } + + if (!dataServerInfo.auth) { + failWithErrorMessage(dataServerInfo, "The data server info is missing the 'auth' field"); + return undefined; + } + } + return dataServerInfo; +} diff --git a/sema4ai/vscode-client/src/robo/actionPackage.ts b/sema4ai/vscode-client/src/robo/actionPackage.ts index 2ec25c95..5900c048 100644 --- a/sema4ai/vscode-client/src/robo/actionPackage.ts +++ b/sema4ai/vscode-client/src/robo/actionPackage.ts @@ -56,11 +56,7 @@ import { loginToAuth2WhereRequired } from "./oauth2InInput"; import { RobotEntryType } from "../viewsCommon"; import { createActionInputs, errorMessageValidatingV2Input } from "./actionInputs"; import { langServer } from "../extension"; -import { - DATA_SERVER_START_COMMAND_ID, - DATA_SERVER_STATUS_COMMAND_ID, - verifyDataExtensionIsInstalled, -} from "../dataExtension"; +import { startDataServerAndGetInfo, verifyDataExtensionIsInstalled } from "../dataExtension"; export interface QuickPickItemAction extends QuickPickItem { actionPackageUri: vscode.Uri; @@ -574,12 +570,10 @@ advised to regenerate it as it may not work with future versions of the extensio progress.report({ message: "Waiting for data server info... ", }); - const dataServerInfo = (await commands.executeCommand(DATA_SERVER_START_COMMAND_ID, { - "showUIMessages": false, - })) as DataServerConfig | undefined; + const dataServerInfo = await startDataServerAndGetInfo(); if (!dataServerInfo) { window.showErrorMessage( - "Unable to run (error getting local data server connection info and validating data sources):\n" + + "Unable to run (error getting local data server connection info):\n" + JSON.stringify(dataServerInfo, null, 4) ); return false; diff --git a/sema4ai/vscode-client/src/robo/dataSourceHandling.ts b/sema4ai/vscode-client/src/robo/dataSourceHandling.ts index f5fdaafc..f713179c 100644 --- a/sema4ai/vscode-client/src/robo/dataSourceHandling.ts +++ b/sema4ai/vscode-client/src/robo/dataSourceHandling.ts @@ -1,10 +1,9 @@ -import { commands, Uri, window } from "vscode"; +import { Uri, window } from "vscode"; import { OUTPUT_CHANNEL } from "../channel"; import { RobotEntry } from "../viewsCommon"; import { DatasourceInfo } from "../protocols"; import { langServer } from "../extension"; -import { DataServerConfig } from "./actionPackage"; -import { DATA_SERVER_START_COMMAND_ID } from "../dataExtension"; +import { startDataServerAndGetInfo } from "../dataExtension"; export const setupDataSource = async (entry?: RobotEntry) => { if (!entry || !entry.extraData || !entry.extraData.datasource) { @@ -12,12 +11,10 @@ export const setupDataSource = async (entry?: RobotEntry) => { return; } - const dataServerInfo = (await commands.executeCommand(DATA_SERVER_START_COMMAND_ID, { - "showUIMessages": false, - })) as DataServerConfig | undefined; + const dataServerInfo = await startDataServerAndGetInfo(); if (!dataServerInfo) { window.showErrorMessage( - "Unable to run (error getting local data server connection info and validating data sources):\n" + + "Unable to run (error getting local data server connection info):\n" + JSON.stringify(dataServerInfo, null, 4) ); return false;