Search in sources :

Example 1 with DataAdapterApi

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

the class LookupTableResource method adapters.

@GET
@Path("adapters")
@ApiOperation(value = "List available data adapters")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public DataAdapterPage adapters(@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(DataAdapterDto.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 (!ADAPTER_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = DataAdapterDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = adapterSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<DataAdapterDto> paginated = dbDataAdapterService.findPaginated(dbQuery, sortBuilder, page, perPage);
        return new DataAdapterPage(query, paginated.pagination(), paginated.stream().map(DataAdapterApi::fromDto).collect(Collectors.toList()));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) DataAdapterApi(org.graylog2.rest.models.system.lookup.DataAdapterApi) 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 2 with DataAdapterApi

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

the class LookupTableResource method createAdapter.

@POST
@Path("adapters")
@AuditEvent(type = AuditEventTypes.LOOKUP_ADAPTER_CREATE)
@ApiOperation(value = "Create a new data adapter")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_CREATE)
public DataAdapterApi createAdapter(@Valid @ApiParam DataAdapterApi newAdapter) {
    try {
        DataAdapterDto dto = newAdapter.toDto();
        DataAdapterDto saved = dbDataAdapterService.save(dto);
        return DataAdapterApi.fromDto(saved);
    } catch (DuplicateKeyException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) BadRequestException(javax.ws.rs.BadRequestException) DuplicateKeyException(com.mongodb.DuplicateKeyException) 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) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 3 with DataAdapterApi

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

the class LookupTableResource method deleteAdapter.

@DELETE
@Path("adapters/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_ADAPTER_DELETE)
@ApiOperation(value = "Delete the given data adapter", notes = "The data adapter cannot be in use by any lookup table, otherwise the request will fail.")
public DataAdapterApi deleteAdapter(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName) {
    Optional<DataAdapterDto> dataAdapterDto = dbDataAdapterService.get(idOrName);
    if (!dataAdapterDto.isPresent()) {
        throw new NotFoundException();
    }
    DataAdapterDto dto = dataAdapterDto.get();
    checkPermission(RestPermissions.LOOKUP_TABLES_DELETE, dto.id());
    boolean unused = dbTableService.findByDataAdapterIds(singleton(dto.id())).isEmpty();
    if (!unused) {
        throw new BadRequestException("The adapter is still in use, cannot delete.");
    }
    dbDataAdapterService.delete(idOrName);
    return DataAdapterApi.fromDto(dto);
}
Also used : DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) 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 4 with DataAdapterApi

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

the class LookupTableResource method updateAdapter.

@PUT
@Path("adapters/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_ADAPTER_UPDATE)
@ApiOperation(value = "Update the given data adapter settings")
public DataAdapterApi updateAdapter(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @Valid @ApiParam DataAdapterApi toUpdate) {
    checkLookupAdapterId(idOrName, toUpdate);
    checkPermission(RestPermissions.LOOKUP_TABLES_EDIT, toUpdate.id());
    DataAdapterDto saved = dbDataAdapterService.save(toUpdate.toDto());
    return DataAdapterApi.fromDto(saved);
}
Also used : DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) 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 5 with DataAdapterApi

use of org.graylog2.rest.models.system.lookup.DataAdapterApi 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)7 Path (javax.ws.rs.Path)7 DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)5 BadRequestException (javax.ws.rs.BadRequestException)4 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)4 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)4 GET (javax.ws.rs.GET)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 DataAdapterApi (org.graylog2.rest.models.system.lookup.DataAdapterApi)3 NotFoundException (javax.ws.rs.NotFoundException)2 POST (javax.ws.rs.POST)2 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)2 CacheApi (org.graylog2.rest.models.system.lookup.CacheApi)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 DuplicateKeyException (com.mongodb.DuplicateKeyException)1