From 184910ee6c2249c79faf92eecd10472daf95c9be Mon Sep 17 00:00:00 2001 From: hardiknai-techm <80053762+hardiknai-techm@users.noreply.github.com> Date: Thu, 11 Mar 2021 09:17:08 +0530 Subject: [PATCH] Resolve Lint errors in NotebookConfigurationUtils.ts (#490) --- .eslintignore | 1 - src/Explorer/Tabs/NotebookV2Tab.ts | 2 +- src/Utils/NotebookConfigurationUtils.ts | 150 ++++++++++++------------ 3 files changed, 74 insertions(+), 79 deletions(-) diff --git a/.eslintignore b/.eslintignore index 35b4ceda5..0abcdf33e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -258,7 +258,6 @@ src/TokenProviders/PortalTokenProvider.ts src/TokenProviders/TokenProviderFactory.ts src/Utils/DatabaseAccountUtils.test.ts src/Utils/DatabaseAccountUtils.ts -src/Utils/NotebookConfigurationUtils.ts src/Utils/PricingUtils.test.ts src/Utils/QueryUtils.test.ts src/Utils/QueryUtils.ts diff --git a/src/Explorer/Tabs/NotebookV2Tab.ts b/src/Explorer/Tabs/NotebookV2Tab.ts index 4910c0fc1..02dd85d9f 100644 --- a/src/Explorer/Tabs/NotebookV2Tab.ts +++ b/src/Explorer/Tabs/NotebookV2Tab.ts @@ -24,7 +24,7 @@ import * as CommandBarComponentButtonFactory from "../Menus/CommandBar/CommandBa import { ConsoleDataType } from "../Menus/NotificationConsole/NotificationConsoleComponent"; import * as NotificationConsoleUtils from "../../Utils/NotificationConsoleUtils"; import { NotebookComponentAdapter } from "../Notebook/NotebookComponent/NotebookComponentAdapter"; -import { NotebookConfigurationUtils } from "../../Utils/NotebookConfigurationUtils"; +import * as NotebookConfigurationUtils from "../../Utils/NotebookConfigurationUtils"; import { KernelSpecsDisplay, NotebookClientV2 } from "../Notebook/NotebookClientV2"; import { configContext } from "../../ConfigContext"; import Explorer from "../Explorer"; diff --git a/src/Utils/NotebookConfigurationUtils.ts b/src/Utils/NotebookConfigurationUtils.ts index d108d87c9..2159a9162 100644 --- a/src/Utils/NotebookConfigurationUtils.ts +++ b/src/Utils/NotebookConfigurationUtils.ts @@ -8,85 +8,81 @@ interface KernelConnectionMetadata { notebookConnectionInfo: DataModels.NotebookWorkspaceConnectionInfo; } -export class NotebookConfigurationUtils { - private constructor() {} - - public static async configureServiceEndpoints( - notebookPath: string, - notebookConnectionInfo: DataModels.NotebookWorkspaceConnectionInfo, - kernelName: string, - clusterConnectionInfo: DataModels.SparkClusterConnectionInfo - ): Promise { - if (!notebookPath || !notebookConnectionInfo || !kernelName) { - Logger.logError( - "Invalid or missing notebook connection info/path", - "NotebookConfigurationUtils/configureServiceEndpoints" - ); - return Promise.reject("Invalid or missing notebook connection info"); - } - - if (!clusterConnectionInfo || !clusterConnectionInfo.endpoints || clusterConnectionInfo.endpoints.length === 0) { - Logger.logError( - "Invalid or missing cluster connection info/endpoints", - "NotebookConfigurationUtils/configureServiceEndpoints" - ); - return Promise.reject("Invalid or missing cluster connection info"); - } - - const dataExplorer = window.dataExplorer; - const notebookEndpointInfo: DataModels.NotebookConfigurationEndpointInfo[] = clusterConnectionInfo.endpoints.map( - (clusterEndpoint) => ({ - type: clusterEndpoint.kind.toLowerCase(), - endpoint: clusterEndpoint && clusterEndpoint.endpoint, - username: clusterConnectionInfo.userName, - password: clusterConnectionInfo.password, - token: dataExplorer && dataExplorer.arcadiaToken(), - }) - ); - const configurationEndpoints: DataModels.NotebookConfigurationEndpoints = { - path: notebookPath, - endpoints: notebookEndpointInfo, - }; - const kernelMetadata: KernelConnectionMetadata = { - configurationEndpoints, - notebookConnectionInfo, - name: kernelName, - }; - - return await NotebookConfigurationUtils._configureServiceEndpoints(kernelMetadata); +export const _configureServiceEndpoints = async (kernelMetadata: KernelConnectionMetadata): Promise => { + if (!kernelMetadata) { + // should never get into this state + Logger.logWarning("kernel metadata is null or undefined", "NotebookConfigurationUtils/configureServiceEndpoints"); + return; } - private static async _configureServiceEndpoints(kernelMetadata: KernelConnectionMetadata): Promise { - if (!kernelMetadata) { - // should never get into this state - Logger.logWarning("kernel metadata is null or undefined", "NotebookConfigurationUtils/configureServiceEndpoints"); - return; - } - - const notebookConnectionInfo = kernelMetadata.notebookConnectionInfo; - const configurationEndpoints = kernelMetadata.configurationEndpoints; - if (notebookConnectionInfo && configurationEndpoints) { - try { - const headers: any = { "Content-Type": "application/json" }; - if (notebookConnectionInfo.authToken) { - headers["Authorization"] = `token ${notebookConnectionInfo.authToken}`; - } - const response = await fetch(`${notebookConnectionInfo.notebookServerEndpoint}/api/configureEndpoints`, { - method: "POST", - headers, - body: JSON.stringify(configurationEndpoints), - }); - if (!response.ok) { - const responseMessage = await response.json(); - Logger.logError( - getErrorMessage(responseMessage), - "NotebookConfigurationUtils/configureServiceEndpoints", - response.status - ); - } - } catch (error) { - Logger.logError(getErrorMessage(error), "NotebookConfigurationUtils/configureServiceEndpoints"); + const notebookConnectionInfo = kernelMetadata.notebookConnectionInfo; + const configurationEndpoints = kernelMetadata.configurationEndpoints; + if (notebookConnectionInfo && configurationEndpoints) { + try { + const headers: HeadersInit = { "Content-Type": "application/json" }; + if (notebookConnectionInfo.authToken) { + headers["Authorization"] = `token ${notebookConnectionInfo.authToken}`; } + const response = await fetch(`${notebookConnectionInfo.notebookServerEndpoint}/api/configureEndpoints`, { + method: "POST", + headers, + body: JSON.stringify(configurationEndpoints), + }); + if (!response.ok) { + const responseMessage = await response.json(); + Logger.logError( + getErrorMessage(responseMessage), + "NotebookConfigurationUtils/configureServiceEndpoints", + response.status + ); + } + } catch (error) { + Logger.logError(getErrorMessage(error), "NotebookConfigurationUtils/configureServiceEndpoints"); } } -} +}; + +export const configureServiceEndpoints = async ( + notebookPath: string, + notebookConnectionInfo: DataModels.NotebookWorkspaceConnectionInfo, + kernelName: string, + clusterConnectionInfo: DataModels.SparkClusterConnectionInfo +): Promise => { + if (!notebookPath || !notebookConnectionInfo || !kernelName) { + Logger.logError( + "Invalid or missing notebook connection info/path", + "NotebookConfigurationUtils/configureServiceEndpoints" + ); + return Promise.reject("Invalid or missing notebook connection info"); + } + + if (!clusterConnectionInfo || !clusterConnectionInfo.endpoints || clusterConnectionInfo.endpoints.length === 0) { + Logger.logError( + "Invalid or missing cluster connection info/endpoints", + "NotebookConfigurationUtils/configureServiceEndpoints" + ); + return Promise.reject("Invalid or missing cluster connection info"); + } + + const dataExplorer = window.dataExplorer; + const notebookEndpointInfo: DataModels.NotebookConfigurationEndpointInfo[] = clusterConnectionInfo.endpoints.map( + (clusterEndpoint) => ({ + type: clusterEndpoint.kind.toLowerCase(), + endpoint: clusterEndpoint && clusterEndpoint.endpoint, + username: clusterConnectionInfo.userName, + password: clusterConnectionInfo.password, + token: dataExplorer && dataExplorer.arcadiaToken(), + }) + ); + const configurationEndpoints: DataModels.NotebookConfigurationEndpoints = { + path: notebookPath, + endpoints: notebookEndpointInfo, + }; + const kernelMetadata: KernelConnectionMetadata = { + configurationEndpoints, + notebookConnectionInfo, + name: kernelName, + }; + + return await _configureServiceEndpoints(kernelMetadata); +};