use of org.graylog2.contentpacks.model.ContentPackV1 in project graylog2-server by Graylog2.
the class ContentPackService method checkConstraintsV1.
private Set<ConstraintCheckResult> checkConstraintsV1(ContentPackV1 contentPackV1) {
Set<Constraint> requiredConstraints = contentPackV1.constraints();
final Set<ConstraintCheckResult> fulfilledConstraints = new HashSet<>();
for (ConstraintChecker constraintChecker : constraintCheckers) {
fulfilledConstraints.addAll(constraintChecker.checkConstraints(requiredConstraints));
}
return fulfilledConstraints;
}
use of org.graylog2.contentpacks.model.ContentPackV1 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);
}
use of org.graylog2.contentpacks.model.ContentPackV1 in project graylog2-server by Graylog2.
the class ContentPackPersistenceService method filterMissingResourcesAndInsert.
public Optional<ContentPack> filterMissingResourcesAndInsert(final ContentPack pack) {
ContentPackV1 cpv1 = (ContentPackV1) pack;
final Set<String> allStreams = streamService.loadAll().stream().map(stream -> stream.getTitle()).collect(Collectors.toSet());
final Map<String, String> streamsInContentPack = new HashMap<>();
cpv1.entities().stream().filter(entity -> "stream".equals(entity.type().name()) && "1".equals(entity.type().version())).map(entity -> new Tuple2<String, JsonNode>(entity.id().id(), ((EntityV1) entity).data().findValue("title"))).forEach(tuple2 -> {
JsonNode title = tuple2.v2().findValue("@value");
streamsInContentPack.put(tuple2.v1(), title.textValue());
});
cpv1.entities().stream().filter(entity -> "dashboard".equals(entity.type().name()) && "2".equals(entity.type().version())).map(entity -> ((EntityV1) entity).data().findValue("search")).map(node -> node.findValue("queries")).map(node -> node.findValue("search_types")).forEach(node -> {
final ObjectNode parent = (ObjectNode) node.findParent("streams");
final ArrayNode streams = (ArrayNode) node.findValue("streams");
if (streams != null) {
final ArrayNode filtered = streams.deepCopy();
filtered.removeAll();
streams.forEach(stream -> {
final String sid = stream.textValue();
final String stitle = streamsInContentPack.get(sid);
if (allStreams.contains(stitle))
filtered.add(stream);
});
parent.replace("streams", filtered);
}
});
return this.insert(cpv1);
}
use of org.graylog2.contentpacks.model.ContentPackV1 in project graylog2-server by Graylog2.
the class ContentPackLoaderPeriodical method doRun.
@Override
public void doRun() {
if (!Files.exists(contentPacksDir.toAbsolutePath())) {
LOG.warn("Could not find content packs directory {}. Please check your graylog configuration", contentPacksDir.toAbsolutePath());
return;
}
final List<Path> files = getFiles(contentPacksDir);
final Map<String, ContentPack> contentPacks = new HashMap<>(files.size());
for (Path file : files) {
final String fileName = file.getFileName().toString();
LOG.debug("Reading content pack from {}", file);
final byte[] bytes;
try {
bytes = Files.readAllBytes(file);
} catch (IOException e) {
LOG.warn("Couldn't read " + file + ". Skipping.", e);
continue;
}
LOG.debug("Parsing content pack from {}", file);
final ContentPack contentPack;
try {
contentPack = objectMapper.readValue(bytes, ContentPack.class);
} catch (IOException e) {
LOG.warn("Couldn't parse content pack in file " + file + ". Skipping", e);
continue;
}
if (contentPack instanceof ContentPackV1) {
ContentPackV1 contentPackV1 = (ContentPackV1) contentPack;
if (contentPackV1.parameters().asList().size() > 0) {
LOG.warn("Cannot accept content packs with parameters. Content Pack {}/{} rejected", contentPack.id(), contentPack.revision());
}
}
final Optional<ContentPack> existingContentPack = contentPackPersistenceService.findByIdAndRevision(contentPack.id(), contentPack.revision());
if (existingContentPack.isPresent()) {
LOG.debug("Content pack {}/{} already exists in database. Skipping.", contentPack.id(), contentPack.revision());
contentPacks.put(fileName, existingContentPack.get());
continue;
}
final Optional<ContentPack> insertedContentPack = contentPackPersistenceService.insert(contentPack);
if (!insertedContentPack.isPresent()) {
LOG.error("Error while inserting content pack " + file + " into database. Skipping.");
continue;
}
contentPacks.put(fileName, insertedContentPack.get());
}
LOG.debug("Applying selected content packs");
for (Map.Entry<String, ContentPack> entry : contentPacks.entrySet()) {
final String fileName = entry.getKey();
final ContentPack contentPack = entry.getValue();
if (contentPacksAutoInstall.contains(fileName)) {
if (!contentPackInstallationPersistenceService.findByContentPackIdAndRevision(contentPack.id(), contentPack.revision()).isEmpty()) {
LOG.debug("Content pack {}/{} ({}) already applied. Skipping.", contentPack.id(), contentPack.revision(), fileName);
continue;
}
contentPackService.installContentPack(contentPack, Collections.emptyMap(), "Installed by auto loader", configuration.getRootUsername());
}
}
}
use of org.graylog2.contentpacks.model.ContentPackV1 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);
}
Aggregations