Skip to content

Commit 139ad6c

Browse files
committed
access list support
1 parent 42adbad commit 139ad6c

26 files changed

+683
-87
lines changed

astra-sdk-devops/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<groupId>com.fasterxml.jackson.core</groupId>
3535
<artifactId>jackson-databind</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>com.fasterxml.jackson.datatype</groupId>
39+
<artifactId>jackson-datatype-jsr310</artifactId>
40+
</dependency>
3741
<dependency>
3842
<groupId>org.junit.jupiter</groupId>
3943
<artifactId>junit-jupiter-engine</artifactId>

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/AbstractApiClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ public void PUT(String url, String body) {
106106
getHttpClient().PUT(url, getToken(), body);
107107
}
108108

109+
/**
110+
* Syntax sugar http requests.
111+
*
112+
* @param url
113+
* url
114+
* @param body
115+
* body
116+
*/
117+
public void PATCH(String url, String body) {
118+
getHttpClient().PATCH(url, getToken(), body);
119+
}
120+
109121
/**
110122
* Syntax sugar http requests.
111123
*

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/db/AstraDbClient.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33

44
import com.dtsx.astra.sdk.AbstractApiClient;
5+
import com.dtsx.astra.sdk.db.domain.*;
56
import com.dtsx.astra.sdk.utils.ApiLocator;
67
import com.dtsx.astra.sdk.utils.ApiResponseHttp;
78
import com.dtsx.astra.sdk.utils.Assert;
89
import com.dtsx.astra.sdk.utils.JsonUtils;
910
import com.fasterxml.jackson.core.type.TypeReference;
10-
import com.dtsx.astra.sdk.db.domain.CloudProviderType;
11-
import com.dtsx.astra.sdk.db.domain.Database;
12-
import com.dtsx.astra.sdk.db.domain.DatabaseCreationRequest;
13-
import com.dtsx.astra.sdk.db.domain.DatabaseFilter;
1411
import com.dtsx.astra.sdk.db.domain.DatabaseFilter.Include;
1512

1613
import java.net.HttpURLConnection;
@@ -28,6 +25,10 @@ public class AstraDbClient extends AbstractApiClient {
2825
private static final TypeReference<List<Database>> RESPONSE_DATABASES =
2926
new TypeReference<List<Database>>(){};
3027

28+
/** Load Database responses. */
29+
private static final TypeReference<List<AccessList>> RESPONSE_ACCESS_LIST =
30+
new TypeReference<List<AccessList>>(){};
31+
3132
/**
3233
* As immutable object use builder to initiate the object.
3334
*
@@ -52,6 +53,20 @@ public DbRegionsClient regions() {
5253
return new DbRegionsClient(token);
5354
}
5455

56+
57+
// ---------------------------------
58+
// ---- GLOBAL ACCESS LIST ----
59+
// ---------------------------------
60+
61+
/**
62+
* Find All Access List.
63+
* @return
64+
* access list
65+
*/
66+
public Stream<AccessList> findAllAccessLists() {
67+
return JsonUtils.unmarshallType(GET(getEndpointAccessLists()).getBody(), RESPONSE_ACCESS_LIST).stream();
68+
}
69+
5570
// ---------------------------------
5671
// ---- CRUD ----
5772
// ---------------------------------
@@ -119,7 +134,7 @@ public Optional<Database> findById(String id) {
119134
*/
120135
public Stream<Database> search(DatabaseFilter filter) {
121136
Assert.notNull(filter, "filter");
122-
ApiResponseHttp res = GET(getApiDevopsEndpointDatabases() + filter.urlParams());
137+
ApiResponseHttp res = GET(getEndpointDatabases() + filter.urlParams());
123138
return JsonUtils.unmarshallType(res.getBody(), RESPONSE_DATABASES).stream();
124139
}
125140

@@ -135,7 +150,7 @@ public Stream<Database> search(DatabaseFilter filter) {
135150
*/
136151
public String create(DatabaseCreationRequest dbCreationRequest) {
137152
Assert.notNull(dbCreationRequest, "Database creation request");
138-
ApiResponseHttp res = POST(getApiDevopsEndpointDatabases(), JsonUtils.marshall(dbCreationRequest));
153+
ApiResponseHttp res = POST(getEndpointDatabases(), JsonUtils.marshall(dbCreationRequest));
139154
if (HttpURLConnection.HTTP_CREATED != res.getCode()) {
140155
throw new IllegalStateException("Expected code 201 to create db but got "
141156
+ res.getCode() + "body=" + res.getBody());
@@ -182,8 +197,18 @@ public DatabaseClient databaseByName(String dbName) {
182197
* @return
183198
* endpoint
184199
*/
185-
public static String getApiDevopsEndpointDatabases() {
200+
public static String getEndpointDatabases() {
186201
return ApiLocator.getApiDevopsEndpoint() + "/databases";
187202
}
188203

204+
/**
205+
* Endpoint to access schema for namespace.
206+
*
207+
* @return
208+
* endpoint
209+
*/
210+
public static String getEndpointAccessLists() {
211+
return ApiLocator.getApiDevopsEndpoint() + "/access-lists";
212+
}
213+
189214
}

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/db/DatabaseClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public String getEndpointDatabase() {
371371
* @return database endpoint
372372
*/
373373
public static String getEndpointDatabase(String dbId) {
374-
return AstraDbClient.getApiDevopsEndpointDatabases() + "/" + dbId;
374+
return AstraDbClient.getEndpointDatabases() + "/" + dbId;
375375
}
376376

377377
}
Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package com.dtsx.astra.sdk.db;
22

33
import 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;
47
import com.dtsx.astra.sdk.db.domain.Database;
8+
import com.dtsx.astra.sdk.utils.ApiLocator;
59
import 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
*/
1018
public 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

Comments
 (0)