Skip to content

Commit

Permalink
Add "replace" query param to Grok bulk import REST API endpoint.
Browse files Browse the repository at this point in the history
Existing Grok patterns will be removed before adding the new ones if set
to true.

Fixes #937
Refs graylog-labs/graylog2-web-interface#1150
  • Loading branch information
bernd committed Mar 5, 2015
1 parent b880f19 commit ff1ddb6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Expand Up @@ -130,8 +130,9 @@ public void deleteGrokPattern(GrokPattern pattern) throws APIException, IOExcept
api.path(routes.GrokResource().removePattern(pattern.id)).execute();
}

public void bulkLoadGrokPatterns(Collection<GrokPattern> patterns) throws APIException, IOException {
public void bulkLoadGrokPatterns(Collection<GrokPattern> patterns, boolean replace) throws APIException, IOException {
api.path(routes.GrokResource().bulkUpdatePatterns())
.queryParam("replace", String.valueOf(replace))
.body(new GrokPatternUpdateRequest(patterns))
.execute();

Expand Down
Expand Up @@ -33,4 +33,6 @@ public interface GrokPatternService {
boolean validate(GrokPattern pattern);

int delete(String patternName);

void deleteAll();
}
Expand Up @@ -98,4 +98,9 @@ public boolean validate(GrokPattern pattern) {
public int delete(String patternId) {
return dbCollection.removeById(new ObjectId(patternId)).getN();
}

@Override
public void deleteAll() {
dbCollection.drop();
}
}
Expand Up @@ -40,6 +40,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
Expand Down Expand Up @@ -96,14 +97,19 @@ public Response createPattern(@ApiParam(name = "pattern", required = true)
@PUT
@Timed
@ApiOperation("Add a list of new patterns")
public Response bulkUpdatePatterns(@ApiParam(name = "patterns", required = true) @NotNull GrokPatternList patternList) throws ValidationException {
public Response bulkUpdatePatterns(@ApiParam(name = "patterns", required = true) @NotNull GrokPatternList patternList,
@ApiParam(name = "replace", value = "Replace all patterns with the new ones.")
@QueryParam("replace") boolean replace) throws ValidationException {
checkPermission(RestPermissions.INPUTS_CREATE);

for (final GrokPattern pattern : patternList.patterns()) {
if (!grokPatternService.validate(pattern)) {
throw new ValidationException("Invalid pattern " + pattern + ". Did not save any patterns.");
}
}
if (replace) {
grokPatternService.deleteAll();
}
for (final GrokPattern pattern : patternList.patterns()) {
grokPatternService.save(pattern);
}
Expand Down

0 comments on commit ff1ddb6

Please sign in to comment.