use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.
the class ClientResource method createClient.
/**
* Creates a client and assigns to given groups
*
* @excludeParams automationClient
* @param request JSON request to create a client
*
* @responseMessage 201 Created client and assigned to given groups
* @responseMessage 409 Client already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createClient(@Auth AutomationClient automationClient, @Valid CreateClientRequestV2 request) {
String creator = automationClient.getName();
String client = request.name();
clientDAOReadWrite.getClient(client).ifPresent((c) -> {
logger.info("Automation ({}) - Client {} already exists", creator, client);
throw new ConflictException("Client name already exists.");
});
// Creates new client record
long clientId = clientDAOReadWrite.createClient(client, creator, request.description());
auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, creator, client));
// Enrolls client in any requested groups
groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAOReadWrite.findAndEnrollClient(clientId, groupId, auditLog, creator, new HashMap<>())));
URI uri = UriBuilder.fromResource(ClientResource.class).path(client).build();
return Response.created(uri).build();
}
use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.
the class GroupResource method createGroup.
/**
* Creates a group
*
* @excludeParams automationClient
* @param request JSON request to create a group
*
* @responseMessage 201 Created group
* @responseMessage 409 Group already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createGroup(@Auth AutomationClient automationClient, @Valid CreateGroupRequestV2 request) {
String creator = automationClient.getName();
String group = request.name();
groupDAOReadWrite.getGroup(group).ifPresent((g) -> {
logger.info("Automation ({}) - Group {} already exists", creator, group);
throw new ConflictException(format("Group %s already exists", group));
});
groupDAOReadWrite.createGroup(group, creator, request.description(), request.metadata());
Map<String, String> extraInfo = new HashMap<>();
if (request.description() != null) {
extraInfo.put("description", request.description());
}
if (request.metadata() != null) {
extraInfo.put("metadata", request.metadata().toString());
}
auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_CREATE, creator, group, extraInfo));
URI uri = UriBuilder.fromResource(GroupResource.class).path(group).build();
return Response.created(uri).build();
}
use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.
the class ClientsResource method createClient.
/**
* Create Client
*
* @excludeParams user
* @param createClientRequest the JSON client request used to formulate the Client
*
* @description Creates a Client with the name from a valid client request.
* Used by Keywhiz CLI and the web ui.
* @responseMessage 200 Successfully created Client
* @responseMessage 409 Client with given name already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createClient(@Auth User user, @Valid CreateClientRequest createClientRequest) {
logger.info("User '{}' creating client '{}'.", user, createClientRequest.name);
long clientId;
try {
clientId = clientDAO.createClient(createClientRequest.name, user.getName(), "");
} catch (DataAccessException e) {
logger.warn("Cannot create client {}: {}", createClientRequest.name, e);
throw new ConflictException("Conflict creating client.");
}
URI uri = UriBuilder.fromResource(ClientsResource.class).path("{clientId}").build(clientId);
Response response = Response.created(uri).entity(clientDetailResponseFromId(clientId)).build();
if (response.getStatus() == HttpStatus.SC_CREATED) {
auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, user.getName(), createClientRequest.name));
}
return response;
}
Aggregations