1- package com .dtsx .astra .sdk .databases ;
1+ package com .dtsx .astra .sdk .db ;
22
33import com .dtsx .astra .sdk .HttpClientWrapper ;
4+ import com .dtsx .astra .sdk .db .domain .*;
5+ import com .dtsx .astra .sdk .db .domain .exception .RegionNotFoundException ;
6+ import com .dtsx .astra .sdk .db .telemetry .TelemetryClient ;
47import com .dtsx .astra .sdk .utils .ApiResponseHttp ;
58import com .dtsx .astra .sdk .utils .Assert ;
69import com .dtsx .astra .sdk .utils .JsonUtils ;
710import com .dtsx .astra .sdk .utils .Utils ;
8- import com .dtsx .astra .sdk .databases .domain .Database ;
9- import com .dtsx .astra .sdk .databases .domain .DatabaseStatusType ;
11+ import com .fasterxml .jackson .core .type .TypeReference ;
1012import org .slf4j .Logger ;
1113import org .slf4j .LoggerFactory ;
1214
1315import java .io .File ;
1416import java .net .HttpURLConnection ;
17+ import java .util .Arrays ;
18+ import java .util .List ;
1519import java .util .Map ;
1620import java .util .Optional ;
21+ import java .util .stream .Stream ;
1722
1823import static java .net .HttpURLConnection .HTTP_ACCEPTED ;
1924import static java .net .HttpURLConnection .HTTP_OK ;
@@ -27,13 +32,18 @@ public class DatabaseClient {
2732
2833 /** Logger for our Client. */
2934 private static final Logger LOGGER = LoggerFactory .getLogger (DatabaseClient .class );
30-
35+
36+ /** Returned type. */
37+ private static final TypeReference <List <Datacenter >> DATACENTER_LIST =
38+ new TypeReference <List <Datacenter >>(){};
39+
3140 /** unique db identifier. */
3241 private final String databaseId ;
3342
3443 /** Wrapper handling header and error management as a singleton. */
3544 private final HttpClientWrapper http = HttpClientWrapper .getInstance ();
36-
45+
46+
3747 /** Reference to upper resource. */
3848 private final DatabasesClient databasesClient ;
3949
@@ -258,59 +268,92 @@ public void resetPassword(String username, String password) {
258268 // ---------------------------------
259269 // ---- Regions ----
260270 // ---------------------------------
261-
271+
262272 /**
263- * TODO Add a region to the DB.
264- *
273+ * Get Datacenters details for a region
274+ *
275+ * @return
276+ * list of datacenters.
277+ */
278+ public Stream <Datacenter > regions () {
279+ ApiResponseHttp res = http .GET (getEndpointRegions (), databasesClient .bearerAuthToken );
280+ if (HttpURLConnection .HTTP_NOT_FOUND == res .getCode ()) {
281+ return Stream .of ();
282+ } else {
283+ return JsonUtils .unmarshallType (res .getBody (), DATACENTER_LIST ).stream ();
284+ }
285+ }
286+
287+ /**
288+ * Get a region from its name.
289+ *
290+ * @param regionName
291+ * region name
292+ * @return
293+ * datacenter if exists
294+ i */
295+ public Optional <Datacenter > findRegion (String regionName ) {
296+ Assert .hasLength (regionName , "regionName" );
297+ return regions ().filter (dc -> regionName .equals (dc .getRegion ())).findFirst ();
298+ }
299+
300+ /**
301+ * Create a Region.
302+ *
303+ * @param tier
304+ * tier for the db
305+ * @param cloudProvider
306+ * Cloud provider to add a region
265307 * @param regionName
266308 * name of the region
267309 *
268310 * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/addDatacenters
269311 */
270- public void addRegion (String regionName ) {
271- throw new RuntimeException ("This function is not yet implemented" );
312+ public void addRegion (String tier , CloudProviderType cloudProvider , String regionName ) {
313+ Assert .hasLength (tier , "tier" );
314+ Assert .notNull (cloudProvider , "cloudProvider" );
315+ Assert .hasLength (regionName , "regionName" );
316+ DatabaseRegionCreationRequest req = new DatabaseRegionCreationRequest (tier , cloudProvider .getCode (), regionName );
317+ String body = JsonUtils .marshall (Arrays .asList (req ));
318+ ApiResponseHttp res = http .POST (getEndpointRegions (), databasesClient .bearerAuthToken ,body );
319+ if (res .getCode () != HttpURLConnection .HTTP_CREATED ) {
320+ throw new IllegalStateException ("Cannot Add Region: " + res .getBody ());
321+ }
272322 }
273-
274-
323+
275324 /**
276- * TODO Delete a region to the DB .
325+ * Delete a region from its name .
277326 *
278327 * @param regionName
279328 * name of the region
280329 *
281330 * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/terminateDatacenter
282331 */
283332 public void deleteRegion (String regionName ) {
284- throw new RuntimeException ("This function is not yet implemented" );
285- }
286-
287- /**
288- * TODO List all database regions
289- *
290- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/listAvailableRegions
291- */
292- public void regions () {
293- throw new RuntimeException ("This function is not yet implemented" );
294- }
295-
296- /**
297- * Configure Astra Remote Telemetry
298- *
299- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/configureTelemetry
300- */
301- public void configureTelemetry () {
302- throw new RuntimeException ("This function is not yet implemented" );
333+ Optional <Datacenter > optDc = findRegion (regionName );
334+ if (!optDc .isPresent ()) {
335+ throw new RegionNotFoundException (databaseId , regionName );
336+ }
337+ // Invoke Http endpoint
338+ ApiResponseHttp res = http .POST (getEndpointRegions () + "/"
339+ + optDc .get ().getId () + "/terminate" , databasesClient .getToken ());
340+ // Check response code
341+ checkResponse (res , "deleteRegion" );
303342 }
304-
343+
344+ // ---------------------------------
345+ // ---- Telemetry ----
346+ // ---------------------------------
347+
305348 /**
306- * Retrieve Remote Telemetry configuration
307- *
308- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/getTelemetryConfig
349+ * Delegate Telemetry operation in a dedicated class
350+ *
351+ * @return
309352 */
310- public void getTelemetryConfiguration () {
311- throw new RuntimeException ( "This function is not yet implemented" );
353+ public TelemetryClient telemetry () {
354+ return new TelemetryClient ( this , databaseId );
312355 }
313-
356+
314357 // ---------------------------------
315358 // ---- Access List ----
316359 // ---------------------------------
@@ -464,6 +507,16 @@ public void deletePrivateEndpoint(String region, String endpointId) {
464507 public String getEndpointDatabase () {
465508 return getEndpointDatabase (databaseId );
466509 }
510+
511+ /**
512+ * Endpoint to access datacenters of a db
513+ *
514+ * @return
515+ * database endpoint
516+ */
517+ public String getEndpointRegions () {
518+ return getEndpointDatabase () + "/datacenters" ;
519+ }
467520
468521 /**
469522 * Endpoint to access dbs (static)
@@ -529,5 +582,15 @@ private void checkResponse(ApiResponseHttp res, String action) {
529582 public String getDatabaseId () {
530583 return databaseId ;
531584 }
532-
585+
586+ /**
587+ * Access current token.
588+ *
589+ * @return
590+ * current token
591+ */
592+ public String getToken () {
593+ return databasesClient .getToken ();
594+ }
595+
533596}
0 commit comments