Search in sources :

Example 1 with DataAdapterDto

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);
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) LookupDefaultSingleValue(org.graylog2.lookup.LookupDefaultSingleValue) LookupDefaultMultiValue(org.graylog2.lookup.LookupDefaultMultiValue) MissingNativeEntityException(org.graylog2.contentpacks.exceptions.MissingNativeEntityException) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) CacheDto(org.graylog2.lookup.dto.CacheDto)

Example 2 with DataAdapterDto

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));
}
Also used : LookupDataAdapterEntity(org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto)

Example 3 with DataAdapterDto

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();
}
Also used : LookupDataAdapterEntity(org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) JsonNode(com.fasterxml.jackson.databind.JsonNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with DataAdapterDto

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);
}
Also used : LookupDataAdapterEntity(org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) LookupDataAdapterConfiguration(org.graylog2.plugin.lookup.LookupDataAdapterConfiguration)

Example 5 with DataAdapterDto

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

Aggregations

DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)17 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)8 Test (org.junit.Test)7 ApiOperation (io.swagger.annotations.ApiOperation)6 Path (javax.ws.rs.Path)6 Entity (org.graylog2.contentpacks.model.entities.Entity)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 BadRequestException (javax.ws.rs.BadRequestException)4 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)4 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)4 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)4 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 FallbackAdapterConfig (org.graylog2.plugin.lookup.FallbackAdapterConfig)3 LookupDataAdapter (org.graylog2.plugin.lookup.LookupDataAdapter)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)2