Skip to content

Commit d102ea2

Browse files
Merge pull request #5 from stephenatsembit/database-admin
Database admin
2 parents 0ce4e10 + d187fc1 commit d102ea2

File tree

11 files changed

+250
-148
lines changed

11 files changed

+250
-148
lines changed

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

Lines changed: 147 additions & 78 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
using System.Text.Json.Serialization;
18+
1719
namespace DataStax.AstraDB.DataApi.Admin;
1820

21+
[JsonConverter(typeof(JsonStringEnumConverter<CloudProviderType>))]
1922
public enum CloudProviderType
2023
{
21-
All,
24+
[JsonStringEnumMemberName("aws")]
25+
AWS,
26+
[JsonStringEnumMemberName("gcp")]
2227
GCP,
23-
GCP_MARKETPLACE,
24-
Azure,
25-
AWS
28+
[JsonStringEnumMemberName("azure")]
29+
Azure
2630
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using DataStax.AstraDB.DataApi.Core;
2+
using System.Text.Json.Serialization;
3+
4+
namespace DataStax.AstraDB.DataApi.Admin;
5+
6+
public class DatabaseCreationOptions
7+
{
8+
[JsonPropertyName("name")]
9+
public string Name { get; set; }
10+
11+
[JsonPropertyName("cloudProvider")]
12+
public CloudProviderType CloudProvider { get; set; } = CloudProviderType.GCP;
13+
14+
[JsonPropertyName("region")]
15+
public string Region { get; set; } = "us-east1";
16+
17+
[JsonPropertyName("keyspace")]
18+
public string Keyspace { get; set; } = Database.DefaultKeyspace;
19+
20+
[JsonPropertyName("capacityUnits")]
21+
public int CapacityUnits { get; set; } = 1;
22+
23+
[JsonPropertyName("tier")]
24+
public string Tier { get; set; } = "serverless";
25+
}

src/DataStax.AstraDB.DataApi/Core/CommandOptions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ namespace DataStax.AstraDB.DataApi.Core;
2222

2323
public class CommandOptions
2424
{
25-
public string Token { get; internal set; }
25+
2626
internal DBEnvironment? Environment { get; set; }
2727
internal RunMode? RunMode { get; set; }
28+
internal string Keyspace { get; set; }
29+
30+
public string Token { get; internal set; }
2831
public DataApiDestination? Destination { get; set; }
2932
public HttpClientOptions HttpClientOptions { get; set; }
3033
public TimeoutOptions TimeoutOptions { get; set; }
@@ -44,7 +47,8 @@ public static CommandOptions Merge(params CommandOptions[] arr)
4447
HttpClientOptions = list.Select(o => o.HttpClientOptions).Merge(),
4548
TimeoutOptions = list.Select(o => o.TimeoutOptions).Merge(),
4649
ApiVersion = list.Select(o => o.ApiVersion).Merge(),
47-
CancellationToken = list.Select(o => o.CancellationToken).Merge()
50+
CancellationToken = list.Select(o => o.CancellationToken).Merge(),
51+
Keyspace = list.Select(o => o.Keyspace).Merge()
4852
};
4953
return options;
5054
}
@@ -57,7 +61,8 @@ public static CommandOptions Defaults()
5761
RunMode = Core.RunMode.Normal,
5862
Destination = DataApiDestination.ASTRA,
5963
ApiVersion = Core.ApiVersion.V1,
60-
HttpClientOptions = new HttpClientOptions()
64+
HttpClientOptions = new HttpClientOptions(),
65+
Keyspace = Database.DefaultKeyspace,
6166
};
6267
}
6368
}

src/DataStax.AstraDB.DataApi/Core/CommandUrlBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal override string BuildUrl()
4444
//TODO: Is this how we want to get the keyspace? (I think not...)
4545
//TODO: factor in environment
4646
var url = $"{_database.ApiEndpoint}/api/json/{options.ApiVersion.Value.ToUrlString()}" +
47-
$"/{_database.DatabaseOptions.CurrentKeyspace}/{_urlPostfix}";
47+
$"/{options.Keyspace}/{_urlPostfix}";
4848
return url;
4949
}
5050
}

