use of org.graylog2.grok.GrokPatternService.ImportStrategy in project graylog2-server by Graylog2.
the class GrokResource method bulkUpdatePatterns.
@PUT
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatterns(@ApiParam(name = "patterns", required = true) @NotNull GrokPatternList patternList, // deprecated. used to drop all existing patterns before import
@Deprecated @QueryParam("replace") @DefaultValue("false") boolean deprecatedDropAllExisting, @ApiParam(name = "import-strategy", value = "Strategy to apply when importing.") @QueryParam("import-strategy") ImportStrategy importStrategy) throws ValidationException {
checkPermission(RestPermissions.INPUTS_CREATE);
try {
if (!grokPatternService.validateAll(patternList.patterns())) {
throw new ValidationException("Invalid pattern contained. Did not save any patterns.");
}
} catch (GrokException | IllegalArgumentException e) {
throw new ValidationException("Invalid pattern. Did not save any patterns\n" + e.getMessage());
}
ImportStrategy resolvedStrategy = importStrategy != null ? importStrategy : deprecatedDropAllExisting ? ImportStrategy.DROP_ALL_EXISTING : ImportStrategy.ABORT_ON_CONFLICT;
grokPatternService.saveAll(patternList.patterns(), resolvedStrategy);
return Response.accepted().build();
}
use of org.graylog2.grok.GrokPatternService.ImportStrategy in project graylog2-server by Graylog2.
the class GrokResource method bulkUpdatePatternsFromTextFile.
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatternsFromTextFile(@ApiParam(name = "patterns", required = true) @NotNull InputStream patternsFile, // deprecated. used to drop all existing patterns before import
@Deprecated @QueryParam("replace") @DefaultValue("false") boolean deprecatedDropAllExisting, @ApiParam(name = "import-strategy", value = "Strategy to apply when importing.") @QueryParam("import-strategy") ImportStrategy importStrategy) throws ValidationException, IOException {
checkPermission(RestPermissions.INPUTS_CREATE);
final List<GrokPattern> grokPatterns = readGrokPatterns(patternsFile);
if (!grokPatterns.isEmpty()) {
try {
if (!grokPatternService.validateAll(grokPatterns)) {
throw new ValidationException("Invalid pattern contained. Did not save any patterns.");
}
} catch (GrokException | IllegalArgumentException e) {
throw new ValidationException("Invalid pattern. Did not save any patterns\n" + e.getMessage());
}
ImportStrategy resolvedStrategy = importStrategy != null ? importStrategy : deprecatedDropAllExisting ? ImportStrategy.DROP_ALL_EXISTING : ImportStrategy.ABORT_ON_CONFLICT;
grokPatternService.saveAll(grokPatterns, resolvedStrategy);
}
return Response.accepted().build();
}
use of org.graylog2.grok.GrokPatternService.ImportStrategy in project graylog2-server by Graylog2.
the class MongoDbGrokPatternService method saveAll.
@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
final Map<String, GrokPattern> newPatternsByName;
try {
newPatternsByName = patterns.stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
} catch (IllegalStateException e) {
throw new ValidationException("The supplied Grok patterns contain conflicting names: " + e.getLocalizedMessage());
}
final Map<String, GrokPattern> existingPatternsByName = loadAll().stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
if (importStrategy == ABORT_ON_CONFLICT) {
final Sets.SetView<String> conflictingNames = Sets.intersection(newPatternsByName.keySet(), existingPatternsByName.keySet());
if (!conflictingNames.isEmpty()) {
final Iterable<String> limited = Iterables.limit(conflictingNames, MAX_DISPLAYED_CONFLICTS);
throw new ValidationException("The following Grok patterns already exist: " + StringUtils.join(limited, ", ") + (conflictingNames.size() > MAX_DISPLAYED_CONFLICTS ? " (+ " + (conflictingNames.size() - MAX_DISPLAYED_CONFLICTS) + " more)" : "") + ".");
}
}
try {
if (!validateAll(patterns)) {
throw new ValidationException("Invalid patterns");
}
} catch (GrokException | PatternSyntaxException e) {
throw new ValidationException("Invalid patterns.\n" + e.getMessage());
}
if (importStrategy == DROP_ALL_EXISTING) {
deleteAll();
}
final List<GrokPattern> savedPatterns = patterns.stream().map(newPattern -> {
final GrokPattern existingPattern = existingPatternsByName.get(newPattern.name());
if (existingPattern != null) {
return newPattern.toBuilder().id(existingPattern.id()).build();
} else {
return newPattern;
}
}).map(dbCollection::save).map(WriteResult::getSavedObject).collect(Collectors.toList());
clusterBus.post(GrokPatternsUpdatedEvent.create(newPatternsByName.keySet()));
return savedPatterns;
}
use of org.graylog2.grok.GrokPatternService.ImportStrategy in project graylog2-server by Graylog2.
the class InMemoryGrokPatternService method saveAll.
@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
if (importStrategy == ABORT_ON_CONFLICT) {
for (GrokPattern pattern : loadAll()) {
final boolean patternExists = patterns.stream().anyMatch(p -> p.name().equals(pattern.name()));
if (patternExists) {
throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
}
}
}
try {
if (!validateAll(patterns)) {
throw new ValidationException("Patterns invalid.");
}
} catch (GrokException | PatternSyntaxException e) {
throw new ValidationException("Invalid patterns.\n" + e.getMessage());
}
if (importStrategy == DROP_ALL_EXISTING) {
deleteAll();
}
final List<GrokPattern> grokPatterns = patterns.stream().map(this::uncheckedSave).collect(Collectors.toList());
final Set<String> patternNames = grokPatterns.stream().map(GrokPattern::name).collect(Collectors.toSet());
if (!patternNames.isEmpty()) {
clusterBus.post(GrokPatternsUpdatedEvent.create(patternNames));
}
return grokPatterns;
}
Aggregations