2020using System . Collections . Generic ;
2121using System . Linq ;
2222using System . Net . Http ;
23- using System . Threading ;
2423using System . Threading . Tasks ;
2524
2625namespace DataStax . AstraDB . DataApi . Admin ;
@@ -58,7 +57,7 @@ internal AstraDatabasesAdmin(DataApiClient client, CommandOptions adminOptions)
5857 /// </example>
5958 public List < string > ListDatabaseNames ( )
6059 {
61- return ListDatabases ( ) . Select ( db => db . Info . Name ) . ToList ( ) ;
60+ return ListDatabases ( ) . Select ( db => db . Name ) . ToList ( ) ;
6261 }
6362
6463 /// <summary>
@@ -87,7 +86,7 @@ public Task<List<string>> ListDatabaseNamesAsync()
8786 /// </example>
8887 public List < string > ListDatabaseNames ( CommandOptions options )
8988 {
90- return ListDatabases ( options ) . Select ( db => db . Info . Name ) . ToList ( ) ;
89+ return ListDatabases ( options ) . Select ( db => db . Name ) . ToList ( ) ;
9190 }
9291
9392 /// <summary>
@@ -103,7 +102,7 @@ public List<string> ListDatabaseNames(CommandOptions options)
103102 public async Task < List < string > > ListDatabaseNamesAsync ( CommandOptions options )
104103 {
105104 var databases = await ListDatabasesAsync ( options ) . ConfigureAwait ( false ) ;
106- return databases . Select ( db => db . Info . Name ) . ToList ( ) ;
105+ return databases . Select ( db => db . Name ) . ToList ( ) ;
107106 }
108107
109108 /// <summary>
@@ -164,14 +163,15 @@ public Task<List<DatabaseInfo>> ListDatabasesAsync(CommandOptions options)
164163 return ListDatabasesAsync ( options , false ) ;
165164 }
166165
167- internal Task < List < DatabaseInfo > > ListDatabasesAsync ( CommandOptions options , bool runSynchronously )
166+ internal async Task < List < DatabaseInfo > > ListDatabasesAsync ( CommandOptions options , bool runSynchronously )
168167 {
169168 var command = CreateCommand ( )
170169 . AddUrlPath ( "databases" )
170+ . WithTimeoutManager ( new DatabaseAdminTimeoutManager ( ) )
171171 . AddCommandOptions ( options ) ;
172172
173- var response = command . RunAsyncRaw < List < DatabaseInfo > > ( HttpMethod . Get , runSynchronously ) ;
174- return response ;
173+ var rawResults = await command . RunAsyncRaw < List < RawDatabaseInfo > > ( HttpMethod . Get , runSynchronously ) . ConfigureAwait ( false ) ;
174+ return rawResults ? . Select ( db => new DatabaseInfo ( db ) ) . ToList ( ) ?? new List < DatabaseInfo > ( ) ;
175175 }
176176
177177 /// <summary>
@@ -351,11 +351,11 @@ internal async Task<IDatabaseAdmin> CreateDatabaseAsync(DatabaseCreationOptions
351351
352352 List < DatabaseInfo > dbList = await ListDatabasesAsync ( commandOptions , runSynchronously ) . ConfigureAwait ( false ) ;
353353
354- DatabaseInfo existingDb = dbList . FirstOrDefault ( item => databaseName . Equals ( item . Info . Name ) ) ;
354+ DatabaseInfo existingDb = dbList . FirstOrDefault ( item => databaseName . Equals ( item . Name ) ) ;
355355
356356 if ( existingDb != null )
357357 {
358- if ( existingDb . Status == " ACTIVE" )
358+ if ( existingDb . Status == AstraDatabaseStatus . ACTIVE )
359359 {
360360 Console . WriteLine ( $ "Database { databaseName } already exists and is ACTIVE.") ;
361361 return GetDatabaseAdmin ( existingDb ) ;
@@ -367,6 +367,7 @@ internal async Task<IDatabaseAdmin> CreateDatabaseAsync(DatabaseCreationOptions
367367 Command command = CreateCommand ( )
368368 . AddUrlPath ( "databases" )
369369 . WithPayload ( creationOptions )
370+ . WithTimeoutManager ( new DatabaseAdminTimeoutManager ( ) )
370371 . AddCommandOptions ( commandOptions ) ;
371372
372373 Guid newDbId = Guid . Empty ;
@@ -396,7 +397,7 @@ internal async Task<IDatabaseAdmin> CreateDatabaseAsync(DatabaseCreationOptions
396397 }
397398 }
398399
399- return GetDatabaseAdmin ( newDbId ) ;
400+ return await GetDatabaseAdminAsync ( newDbId ) ;
400401 }
401402
402403 private void WaitForDatabase ( string databaseName )
@@ -414,8 +415,8 @@ internal async Task WaitForDatabaseAsync(string databaseName)
414415
415416 while ( secondsWaited < MAX_WAIT_IN_SECONDS )
416417 {
417- string status = await GetDatabaseStatusAsync ( databaseName ) . ConfigureAwait ( false ) ;
418- if ( status == " ACTIVE" )
418+ var status = await GetDatabaseStatusAsync ( databaseName ) . ConfigureAwait ( false ) ;
419+ if ( status == AstraDatabaseStatus . ACTIVE )
419420 {
420421 return ;
421422 }
@@ -426,11 +427,11 @@ internal async Task WaitForDatabaseAsync(string databaseName)
426427 throw new Exception ( $ "Database { databaseName } did not become ready within { MAX_WAIT_IN_SECONDS } seconds.") ;
427428 }
428429
429- internal async Task < string > GetDatabaseStatusAsync ( string databaseName )
430+ internal async Task < AstraDatabaseStatus > GetDatabaseStatusAsync ( string databaseName )
430431 {
431432 Guard . NotNullOrEmpty ( databaseName , nameof ( databaseName ) ) ;
432433 var dbList = await ListDatabasesAsync ( ) ;
433- var db = dbList . FirstOrDefault ( item => databaseName . Equals ( item . Info . Name ) ) ;
434+ var db = dbList . FirstOrDefault ( item => databaseName . Equals ( item . Name ) ) ;
434435
435436 if ( db == null )
436437 {
@@ -569,7 +570,7 @@ internal async Task<bool> DropDatabaseAsync(string databaseName, CommandOptions
569570 Guard . NotNullOrEmpty ( databaseName , nameof ( databaseName ) ) ;
570571 var dbList = await ListDatabasesAsync ( options , runSynchronously ) . ConfigureAwait ( false ) ;
571572
572- var dbInfo = dbList . FirstOrDefault ( item => item . Info . Name . Equals ( databaseName ) ) ;
573+ var dbInfo = dbList . FirstOrDefault ( item => item . Name . Equals ( databaseName ) ) ;
573574 if ( dbInfo == null )
574575 {
575576 return false ;
@@ -593,6 +594,7 @@ internal async Task<bool> DropDatabaseAsync(Guid dbGuid, CommandOptions options,
593594 . AddUrlPath ( "databases" )
594595 . AddUrlPath ( dbGuid . ToString ( ) )
595596 . AddUrlPath ( "terminate" )
597+ . WithTimeoutManager ( new DatabaseAdminTimeoutManager ( ) )
596598 . AddCommandOptions ( options ) ;
597599
598600 await command . RunAsyncRaw < Command . EmptyResult > ( runSynchronously ) . ConfigureAwait ( false ) ;
@@ -602,21 +604,6 @@ internal async Task<bool> DropDatabaseAsync(Guid dbGuid, CommandOptions options,
602604 return false ;
603605 }
604606
605- private DatabaseAdminAstra GetDatabaseAdmin ( DatabaseInfo dbInfo )
606- {
607- var apiEndpoint = $ "https://{ dbInfo . Id } -{ dbInfo . Info . Region } .apps.astra.datastax.com";
608- var database = _client . GetDatabase ( apiEndpoint ) ;
609- return new DatabaseAdminAstra ( database , _client , null ) ;
610- }
611-
612- private DatabaseAdminAstra GetDatabaseAdmin ( Guid dbGuid )
613- {
614- var dbInfo = GetDatabaseInfo ( dbGuid ) ;
615- var apiEndpoint = $ "https://{ dbGuid } -{ dbInfo . Info . Region } .apps.astra.datastax.com";
616- var database = _client . GetDatabase ( apiEndpoint ) ;
617- return new DatabaseAdminAstra ( database , _client , null ) ;
618- }
619-
620607 /// <summary>
621608 /// Retrieves database information for the specified GUID.
622609 /// </summary>
@@ -684,18 +671,35 @@ internal async Task<DatabaseInfo> GetDatabaseInfoAsync(Guid dbGuid, bool runSync
684671 return await GetDatabaseInfoAsync ( dbGuid , null , runSynchronously ) . ConfigureAwait ( false ) ;
685672 }
686673
687- internal Task < DatabaseInfo > GetDatabaseInfoAsync ( Guid dbGuid , CommandOptions options , bool runSynchronously )
674+ internal async Task < DatabaseInfo > GetDatabaseInfoAsync ( Guid dbGuid , CommandOptions options , bool runSynchronously )
688675 {
689676 Guard . NotEmpty ( dbGuid , nameof ( dbGuid ) ) ;
690677 var command = CreateCommand ( )
691678 . AddUrlPath ( "databases" )
692679 . AddUrlPath ( dbGuid . ToString ( ) )
680+ . WithTimeoutManager ( new DatabaseAdminTimeoutManager ( ) )
693681 . AddCommandOptions ( options ) ;
694682
695- var response = command . RunAsyncRaw < DatabaseInfo > ( HttpMethod . Get , runSynchronously ) ;
683+ var rawInfo = await command . RunAsyncRaw < RawDatabaseInfo > ( HttpMethod . Get , runSynchronously ) ;
684+ var response = new DatabaseInfo ( rawInfo ) ;
696685 return response ;
697686 }
698687
688+ private DatabaseAdminAstra GetDatabaseAdmin ( DatabaseInfo dbInfo )
689+ {
690+ var apiEndpoint = $ "https://{ dbInfo . Id } -{ dbInfo . Region } .apps.astra.datastax.com";
691+ var database = _client . GetDatabase ( apiEndpoint ) ;
692+ return new DatabaseAdminAstra ( database , _client , null ) ;
693+ }
694+
695+ private async Task < DatabaseAdminAstra > GetDatabaseAdminAsync ( Guid dbGuid )
696+ {
697+ var dbInfo = await GetDatabaseInfoAsync ( dbGuid ) . ConfigureAwait ( false ) ;
698+ var apiEndpoint = $ "https://{ dbGuid } -{ dbInfo . Region } .apps.astra.datastax.com";
699+ var database = _client . GetDatabase ( apiEndpoint ) ;
700+ return new DatabaseAdminAstra ( database , _client , null ) ;
701+ }
702+
699703 private Command CreateCommand ( )
700704 {
701705 return new Command ( _client , OptionsTree , new AdminCommandUrlBuilder ( ) ) ;
0 commit comments