Search in sources :

Example 6 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class HTTPAlarmCallback method getRequestedConfiguration.

@Override
public ConfigurationRequest getRequestedConfiguration() {
    final ConfigurationRequest configurationRequest = new ConfigurationRequest();
    configurationRequest.addField(new TextField(CK_URL, "URL", "https://example.org/alerts", "The URL to POST to when an alert is triggered", ConfigurationField.Optional.NOT_OPTIONAL));
    return configurationRequest;
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) TextField(org.graylog2.plugin.configuration.fields.TextField)

Example 7 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class ConfigurationMapConverter method convertValues.

/**
     * Converts the values in the map to the requested types. This has been copied from the Graylog web interface
     * and should be removed once we have better configuration objects.
     */
public static Map<String, Object> convertValues(final Map<String, Object> data, final ConfigurationRequest configurationRequest) throws ValidationException {
    final Map<String, Object> configuration = Maps.newHashMapWithExpectedSize(data.size());
    final Map<String, Map<String, Object>> configurationFields = configurationRequest.asList();
    for (final Map.Entry<String, Object> entry : data.entrySet()) {
        final String field = entry.getKey();
        final Map<String, Object> fieldDescription = configurationFields.get(field);
        if (fieldDescription == null || fieldDescription.isEmpty()) {
            throw new ValidationException(field, "Unknown configuration field description for field \"" + field + "\"");
        }
        final String type = (String) fieldDescription.get("type");
        // Decide what to cast to. (string, bool, number)
        Object value;
        switch(type) {
            case "text":
            case "dropdown":
                value = entry.getValue() == null ? "" : String.valueOf(entry.getValue());
                break;
            case "number":
                try {
                    value = Integer.parseInt(String.valueOf(entry.getValue()));
                } catch (NumberFormatException e) {
                    // If a numeric field is optional and not provided, use null as value
                    if ("true".equals(String.valueOf(fieldDescription.get("is_optional")))) {
                        value = null;
                    } else {
                        throw new ValidationException(field, e.getMessage());
                    }
                }
                break;
            case "boolean":
                value = "true".equalsIgnoreCase(String.valueOf(entry.getValue()));
                break;
            case "list":
                final List<?> valueList = entry.getValue() == null ? Collections.emptyList() : (List<?>) entry.getValue();
                value = valueList.stream().filter(o -> o != null && o instanceof String).map(String::valueOf).collect(Collectors.toList());
                break;
            default:
                throw new ValidationException(field, "Unknown configuration field type \"" + type + "\"");
        }
        configuration.put(field, value);
    }
    return configuration;
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) List(java.util.List) ValidationException(org.graylog2.plugin.database.ValidationException) Map(java.util.Map) Maps(com.google.common.collect.Maps) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) ValidationException(org.graylog2.plugin.database.ValidationException) Map(java.util.Map)

Example 8 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class ConfigurationMapConverterTest method testConvertValues.

@Test
public void testConvertValues() throws Exception {
    final ImmutableMap<String, String> dropdownChoices = ImmutableMap.of("a", "1", "b", "2");
    final ConfigurationRequest cr = new ConfigurationRequest();
    cr.addField(new TextField("string", "string", "default", ""));
    cr.addField(new TextField("empty-string", "empty", "", ""));
    cr.addField(new TextField("null-string", "null", null, ""));
    cr.addField(new TextField("non-string", "non-string", null, ""));
    cr.addField(new NumberField("number", "number", 42, ""));
    cr.addField(new BooleanField("boolean-true", "true", false, ""));
    cr.addField(new BooleanField("boolean-false", "false", false, ""));
    cr.addField(new DropdownField("dropdown", "dropdown", "a", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
    cr.addField(new DropdownField("dropdown-empty", "dropdown-empty", "", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
    cr.addField(new DropdownField("dropdown-null", "dropdown-null", "", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
    final UUID uuid = UUID.randomUUID();
    final Map<String, Object> data = new HashMap<>();
    data.put("string", "foo");
    data.put("empty-string", "");
    data.put("null-string", null);
    data.put("non-string", uuid);
    data.put("number", "5");
    data.put("boolean-true", "true");
    data.put("boolean-false", "false");
    data.put("dropdown", "a");
    data.put("dropdown-empty", "");
    data.put("dropdown-null", null);
    final Map<String, Object> config = ConfigurationMapConverter.convertValues(data, cr);
    assertThat(config).contains(entry("string", "foo"), entry("empty-string", ""), entry("null-string", ""), entry("non-string", uuid.toString()), entry("number", 5), entry("boolean-true", true), entry("boolean-false", false), entry("dropdown", "a"), entry("dropdown-empty", ""), entry("dropdown-null", ""));
}
Also used : DropdownField(org.graylog2.plugin.configuration.fields.DropdownField) BooleanField(org.graylog2.plugin.configuration.fields.BooleanField) HashMap(java.util.HashMap) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) TextField(org.graylog2.plugin.configuration.fields.TextField) UUID(java.util.UUID) NumberField(org.graylog2.plugin.configuration.fields.NumberField) Test(org.junit.Test)

Example 9 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class ConfigurationMapConverterTest method convertValuesThrowsIllegalArgumentExceptionOnUnknwonType.

@Test
public void convertValuesThrowsIllegalArgumentExceptionOnUnknwonType() throws Exception {
    thrown.expect(ValidationException.class);
    thrown.expectMessage("Unknown configuration field type \"dummy\"");
    final ConfigurationRequest cr = new ConfigurationRequest();
    cr.addField(new DummyField());
    final Map<String, Object> data = new HashMap<>();
    data.put("dummy", "foo");
    ConfigurationMapConverter.convertValues(data, cr);
}
Also used : HashMap(java.util.HashMap) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) Test(org.junit.Test)

Example 10 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class MessageInput method checkConfiguration.

public void checkConfiguration() throws ConfigurationException {
    final ConfigurationRequest cr = getRequestedConfiguration();
    cr.check(getConfiguration());
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest)

Aggregations

ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)18 Test (org.junit.Test)11 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)4 HashMap (java.util.HashMap)3 TextField (org.graylog2.plugin.configuration.fields.TextField)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 BadRequestException (javax.ws.rs.BadRequestException)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 ConfigurationField (org.graylog2.plugin.configuration.fields.ConfigurationField)2 Stream (org.graylog2.plugin.streams.Stream)2 CreateAlarmCallbackRequest (org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest)2 Maps (com.google.common.collect.Maps)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)1 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)1 Configuration (org.graylog2.plugin.configuration.Configuration)1