Skip to content

Commit

Permalink
remove ensureLocalConnection()
Browse files Browse the repository at this point in the history
  • Loading branch information
shouples committed Oct 30, 2024
1 parent bf36aec commit 3af86d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
46 changes: 11 additions & 35 deletions src/graphql/local.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -13,7 +13,15 @@ export async function getLocalKafkaClusters(): Promise<LocalKafkaCluster[]> {

// 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 {
Expand Down Expand Up @@ -59,35 +67,3 @@ export async function getLocalKafkaClusters(): Promise<LocalKafkaCluster[]> {
await setContextValue(ContextValues.localKafkaClusterAvailable, localKafkaClusters.length > 0);
return localKafkaClusters;
}

async function ensureLocalConnection(): Promise<void> {
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;
}
14 changes: 7 additions & 7 deletions src/sidecar/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const logger = new Logger("sidecar.connections");
/** Get the existing {@link Connection} (if it exists). */
async function tryToGetConnection(id: string): Promise<Connection | null> {
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) {
if (error instanceof ResponseError) {
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),
Expand All @@ -40,7 +40,7 @@ async function tryToGetConnection(id: string): Promise<Connection | null> {
}
} 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;
Expand All @@ -64,11 +64,11 @@ async function tryToCreateConnection(spec: ConnectionSpec): Promise<Connection>
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;
}
}

Expand All @@ -84,7 +84,7 @@ export async function createLocalConnection(): Promise<Connection> {

/** Delete the existing Confluent Cloud {@link Connection} (if it exists). */
export async function deleteCCloudConnection(): Promise<void> {
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");
Expand Down

0 comments on commit 3af86d4

Please sign in to comment.