22
22
23
23
import com .datastax .astra .client .DataAPIDestination ;
24
24
import com .datastax .astra .client .core .commands .BaseOptions ;
25
+ import com .datastax .astra .client .core .commands .CommandType ;
25
26
import com .datastax .astra .client .core .options .DataAPIClientOptions ;
26
27
import com .datastax .astra .client .core .results .FindEmbeddingProvidersResult ;
27
28
import com .datastax .astra .client .databases .DatabaseOptions ;
28
29
import com .datastax .astra .internal .api .AstraApiEndpoint ;
30
+ import com .datastax .astra .internal .command .AbstractCommandRunner ;
31
+ import com .datastax .astra .internal .utils .Assert ;
29
32
import com .dtsx .astra .sdk .db .AstraDBOpsClient ;
30
33
import com .dtsx .astra .sdk .db .domain .Database ;
31
34
import com .dtsx .astra .sdk .db .exception .DatabaseNotFoundException ;
40
43
* Implementation of the DatabaseAdmin interface for Astra. To create the namespace the devops APi is leverage. To use this class a higher token permission is required.
41
44
*/
42
45
@ Slf4j
43
- public class AstraDBDatabaseAdmin implements DatabaseAdmin {
44
-
45
- /** Token used for the credentials. */
46
- final String token ;
46
+ public class AstraDBDatabaseAdmin extends AbstractCommandRunner <AdminOptions > implements DatabaseAdmin {
47
47
48
48
/** Token used for the credentials. */
49
49
final UUID databaseId ;
50
50
51
- /** Data Api Options. */
52
- final DataAPIClientOptions dataAPIClientOptions ;
53
-
54
51
/** Client for Astra Devops Api. */
55
52
final AstraDBOpsClient devopsDbClient ;
56
53
57
54
/** Database if initialized from the DB. */
58
- protected com .datastax .astra .client .databases .Database db ;
55
+ final com .datastax .astra .client .databases .Database db ;
59
56
60
57
/**
61
58
* Initialize a database admin from token and database id.
62
59
*
63
60
* @param db
64
61
* target database
65
62
*/
66
- public AstraDBDatabaseAdmin (com .datastax .astra .client .databases .Database db ) {
67
- this .databaseId = UUID .fromString (db .getApiEndpoint ().substring (8 , 44 ));
68
- this .dataAPIClientOptions = db .getDatabaseOptions ().getDataAPIClientOptions ();
69
- this .token = db .getDatabaseOptions ().getToken ();
70
- this .db = db ;
71
- this .devopsDbClient = new AstraDBOpsClient (token , dataAPIClientOptions .getAstraEnvironment ());
63
+ public AstraDBDatabaseAdmin (com .datastax .astra .client .databases .Database db , AdminOptions adminOptions ) {
64
+ Assert .notNull (db , "database" );
65
+ this .db = db ;
66
+ this .databaseId = db .getId (); // UUID.fromString(db.getApiEndpoint().substring(8, 44));
67
+ this .options = adminOptions ;
68
+ if (adminOptions == null ) {
69
+ this .options = new AdminOptions ();
70
+ }
71
+ // Using database options if no extra parameters provided
72
+ if (this .options .getToken () == null ) {
73
+ this .options .token (db .getOptions ().getToken ());
74
+ }
75
+ if (this .options .getDataAPIClientOptions () == null ) {
76
+ this .options .dataAPIClientOptions (db .getOptions ().getDataAPIClientOptions ());
77
+ }
78
+ this .devopsDbClient = new AstraDBOpsClient (
79
+ options .getToken (),
80
+ options .getDataAPIClientOptions ().getAstraEnvironment ());
72
81
}
73
82
74
83
/**
@@ -78,16 +87,16 @@ public AstraDBDatabaseAdmin(com.datastax.astra.client.databases.Database db) {
78
87
* token value
79
88
* @param databaseId
80
89
* database identifier
81
- * @param options
90
+ * @param clientOptions
82
91
* options used to initialize the http client
83
92
*/
84
- public AstraDBDatabaseAdmin (String token , UUID databaseId , DataAPIClientOptions options ) {
85
- this .token = token ;
93
+ public AstraDBDatabaseAdmin (String token , UUID databaseId , DataAPIClientOptions clientOptions ) {
86
94
this .databaseId = databaseId ;
87
- this .dataAPIClientOptions = options ;
88
- this .devopsDbClient = new AstraDBOpsClient (token , options .getAstraEnvironment ());
95
+ this .options = new AdminOptions (token , clientOptions );
96
+ this .devopsDbClient = new AstraDBOpsClient (token , options .getDataAPIClientOptions ().getAstraEnvironment ());
97
+
89
98
this .db = new com .datastax .astra .client .databases .Database (getApiEndpoint (),
90
- new DatabaseOptions (token , options ));
99
+ new DatabaseOptions (token , options . getDataAPIClientOptions () ));
91
100
}
92
101
93
102
/**
@@ -134,9 +143,9 @@ private static AstraEnvironment getEnvironment(DataAPIDestination destination) {
134
143
* @return
135
144
* the endpoint as an url.
136
145
*/
137
- private String getApiEndpoint () {
146
+ public String getApiEndpoint () {
138
147
return new AstraApiEndpoint (databaseId ,
139
- getDatabaseInformations ().getInfo ().getRegion (), dataAPIClientOptions .getAstraEnvironment ())
148
+ getDatabaseInformations ().getInfo ().getRegion (), options . getDataAPIClientOptions () .getAstraEnvironment ())
140
149
.getApiEndPoint ();
141
150
}
142
151
@@ -162,7 +171,7 @@ public com.datastax.astra.client.databases.Database getDatabase(String keyspace)
162
171
* client to interact with database DML.
163
172
*/
164
173
public com .datastax .astra .client .databases .Database getDatabase (String keyspace , String tokenUser ) {
165
- return new com .datastax .astra .client .databases .Database (getApiEndpoint (), db .getDatabaseOptions ());
174
+ return new com .datastax .astra .client .databases .Database (getApiEndpoint (), db .getOptions ());
166
175
}
167
176
168
177
@ Override
@@ -177,9 +186,7 @@ public Set<String> listKeyspaceNames() {
177
186
@ Override
178
187
public FindEmbeddingProvidersResult findEmbeddingProviders () {
179
188
log .debug ("findEmbeddingProviders" );
180
- DataAPIDatabaseAdmin admin =
181
- new DataAPIDatabaseAdmin (getApiEndpoint () + "/" + db .getDatabaseOptions ()
182
- .getDataAPIClientOptions ().getApiVersion (), db .getDatabaseOptions ());
189
+ DataAPIDatabaseAdmin admin = new DataAPIDatabaseAdmin (db , this .options );
183
190
return new FindEmbeddingProvidersResult (admin .findEmbeddingProviders ().getEmbeddingProviders ());
184
191
}
185
192
0 commit comments