use of org.graylog.plugins.sidecar.rest.models.Configuration in project graylog2-server by Graylog2.
the class ConfigurationResource method getConfigurationSidecars.
@GET
@Path("/{id}/sidecars")
@RequiresPermissions({ SidecarRestPermissions.CONFIGURATIONS_READ, SidecarRestPermissions.SIDECARS_READ })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Show sidecars using the given configuration")
public ConfigurationSidecarsResponse getConfigurationSidecars(@ApiParam(name = "id", required = true) @PathParam("id") String id) {
final Configuration configuration = this.configurationService.find(id);
if (configuration == null) {
throw new NotFoundException("Could not find Configuration <" + id + ">.");
}
final List<String> sidecarsWithConfiguration = sidecarService.all().stream().filter(sidecar -> isConfigurationAssignedToSidecar(configuration.id(), sidecar)).map(Sidecar::id).collect(Collectors.toList());
return ConfigurationSidecarsResponse.create(configuration.id(), sidecarsWithConfiguration);
}
use of org.graylog.plugins.sidecar.rest.models.Configuration 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.graylog.plugins.sidecar.rest.models.Configuration 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.graylog.plugins.sidecar.rest.models.Configuration in project graylog2-server by Graylog2.
the class ConfigurationResource method listConfigurations.
@GET
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List all configurations")
public ConfigurationListResponse listConfigurations(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "name,id,collector_id") @DefaultValue(Configuration.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<Configuration> configurations = this.configurationService.findPaginated(searchQuery, page, perPage, sort, order);
final long total = this.configurationService.count();
final List<ConfigurationSummary> result = configurations.stream().map(ConfigurationSummary::create).collect(Collectors.toList());
return ConfigurationListResponse.create(query, configurations.pagination(), total, sort, order, result);
}
use of org.graylog.plugins.sidecar.rest.models.Configuration 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