use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class ConfigurationVariableResource method deleteConfigurationVariable.
@DELETE
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Delete a configuration variable")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_VARIABLE_DELETE)
public Response deleteConfigurationVariable(@ApiParam(name = "id", required = true) @PathParam("id") String id) {
final ConfigurationVariable configurationVariable = findVariableOrFail(id);
final List<Configuration> configurations = this.configurationService.findByConfigurationVariable(configurationVariable);
if (!configurations.isEmpty()) {
final ValidationResult validationResult = new ValidationResult();
validationResult.addError("name", "Variable is still used in the following configurations: " + configurations.stream().map(c -> c.name()).collect(Collectors.joining(", ")));
return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
}
int deleted = configurationVariableService.delete(id);
if (deleted == 0) {
return Response.notModified().build();
}
etagService.invalidateAll();
return Response.accepted().build();
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class ConfigurationVariableResource method createConfigurationVariable.
@POST
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new configuration variable")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_VARIABLE_CREATE)
public Response createConfigurationVariable(@ApiParam(name = "JSON body", required = true) @Valid @NotNull ConfigurationVariable request) {
ValidationResult validationResult = validateConfigurationVariableHelper(request);
if (validationResult.failed()) {
return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
}
final ConfigurationVariable configurationVariable = persistConfigurationVariable(null, request);
return Response.ok().entity(configurationVariable).build();
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class ConfigurationResource method createConfiguration.
@POST
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new configuration")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_CREATE)
public Response createConfiguration(@ApiParam(name = "JSON body", required = true) @Valid @NotNull Configuration request) {
final Configuration configuration = configurationFromRequest(null, request);
final ValidationResult validationResult = validate(configuration);
if (validationResult.failed()) {
return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
}
return Response.ok().entity(configurationService.save(configuration)).build();
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class ConfigurationResource method updateConfiguration.
@PUT
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a configuration")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_UPDATE)
public Response updateConfiguration(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "JSON body", required = true) @Valid @NotNull Configuration request) {
final Configuration previousConfiguration = configurationService.find(id);
if (previousConfiguration == null) {
throw new NotFoundException("Could not find Configuration <" + id + ">.");
}
// Only allow changing the associated collector ID if the configuration is not in use
if (!previousConfiguration.collectorId().equals(request.collectorId())) {
if (isConfigurationInUse(id)) {
throw new BadRequestException("Configuration still in use, cannot change collector type.");
}
}
final Configuration updatedConfiguration = configurationFromRequest(id, request);
final ValidationResult validationResult = validate(updatedConfiguration);
if (validationResult.failed()) {
return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
}
etagService.invalidateAll();
return Response.ok().entity(configurationService.save(updatedConfiguration)).build();
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class ConfigurationResource method validate.
private ValidationResult validate(Configuration toValidate) {
final Optional<Configuration> configurationOptional;
final Configuration configuration;
final ValidationResult validation = new ValidationResult();
configurationOptional = Optional.ofNullable(configurationService.findByName(toValidate.name()));
if (configurationOptional.isPresent()) {
configuration = configurationOptional.get();
if (!configuration.id().equals(toValidate.id())) {
// a configuration exists with a different id, so the name is already in use, fail validation
validation.addError("name", "Configuration \"" + toValidate.name() + "\" already exists");
}
}
if (toValidate.name().isEmpty()) {
validation.addError("name", "Configuration name cannot be empty.");
} else if (!VALID_NAME_PATTERN.matcher(toValidate.name()).matches()) {
validation.addError("name", "Configuration name can not include the following characters: ; * ? \" < > | &");
}
if (toValidate.collectorId().isEmpty()) {
validation.addError("collector_id", "Associated collector ID cannot be empty.");
}
if (toValidate.color().isEmpty()) {
validation.addError("color", "Collector color cannot be empty.");
}
if (toValidate.template().isEmpty()) {
validation.addError("template", "Collector template cannot be empty.");
}
try {
this.configurationService.renderPreview(toValidate.template());
} catch (RenderTemplateException e) {
validation.addError("template", "Template error: " + e.getMessage());
}
return validation;
}
Aggregations