Search in sources :

Example 1 with ContentPackInstallation

use of org.graylog2.contentpacks.model.ContentPackInstallation in project graylog2-server by Graylog2.

the class ContentPackService method getUninstallDetails.

private ContentPackUninstallDetails getUninstallDetails(ContentPackV1 contentPack, ContentPackInstallation installation) {
    final Entity rootEntity = EntityV1.createRoot(contentPack);
    final ImmutableMap<String, ValueReference> parameters = installation.parameters();
    final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), parameters);
    final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
    final Iterable<Entity> entitiesInOrder = entityTraverser.breadthFirst(rootEntity);
    final Set<NativeEntityDescriptor> nativeEntityDescriptors = new HashSet<>();
    entitiesInOrder.forEach((entity -> {
        if (entity.equals(rootEntity)) {
            return;
        }
        final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
        if (nativeEntityDescriptorOptional.isPresent()) {
            NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
            if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 1) {
                nativeEntityDescriptors.add(nativeEntityDescriptor);
            }
        }
    }));
    return ContentPackUninstallDetails.create(nativeEntityDescriptors);
}
Also used : ElementOrder(com.google.common.graph.ElementOrder) Graphs(org.graylog2.utilities.Graphs) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ContentPack(org.graylog2.contentpacks.model.ContentPack) LoggerFactory(org.slf4j.LoggerFactory) ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) LegacyContentPack(org.graylog2.contentpacks.model.LegacyContentPack) Map(java.util.Map) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) UnsupportedEntityFacade(org.graylog2.contentpacks.facades.UnsupportedEntityFacade) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GraphBuilder(com.google.common.graph.GraphBuilder) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) Parameter(org.graylog2.contentpacks.model.parameters.Parameter) Optional(java.util.Optional) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) ContentPackUninstallDetails(org.graylog2.contentpacks.model.ContentPackUninstallDetails) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) Singleton(javax.inject.Singleton) Function(java.util.function.Function) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) EntityFacade(org.graylog2.contentpacks.facades.EntityFacade) Logger(org.slf4j.Logger) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Traverser(com.google.common.graph.Traverser) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) Optional(java.util.Optional) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) HashSet(java.util.HashSet)

Example 2 with ContentPackInstallation

use of org.graylog2.contentpacks.model.ContentPackInstallation in project graylog2-server by Graylog2.

the class ContentPackInstallationPersistenceService method getInstallationMetadata.

