Skip to content

Commit 4cf0999

Browse files
Refactoring, multiple timeouts, code hardening and cleanup (#19)
Co-authored-by: jeffrey-elliott <[email protected]> Co-authored-by: Steve Hewitt <[email protected]>
1 parent 0174af8 commit 4cf0999

File tree

72 files changed

+1919
-1039
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1919
-1039
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ jobs:
3535
- name: Pack
3636
run: dotnet pack ./src/DataStax.AstraDB.DataApi/ --configuration Release --no-build --output nupkgs -p:Version=${{ env.PACKAGE_VERSION }}
3737

38-
# - name: Push to NuGet
39-
# run: dotnet nuget push nupkgs/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
40-
# env:
41-
# NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
42-
43-
- name: Dry-run NuGet push
44-
run: |
45-
echo "Would run: dotnet nuget push nupkgs/*.nupkg --api-key [REDACTED] --source https://api.nuget.org/v3/index.json"
38+
- name: Push to NuGet
39+
run: dotnet nuget push nupkgs/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
40+
env:
41+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
4642

4743
- name: Upload NuGet package as artifact
4844
uses: actions/upload-artifact@v4

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ appsettings.json
1111
_site
1212
api
1313
api-docs
14+
test/DataStax.AstraDB.DataAPI.IntegrationTests/TestResults
15+
test/DataStax.AstraDB.DataAPI.IntegrationTests/coveragereport

src/DataStax.AstraDB.DataApi/Admin/AstraDatabasesAdmin.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Collections.Generic;
2121
using System.Linq;
2222
using System.Net.Http;
23-
using System.Threading;
2423
using System.Threading.Tasks;
2524

2625
namespace 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

Comments
 (0)