class SecretsResource {

  @Timed @ExceptionMetered
  @POST
  @Consumes(APPLICATION_JSON)
  public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) {

    logger.info("User '{}' creating secret '{}'.", user, request.name());

    Secret secret;
    try {
      SecretController.SecretBuilder builder =
          secretController.builder(request.name(), request.content(), user.getName(),
              request.expiry());

      if (request.description() != null) {
        builder.withDescription(request.description());
      }

      if (request.metadata() != null) {
        builder.withMetadata(request.metadata());
      }

      secret = builder.create();
    } catch (DataAccessException e) {
      logger.info(format("Cannot create secret %s", request.name()), e);
      throw new ConflictException(format("Cannot create secret %s.", request.name()));
    }

    URI uri =
        UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId());
    Response response = Response
        .created(uri)
        .entity(secretDetailResponseFromId(secret.getId()))
        .build();

    if (response.getStatus() == HttpStatus.SC_CREATED) {
      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());
      }
      extraInfo.put("expiry", Long.toString(request.expiry()));
      auditLog.recordEvent(
          new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(),
              extraInfo));
    }
    // TODO (jessep): Should we also log failures?

    return response;
  }

  @SuppressWarnings("unused") @Inject public  SecretsResource(SecretController secretController, AclDAOFactory aclDAOFactory,
      SecretDAOFactory secretDAOFactory, AuditLog auditLog);
  @VisibleForTesting  SecretsResource(SecretController secretController, AclDAO aclDAOReadOnly,
      SecretDAO secretDAOReadWrite, AuditLog auditLog);

}