1
- package com .datastax .astradb . client ;
2
-
3
- import com . dtsx . astra . sdk . db . AstraDBOpsClient ;
4
- import com .dtsx .astra .sdk .db .DbOpsClient ;
5
- import com .dtsx .astra .sdk .db .domain . CloudProviderType ;
6
- import com .dtsx .astra .sdk .db .domain .Database ;
7
- import com .dtsx .astra .sdk .db .domain .DatabaseCreationRequest ;
8
- import com .dtsx .astra .sdk .db .domain .DatabaseStatusType ;
9
- import com .dtsx .astra .sdk .db .exception . DatabaseNotFoundException ;
10
- import com .dtsx .astra .sdk . utils . ApiLocator ;
11
- import com .dtsx .astra .sdk .utils .Assert ;
12
- import com .dtsx .astra .sdk .utils .AstraEnvironment ;
13
- import com .dtsx .astra .sdk .utils .AstraRc ;
14
- import io . stargate . sdk . data . DataApiClient ;
15
- import io .stargate .sdk .http . RetryHttpClient ;
1
+ package com .datastax .astra . db ;
2
+
3
+
4
+ import com .datastax .astra .devops .db .AstraDBDevopsClient ;
5
+ import com .datastax .astra .devops .db .DbOpsClient ;
6
+ import com .datastax .astra .devops .db .domain .CloudProviderType ;
7
+ import com .datastax .astra .devops .db .domain .Database ;
8
+ import com .datastax .astra .devops .db .domain .DatabaseCreationRequest ;
9
+ import com .datastax .astra .devops .db .domain . DatabaseStatusType ;
10
+ import com .datastax .astra .devops . db . exception . DatabaseNotFoundException ;
11
+ import com .datastax .astra .devops .utils .ApiLocator ;
12
+ import com .datastax .astra .devops .utils .Assert ;
13
+ import com .datastax .astra .devops .utils .AstraEnvironment ;
14
+ import com . datastax . astra . devops . utils . AstraRc ;
15
+ import io .stargate .sdk .data . client . DataApiClient ;
16
16
import lombok .Getter ;
17
17
import lombok .NonNull ;
18
18
import lombok .extern .slf4j .Slf4j ;
19
19
20
- import java .io .Closeable ;
21
- import java .io .IOException ;
22
20
import java .net .URI ;
23
21
import java .net .http .HttpClient ;
24
- import java .net .http .HttpClient .Version ;
25
22
import java .net .http .HttpRequest ;
26
23
import java .net .http .HttpResponse ;
27
24
import java .net .http .HttpResponse .BodyHandlers ;
36
33
import static io .stargate .sdk .utils .Utils .readEnvVariable ;
37
34
38
35
/**
39
- * Client for AstraDB at organization level (crud for databases).
36
+ * Main Client for AstraDB, it implements administration and Data Api Operations.
37
+ *
38
+ * @see AstraDBDevopsClient Client specialized in the Devops API operations
39
+ * @see DataApiClient Client specialized for Data API operation
40
40
*/
41
41
@ Slf4j
42
- public class AstraDBAdmin implements Closeable {
42
+ public class AstraDBClient {
43
43
44
- public static final String USER_AGENT = "astra-db-java" ;
45
-
46
- /** Default timeout for initiating connection. */
47
- public static final int CONNECT_TIMEOUT_SECONDS = 20 ;
48
-
49
- /** Default cloud provider if not provided by user. (free-tier) */
44
+ /** Default cloud provider. (free-tier) */
50
45
public static final CloudProviderType FREE_TIER_CLOUD = CloudProviderType .GCP ;
51
46
52
- /** Default region if not provided by user . (free-tier) */
47
+ /** Default region. (free-tier) */
53
48
public static final String FREE_TIER_CLOUD_REGION = "us-east1" ;
54
49
55
50
/** Header name used to hold the Astra Token. */
56
51
public static final String TOKEN_HEADER_PARAM = "X-Token" ;
57
52
58
- /** Default keyspace name if not provided by user . */
53
+ /** Default keyspace (same created by the ui) . */
59
54
public static final String DEFAULT_KEYSPACE = "default_keyspace" ;
60
55
61
- /** Client for the Astra Devops Api. (crud on databases) */
62
- final AstraDBOpsClient devopsDbClient ;
56
+ /** Client for Astra Devops Api. */
57
+ final AstraDBDevopsClient devopsDbClient ;
58
+
59
+ /** Options to personalized http client other client options. */
60
+ final AstraDBOptions astraDbOptions ;
63
61
64
- /** Target Astra Environment, default is PROD . */
62
+ /** Astra Environment. */
65
63
final AstraEnvironment env ;
66
64
67
- /** Astra Token used as credentials. */
65
+ /** Astra Token ( credentials) . */
68
66
@ Getter
69
67
final String token ;
70
68
71
- /** JDK11 HttpClient to interact with apis . */
69
+ /** Side Http Client (use only to resume a db) . */
72
70
final HttpClient httpClient ;
73
71
74
- /** Token value read for environment variable. */
72
+ /** Token read for environment variable ASTRA_DB_APPLICATION_TOKEN (if any) . */
75
73
static String astraConfigToken ;
76
74
77
75
/*
@@ -86,21 +84,21 @@ public class AstraDBAdmin implements Closeable {
86
84
}
87
85
88
86
/**
89
- * Default initialization, the token is retrieved from environment variable <code>ASTRA_DB_APPLICATION_TOKEN</code> or from
87
+ * Default constructor. The token is read from environment variable <code>ASTRA_DB_APPLICATION_TOKEN</code> or
90
88
* file <code>~/.astrarc</code>, section <code>default</code>, key <code>ASTRA_DB_APPLICATION_TOKEN</code>.
91
89
*/
92
- public AstraDBAdmin () {
90
+ public AstraDBClient () {
93
91
this (astraConfigToken );
94
92
}
95
93
96
94
/**
97
- * Initialization with an authentification token, defaulting to production environment.
95
+ * Constructor with an authentification token, defaulting to production environment, and default http options .
98
96
*
99
97
* @param token
100
98
* authentication token
101
99
*/
102
- public AstraDBAdmin (String token ) {
103
- this (token , AstraEnvironment .PROD );
100
+ public AstraDBClient (String token ) {
101
+ this (token , AstraEnvironment .PROD , new AstraDBOptions () );
104
102
}
105
103
106
104
/**
@@ -110,46 +108,25 @@ public AstraDBAdmin(String token) {
110
108
* authentication token
111
109
* @param env
112
110
* target Astra environment
111
+ * @param astraDbOptions
112
+ * options for AstraDb.
113
113
*/
114
- public AstraDBAdmin (String token , AstraEnvironment env ) {
114
+ public AstraDBClient (String token , AstraEnvironment env , AstraDBOptions astraDbOptions ) {
115
115
this .env = env ;
116
116
this .token = token ;
117
- this .devopsDbClient = new AstraDBOpsClient (token , env );
118
- this .httpClient = HttpClient .newBuilder ()
119
- .version (Version .HTTP_2 )
120
- .connectTimeout (Duration .ofSeconds (CONNECT_TIMEOUT_SECONDS ))
121
- .build ();
122
- String version = AstraDBAdmin .class .getPackage ().getImplementationVersion ();
123
- AstraDBAdmin .setCallerName (AstraDBAdmin .USER_AGENT , (null != version ) ? version : "dev" );
124
- }
125
-
126
- // --------------------------
127
- // --- User Agent ----
128
- // --------------------------
129
-
130
- /**
131
- * Allow user to set the client name
132
- *
133
- * @param callerName
134
- * client name
135
- * @param callerVersion
136
- * client version
137
- */
138
- public static void setCallerName (String callerName , String callerVersion ) {
139
- RetryHttpClient .getInstance ().pushUserAgent (callerName , callerVersion );
140
- }
141
-
142
- // --------------------
143
- // -- Watch ---
144
- // --------------------
145
-
146
- public void watch () {
147
- throw new UnsupportedOperationException ("As we connect to a HTTP apis without hooks, no watch is possible." );
117
+ this .devopsDbClient = new AstraDBDevopsClient (token , env );
118
+ this .astraDbOptions = astraDbOptions ;
119
+
120
+ // Local Agent for Resume
121
+ HttpClient .Builder httpClientBuilder = HttpClient .newBuilder ();
122
+ httpClientBuilder .version (astraDbOptions .getHttpClientOptions ().getHttpVersion ());
123
+ httpClientBuilder .connectTimeout (Duration .ofSeconds (astraDbOptions .getHttpClientOptions ().getConnectionRequestTimeoutInSeconds ()));
124
+ this .httpClient = httpClientBuilder .build ();
148
125
}
149
126
150
- // --------------------
151
- // -- Keyspace ---
152
- // --------------------
127
+ // -------------------------------
128
+ // -- Working with Namespaces ---
129
+ // -------------------------------
153
130
154
131
/**
155
132
* Create a keyspace.
@@ -364,7 +341,7 @@ public Optional<Database> getDatabaseInformations(@NonNull UUID id) {
364
341
* @return
365
342
* database client
366
343
*/
367
- public AstraDB getDatabase (@ NonNull String databaseName ) {
344
+ public AstraDBDatabase getDatabase (@ NonNull String databaseName ) {
368
345
List <Database > dbs = getDatabaseInformations (databaseName ).collect (Collectors .toList ());
369
346
if (dbs .isEmpty ()) {
370
347
throw new DatabaseNotFoundException (databaseName );
@@ -387,8 +364,14 @@ public AstraDB getDatabase(@NonNull String databaseName) {
387
364
* @return
388
365
* database client
389
366
*/
390
- public AstraDB getDatabase (UUID databaseId ) {
391
- return new AstraDB (token , databaseId , null , env , AstraDBAdmin .DEFAULT_KEYSPACE );
367
+ public AstraDBDatabase getDatabase (UUID databaseId ) {
368
+ String databaseRegion = devopsDbClient
369
+ .findById (databaseId .toString ())
370
+ .map (db -> db .getInfo ().getRegion ())
371
+ .orElseThrow (() -> new DatabaseNotFoundException (databaseId .toString ()));
372
+ return new AstraDBDatabase (
373
+ new AstraDBEndpoint (databaseId , databaseRegion , env ),
374
+ token , AstraDBClient .DEFAULT_KEYSPACE , astraDbOptions );
392
375
}
393
376
394
377
/**
@@ -464,7 +447,7 @@ private void resumeDb(Database db) {
464
447
* database client
465
448
*/
466
449
public DataApiClient getDataApiClient (@ NonNull String databaseName ) {
467
- return getDatabase (databaseName ).getApiClient ();
450
+ return getDatabase (databaseName ).getDataApiClient ();
468
451
}
469
452
470
453
/**
@@ -476,7 +459,7 @@ public DataApiClient getDataApiClient(@NonNull String databaseName) {
476
459
* database client
477
460
*/
478
461
public DataApiClient getDataApiClient (@ NonNull UUID databaseId ) {
479
- return getDatabase (databaseId ).getApiClient ();
462
+ return getDatabase (databaseId ).getDataApiClient ();
480
463
}
481
464
482
465
/**
@@ -485,15 +468,8 @@ public DataApiClient getDataApiClient(@NonNull UUID databaseId) {
485
468
* @return
486
469
* devops client.
487
470
*/
488
- public AstraDBOpsClient getDevopsApiClient () {
471
+ public AstraDBDevopsClient getDevopsApiClient () {
489
472
return this .devopsDbClient ;
490
473
}
491
474
492
-
493
- /**
494
- * Close the client.
495
- */
496
- @ Override
497
- public void close () throws IOException {
498
- }
499
475
}
0 commit comments