14
14
// noinspection ExceptionCaughtLocallyJS
15
15
16
16
import {
17
- AdminBlockingOptions ,
18
17
AdminSpawnOptions ,
19
- CreateDatabaseOptions ,
20
- DatabaseConfig ,
21
- FullDatabaseInfo ,
22
- ListDatabasesOptions ,
18
+ AstraAdminBlockingOptions ,
19
+ AstraDatabaseConfig ,
20
+ CreateAstraDatabaseOptions ,
21
+ ListAstraDatabasesOptions ,
23
22
} from '@/src/administration/types' ;
24
23
import { AstraDbAdmin } from '@/src/administration/astra-db-admin' ;
25
24
import { Db } from '@/src/db/db' ;
25
+ import { buildAstraDatabaseAdminInfo } from '@/src/administration/utils' ;
26
26
import { DEFAULT_DEVOPS_API_ENDPOINTS , DEFAULT_KEYSPACE , HttpMethods } from '@/src/lib/api/constants' ;
27
27
import { DevOpsAPIHttpClient } from '@/src/lib/api/clients/devops-api-http-client' ;
28
28
import { TokenProvider , WithTimeout } from '@/src/lib' ;
29
+ import { AstraDbAdminInfo } from '@/src/administration/types/admin/database-info' ;
29
30
import { parseAdminSpawnOpts } from '@/src/client/parsers/spawn-admin' ;
30
31
import { InternalRootClientOpts } from '@/src/client/types/internal' ;
31
32
import { buildAstraEndpoint } from '@/src/lib/utils' ;
32
33
import { Logger } from '@/src/lib/logging/logger' ;
33
34
import { DbSpawnOptions } from '@/src/client' ;
34
35
import { $CustomInspect } from '@/src/lib/constants' ;
36
+ import { SomeDoc } from '@/src/documents' ;
35
37
36
38
/**
37
39
* An administrative class for managing Astra databases, including creating, listing, and deleting databases.
@@ -62,6 +64,7 @@ import { $CustomInspect } from '@/src/lib/constants';
62
64
export class AstraAdmin {
63
65
readonly #defaultOpts: InternalRootClientOpts ;
64
66
readonly #httpClient: DevOpsAPIHttpClient ;
67
+ readonly #environment: 'dev' | 'test' | 'prod' ;
65
68
66
69
/**
67
70
* Use {@link DataAPIClient.admin} to obtain an instance of this class.
@@ -80,16 +83,19 @@ export class AstraAdmin {
80
83
adminToken : token ,
81
84
logging : Logger . advanceConfig ( rootOpts . adminOptions . logging , adminOpts ?. logging ) ,
82
85
additionalHeaders : { ...rootOpts . adminOptions . additionalHeaders , ...adminOpts ?. additionalHeaders } ,
86
+ astraEnv : adminOpts ?. astraEnv ?? rootOpts . adminOptions . astraEnv ,
83
87
} ,
84
88
dbOptions : {
85
89
...rootOpts . dbOptions ,
86
90
token : TokenProvider . parseToken ( [ rootOpts . dbOptions . token , token ] , 'admin token' ) ,
87
91
} ,
88
92
} ;
89
93
94
+ this . #environment = this . #defaultOpts. adminOptions . astraEnv ?? 'prod' ;
95
+
90
96
this . #httpClient = new DevOpsAPIHttpClient ( {
91
- baseUrl : this . #defaultOpts. adminOptions . endpointUrl || DEFAULT_DEVOPS_API_ENDPOINTS . prod ,
92
97
logging : this . #defaultOpts. adminOptions . logging ,
98
+ baseUrl : this . #defaultOpts. adminOptions . endpointUrl ?? DEFAULT_DEVOPS_API_ENDPOINTS [ this . #environment] ,
93
99
emitter : rootOpts . emitter ,
94
100
fetchCtx : rootOpts . fetchCtx ,
95
101
userAgent : rootOpts . userAgent ,
@@ -262,8 +268,7 @@ export class AstraAdmin {
262
268
263
269
public dbAdmin ( endpointOrId : string , regionOrOptions ?: string | DbSpawnOptions , maybeOptions ?: DbSpawnOptions ) : AstraDbAdmin {
264
270
/* @ts -expect-error - calls internal representation of method */
265
- return this . db ( endpointOrId , regionOrOptions , maybeOptions )
266
- . admin ( this . #defaultOpts. adminOptions ) ;
271
+ return this . db ( endpointOrId , regionOrOptions , maybeOptions ) . admin ( this . #defaultOpts. adminOptions ) ;
267
272
}
268
273
269
274
/**
@@ -278,13 +283,13 @@ export class AstraAdmin {
278
283
*
279
284
* @returns A promise that resolves to the complete database information.
280
285
*/
281
- public async dbInfo ( id : string , options ?: WithTimeout ) : Promise < FullDatabaseInfo > {
286
+ public async dbInfo ( id : string , options ?: WithTimeout ) : Promise < AstraDbAdminInfo > {
282
287
const resp = await this . #httpClient. request ( {
283
288
method : HttpMethods . Get ,
284
289
path : `/databases/${ id } ` ,
285
290
} , options ) ;
286
291
287
- return resp . data as FullDatabaseInfo ;
292
+ return buildAstraDatabaseAdminInfo ( resp . data ! , this . #environment ) ;
288
293
}
289
294
290
295
/**
@@ -297,7 +302,7 @@ export class AstraAdmin {
297
302
* You can also filter by the database status using the `include` option, and by the database provider using the
298
303
* `provider` option.
299
304
*
300
- * See {@link ListDatabasesOptions } for complete information about the options available for this operation.
305
+ * See {@link ListAstraDatabasesOptions } for complete information about the options available for this operation.
301
306
*
302
307
* @example
303
308
* ```typescript
@@ -313,23 +318,23 @@ export class AstraAdmin {
313
318
* @param options - The options to filter the databases by.
314
319
* @returns A list of the complete information for all the databases matching the given filter.
315
320
*/
316
- public async listDatabases ( options ?: ListDatabasesOptions ) : Promise < FullDatabaseInfo [ ] > {
321
+ public async listDatabases ( options ?: ListAstraDatabasesOptions ) : Promise < AstraDbAdminInfo [ ] > {
317
322
const params = { } as Record < string , string > ;
318
323
319
324
if ( typeof options ?. include === 'string' ) {
320
- ( params [ 'include' ] = options . include ) ;
325
+ params [ 'include' ] = options . include ;
321
326
}
322
327
323
328
if ( typeof options ?. provider === 'string' ) {
324
- ( params [ 'provider' ] = options . provider ) ;
329
+ params [ 'provider' ] = options . provider ;
325
330
}
326
331
327
332
if ( typeof options ?. limit === 'number' ) {
328
- ( params [ 'limit' ] = String ( options . skip ) ) ;
333
+ params [ 'limit' ] = String ( options . skip ) ;
329
334
}
330
335
331
336
if ( typeof options ?. skip === 'number' ) {
332
- ( params [ 'starting_after' ] = String ( options . skip ) ) ;
337
+ params [ 'starting_after' ] = String ( options . skip ) ;
333
338
}
334
339
335
340
const resp = await this . #httpClient. request ( {
@@ -338,13 +343,13 @@ export class AstraAdmin {
338
343
params : params ,
339
344
} , options ) ;
340
345
341
- return resp . data as FullDatabaseInfo [ ] ;
346
+ return resp . data ! . map ( ( d : SomeDoc ) => buildAstraDatabaseAdminInfo ( d , this . #environment ) ) ;
342
347
}
343
348
344
349
/**
345
350
* Creates a new database with the given configuration.
346
351
*
347
- * **NB. this is a long-running operation. See {@link AdminBlockingOptions } about such blocking operations.** The
352
+ * **NB. this is a long-running operation. See {@link AstraAdminBlockingOptions } about such blocking operations.** The
348
353
* default polling interval is 10 seconds. Expect it to take roughly 2 min to complete.
349
354
*
350
355
* Note that **the `name` field is non-unique** and thus creating a database, even with the same options, is **not
@@ -354,7 +359,7 @@ export class AstraAdmin {
354
359
* will override any default options set when creating the {@link DataAPIClient} through a deep merge (i.e. unset
355
360
* properties in the options object will just default to the default options).
356
361
*
357
- * See {@link CreateDatabaseOptions } for complete information about the options available for this operation.
362
+ * See {@link CreateAstraDatabaseOptions } for complete information about the options available for this operation.
358
363
*
359
364
* @example
360
365
* ```typescript
@@ -393,7 +398,7 @@ export class AstraAdmin {
393
398
*
394
399
* @returns The AstraDbAdmin instance for the newly created database.
395
400
*/
396
- public async createDatabase ( config : DatabaseConfig , options ?: CreateDatabaseOptions ) : Promise < AstraDbAdmin > {
401
+ public async createDatabase ( config : AstraDatabaseConfig , options ?: CreateAstraDatabaseOptions ) : Promise < AstraDbAdmin > {
397
402
const definition = {
398
403
capacityUnits : 1 ,
399
404
tier : 'serverless' ,
@@ -422,7 +427,7 @@ export class AstraAdmin {
422
427
/**
423
428
* Terminates a database by ID or by a given {@link Db} instance.
424
429
*
425
- * **NB. this is a long-running operation. See {@link AdminBlockingOptions } about such blocking operations.** The
430
+ * **NB. this is a long-running operation. See {@link AstraAdminBlockingOptions } about such blocking operations.** The
426
431
* default polling interval is 10 seconds. Expect it to take roughly 6-7 min to complete.
427
432
*
428
433
* The database info will still be accessible by ID, or by using the {@link AstraAdmin.listDatabases} method with the filter
@@ -444,7 +449,7 @@ export class AstraAdmin {
444
449
*
445
450
* @remarks Use with caution. Wear a harness. Don't say I didn't warn you.
446
451
*/
447
- public async dropDatabase ( db : Db | string , options ?: AdminBlockingOptions ) : Promise < void > {
452
+ public async dropDatabase ( db : Db | string , options ?: AstraAdminBlockingOptions ) : Promise < void > {
448
453
const id = typeof db === 'string' ? db : db . id ;
449
454
450
455
await this . #httpClient. requestLongRunning ( {
0 commit comments