use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.
the class LookupTableFacade method decode.
private NativeEntity<LookupTableDto> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities) {
final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
final String referencedDataAdapterName = lookupTableEntity.dataAdapterName().asString(parameters);
final EntityDescriptor dataAdapterDescriptor = adapterDescriptor(referencedDataAdapterName);
final Object dataAdapter = nativeEntities.get(dataAdapterDescriptor);
final String dataAdapterId;
if (dataAdapter instanceof DataAdapterDto) {
dataAdapterId = ((DataAdapterDto) dataAdapter).id();
} else {
throw new MissingNativeEntityException(dataAdapterDescriptor);
}
final String referencedCacheName = lookupTableEntity.cacheName().asString(parameters);
final EntityDescriptor cacheDescriptor = cacheDescriptor(referencedCacheName);
final Object cache = nativeEntities.get(cacheDescriptor);
final String cacheId;
if (cache instanceof CacheDto) {
cacheId = ((CacheDto) cache).id();
} else {
throw new MissingNativeEntityException(cacheDescriptor);
}
final LookupTableDto lookupTableDto = LookupTableDto.builder().name(lookupTableEntity.name().asString(parameters)).title(lookupTableEntity.title().asString(parameters)).description(lookupTableEntity.description().asString(parameters)).dataAdapterId(dataAdapterId).cacheId(cacheId).defaultSingleValue(lookupTableEntity.defaultSingleValue().asString(parameters)).defaultSingleValueType(lookupTableEntity.defaultSingleValueType().asEnum(parameters, LookupDefaultSingleValue.Type.class)).defaultMultiValue(lookupTableEntity.defaultMultiValue().asString(parameters)).defaultMultiValueType(lookupTableEntity.defaultMultiValueType().asEnum(parameters, LookupDefaultMultiValue.Type.class)).build();
final LookupTableDto savedLookupTableDto = lookupTableService.save(lookupTableDto);
return NativeEntity.create(entity.id(), savedLookupTableDto.id(), TYPE_V1, lookupTableDto.title(), savedLookupTableDto);
}
use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.
the class LookupDataAdapterFacade method findExisting.
private Optional<NativeEntity<DataAdapterDto>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
final LookupDataAdapterEntity dataAdapterEntity = objectMapper.convertValue(entity.data(), LookupDataAdapterEntity.class);
final String name = dataAdapterEntity.name().asString(parameters);
final Optional<DataAdapterDto> existingDataAdapter = dataAdapterService.get(name);
return existingDataAdapter.map(dataAdapter -> NativeEntity.create(entity.id(), dataAdapter.id(), TYPE_V1, dataAdapter.title(), dataAdapter));
}
use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.
the class LookupDataAdapterFacade method exportNativeEntity.
@VisibleForTesting
Entity exportNativeEntity(DataAdapterDto dataAdapterDto, EntityDescriptorIds entityDescriptorIds) {
// TODO: Create independent representation of entity?
final Map<String, Object> configuration = objectMapper.convertValue(dataAdapterDto.config(), TypeReferences.MAP_STRING_OBJECT);
final LookupDataAdapterEntity lookupDataAdapterEntity = LookupDataAdapterEntity.create(ValueReference.of(dataAdapterDto.name()), ValueReference.of(dataAdapterDto.title()), ValueReference.of(dataAdapterDto.description()), toReferenceMap(configuration));
final JsonNode data = objectMapper.convertValue(lookupDataAdapterEntity, JsonNode.class);
final Set<Constraint> constraints = versionConstraints(dataAdapterDto);
return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(dataAdapterDto.id(), ModelTypes.LOOKUP_ADAPTER_V1))).type(ModelTypes.LOOKUP_ADAPTER_V1).constraints(ImmutableSet.copyOf(constraints)).data(data).build();
}
use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.
the class LookupDataAdapterFacade method decode.
private NativeEntity<DataAdapterDto> decode(EntityV1 entity, final Map<String, ValueReference> parameters) {
final LookupDataAdapterEntity lookupDataAdapterEntity = objectMapper.convertValue(entity.data(), LookupDataAdapterEntity.class);
final LookupDataAdapterConfiguration configuration = objectMapper.convertValue(toValueMap(lookupDataAdapterEntity.configuration(), parameters), LookupDataAdapterConfiguration.class);
final DataAdapterDto dataAdapterDto = DataAdapterDto.builder().name(lookupDataAdapterEntity.name().asString(parameters)).title(lookupDataAdapterEntity.title().asString(parameters)).description(lookupDataAdapterEntity.description().asString(parameters)).config(configuration).build();
final DataAdapterDto savedDataAdapterDto = dataAdapterService.save(dataAdapterDto);
return NativeEntity.create(entity.id(), savedDataAdapterDto.id(), TYPE_V1, savedDataAdapterDto.title(), savedDataAdapterDto);
}
use of org.graylog2.lookup.dto.DataAdapterDto 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);
}
}
Aggregations