use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.
the class PipelineRuleFacade method findExisting.
private Optional<NativeEntity<RuleDao>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
final String title = ruleEntity.title().asString(parameters);
final String source = ruleEntity.source().asString(parameters);
try {
final RuleDao ruleDao = ruleService.loadByName(title);
compareRuleSources(title, source, ruleDao.source());
return Optional.of(NativeEntity.create(entity.id(), ruleDao.id(), TYPE_V1, ruleDao.title(), ruleDao));
} catch (NotFoundException e) {
return Optional.empty();
}
}
use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.
the class PipelineRuleFacade method exportNativeEntity.
@VisibleForTesting
Entity exportNativeEntity(RuleDao ruleDao, EntityDescriptorIds entityDescriptorIds) {
final PipelineRuleEntity ruleEntity = PipelineRuleEntity.create(ValueReference.of(ruleDao.title()), ValueReference.of(ruleDao.description()), ValueReference.of(ruleDao.source()));
final JsonNode data = objectMapper.convertValue(ruleEntity, JsonNode.class);
return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(ruleDao.id(), ModelTypes.PIPELINE_RULE_V1))).type(ModelTypes.PIPELINE_RULE_V1).data(data).build();
}
use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.
the class PipelineRuleFacadeTest method exportEntity.
@Test
public void exportEntity() {
final RuleDao pipelineRule = RuleDao.builder().id("id").title("title").description("description").source("rule \"debug\"\nwhen\n true\nthen\n debug($message.message);\nend").build();
final EntityDescriptor descriptor = EntityDescriptor.create("id", ModelTypes.PIPELINE_RULE_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportNativeEntity(pipelineRule, entityDescriptorIds);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entityV1.data(), PipelineRuleEntity.class);
assertThat(ruleEntity.title()).isEqualTo(ValueReference.of("title"));
assertThat(ruleEntity.description()).isEqualTo(ValueReference.of("description"));
assertThat(ruleEntity.source().asString(Collections.emptyMap())).startsWith("rule \"debug\"\n");
}
use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.
the class PipelineRuleFacadeTest method collectEntity.
@Test
@MongoDBFixtures("PipelineRuleFacadeTest.json")
public void collectEntity() {
final EntityDescriptor descriptor = EntityDescriptor.create("5adf25034b900a0fdb4e5338", ModelTypes.PIPELINE_RULE_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Optional<Entity> collectedEntity = facade.exportEntity(descriptor, entityDescriptorIds);
assertThat(collectedEntity).isPresent().containsInstanceOf(EntityV1.class);
final EntityV1 entity = (EntityV1) collectedEntity.orElseThrow(AssertionError::new);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
final PipelineRuleEntity pipelineRuleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
assertThat(pipelineRuleEntity.title()).isEqualTo(ValueReference.of("debug"));
assertThat(pipelineRuleEntity.description()).isEqualTo(ValueReference.of("Debug"));
assertThat(pipelineRuleEntity.source().asString(Collections.emptyMap())).startsWith("rule \"debug\"\n");
}
use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.
the class PipelineRuleFacade method decode.
private NativeEntity<RuleDao> decode(EntityV1 entity, Map<String, ValueReference> parameters) {
final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
final String title = ruleEntity.title().asString(parameters);
final String source = ruleEntity.source().asString(parameters);
final DateTime now = Tools.nowUTC();
final ValueReference description = ruleEntity.description();
final RuleDao ruleDao = RuleDao.builder().title(title).description(description == null ? null : description.asString(parameters)).source(source).createdAt(now).modifiedAt(now).build();
final RuleDao savedRuleDao = ruleService.save(ruleDao);
return NativeEntity.create(entity.id(), savedRuleDao.id(), TYPE_V1, savedRuleDao.title(), savedRuleDao);
}
Aggregations