Skip to content

Commit

Permalink
Set return value to be Role object instead of untyped Strings for lis…
Browse files Browse the repository at this point in the history
…tRoles
  • Loading branch information
burmanm committed Nov 12, 2024
1 parent c53af95 commit e2076b6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
26 changes: 24 additions & 2 deletions management-api-server/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@
"200" : {
"content" : {
"application/json" : {
"example" : "OK",
"schema" : {
"type" : "string"
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/Role"
}
}
}
},
Expand Down Expand Up @@ -2374,6 +2376,26 @@
},
"required" : [ "end", "start" ]
},
"Role" : {
"type" : "object",
"properties" : {
"datacenters" : {
"type" : "string"
},
"login" : {
"type" : "boolean"
},
"name" : {
"type" : "string"
},
"options" : {
"type" : "string"
},
"super" : {
"type" : "boolean"
}
}
},
"ScrubRequest" : {
"type" : "object",
"properties" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

import com.datastax.mgmtapi.ManagementApplication;
import com.datastax.mgmtapi.resources.common.BaseResources;
import com.datastax.mgmtapi.resources.models.Role;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -25,10 +27,10 @@
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;

@Path("/api/v0/ops/auth")
public class AuthResources extends BaseResources {
Expand Down Expand Up @@ -129,8 +131,7 @@ public Response dropRole(@QueryParam(value = "username") String name) {
content =
@Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = String.class),
examples = @ExampleObject(value = "OK")))
array = @ArraySchema(schema = @Schema(implementation = Role.class))))
@ApiResponse(
responseCode = "400",
description = "Username is empty",
Expand All @@ -142,18 +143,27 @@ public Response dropRole(@QueryParam(value = "username") String name) {
public Response listRoles() {
return handle(
() -> {
List<Role> roleResult = new ArrayList<>();

ResultSet result =
app.cqlService.executePreparedStatement(
app.dbUnixSocketFile, "CALL NodeOps.listRoles()");
Row row = result.one();
if (row != null) {
List<Map<String, String>> roles = row.get(0, LIST_OF_MAP_OF_STRINGS);

return Response.ok(jsonMapper.writeValueAsString(roles), MediaType.APPLICATION_JSON)
.build();
for (Map<String, String> role : roles) {
roleResult.add(
new Role(
role.get("name"),
Boolean.valueOf(role.get("super")),
Boolean.valueOf(role.get("login")),
role.get("datacenters"),
role.get("options")));
}
}

return Response.status(HttpStatus.SC_NO_CONTENT).build();
return Response.ok(roleResult, MediaType.APPLICATION_JSON).build();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright DataStax, Inc.
*
* Please see the included license file for details.
*/
package com.datastax.mgmtapi.resources.models;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Role {

@JsonProperty(value = "name")
private String name;

@JsonProperty(value = "super")
private boolean isSuperUser;

@JsonProperty(value = "login")
private boolean canLogin;

@JsonProperty(value = "datacenters")
private String datacenters;

@JsonProperty(value = "options")
private String options;

public Role(
String name, boolean isSuperUser, boolean canLogin, String datacenters, String options) {
this.name = name;
this.isSuperUser = isSuperUser;
this.canLogin = canLogin;
this.datacenters = datacenters;
this.options = options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,8 @@ public void testAuthMethods() throws Exception {
MockHttpRequest getRequest = MockHttpRequest.get(ROOT_PATH + "/ops/auth/role");
MockHttpResponse response = context.invoke(getRequest);

assertEquals(HttpStatus.SC_NO_CONTENT, response.getStatus());
assertEquals(HttpStatus.SC_OK, response.getStatus());
assertEquals("[]", response.getContentAsString());

verify(context.cqlService).executePreparedStatement(any(), eq("CALL NodeOps.listRoles()"));

Expand Down Expand Up @@ -2292,7 +2293,7 @@ public void testAuthMethods() throws Exception {
verify(context.cqlService).executePreparedStatement(any(), eq("CALL NodeOps.listRoles()"));

assertEquals(
"[{\"super\":\"true\",\"datacenters\":\"ALL\",\"name\":\"abc\",\"options\":\"{}\",\"login\":\"false\"}]",
"[{\"name\":\"abc\",\"super\":true,\"login\":false,\"datacenters\":\"ALL\",\"options\":\"{}\"}]",
response.getContentAsString());

MockHttpRequest deleteRequest =
Expand Down

0 comments on commit e2076b6

Please sign in to comment.