public Map<ModelId, Map<Integer, ContentPackMetadata>> getInstallationMetadata(Set<ModelId> ids) {
    final Set<ContentPackInstallation> contentPackInstallations = findByContentPackIds(ids);
    Map<ModelId, Map<Integer, ContentPackMetadata>> installationMetaData = new HashMap<>();
    for (ContentPackInstallation installation : contentPackInstallations) {
        Map<Integer, ContentPackMetadata> metadataMap = installationMetaData.get(installation.contentPackId());
        if (metadataMap == null) {
            metadataMap = new HashMap<>();
        }
        ContentPackMetadata metadata = metadataMap.get(installation.contentPackRevision());
        int count = 1;
        if (metadata != null) {
            count = metadata.installationCount() + 1;
        }
        ContentPackMetadata newMetadata = ContentPackMetadata.create(count);
        metadataMap.put(installation.contentPackRevision(), newMetadata);
        installationMetaData.put(installation.contentPackId(), metadataMap);
    }
    return installationMetaData;
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ModelId(org.graylog2.contentpacks.model.ModelId) ContentPackMetadata(org.graylog2.rest.models.system.contentpacks.responses.ContentPackMetadata)

Example 3 with ContentPackInstallation

use of org.graylog2.contentpacks.model.ContentPackInstallation in project graylog2-server by Graylog2.

the class ContentPackResource method deleteContentPackInstallationById.

@DELETE
@Path("{contentPackId}/installations/{installationId}")
@Timed
@ApiOperation(value = "Uninstall a content pack installation")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_UNINSTALL)
@JsonView(ContentPackView.HttpView.class)
public Response deleteContentPackInstallationById(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId contentPackId, @ApiParam(name = "installationId", value = "Installation ID", required = true) @PathParam("installationId") String installationId) {
    checkPermission(RestPermissions.CONTENT_PACK_UNINSTALL, contentPackId.toString());
    final ContentPackInstallation installation = contentPackInstallationPersistenceService.findById(new ObjectId(installationId)).orElseThrow(() -> new NotFoundException("Couldn't find installation " + installationId));
    final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(installation.contentPackId(), installation.contentPackRevision()).orElseThrow(() -> new NotFoundException("Couldn't find content pack " + installation.contentPackId() + " rev " + installation.contentPackRevision()));
    final ContentPackUninstallation removedInstallation = contentPackService.uninstallContentPack(contentPack, installation);
    return Response.ok(ImmutableMap.of("content_pack", contentPack, "uninstalled", removedInstallation)).build();
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ObjectId(org.bson.types.ObjectId) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with ContentPackInstallation

use of org.graylog2.contentpacks.model.ContentPackInstallation in project graylog2-server by Graylog2.

the class V20191219090834_AddSourcesPageTest method upgradeFailsBecauseSourcePageContentPackExists.

@Test
public void upgradeFailsBecauseSourcePageContentPackExists() throws IOException {
    final URL contentPackURL = V20191219090834_AddSourcesPage.class.getResource("V20191219090834_AddSourcesPage_Content_Pack.json");
    final ContentPack contentPack = ContentPackV1.builder().id(ModelId.of("04fcf179-49e0-4e8f-9c02-0ff13062efbe")).summary("summary").revision(1).name("Sources Page").description("description").vendor("").url(URI.create("http://graylog.com")).entities(ImmutableSet.of()).build();
    final ContentPackInstallation contentPackInstallation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(1).parameters(ImmutableMap.of()).entities(ImmutableSet.of()).comment("Comment").createdAt(Instant.now()).createdBy("admin").build();
    when(configService.get(V20191219090834_AddSourcesPage.MigrationCompleted.class)).thenReturn(null);
    when(objectMapper.readValue(contentPackURL, ContentPack.class)).thenReturn(contentPack);
    when(contentPackPersistenceService.insert(contentPack)).thenReturn(Optional.empty());
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> migration.upgrade()).withMessage("Could not install Source Page Content Pack.");
    verify(configService).write(V20191219090834_AddSourcesPage.MigrationCompleted.create("04fcf179-49e0-4e8f-9c02-0ff13062efbe"));
    verifyZeroInteractions(contentPackService);
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) URL(java.net.URL) Test(org.junit.Test)

Example 5 with ContentPackInstallation

use of org.graylog2.contentpacks.model.ContentPackInstallation in project graylog2-server by Graylog2.

the class ContentPackInstallationPersistenceServiceTest method findByIdWithInvalidId.

@Test
@MongoDBFixtures("ContentPackInstallationPersistenceServiceTest.json")
public void findByIdWithInvalidId() {
    final Optional<ContentPackInstallation> contentPacks = persistenceService.findById(new ObjectId("0000000000000000deadbeef"));
    assertThat(contentPacks).isEmpty();
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ObjectId(org.bson.types.ObjectId) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)17 Test (org.junit.Test)11 ObjectId (org.bson.types.ObjectId)8 ContentPack (org.graylog2.contentpacks.model.ContentPack)7 HashMap (java.util.HashMap)6 ModelId (org.graylog2.contentpacks.model.ModelId)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)5 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)5 Map (java.util.Map)4 Optional (java.util.Optional)4 Set (java.util.Set)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ContentPackUninstallation (org.graylog2.contentpacks.model.ContentPackUninstallation)4 Entity (org.graylog2.contentpacks.model.entities.Entity)4 Timed (com.codahale.metrics.annotation.Timed)3 JsonView (com.fasterxml.jackson.annotation.JsonView)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3