Search in sources :

Example 1 with CacheApi

use of org.graylog2.rest.models.system.lookup.CacheApi in project graylog2-server by Graylog2.

the class LookupTableResource method validateCache.

@POST
@Path("caches/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the cache config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateCache(@Valid @ApiParam CacheApi toValidate) {
    final ValidationResult validation = new ValidationResult();
    final Optional<CacheDto> dtoOptional = dbCacheService.get(toValidate.name());
    if (dtoOptional.isPresent()) {
        // a cache exist with the given name, check that the IDs are the same, this might be an update
        final CacheDto cacheDto = dtoOptional.get();
        // noinspection ConstantConditions
        if (!cacheDto.id().equals(toValidate.id())) {
            // a ache exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "The cache name is already in use.");
        }
    }
    final Optional<Multimap<String, String>> configValidations = toValidate.config().validate();
    configValidations.ifPresent(validation::addAll);
    return validation;
}
Also used : Multimap(com.google.common.collect.Multimap) ValidationResult(org.graylog2.plugin.rest.ValidationResult) CacheDto(org.graylog2.lookup.dto.CacheDto) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 2 with CacheApi

use of org.graylog2.rest.models.system.lookup.CacheApi in project graylog2-server by Graylog2.

the class LookupTableResource method caches.

@GET
@Path("caches")
@ApiOperation(value = "List available caches")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public CachesPage caches(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,name,id") @DefaultValue(CacheDto.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("desc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query) {
    if (!CACHE_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = CacheDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = cacheSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<CacheDto> paginated = dbCacheService.findPaginated(dbQuery, sortBuilder, page, perPage);
        return new CachesPage(query, paginated.pagination(), paginated.stream().map(CacheApi::fromDto).collect(Collectors.toList()));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) CacheApi(org.graylog2.rest.models.system.lookup.CacheApi) CacheDto(org.graylog2.lookup.dto.CacheDto) DBSort(org.mongojack.DBSort) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with CacheApi

use of org.graylog2.rest.models.system.lookup.CacheApi in project graylog2-server by Graylog2.

the class LookupTableResource method updateCache.

@PUT
@Path("caches/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_CACHE_UPDATE)
@ApiOperation(value = "Update the given cache settings")
public CacheApi updateCache(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @ApiParam CacheApi toUpdate) {
    checkLookupCacheId(idOrName, toUpdate);
    checkPermission(RestPermissions.LOOKUP_TABLES_EDIT, toUpdate.id());
    CacheDto saved = dbCacheService.save(toUpdate.toDto());
    return CacheApi.fromDto(saved);
}
Also used : CacheDto(org.graylog2.lookup.dto.CacheDto) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 4 with CacheApi

use of org.graylog2.rest.models.system.lookup.CacheApi in project graylog2-server by Graylog2.

the class LookupTableResource method deleteCache.

@DELETE
@Path("caches/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_CACHE_DELETE)
@ApiOperation(value = "Delete the given cache", notes = "The cache cannot be in use by any lookup table, otherwise the request will fail.")
public CacheApi deleteCache(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName) {
    Optional<CacheDto> cacheDto = dbCacheService.get(idOrName);
    if (!cacheDto.isPresent()) {
        throw new NotFoundException();
    }
    CacheDto dto = cacheDto.get();
    checkPermission(RestPermissions.LOOKUP_TABLES_DELETE, dto.id());
    boolean unused = dbTableService.findByCacheIds(singleton(dto.id())).isEmpty();
    if (!unused) {
        throw new BadRequestException("The cache is still in use, cannot delete.");
    }
    dbCacheService.delete(idOrName);
    return CacheApi.fromDto(dto);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) CacheDto(org.graylog2.lookup.dto.CacheDto) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 5 with CacheApi

use of org.graylog2.rest.models.system.lookup.CacheApi in project graylog2-server by Graylog2.

the class LookupTableResource method tables.

@GET
@Path("tables")
@ApiOperation(value = "List configured lookup tables")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public LookupTablePage tables(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,name,id") @DefaultValue(LookupTableDto.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("desc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query, @ApiParam(name = "resolve") @QueryParam("resolve") @DefaultValue("false") boolean resolveObjects) {
    if (!LUT_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = LookupTableDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = lutSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<LookupTableDto> paginated = dbTableService.findPaginated(dbQuery, sortBuilder, page, perPage);
        ImmutableSet.Builder<CacheApi> caches = ImmutableSet.builder();
        ImmutableSet.Builder<DataAdapterApi> dataAdapters = ImmutableSet.builder();
        if (resolveObjects) {
            ImmutableSet.Builder<String> cacheIds = ImmutableSet.builder();
            ImmutableSet.Builder<String> dataAdapterIds = ImmutableSet.builder();
            paginated.forEach(dto -> {
                cacheIds.add(dto.cacheId());
                dataAdapterIds.add(dto.dataAdapterId());
            });
            dbCacheService.findByIds(cacheIds.build()).forEach(cacheDto -> caches.add(CacheApi.fromDto(cacheDto)));
            dbDataAdapterService.findByIds(dataAdapterIds.build()).forEach(dataAdapterDto -> dataAdapters.add(DataAdapterApi.fromDto(dataAdapterDto)));
        }
        return new LookupTablePage(query, paginated.pagination(), paginated.stream().map(LookupTableApi::fromDto).collect(Collectors.toList()), caches.build(), dataAdapters.build());
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) LookupTableApi(org.graylog2.rest.models.system.lookup.LookupTableApi) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) DataAdapterApi(org.graylog2.rest.models.system.lookup.DataAdapterApi) DBSort(org.mongojack.DBSort) ImmutableSet(com.google.common.collect.ImmutableSet) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) CacheApi(org.graylog2.rest.models.system.lookup.CacheApi) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)6 Path (javax.ws.rs.Path)6 CacheDto (org.graylog2.lookup.dto.CacheDto)4 BadRequestException (javax.ws.rs.BadRequestException)3 GET (javax.ws.rs.GET)3 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 CacheApi (org.graylog2.rest.models.system.lookup.CacheApi)3 NotFoundException (javax.ws.rs.NotFoundException)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)2 DataAdapterApi (org.graylog2.rest.models.system.lookup.DataAdapterApi)2 LookupTableApi (org.graylog2.rest.models.system.lookup.LookupTableApi)2 SearchQuery (org.graylog2.search.SearchQuery)2 DBQuery (org.mongojack.DBQuery)2 DBSort (org.mongojack.DBSort)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Multimap (com.google.common.collect.Multimap)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1