use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.
the class MongoDbRuleService method loadByName.
@Override
public RuleDao loadByName(String name) throws NotFoundException {
final DBQuery.Query query = DBQuery.is("title", name);
final RuleDao rule = dbCollection.findOne(query);
if (rule == null) {
throw new NotFoundException("No rule with name " + name);
}
return rule;
}
use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.
the class PipelineRuleFacade method findExisting.
private Optional<NativeEntity<RuleDao>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
final String title = ruleEntity.title().asString(parameters);
final String source = ruleEntity.source().asString(parameters);
try {
final RuleDao ruleDao = ruleService.loadByName(title);
compareRuleSources(title, source, ruleDao.source());
return Optional.of(NativeEntity.create(entity.id(), ruleDao.id(), TYPE_V1, ruleDao.title(), ruleDao));
} catch (NotFoundException e) {
return Optional.empty();
}
}
use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.
the class RuleResource method getPage.
@GET
@Path("/paginated")
@ApiOperation(value = "Get a paginated list of pipeline rules")
@Produces(MediaType.APPLICATION_JSON)
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_READ)
public PaginatedResponse<RuleSource> getPage(@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 = "title,description,id") @DefaultValue(RuleDao.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<RuleDao> result = paginatedRuleService.findPaginated(searchQuery, page, perPage, sort, order);
final List<RuleSource> ruleSourceList = result.stream().map(dao -> RuleSource.fromDao(pipelineRuleParser, dao)).collect(Collectors.toList());
final PaginatedList<RuleSource> rules = new PaginatedList<>(ruleSourceList, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
return PaginatedResponse.create("rules", rules, prepareContextForPaginatedResponse(result.delegate()));
}
use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.
the class RuleResource method update.
@ApiOperation(value = "Modify a processing rule", notes = "It can take up to a second until the change is applied")
@Path("/{id}")
@PUT
@AuditEvent(type = PipelineProcessorAuditEventTypes.RULE_UPDATE)
public RuleSource update(@ApiParam(name = "id") @PathParam("id") String id, @ApiParam(name = "rule", required = true) @NotNull RuleSource update) throws NotFoundException {
checkPermission(PipelineRestPermissions.PIPELINE_RULE_EDIT, id);
final RuleDao ruleDao = ruleService.load(id);
final Rule rule;
try {
rule = pipelineRuleParser.parseRule(id, update.source(), false);
} catch (ParseException e) {
throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
}
final RuleDao toSave = ruleDao.toBuilder().title(rule.name()).description(update.description()).source(update.source()).modifiedAt(DateTime.now(DateTimeZone.UTC)).build();
final RuleDao savedRule = ruleService.save(toSave);
return RuleSource.fromDao(pipelineRuleParser, savedRule);
}
use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.
the class RuleResource method createFromParser.
@ApiOperation(value = "Create a processing rule from source", notes = "")
@POST
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_CREATE)
@AuditEvent(type = PipelineProcessorAuditEventTypes.RULE_CREATE)
public RuleSource createFromParser(@ApiParam(name = "rule", required = true) @NotNull RuleSource ruleSource) throws ParseException {
final Rule rule;
try {
rule = pipelineRuleParser.parseRule(ruleSource.id(), ruleSource.source(), false);
} catch (ParseException e) {
throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
}
final DateTime now = DateTime.now(DateTimeZone.UTC);
final RuleDao newRuleSource = RuleDao.builder().title(// use the name from the parsed rule source.
rule.name()).description(ruleSource.description()).source(ruleSource.source()).createdAt(now).modifiedAt(now).build();
final RuleDao save = ruleService.save(newRuleSource);
log.debug("Created new rule {}", save);
return RuleSource.fromDao(pipelineRuleParser, save);
}
Aggregations