@@ -6,6 +6,7 @@ import { generateSecurePassword } from "../../../helpers/generatePassword.js";
6
6
import logger , { LogId } from "../../../common/logger.js" ;
7
7
import { inspectCluster } from "../../../common/atlas/cluster.js" ;
8
8
import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUtils.js" ;
9
+ import { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js" ;
9
10
10
11
const EXPIRY_MS = 1000 * 60 * 60 * 12 ; // 12 hours
11
12
@@ -27,7 +28,7 @@ export class ConnectClusterTool extends AtlasToolBase {
27
28
clusterName : string
28
29
) : Promise < "connected" | "disconnected" | "connecting" | "connected-to-other-cluster" | "unknown" > {
29
30
if ( ! this . session . connectedAtlasCluster ) {
30
- if ( this . session . isConnectedToMongoDB ( ) ) {
31
+ if ( this . session . isConnectedToMongoDB ) {
31
32
return "connected-to-other-cluster" ;
32
33
}
33
34
return "disconnected" ;
@@ -40,7 +41,7 @@ export class ConnectClusterTool extends AtlasToolBase {
40
41
return "connected-to-other-cluster" ;
41
42
}
42
43
43
- if ( ! this . session . isConnectedToMongoDB ( ) ) {
44
+ if ( ! this . session . isConnectedToMongoDB ) {
44
45
return "connecting" ;
45
46
}
46
47
@@ -61,7 +62,10 @@ export class ConnectClusterTool extends AtlasToolBase {
61
62
}
62
63
}
63
64
64
- private async prepareClusterConnection ( projectId : string , clusterName : string ) : Promise < string > {
65
+ private async prepareClusterConnection (
66
+ projectId : string ,
67
+ clusterName : string
68
+ ) : Promise < [ string , AtlasClusterConnectionInfo ] > {
65
69
const cluster = await inspectCluster ( this . session . apiClient , projectId , clusterName ) ;
66
70
67
71
if ( ! cluster . connectionString ) {
@@ -109,7 +113,7 @@ export class ConnectClusterTool extends AtlasToolBase {
109
113
} ,
110
114
} ) ;
111
115
112
- this . session . connectedAtlasCluster = {
116
+ const connectedAtlasCluster = {
113
117
username,
114
118
projectId,
115
119
clusterName,
@@ -120,10 +124,15 @@ export class ConnectClusterTool extends AtlasToolBase {
120
124
cn . username = username ;
121
125
cn . password = password ;
122
126
cn . searchParams . set ( "authSource" , "admin" ) ;
123
- return cn . toString ( ) ;
127
+ return [ cn . toString ( ) , connectedAtlasCluster ] ;
124
128
}
125
129
126
- private async connectToCluster ( projectId : string , clusterName : string , connectionString : string ) : Promise < void > {
130
+ private async connectToCluster (
131
+ projectId : string ,
132
+ clusterName : string ,
133
+ connectionString : string ,
134
+ atlas : AtlasClusterConnectionInfo
135
+ ) : Promise < void > {
127
136
let lastError : Error | undefined = undefined ;
128
137
129
138
logger . debug (
@@ -145,7 +154,7 @@ export class ConnectClusterTool extends AtlasToolBase {
145
154
try {
146
155
lastError = undefined ;
147
156
148
- await this . session . connectToMongoDB ( { connectionString, ...this . config . connectOptions } ) ;
157
+ await this . session . connectToMongoDB ( { connectionString, ...this . config . connectOptions , atlas } ) ;
149
158
break ;
150
159
} catch ( err : unknown ) {
151
160
const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
@@ -187,7 +196,6 @@ export class ConnectClusterTool extends AtlasToolBase {
187
196
) ;
188
197
} ) ;
189
198
}
190
- this . session . connectedAtlasCluster = undefined ;
191
199
throw lastError ;
192
200
}
193
201
@@ -221,17 +229,19 @@ export class ConnectClusterTool extends AtlasToolBase {
221
229
case "disconnected" :
222
230
default : {
223
231
await this . session . disconnect ( ) ;
224
- const connectionString = await this . prepareClusterConnection ( projectId , clusterName ) ;
232
+ const [ connectionString , atlas ] = await this . prepareClusterConnection ( projectId , clusterName ) ;
225
233
226
234
// try to connect for about 5 minutes asynchronously
227
- void this . connectToCluster ( projectId , clusterName , connectionString ) . catch ( ( err : unknown ) => {
228
- const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
229
- logger . error (
230
- LogId . atlasConnectFailure ,
231
- "atlas-connect-cluster" ,
232
- `error connecting to cluster: ${ error . message } `
233
- ) ;
234
- } ) ;
235
+ void this . connectToCluster ( projectId , clusterName , connectionString , atlas ) . catch (
236
+ ( err : unknown ) => {
237
+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
238
+ logger . error (
239
+ LogId . atlasConnectFailure ,
240
+ "atlas-connect-cluster" ,
241
+ `error connecting to cluster: ${ error . message } `
242
+ ) ;
243
+ }
244
+ ) ;
235
245
break ;
236
246
}
237
247
}
0 commit comments