Skip to content

Commit

Permalink
Added support for negating boolean logstash attribute value
Browse files Browse the repository at this point in the history
Signed-off-by: Asif Sohail Mohammed <[email protected]>
  • Loading branch information
asifsmohammed committed Dec 21, 2021
1 parent 6a29cfd commit a429acc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,24 @@ public Map<String, Object> mapAttributes(final List<LogstashAttribute> logstashA
.stream()
.filter(logstashAttribute -> !customMappedAttributeNames.contains(logstashAttribute.getAttributeName()))
.forEach(logstashAttribute -> {
if (mappedAttributeNames.containsKey(logstashAttribute.getAttributeName())) {
pluginSettings.put(
mappedAttributeNames.get(logstashAttribute.getAttributeName()),
logstashAttribute.getAttributeValue().getValue()
);
}
else {
LOG.warn("Attribute name {} is not found in mapping file.", logstashAttribute.getAttributeName());
}
final String logstashAttributeName = logstashAttribute.getAttributeName();

if (mappedAttributeNames.containsKey(logstashAttributeName)) {
if (mappedAttributeNames.get(logstashAttributeName).startsWith("!")) {
pluginSettings.put(
mappedAttributeNames.get(logstashAttributeName).substring(1),
!(Boolean) logstashAttribute.getAttributeValue().getValue()
);
}
else {
pluginSettings.put(
mappedAttributeNames.get(logstashAttributeName),
logstashAttribute.getAttributeValue().getValue());
}
}
else {
LOG.warn("Attribute name {} is not found in mapping file.", logstashAttributeName);
}
});

if (!customMappedAttributeNames.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pluginName: opensearch
mappedAttributeNames:
hosts: hosts
user: username
password: password
index: index
ssl_certificate_verification: !insecure
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class DefaultLogstashPluginAttributesMapperTest {
private String logstashAttributeName;
private String value;
private List<LogstashAttribute> logstashAttributes;
private LogstashAttributeValue logstashAttributeValue;
private LogstashAttributesMappings mappings;

@BeforeEach
void setUp() {
value = UUID.randomUUID().toString();
logstashAttributeName = UUID.randomUUID().toString();
final LogstashAttribute logstashAttribute = mock(LogstashAttribute.class);
final LogstashAttributeValue logstashAttributeValue = mock(LogstashAttributeValue.class);
logstashAttributeValue = mock(LogstashAttributeValue.class);
when(logstashAttributeValue.getValue()).thenReturn(value);
when(logstashAttribute.getAttributeName()).thenReturn(logstashAttributeName);
when(logstashAttribute.getAttributeValue()).thenReturn(logstashAttributeValue);
Expand Down Expand Up @@ -114,4 +115,20 @@ void mapAttributes_sets_additional_attributes_to_those_values() {
assertThat(actualPluginSettings, hasKey(additionalAttributeName));
assertThat(actualPluginSettings.get(additionalAttributeName), equalTo(additionalAttributeValue));
}

@Test
void mapAttributes_with_negation_expression_negates_boolean_value() {
final String dataPrepperAttribute = "!".concat(UUID.randomUUID().toString());
boolean value = true;

when(logstashAttributeValue.getValue()).thenReturn(value);
when(mappings.getMappedAttributeNames()).thenReturn(Collections.singletonMap(logstashAttributeName, dataPrepperAttribute));

final Map<String, Object> actualPluginSettings =
createObjectUnderTest().mapAttributes(logstashAttributes, mappings);

assertThat(actualPluginSettings, notNullValue());
assertThat(actualPluginSettings.size(), equalTo(1));
assertThat(actualPluginSettings.get(dataPrepperAttribute.substring(1)), equalTo(!value));
}
}

0 comments on commit a429acc

Please sign in to comment.