use of org.graylog2.contentpacks.model.entities.EntityDescriptor in project graylog2-server by Graylog2.
the class GrokPatternFacadeTest method exportNativeEntity.
@Test
public void exportNativeEntity() {
final GrokPattern grokPattern = GrokPattern.create("01234567890", "name", "pattern", null);
final EntityDescriptor descriptor = EntityDescriptor.create(grokPattern.id(), ModelTypes.GROK_PATTERN_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportNativeEntity(grokPattern, entityDescriptorIds);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.GROK_PATTERN_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final GrokPatternEntity grokPatternEntity = objectMapper.convertValue(entityV1.data(), GrokPatternEntity.class);
assertThat(grokPatternEntity.name()).isEqualTo("name");
assertThat(grokPatternEntity.pattern()).isEqualTo("pattern");
}
use of org.graylog2.contentpacks.model.entities.EntityDescriptor in project graylog2-server by Graylog2.
the class LookupCacheFacadeTest method exportEntity.
@Test
@MongoDBFixtures("LookupCacheFacadeTest.json")
public void exportEntity() {
final EntityDescriptor descriptor = EntityDescriptor.create("5adf24b24b900a0fdb4e52dd", ModelTypes.LOOKUP_CACHE_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportEntity(descriptor, entityDescriptorIds).orElseThrow(AssertionError::new);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.LOOKUP_CACHE_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final LookupCacheEntity lookupCacheEntity = objectMapper.convertValue(entityV1.data(), LookupCacheEntity.class);
assertThat(lookupCacheEntity.name()).isEqualTo(ValueReference.of("no-op-cache"));
assertThat(lookupCacheEntity.title()).isEqualTo(ValueReference.of("No-op cache"));
assertThat(lookupCacheEntity.description()).isEqualTo(ValueReference.of("No-op cache"));
assertThat(lookupCacheEntity.configuration()).containsEntry("type", ValueReference.of("none"));
}
use of org.graylog2.contentpacks.model.entities.EntityDescriptor in project graylog2-server by Graylog2.
the class EntitySharesResource method get.
@GET
@ApiOperation(value = "Return shares for a user")
@Path("user/{userId}")
public PaginatedResponse<EntityDescriptor> get(@ApiParam(name = "pagination parameters") @BeanParam PaginationParameters paginationParameters, @ApiParam(name = "userId", required = true) @PathParam("userId") @NotBlank String userId, @ApiParam(name = "capability") @QueryParam("capability") @DefaultValue("") String capabilityFilter, @ApiParam(name = "entity_type") @QueryParam("entity_type") @DefaultValue("") String entityTypeFilter) {
final User user = userService.loadById(userId);
if (user == null) {
throw new NotFoundException("Couldn't find user <" + userId + ">");
}
if (!isPermitted(USERS_EDIT, user.getName())) {
throw new ForbiddenException("Couldn't access user <" + userId + ">");
}
final GranteeSharesService.SharesResponse response = granteeSharesService.getPaginatedSharesFor(grnRegistry.ofUser(user), paginationParameters, capabilityFilter, entityTypeFilter);
return PaginatedResponse.create("entities", response.paginatedEntities(), Collections.singletonMap("grantee_capabilities", response.capabilities()));
}
use of org.graylog2.contentpacks.model.entities.EntityDescriptor in project graylog2-server by Graylog2.
the class ContentPackService method buildEntityGraph.
private ImmutableGraph<Entity> buildEntityGraph(Entity rootEntity, Set<Entity> entities, Map<String, ValueReference> parameters) {
final Map<EntityDescriptor, Entity> entityDescriptorMap = entities.stream().collect(Collectors.toMap(Entity::toEntityDescriptor, Function.identity()));
final MutableGraph<Entity> dependencyGraph = GraphBuilder.directed().allowsSelfLoops(false).expectedNodeCount(entities.size()).build();
for (Map.Entry<EntityDescriptor, Entity> entry : entityDescriptorMap.entrySet()) {
final EntityDescriptor entityDescriptor = entry.getKey();
final Entity entity = entry.getValue();
final EntityWithExcerptFacade<?, ?> facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
final Graph<Entity> entityGraph = facade.resolveForInstallation(entity, parameters, entityDescriptorMap);
LOG.trace("Dependencies of entity {}: {}", entityDescriptor, entityGraph);
dependencyGraph.putEdge(rootEntity, entity);
Graphs.merge(dependencyGraph, entityGraph);
LOG.trace("New dependency graph: {}", dependencyGraph);
}
final Set<Entity> unexpectedEntities = dependencyGraph.nodes().stream().filter(entity -> !rootEntity.equals(entity)).filter(entity -> !entities.contains(entity)).collect(Collectors.toSet());
if (!unexpectedEntities.isEmpty()) {
throw new UnexpectedEntitiesException(unexpectedEntities);
}
return ImmutableGraph.copyOf(dependencyGraph);
}
use of org.graylog2.contentpacks.model.entities.EntityDescriptor in project graylog2-server by Graylog2.
the class ContentPackService method installContentPack.
private ContentPackInstallation installContentPack(ContentPackV1 contentPack, Map<String, ValueReference> parameters, String comment, String user) {
ensureConstraints(contentPack.constraints());
final Entity rootEntity = EntityV1.createRoot(contentPack);
final ImmutableMap<String, ValueReference> validatedParameters = validateParameters(parameters, contentPack.parameters());
final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), validatedParameters);
final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
final Iterable<Entity> entitiesInOrder = entityTraverser.depthFirstPostOrder(rootEntity);
// Insertion order is important for created entities so we can roll back in order!
final Map<EntityDescriptor, Object> createdEntities = new LinkedHashMap<>();
final Map<EntityDescriptor, Object> allEntities = new HashMap<>();
final ImmutableSet.Builder<NativeEntityDescriptor> allEntityDescriptors = ImmutableSet.builder();
try {
for (Entity entity : entitiesInOrder) {
if (entity.equals(rootEntity)) {
continue;
}
final EntityDescriptor entityDescriptor = entity.toEntityDescriptor();
final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
@SuppressWarnings({ "rawtypes", "unchecked" }) final Optional<NativeEntity> existingEntity = facade.findExisting(entity, parameters);
if (existingEntity.isPresent()) {
LOG.trace("Found existing entity for {}", entityDescriptor);
final NativeEntity<?> nativeEntity = existingEntity.get();
final NativeEntityDescriptor nativeEntityDescriptor = nativeEntity.descriptor();
/* Found entity on the system or we found a other installation which stated that */
if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 0 || contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(nativeEntityDescriptor.id()) > 0) {
final NativeEntityDescriptor serverDescriptor = nativeEntityDescriptor.toBuilder().foundOnSystem(true).build();
allEntityDescriptors.add(serverDescriptor);
} else {
allEntityDescriptors.add(nativeEntity.descriptor());
}
allEntities.put(entityDescriptor, nativeEntity.entity());
} else {
LOG.trace("Creating new entity for {}", entityDescriptor);
final NativeEntity<?> createdEntity = facade.createNativeEntity(entity, validatedParameters, allEntities, user);
allEntityDescriptors.add(createdEntity.descriptor());
createdEntities.put(entityDescriptor, createdEntity.entity());
allEntities.put(entityDescriptor, createdEntity.entity());
}
}
} catch (Exception e) {
rollback(createdEntities);
throw new ContentPackException("Failed to install content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
}
final ContentPackInstallation installation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(contentPack.revision()).parameters(validatedParameters).comment(comment).entities(allEntityDescriptors.build()).createdAt(Instant.now()).createdBy(user).build();
return contentPackInstallationPersistenceService.insert(installation);
}
Aggregations