From 3af86d419500025bc98a5d0f295fce9fa12a9776 Mon Sep 17 00:00:00 2001 From: Dave Shoup Date: Wed, 23 Oct 2024 12:10:29 -0400 Subject: [PATCH] remove `ensureLocalConnection()` --- src/graphql/local.ts | 46 +++++++++----------------------------- src/sidecar/connections.ts | 14 ++++++------ 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/src/graphql/local.ts b/src/graphql/local.ts index d15d8001..db25022b 100644 --- a/src/graphql/local.ts +++ b/src/graphql/local.ts @@ -1,10 +1,10 @@ import { graphql } from "gql.tada"; -import { Connection, ConnectionsResourceApi, ResponseError } from "../clients/sidecar"; -import { LOCAL_CONNECTION_ID, LOCAL_CONNECTION_SPEC } from "../constants"; +import { LOCAL_CONNECTION_ID } from "../constants"; import { ContextValues, setContextValue } from "../context"; import { Logger } from "../logging"; import { LocalKafkaCluster } from "../models/kafkaCluster"; import { getSidecar } from "../sidecar"; +import { createLocalConnection, getLocalConnection } from "../sidecar/connections"; const logger = new Logger("graphql.local"); @@ -13,7 +13,15 @@ export async function getLocalKafkaClusters(): Promise { // this is a bit odd, but we need to have a local "connection" to the sidecar before we can query // it for local Kafka clusters, so check if we have a connection first - await ensureLocalConnection(); + if (!(await getLocalConnection())) { + try { + await createLocalConnection(); + } catch { + // error should be caught+logged in createLocalConnection + // TODO: window.showErrorMessage here? might get noisy since this is triggered from refreshes + return localKafkaClusters; + } + } const query = graphql(` query localConnections { @@ -59,35 +67,3 @@ export async function getLocalKafkaClusters(): Promise { await setContextValue(ContextValues.localKafkaClusterAvailable, localKafkaClusters.length > 0); return localKafkaClusters; } - -async function ensureLocalConnection(): Promise { - const client: ConnectionsResourceApi = (await getSidecar()).getConnectionsResourceApi(); - - let localConnection: Connection | null = null; - try { - localConnection = await client.gatewayV1ConnectionsIdGet({ - id: LOCAL_CONNECTION_ID, - }); - } catch (e) { - if (e instanceof ResponseError) { - if (e.response.status === 404) { - logger.debug("No local connection"); - } else { - logger.error("Error response from fetching existing local connection:", { - status: e.response.status, - statusText: e.response.statusText, - body: JSON.stringify(e.response.body), - }); - } - } else { - logger.error("Error while fetching local connection:", e); - } - } - - if (!localConnection) { - await client.gatewayV1ConnectionsPost({ - ConnectionSpec: LOCAL_CONNECTION_SPEC, - }); - } - return; -} diff --git a/src/sidecar/connections.ts b/src/sidecar/connections.ts index 53bda987..73268b6f 100644 --- a/src/sidecar/connections.ts +++ b/src/sidecar/connections.ts @@ -23,7 +23,7 @@ const logger = new Logger("sidecar.connections"); /** Get the existing {@link Connection} (if it exists). */ async function tryToGetConnection(id: string): Promise { let connection: Connection | null = null; - const client = (await getSidecar()).getConnectionsResourceApi(); + const client: ConnectionsResourceApi = (await getSidecar()).getConnectionsResourceApi(); try { connection = await client.gatewayV1ConnectionsIdGet({ id: id }); } catch (error) { @@ -31,7 +31,7 @@ async function tryToGetConnection(id: string): Promise { if (error.response.status === 404) { logger.debug("No connection found", { connectionId: id }); } else { - logger.error("Error response from fetching existing local connection:", { + logger.error("Error response fetching existing connection:", { status: error.response.status, statusText: error.response.statusText, body: JSON.stringify(error.response.body), @@ -40,7 +40,7 @@ async function tryToGetConnection(id: string): Promise { } } else { // only log the non-404 errors, since we expect a 404 if the connection doesn't exist - logger.error("Error getting existing connection", { error, connectionId: id }); + logger.error("Error fetching connection", { error, connectionId: id }); } } return connection; @@ -64,11 +64,11 @@ async function tryToCreateConnection(spec: ConnectionSpec): Promise connection = await client.gatewayV1ConnectionsPost({ ConnectionSpec: spec, }); - logger.debug("created new connection", { type: spec.type }); + logger.debug("created new connection:", { type: spec.type }); return connection; } catch (error) { - logger.error("create connection error", error); - throw new Error("Error while trying to create new connection. Please try again."); + logger.error("create connection error:", error); + throw error; } } @@ -84,7 +84,7 @@ export async function createLocalConnection(): Promise { /** Delete the existing Confluent Cloud {@link Connection} (if it exists). */ export async function deleteCCloudConnection(): Promise { - const client = (await getSidecar()).getConnectionsResourceApi(); + const client: ConnectionsResourceApi = (await getSidecar()).getConnectionsResourceApi(); try { await client.gatewayV1ConnectionsIdDelete({ id: CCLOUD_CONNECTION_ID }); logger.debug("deleted existing CCloud connection");