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);
}
}
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());
}
}
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);
}
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);
}
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);
}
}
Aggregations