Search in sources :

Example 1 with ConfigurationAssignment

use of org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment in project graylog2-server by Graylog2.

the class SidecarResource method assignConfiguration.

@PUT
@Timed
@Path("/configurations")
@ApiOperation(value = "Assign configurations to sidecars")
@RequiresPermissions(SidecarRestPermissions.SIDECARS_UPDATE)
@AuditEvent(type = SidecarAuditEventTypes.SIDECAR_UPDATE)
public Response assignConfiguration(@ApiParam(name = "JSON body", required = true) @Valid @NotNull NodeConfigurationRequest request) throws NotFoundException {
    List<String> nodeIdList = request.nodes().stream().filter(distinctByKey(NodeConfiguration::nodeId)).map(NodeConfiguration::nodeId).collect(Collectors.toList());
    for (String nodeId : nodeIdList) {
        List<ConfigurationAssignment> nodeRelations = request.nodes().stream().filter(a -> a.nodeId().equals(nodeId)).flatMap(a -> a.assignments().stream()).collect(Collectors.toList());
        try {
            Sidecar sidecar = sidecarService.assignConfiguration(nodeId, nodeRelations);
            sidecarService.save(sidecar);
        } catch (org.graylog2.database.NotFoundException e) {
            throw new NotFoundException(e.getMessage());
        }
    }
    return Response.accepted().build();
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) Produces(javax.ws.rs.Produces) SidecarService(org.graylog.plugins.sidecar.services.SidecarService) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) NodeConfiguration(org.graylog.plugins.sidecar.rest.models.NodeConfiguration) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SidecarConfiguration(org.graylog.plugins.sidecar.system.SidecarConfiguration) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) Map(java.util.Map) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) BadRequestException(javax.ws.rs.BadRequestException) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) RegistrationRequest(org.graylog.plugins.sidecar.rest.requests.RegistrationRequest) List(java.util.List) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) Response(javax.ws.rs.core.Response) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) PathParam(javax.ws.rs.PathParam) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) SidecarRegistrationConfiguration(org.graylog.plugins.sidecar.rest.models.SidecarRegistrationConfiguration) NodeConfigurationRequest(org.graylog.plugins.sidecar.rest.requests.NodeConfigurationRequest) ApiResponses(io.swagger.annotations.ApiResponses) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RegistrationResponse(org.graylog.plugins.sidecar.rest.responses.RegistrationResponse) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SidecarRestPermissions(org.graylog.plugins.sidecar.permissions.SidecarRestPermissions) SearchQuery(org.graylog2.search.SearchQuery) ActiveSidecarFilter(org.graylog.plugins.sidecar.filter.ActiveSidecarFilter) DateTime(org.joda.time.DateTime) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) RestResource(org.graylog2.shared.rest.resources.RestResource) SidecarListResponse(org.graylog.plugins.sidecar.rest.responses.SidecarListResponse) ActionService(org.graylog.plugins.sidecar.services.ActionService) ApiResponse(io.swagger.annotations.ApiResponse) NotEmpty(org.hibernate.validator.constraints.NotEmpty) PUT(javax.ws.rs.PUT) SidecarStatusMapper(org.graylog.plugins.sidecar.mapper.SidecarStatusMapper) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) NotFoundException(javax.ws.rs.NotFoundException) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 2 with ConfigurationAssignment

use of org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment in project graylog2-server by Graylog2.

the class SidecarResource method register.

@PUT
@Timed
@Path("/{sidecarId}")
@ApiOperation(value = "Create/update a Sidecar registration", notes = "This is a stateless method which upserts a Sidecar registration")
@ApiResponses(value = { @ApiResponse(code = 400, message = "The supplied request is not valid.") })
@RequiresPermissions(SidecarRestPermissions.SIDECARS_UPDATE)
@NoAuditEvent("this is only a ping from Sidecars, and would overflow the audit log")
public Response register(@ApiParam(name = "sidecarId", value = "The id this Sidecar is registering as.", required = true) @PathParam("sidecarId") @NotEmpty String sidecarId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull RegistrationRequest request, @HeaderParam(value = "X-Graylog-Sidecar-Version") @NotEmpty String sidecarVersion) {
    final Sidecar newSidecar;
    final Sidecar oldSidecar = sidecarService.findByNodeId(sidecarId);
    List<ConfigurationAssignment> assignments = null;
    if (oldSidecar != null) {
        assignments = oldSidecar.assignments();
        newSidecar = oldSidecar.toBuilder().nodeName(request.nodeName()).nodeDetails(request.nodeDetails()).sidecarVersion(sidecarVersion).lastSeen(DateTime.now(DateTimeZone.UTC)).build();
    } else {
        newSidecar = sidecarService.fromRequest(sidecarId, request, sidecarVersion);
    }
    sidecarService.save(newSidecar);
    final CollectorActions collectorActions = actionService.findActionBySidecar(sidecarId, true);
    List<CollectorAction> collectorAction = null;
    if (collectorActions != null) {
        collectorAction = collectorActions.action();
    }
    RegistrationResponse sidecarRegistrationResponse = RegistrationResponse.create(SidecarRegistrationConfiguration.create(this.sidecarConfiguration.sidecarUpdateInterval().toStandardDuration().getStandardSeconds(), this.sidecarConfiguration.sidecarSendStatus()), this.sidecarConfiguration.sidecarConfigurationOverride(), collectorAction, assignments);
    return Response.accepted(sidecarRegistrationResponse).build();
}
Also used : CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) RegistrationResponse(org.graylog.plugins.sidecar.rest.responses.RegistrationResponse) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 3 with ConfigurationAssignment

use of org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment in project graylog2-server by Graylog2.

the class SidecarService method assignConfiguration.

public Sidecar assignConfiguration(String collectorNodeId, List<ConfigurationAssignment> assignments) throws NotFoundException {
    Sidecar sidecar = findByNodeId(collectorNodeId);
    if (sidecar == null) {
        throw new NotFoundException("Couldn't find collector with ID " + collectorNodeId);
    }
    for (ConfigurationAssignment assignment : assignments) {
        Collector collector = collectorService.find(assignment.collectorId());
        if (collector == null) {
            throw new NotFoundException("Couldn't find collector with ID " + assignment.collectorId());
        }
        Configuration configuration = configurationService.find(assignment.configurationId());
        if (configuration == null) {
            throw new NotFoundException("Couldn't find configuration with ID " + assignment.configurationId());
        }
        if (!configuration.collectorId().equals(collector.id())) {
            throw new NotFoundException("Configuration doesn't match collector ID " + assignment.collectorId());
        }
    }
    Sidecar toSave = sidecar.toBuilder().assignments(assignments).build();
    return save(toSave);
}
Also used : ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) Collector(org.graylog.plugins.sidecar.rest.models.Collector) NotFoundException(org.graylog2.database.NotFoundException) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar)

Aggregations

Sidecar (org.graylog.plugins.sidecar.rest.models.Sidecar)3 ConfigurationAssignment (org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment)3 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 CollectorAction (org.graylog.plugins.sidecar.rest.models.CollectorAction)2 CollectorActions (org.graylog.plugins.sidecar.rest.models.CollectorActions)2 RegistrationResponse (org.graylog.plugins.sidecar.rest.responses.RegistrationResponse)2 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)2 Function (com.google.common.base.Function)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Api (io.swagger.annotations.Api)1 ApiParam (io.swagger.annotations.ApiParam)1 ApiResponse (io.swagger.annotations.ApiResponse)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1