Skip to content

Commit debe7fc

Browse files
authored
Refactor API resources (#335)
1 parent 1e4567f commit debe7fc

File tree

11 files changed

+166
-200
lines changed

11 files changed

+166
-200
lines changed

management-api-server/src/main/java/com/datastax/mgmtapi/resources/AuthResources.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
*/
66
package com.datastax.mgmtapi.resources;
77

8-
import static com.datastax.mgmtapi.resources.NodeOpsResources.handle;
9-
10-
import com.datastax.mgmtapi.CqlService;
118
import com.datastax.mgmtapi.ManagementApplication;
9+
import com.datastax.mgmtapi.resources.common.BaseResources;
1210
import io.swagger.v3.oas.annotations.Operation;
1311
import io.swagger.v3.oas.annotations.media.Content;
1412
import io.swagger.v3.oas.annotations.media.ExampleObject;
@@ -23,13 +21,10 @@
2321
import org.apache.commons.lang3.StringUtils;
2422

2523
@Path("/api/v0/ops/auth")
26-
public class AuthResources {
27-
private final ManagementApplication app;
28-
private final CqlService cqlService;
24+
public class AuthResources extends BaseResources {
2925

3026
public AuthResources(ManagementApplication application) {
31-
this.app = application;
32-
this.cqlService = application.cqlService;
27+
super(application);
3328
}
3429

3530
@POST
@@ -67,7 +62,7 @@ public Response createRole(
6762
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), "Password is empty")
6863
.build();
6964

70-
cqlService.executePreparedStatement(
65+
app.cqlService.executePreparedStatement(
7166
app.dbUnixSocketFile,
7267
"CALL NodeOps.createRole(?,?,?,?)",
7368
name,

management-api-server/src/main/java/com/datastax/mgmtapi/resources/K8OperatorResources.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
*/
66
package com.datastax.mgmtapi.resources;
77

8-
import static com.datastax.mgmtapi.resources.NodeOpsResources.handle;
9-
10-
import com.datastax.mgmtapi.CqlService;
118
import com.datastax.mgmtapi.ManagementApplication;
9+
import com.datastax.mgmtapi.resources.common.BaseResources;
1210
import com.datastax.mgmtapi.resources.helpers.ResponseTools;
1311
import com.datastax.mgmtapi.resources.models.Job;
1412
import com.datastax.oss.driver.api.core.cql.ResultSet;
@@ -29,20 +27,13 @@
2927
import javax.ws.rs.QueryParam;
3028
import javax.ws.rs.core.MediaType;
3129
import javax.ws.rs.core.Response;
32-
import org.slf4j.Logger;
33-
import org.slf4j.LoggerFactory;
3430

3531
@Path("/api/v0")
36-
public class K8OperatorResources {
37-
private static final Logger logger = LoggerFactory.getLogger(K8OperatorResources.class);
32+
public class K8OperatorResources extends BaseResources {
3833
private static final ObjectMapper jsonMapper = new ObjectMapper();
3934

40-
private final ManagementApplication app;
41-
private final CqlService cqlService;
42-
4335
public K8OperatorResources(ManagementApplication application) {
44-
this.app = application;
45-
this.cqlService = application.cqlService;
36+
super(application);
4637
}
4738

4839
@GET
@@ -80,7 +71,7 @@ public Response checkReadiness() {
8071
return handle(
8172
() -> {
8273
ResultSet resultSet =
83-
cqlService.executeCql(app.dbUnixSocketFile, "SELECT * from system.local");
74+
app.cqlService.executeCql(app.dbUnixSocketFile, "SELECT * from system.local");
8475
Row result = resultSet.one();
8576

8677
if (result != null) {
@@ -131,7 +122,7 @@ public Response checkClusterConsistency(
131122
if (rfPerDc == null) rfPerDc = 3;
132123

133124
ResultSet result =
134-
cqlService.executePreparedStatement(
125+
app.cqlService.executePreparedStatement(
135126
app.dbUnixSocketFile,
136127
"CALL NodeOps.checkConsistencyLevel(?, ?)",
137128
consistencyLevel,
@@ -166,7 +157,7 @@ public Response seedReload() {
166157
return handle(
167158
() -> {
168159
ResultSet result =
169-
cqlService.executeCql(app.dbUnixSocketFile, "CALL NodeOps.reloadSeeds()");
160+
app.cqlService.executeCql(app.dbUnixSocketFile, "CALL NodeOps.reloadSeeds()");
170161

171162
List<String> response = result.one().getList("result", String.class);
172163

@@ -200,7 +191,7 @@ public Response getJobStatus(
200191
Map<String, String> jobResponse =
201192
(Map<String, String>)
202193
ResponseTools.getSingleRowResponse(
203-
app.dbUnixSocketFile, cqlService, "CALL NodeOps.jobStatus(?)", jobId);
194+
app.dbUnixSocketFile, app.cqlService, "CALL NodeOps.jobStatus(?)", jobId);
204195
if (jobResponse.isEmpty()) {
205196
return Response.status(Response.Status.NOT_FOUND).entity(jobResponse).build();
206197
}

management-api-server/src/main/java/com/datastax/mgmtapi/resources/KeyspaceOpsResources.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
package com.datastax.mgmtapi.resources;
77

8-
import com.datastax.mgmtapi.CqlService;
98
import com.datastax.mgmtapi.ManagementApplication;
9+
import com.datastax.mgmtapi.resources.common.BaseResources;
1010
import com.datastax.mgmtapi.resources.helpers.ResponseTools;
1111
import com.datastax.mgmtapi.resources.models.CreateOrAlterKeyspaceRequest;
1212
import com.datastax.mgmtapi.resources.models.KeyspaceRequest;
@@ -35,20 +35,13 @@
3535
import org.apache.commons.collections4.CollectionUtils;
3636
import org.apache.commons.lang3.StringUtils;
3737
import org.apache.http.HttpStatus;
38-
import org.slf4j.Logger;
39-
import org.slf4j.LoggerFactory;
4038

4139
@Path("/api/v0/ops/keyspace")
42-
public class KeyspaceOpsResources {
43-
private static final Logger logger = LoggerFactory.getLogger(KeyspaceOpsResources.class);
40+
public class KeyspaceOpsResources extends BaseResources {
4441
private static final ObjectMapper jsonMapper = new ObjectMapper();
4542

46-
private final ManagementApplication app;
47-
private final CqlService cqlService;
48-
4943
public KeyspaceOpsResources(ManagementApplication application) {
50-
this.app = application;
51-
this.cqlService = application.cqlService;
44+
super(application);
5245
}
5346

5447
@POST
@@ -68,7 +61,7 @@ public KeyspaceOpsResources(ManagementApplication application) {
6861
schema = @Schema(implementation = String.class),
6962
examples = @ExampleObject(value = "d69d1d95-9348-4460-95d2-ae342870fade")))
7063
public Response cleanup(KeyspaceRequest keyspaceRequest) {
71-
return NodeOpsResources.handle(
64+
return handle(
7265
() -> {
7366
List<String> tables = keyspaceRequest.tables;
7467
if (CollectionUtils.isEmpty(tables)) {
@@ -87,7 +80,7 @@ public Response cleanup(KeyspaceRequest keyspaceRequest) {
8780
return Response.ok(
8881
ResponseTools.getSingleRowStringResponse(
8982
app.dbUnixSocketFile,
90-
cqlService,
83+
app.cqlService,
9184
"CALL NodeOps.forceKeyspaceCleanup(?, ?, ?, ?)",
9285
keyspaceRequest.jobs,
9386
keyspaceName,
@@ -122,7 +115,7 @@ public Response cleanup(KeyspaceRequest keyspaceRequest) {
122115
public Response refresh(
123116
@QueryParam(value = "keyspaceName") String keyspaceName,
124117
@QueryParam(value = "table") String table) {
125-
return NodeOpsResources.handle(
118+
return handle(
126119
() -> {
127120
if (StringUtils.isBlank(keyspaceName)) {
128121
return Response.status(HttpStatus.SC_BAD_REQUEST)
@@ -136,7 +129,7 @@ public Response refresh(
136129
.build();
137130
}
138131

139-
cqlService.executePreparedStatement(
132+
app.cqlService.executePreparedStatement(
140133
app.dbUnixSocketFile, "CALL NodeOps.loadNewSSTables(?, ?)", keyspaceName, table);
141134

142135
return Response.ok("OK").build();
@@ -170,7 +163,7 @@ public Response refresh(
170163
summary = "Create a new keyspace with the given name and replication settings",
171164
operationId = "createKeyspace")
172165
public Response create(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest) {
173-
return NodeOpsResources.handle(
166+
return handle(
174167
() -> {
175168
if (StringUtils.isBlank(createOrAlterKeyspaceRequest.keyspaceName)) {
176169
return Response.status(HttpStatus.SC_BAD_REQUEST)
@@ -185,7 +178,7 @@ public Response create(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest
185178
.build();
186179
}
187180

188-
cqlService.executePreparedStatement(
181+
app.cqlService.executePreparedStatement(
189182
app.dbUnixSocketFile,
190183
"CALL NodeOps.createKeyspace(?, ?)",
191184
createOrAlterKeyspaceRequest.keyspaceName,
@@ -222,7 +215,7 @@ public Response create(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest
222215
summary = "Alter the replication settings of an existing keyspace",
223216
operationId = "alterKeyspace")
224217
public Response alter(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest) {
225-
return NodeOpsResources.handle(
218+
return handle(
226219
() -> {
227220
if (StringUtils.isBlank(createOrAlterKeyspaceRequest.keyspaceName)) {
228221
return Response.status(HttpStatus.SC_BAD_REQUEST)
@@ -237,7 +230,7 @@ public Response alter(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest)
237230
.build();
238231
}
239232

240-
cqlService.executePreparedStatement(
233+
app.cqlService.executePreparedStatement(
241234
app.dbUnixSocketFile,
242235
"CALL NodeOps.alterKeyspace(?, ?)",
243236
createOrAlterKeyspaceRequest.keyspaceName,
@@ -263,10 +256,10 @@ public Response alter(CreateOrAlterKeyspaceRequest createOrAlterKeyspaceRequest)
263256
@Consumes(MediaType.APPLICATION_JSON)
264257
@Operation(summary = "List the keyspaces existing in the cluster", operationId = "listKeyspaces")
265258
public Response list(@QueryParam(value = "keyspaceName") String keyspaceName) {
266-
return NodeOpsResources.handle(
259+
return handle(
267260
() -> {
268261
ResultSet result =
269-
cqlService.executePreparedStatement(
262+
app.cqlService.executePreparedStatement(
270263
app.dbUnixSocketFile, "CALL NodeOps.getKeyspaces()");
271264
Row row = result.one();
272265
List<String> keyspaces = null;
@@ -330,10 +323,10 @@ public Response getReplication(
330323
.entity("Get keyspace replication failed. Non-empty 'keyspaceName' must be provided")
331324
.build();
332325
}
333-
return NodeOpsResources.handle(
326+
return handle(
334327
() -> {
335328
ResultSet result =
336-
cqlService.executePreparedStatement(
329+
app.cqlService.executePreparedStatement(
337330
app.dbUnixSocketFile, "CALL NodeOps.getReplication(?)", keyspaceName);
338331
Row row = result.one();
339332
if (row == null) {

management-api-server/src/main/java/com/datastax/mgmtapi/resources/LifecycleResources.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.datastax.mgmtapi.ManagementApplication;
1212
import com.datastax.mgmtapi.UnixCmds;
1313
import com.datastax.mgmtapi.UnixSocketCQLAccess;
14+
import com.datastax.mgmtapi.resources.common.BaseResources;
1415
import com.datastax.mgmtapi.util.ShellUtils;
1516
import com.datastax.oss.driver.api.core.CqlSession;
1617
import com.datastax.oss.driver.shaded.guava.common.collect.Streams;
@@ -49,18 +50,16 @@
4950
import org.slf4j.LoggerFactory;
5051

5152
@Path("/api/v0/lifecycle")
52-
public class LifecycleResources {
53+
public class LifecycleResources extends BaseResources {
5354
private static final Logger logger = LoggerFactory.getLogger(LifecycleResources.class);
54-
private final ManagementApplication app;
5555

5656
static final YAMLMapper yamlMapper = new YAMLMapper();
57-
static final String PROFILE_PATTERN = "[0-9a-zA-Z\\-_]+";
5857
static final String IPV4_PATTERN =
5958
"^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$";
6059
private ObjectMapper objectMapper = new ObjectMapper();
6160

6261
public LifecycleResources(ManagementApplication app) {
63-
this.app = app;
62+
super(app);
6463
}
6564

6665
/**

management-api-server/src/main/java/com/datastax/mgmtapi/resources/MetadataResources.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
*/
66
package com.datastax.mgmtapi.resources;
77

8-
import static com.datastax.mgmtapi.resources.NodeOpsResources.handle;
9-
10-
import com.datastax.mgmtapi.CqlService;
118
import com.datastax.mgmtapi.ManagementApplication;
9+
import com.datastax.mgmtapi.resources.common.BaseResources;
1210
import com.datastax.mgmtapi.resources.helpers.ResponseTools;
1311
import com.datastax.mgmtapi.resources.models.FeatureSet;
1412
import io.swagger.v3.oas.annotations.Operation;
@@ -19,23 +17,15 @@
1917
import javax.ws.rs.GET;
2018
import javax.ws.rs.Path;
2119
import javax.ws.rs.Produces;
22-
import javax.ws.rs.client.Entity;
2320
import javax.ws.rs.core.MediaType;
2421
import javax.ws.rs.core.Response;
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
2722

2823
@Path("/api/v0/metadata")
29-
public class MetadataResources {
30-
private static final Logger logger = LoggerFactory.getLogger(MetadataResources.class);
24+
public class MetadataResources extends BaseResources {
3125
private static final String CASSANDRA_VERSION_CQL_STRING = "CALL NodeOps.getReleaseVersion()";
3226

33-
private final ManagementApplication app;
34-
private final CqlService cqlService;
35-
3627
public MetadataResources(ManagementApplication application) {
37-
this.app = application;
38-
this.cqlService = application.cqlService;
28+
super(application);
3929
}
4030

4131
@GET
@@ -106,48 +96,13 @@ public Response getFeatureSet() {
10696
() -> {
10797
String cassandraVersion =
10898
ResponseTools.getSingleRowStringResponse(
109-
app.dbUnixSocketFile, cqlService, CASSANDRA_VERSION_CQL_STRING);
99+
app.dbUnixSocketFile, app.cqlService, CASSANDRA_VERSION_CQL_STRING);
110100
// TODO management-api-release-version is not included in the release packages
111101
FeatureSet featureSet = new FeatureSet(cassandraVersion, "");
112102
return Response.ok(featureSet).build();
113103
});
114104
}
115105

116-
/**
117-
* Executes a CQL query with the expectation that there will be a single row returned with type
118-
* String
119-
*
120-
* @param query CQL query to execute
121-
* @return Returns a Response with status code 200 and body of query response on success and
122-
* status code 500 on failure
123-
*/
124-
private Response executeWithStringResponse(String query) {
125-
return handle(
126-
() ->
127-
Response.ok(
128-
ResponseTools.getSingleRowStringResponse(
129-
app.dbUnixSocketFile, cqlService, query))
130-
.build());
131-
}
132-
133-
/**
134-
* Executes a CQL query with the expectation that there will be a single row returned with type
135-
* String
136-
*
137-
* @param query CQL query to execute
138-
* @return Returns a Response with status code 200 and body of query response on success and
139-
* status code 500 on failure
140-
*/
141-
private Response executeWithJSONResponse(String query) {
142-
return handle(
143-
() ->
144-
Response.ok(
145-
Entity.json(
146-
ResponseTools.getSingleRowResponse(
147-
app.dbUnixSocketFile, cqlService, query)))
148-
.build());
149-
}
150-
151106
private static final String ENDPOINTS_RESPONSE_EXAMPLE =
152107
"{\n"
153108
+ " \"entity\": [\n"

0 commit comments

Comments
 (0)