Skip to content

feat: support flex clusters to atlas tools #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ function filterOpenapi(openapi: OpenAPIV3_1.Document): OpenAPIV3_1.Document {
"createProject",
"deleteProject",
"listClusters",
"listFlexClusters",
"getCluster",
"getFlexCluster",
"createCluster",
"createFlexCluster",
"deleteCluster",
"deleteFlexCluster",
"listClustersForAllProjects",
"createDatabaseUser",
"deleteDatabaseUser",
Expand Down
40 changes: 40 additions & 0 deletions src/common/atlas/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ export class ApiClient {
}
}

async listFlexClusters(options: FetchOptions<operations["listFlexClusters"]>) {
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}/flexClusters", options);
if (error) {
throw ApiClientError.fromError(response, error);
}
return data;
}

async createFlexCluster(options: FetchOptions<operations["createFlexCluster"]>) {
const { data, error, response } = await this.client.POST(
"/api/atlas/v2/groups/{groupId}/flexClusters",
options
);
if (error) {
throw ApiClientError.fromError(response, error);
}
return data;
}

async deleteFlexCluster(options: FetchOptions<operations["deleteFlexCluster"]>) {
const { error, response } = await this.client.DELETE(
"/api/atlas/v2/groups/{groupId}/flexClusters/{name}",
options
);
if (error) {
throw ApiClientError.fromError(response, error);
}
}

async getFlexCluster(options: FetchOptions<operations["getFlexCluster"]>) {
const { data, error, response } = await this.client.GET(
"/api/atlas/v2/groups/{groupId}/flexClusters/{name}",
options
);
if (error) {
throw ApiClientError.fromError(response, error);
}
return data;
}

async listOrganizations(options?: FetchOptions<operations["listOrganizations"]>) {
const { data, error, response } = await this.client.GET("/api/atlas/v2/orgs", options);
if (error) {
Expand Down
95 changes: 95 additions & 0 deletions src/common/atlas/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { ClusterDescription20240805, FlexClusterDescription20241113 } from "./openapi.js";
import { ApiClient } from "./apiClient.js";
import logger, { LogId } from "../../logger.js";

export interface Cluster {
name?: string;
instanceType: "FREE" | "DEDICATED" | "FLEX";
instanceSize?: string;
state?: "IDLE" | "CREATING" | "UPDATING" | "DELETING" | "REPAIRING";
mongoDBVersion?: string;
connectionString?: string;
}

export function formatFlexCluster(cluster: FlexClusterDescription20241113): Cluster {
return {
name: cluster.name,
instanceType: "FLEX",
instanceSize: undefined,
state: cluster.stateName,
mongoDBVersion: cluster.mongoDBVersion,
connectionString: cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard,
};
}

export function formatCluster(cluster: ClusterDescription20240805): Cluster {
const regionConfigs = (cluster.replicationSpecs || [])
.map(
(replicationSpec) =>
(replicationSpec.regionConfigs || []) as {
providerName: string;
electableSpecs?: {
instanceSize: string;
};
readOnlySpecs?: {
instanceSize: string;
};
analyticsSpecs?: {
instanceSize: string;
};
}[]
)
.flat()
.map((regionConfig) => {
return {
providerName: regionConfig.providerName,
instanceSize:
regionConfig.electableSpecs?.instanceSize ||
regionConfig.readOnlySpecs?.instanceSize ||
regionConfig.analyticsSpecs?.instanceSize,
};
});

const instanceSize = (regionConfigs.length <= 0 ? undefined : regionConfigs[0].instanceSize) || "UNKNOWN";

const clusterInstanceType = instanceSize == "M0" ? "FREE" : "DEDICATED";

return {
name: cluster.name,
instanceType: clusterInstanceType,
instanceSize: clusterInstanceType == "DEDICATED" ? instanceSize : undefined,
state: cluster.stateName,
mongoDBVersion: cluster.mongoDBVersion,
connectionString: cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard,
};
}

export async function inspectCluster(apiClient: ApiClient, projectId: string, clusterName: string): Promise<Cluster> {
try {
const cluster = await apiClient.getCluster({
params: {
path: {
groupId: projectId,
clusterName,
},
},
});
return formatCluster(cluster);
} catch (error) {
try {
const cluster = await apiClient.getFlexCluster({
params: {
path: {
groupId: projectId,
name: clusterName,
},
},
});
return formatFlexCluster(cluster);
} catch (flexError) {
const err = flexError instanceof Error ? flexError : new Error(String(flexError));
logger.error(LogId.atlasInspectFailure, "inspect-cluster", `error inspecting cluster: ${err.message}`);
throw error;
}
}
}
Loading
Loading