Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
92 changes: 92 additions & 0 deletions src/common/atlas/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { ClusterDescription20240805, FlexClusterDescription20241113 } from "./openapi.js";
import { ApiClient } from "./apiClient.js";

export interface Cluster {
name?: string;
instanceType: "FREE" | "DEDICATED" | "FLEX";
instanceSize?: string;
state?: "IDLE" | "CREATING" | "UPDATING" | "DELETING" | "REPAIRING" | "UNKNOWN";
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 {
throw error;
}
}
}
Loading
Loading