src/DataStax.AstraDB.DataApi/Core/Commands/Command.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.Extensions.Logging;
1818
using System;
1919
using System.Collections.Generic;
20-
using System.Diagnostics;
2120
using System.Linq;
2221
using System.Net.Http;
2322
using System.Net.Http.Headers;
@@ -42,8 +41,8 @@ public class Command
4241

4342
public readonly struct EmptyResult { }
4443

45-
private Action<HttpResponseMessage> _responseHandler;
46-
internal Action<HttpResponseMessage> ResponseHandler { set { _responseHandler = value; } }
44+
private Func<HttpResponseMessage, Task> _responseHandler;
45+
internal Func<HttpResponseMessage, Task> ResponseHandler { set { _responseHandler = value; } }
4746

4847
internal Command(DataApiClient client, CommandOptions[] options, CommandUrlBuilder urlBuilder) : this(null, client, options, urlBuilder)
4948
{
@@ -129,6 +128,7 @@ private async Task<T> RunCommandAsync<T>(HttpMethod method, bool runSynchronousl
129128
{
130129
var commandOptions = CommandOptions.Merge(_commandOptionsTree.ToArray());
131130
var content = new StringContent(JsonSerializer.Serialize(BuildContent()), Encoding.UTF8, "application/json");
131+
132132
var url = _urlBuilder.BuildUrl();
133133
if (_urlPaths.Any())
134134
{
@@ -209,21 +209,27 @@ private async Task<T> RunCommandAsync<T>(HttpMethod method, bool runSynchronousl
209209
responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
210210
}
211211

212-
if (_responseHandler != null)
213-
{
214-
_responseHandler(response);
215-
}
212+
if (_responseHandler != null)
213+
{
214+
if (runSynchronously)
215+
{
216+
_responseHandler(response).ResultSync();
217+
}
218+
else
219+
{
220+
await _responseHandler(response);
221+
}
222+
}
216223

217224
MaybeLogDebugMessage("Response Status Code: {StatusCode}", response.StatusCode);
218225
MaybeLogDebugMessage("Content: {Content}", responseContent);
219226

220-
MaybeLogDebugMessage("Raw Response: {Response}", response);
227+
MaybeLogDebugMessage("Raw Response: {Response}", response);
221228

222-
223-
if (string.IsNullOrEmpty(responseContent))
224-
{
225-
return default;
226-
}
229+
if (string.IsNullOrEmpty(responseContent))
230+
{
231+
return default;
232+
}
227233

228234
return JsonSerializer.Deserialize<T>(responseContent);
229235
}

src/DataStax.AstraDB.DataApi/Core/Database.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,29 @@ namespace DataStax.AstraDB.DataApi.Core;
2525

2626
public class Database
2727
{
28+
public const string DefaultKeyspace = "default_keyspace";
29+
2830
private readonly string _apiEndpoint;
29-
private readonly DatabaseOptions _databaseOptions;
3031
private readonly DataApiClient _client;
3132
private readonly string _urlPostfix = "";
32-
private readonly CommandOptions _dbCommandOptions;
33+
private readonly DatabaseOptions _dbCommandOptions;
3334

3435
public string ApiEndpoint => _apiEndpoint;
35-
internal DatabaseOptions DatabaseOptions => _databaseOptions;
3636
internal DataApiClient Client => _client;
3737

3838
internal CommandOptions[] OptionsTree
3939
{
4040
get
4141
{
42-
return new CommandOptions[] { _client.ClientOptions, _dbCommandOptions };
42+
return _dbCommandOptions == null ? new CommandOptions[] { _client.ClientOptions } : new CommandOptions[] { _client.ClientOptions, _dbCommandOptions };
4343
}
4444
}
4545

46-
//TODO: is DatabaseOptions necessary? Perhaps override CommandOptions.
47-
internal Database(string apiEndpoint, DataApiClient client, CommandOptions dbCommandOptions, DatabaseOptions databaseOptions)
46+
internal Database(string apiEndpoint, DataApiClient client, DatabaseOptions dbCommandOptions)
4847
{
4948
Guard.NotNullOrEmpty(apiEndpoint, nameof(apiEndpoint));
50-
Guard.NotNull(databaseOptions, nameof(databaseOptions));
5149
Guard.NotNull(client, nameof(client));
52-
Guard.NotNull(dbCommandOptions, nameof(dbCommandOptions));
5350
_apiEndpoint = apiEndpoint;
54-
_databaseOptions = databaseOptions;
5551
_client = client;
5652
_dbCommandOptions = dbCommandOptions;
5753
}

src/DataStax.AstraDB.DataApi/Core/DatabaseOptions.cs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
using DataStax.AstraDB.DataApi.Utils;
18-
1917
namespace DataStax.AstraDB.DataApi.Core;
2018

21-
public class DatabaseOptions
19+
public class DatabaseOptions : CommandOptions
2220
{
23-
//TODO: move to common area for defaults if used other than directly related to DatabaseOptions.
24-
public const string DefaultKeyspace = "default_keyspace";
25-
26-
private string _currentKeyspace;
27-
28-
public string CurrentKeyspace
21+
public new string Keyspace
2922
{
30-
get => _currentKeyspace;
31-
set
32-
{
33-
Guard.NotNullOrEmpty(value, nameof(value));
34-
_currentKeyspace = value;
35-
}
23+
get => base.Keyspace;
24+
set => base.Keyspace = value;
3625
}
26+
}
3727

38-
public DatabaseOptions()
39-
: this(DefaultKeyspace)
40-
{
41-
}
4228

43-
public DatabaseOptions(string currentKeyspace)
44-
{
45-
_currentKeyspace = currentKeyspace;
46-
}
47-
}

src/DataStax.AstraDB.DataApi/DataAPIClient.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,28 @@ public AstraDatabasesAdmin GetAstraAdmin(string superAdminToken)
8585

8686
public Database GetDatabase(string apiEndpoint)
8787
{
88-
return GetDatabase(apiEndpoint, new DatabaseOptions());
88+
return GetDatabase(apiEndpoint, null as DatabaseOptions);
8989
}
9090

9191
public Database GetDatabase(string apiEndpoint, string keyspace)
9292
{
93-
var dbOptions = new DatabaseOptions(keyspace);
93+
var dbOptions = new DatabaseOptions() { Keyspace = keyspace };
9494
return GetDatabase(apiEndpoint, dbOptions);
9595
}
9696

9797
public Database GetDatabase(string apiEndpoint, DatabaseOptions dbOptions)
9898
{
99-
return new Database(apiEndpoint, this, new CommandOptions(), dbOptions);
99+
return new Database(apiEndpoint, this, dbOptions);
100100
}
101101

102102
public Database GetDatabase(Guid databaseId)
103103
{
104-
return GetDatabase(databaseId, new DatabaseOptions());
104+
return GetDatabase(databaseId, null as DatabaseOptions);
105105
}
106106

107107
public Database GetDatabase(Guid databaseId, string keyspace)
108108
{
109-
var dbOptions = new DatabaseOptions(keyspace);
109+
var dbOptions = new DatabaseOptions() { Keyspace = keyspace };
110110
return GetDatabase(databaseId, dbOptions);
111111
}
112112

@@ -122,7 +122,7 @@ public async Task<Database> GetDatabaseAsync(Guid databaseId)
122122

123123
public async Task<Database> GetDatabaseAsync(Guid databaseId, string keyspace)
124124
{
125-
var dbOptions = new DatabaseOptions(keyspace);
125+
var dbOptions = new DatabaseOptions() { Keyspace = keyspace };
126126
return await GetDatabaseAsync(databaseId, dbOptions, false).ConfigureAwait(false);
127127
}
128128

test/DataStax.AstraDB.DataApi.IntegrationTests/AdminFixture.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using DataStax.AstraDB.DataApi.Core;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.Logging;
5-
using System;
6-
using System.Dynamic;
75
using System.Text.RegularExpressions;
86

97
public class AdminFixture : IDisposable

0 commit comments

Comments
 (0)