|
| 1 | +import { ClusterDescription20240805, FlexClusterDescription20241113 } from "./openapi.js"; |
| 2 | +import { ApiClient } from "./apiClient.js"; |
| 3 | +import logger, { LogId } from "../../logger.js"; |
| 4 | + |
| 5 | +export interface Cluster { |
| 6 | + name?: string; |
| 7 | + instanceType: "FREE" | "DEDICATED" | "FLEX"; |
| 8 | + instanceSize?: string; |
| 9 | + state?: "IDLE" | "CREATING" | "UPDATING" | "DELETING" | "REPAIRING"; |
| 10 | + mongoDBVersion?: string; |
| 11 | + connectionString?: string; |
| 12 | +} |
| 13 | + |
| 14 | +export function formatFlexCluster(cluster: FlexClusterDescription20241113): Cluster { |
| 15 | + return { |
| 16 | + name: cluster.name, |
| 17 | + instanceType: "FLEX", |
| 18 | + instanceSize: undefined, |
| 19 | + state: cluster.stateName, |
| 20 | + mongoDBVersion: cluster.mongoDBVersion, |
| 21 | + connectionString: cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +export function formatCluster(cluster: ClusterDescription20240805): Cluster { |
| 26 | + const regionConfigs = (cluster.replicationSpecs || []) |
| 27 | + .map( |
| 28 | + (replicationSpec) => |
| 29 | + (replicationSpec.regionConfigs || []) as { |
| 30 | + providerName: string; |
| 31 | + electableSpecs?: { |
| 32 | + instanceSize: string; |
| 33 | + }; |
| 34 | + readOnlySpecs?: { |
| 35 | + instanceSize: string; |
| 36 | + }; |
| 37 | + analyticsSpecs?: { |
| 38 | + instanceSize: string; |
| 39 | + }; |
| 40 | + }[] |
| 41 | + ) |
| 42 | + .flat() |
| 43 | + .map((regionConfig) => { |
| 44 | + return { |
| 45 | + providerName: regionConfig.providerName, |
| 46 | + instanceSize: |
| 47 | + regionConfig.electableSpecs?.instanceSize || |
| 48 | + regionConfig.readOnlySpecs?.instanceSize || |
| 49 | + regionConfig.analyticsSpecs?.instanceSize, |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + const instanceSize = (regionConfigs.length <= 0 ? undefined : regionConfigs[0].instanceSize) || "UNKNOWN"; |
| 54 | + |
| 55 | + const clusterInstanceType = instanceSize == "M0" ? "FREE" : "DEDICATED"; |
| 56 | + |
| 57 | + return { |
| 58 | + name: cluster.name, |
| 59 | + instanceType: clusterInstanceType, |
| 60 | + instanceSize: clusterInstanceType == "DEDICATED" ? instanceSize : undefined, |
| 61 | + state: cluster.stateName, |
| 62 | + mongoDBVersion: cluster.mongoDBVersion, |
| 63 | + connectionString: cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +export async function inspectCluster(apiClient: ApiClient, projectId: string, clusterName: string): Promise<Cluster> { |
| 68 | + try { |
| 69 | + const cluster = await apiClient.getCluster({ |
| 70 | + params: { |
| 71 | + path: { |
| 72 | + groupId: projectId, |
| 73 | + clusterName, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }); |
| 77 | + return formatCluster(cluster); |
| 78 | + } catch (error) { |
| 79 | + try { |
| 80 | + const cluster = await apiClient.getFlexCluster({ |
| 81 | + params: { |
| 82 | + path: { |
| 83 | + groupId: projectId, |
| 84 | + name: clusterName, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }); |
| 88 | + return formatFlexCluster(cluster); |
| 89 | + } catch (flexError) { |
| 90 | + const err = flexError instanceof Error ? flexError : new Error(String(flexError)); |
| 91 | + logger.error(LogId.atlasInspectFailure, "inspect-cluster", `error inspecting cluster: ${err.message}`); |
| 92 | + throw error; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments