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();
}
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();
}
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);
}
Aggregations