11package com .dtsx .astra .sdk .db ;
22
33import com .dtsx .astra .sdk .AbstractApiClient ;
4+ import com .dtsx .astra .sdk .db .domain .AccessList ;
5+ import com .dtsx .astra .sdk .db .domain .AccessListAddressRequest ;
6+ import com .dtsx .astra .sdk .db .domain .AccessListRequest ;
47import com .dtsx .astra .sdk .db .domain .Database ;
8+ import com .dtsx .astra .sdk .utils .ApiLocator ;
59import com .dtsx .astra .sdk .utils .Assert ;
10+ import com .dtsx .astra .sdk .utils .JsonUtils ;
11+ import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
12+
13+ import java .util .Arrays ;
614
715/**
816 * Operations on Access List.
917 */
1018public class DbAccessListsClient extends AbstractApiClient {
1119
12- /** Get Available Regions. */
13- public static final String PATH_ACCESS_LISTS = "/access-lists" ;
14-
1520 /**
1621 * unique db identifier.
1722 */
@@ -28,50 +33,88 @@ public class DbAccessListsClient extends AbstractApiClient {
2833 public DbAccessListsClient (String token , String databaseId ) {
2934 super (token );
3035 Assert .hasLength (databaseId , "databaseId" );
31- // Test Db exists
3236 this .db = new DatabaseClient (token , databaseId ).get ();
3337 }
3438
3539 /**
36- * TODO Get access list for a database
37- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/GetAccessListForDatabase
40+ * Retrieve the access list for a DB.
41+ *
42+ * @return
43+ * current access list
44+ */
45+ public AccessList get () {
46+ try {
47+ return JsonUtils .unmarshallBean (GET (getApiDevopsEndpointAccessListsDb ()).getBody (), AccessList .class );
48+ } catch (RuntimeException mex ) {
49+ AccessList ac = new AccessList ();
50+ ac .setDatabaseId (db .getId ());
51+ ac .setOrganizationId (db .getOrgId ());
52+ ac .setConfigurations (new AccessList .Configurations (false ));
53+ return ac ;
54+ }
55+ }
56+
57+ /**
58+ * Create a new Address for the DB.
59+ *
60+ * @param newAddressed
61+ * address to be added
62+ * @see <a href="https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/AddAddressesToAccessListForDatabase">Reference Documentation</a>
3863 */
39- public void findAll () {
40- throw new RuntimeException ("This function is not yet implemented" );
64+ public void addAddress (AccessListAddressRequest ... newAddressed ) {
65+ Assert .notNull (newAddressed , "New addresses should not be null" );
66+ Assert .isTrue (newAddressed .length > 0 , "New address should not be empty" );
67+ POST (getApiDevopsEndpointAccessListsDb (), JsonUtils .marshall (newAddressed ));
4168 }
4269
4370 /**
44- * TODO Replace access list for your database.
45- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/AddAddressesToAccessListForDatabase
71+ * Delete the addresses List.
72+ *
73+ * @see <a href="https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/DeleteAddressesOrAccessListForDatabase">Reference Documentation</a>
4674 */
47- public void replace () {
48- throw new RuntimeException ( "This function is not yet implemented" );
75+ public void delete () {
76+ DELETE ( getApiDevopsEndpointAccessListsDb () );
4977 }
5078
5179 /**
52- * TODO Update existing fields in access list for database
53- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/UpsertAccessListForDatabase
80+ * Replace the addresses for a DB
81+ *
82+ * @param addresses
83+ * address to be added
84+ *
85+ * @see <a href="https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/AddAddressesToAccessListForDatabase">Reference Documentation</a>
5486 */
55- public void update () {
56- throw new RuntimeException ("This function is not yet implemented" );
87+ public void replaceAddresses (AccessListAddressRequest ... addresses ) {
88+ Assert .notNull (addresses , "Addresses should not be null" );
89+ Assert .isTrue (addresses .length > 0 , "Address should not be empty" );
90+ PUT (getApiDevopsEndpointAccessListsDb (), JsonUtils .marshall (addresses ));
5791 }
5892
5993 /**
60- * TODO Add addresses to access list for a database
61- * <p>
62- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/AddAddressesToAccessListForDatabase
94+ * Replace the addresses for a DB
95+ *
96+ * @param addresses
97+ * address to be updated
98+ *
99+ * @see <a href="https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/UpsertAccessListForDatabase">Reference Documentation</a>
63100 */
64- public void create () {
65- throw new RuntimeException ("This function is not yet implemented" );
101+ public void update (AccessListAddressRequest ... addresses ) {
102+ Assert .notNull (addresses , "Addresses should not be null" );
103+ Assert .isTrue (addresses .length > 0 , "Address should not be empty" );
104+ AccessListRequest alr = new AccessListRequest ();
105+ alr .setAddresses (Arrays .asList (addresses ));
106+ alr .setConfigurations (new AccessListRequest .Configurations (true ));
107+ PATCH (getApiDevopsEndpointAccessListsDb (), JsonUtils .marshall (alr ));
66108 }
67109
68110 /**
69- * TODO Delete addresses or access list for database
70- * <p>
71- * https://docs.datastax.com/en/astra/docs/_attachments/devopsv2.html#operation/DeleteAddressesOrAccessListForDatabase
111+ * Endpoint to access schema for namespace.
112+ *
113+ * @return
114+ * endpoint
72115 */
73- public void delete () {
74- throw new RuntimeException ( "This function is not yet implemented" ) ;
116+ public String getApiDevopsEndpointAccessListsDb () {
117+ return ApiLocator . getApiDevopsEndpoint () + "/databases/" + db . getId () + "/access-list" ;
75118 }
76119
77120}
0 commit comments