Search in sources :

Example 11 with Configuration

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);
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 12 with Configuration

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();
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) ValidationResult(org.graylog2.plugin.rest.ValidationResult) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 13 with Configuration

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();
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 14 with Configuration

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);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) ConfigurationSummary(org.graylog.plugins.sidecar.rest.models.ConfigurationSummary) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 15 with Configuration

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;
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) RenderTemplateException(org.graylog.plugins.sidecar.template.RenderTemplateException) ValidationResult(org.graylog2.plugin.rest.ValidationResult)

Aggregations

Configuration (org.graylog.plugins.sidecar.rest.models.Configuration)18 ApiOperation (io.swagger.annotations.ApiOperation)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)8 Produces (javax.ws.rs.Produces)7 Path (javax.ws.rs.Path)6 GET (javax.ws.rs.GET)5 ValidationResult (org.graylog2.plugin.rest.ValidationResult)5 NotFoundException (javax.ws.rs.NotFoundException)4 AuditEvent (org.graylog2.audit.jersey.AuditEvent)4 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)4 POST (javax.ws.rs.POST)3 ConfigurationVariable (org.graylog.plugins.sidecar.rest.models.ConfigurationVariable)3 PUT (javax.ws.rs.PUT)2 Response (javax.ws.rs.core.Response)2 Sidecar (org.graylog.plugins.sidecar.rest.models.Sidecar)2 ModelId (org.graylog2.contentpacks.model.ModelId)2 Test (org.junit.Test)2 Timed (com.codahale.metrics.annotation.Timed)1 BasicDBObject (com.mongodb.BasicDBObject)1 Api (io.swagger.annotations.Api)1