diff --git a/src/main/java/org/opensearch/sdk/sample/helloworld/ExampleCustomSettingConfig.java b/src/main/java/org/opensearch/sdk/sample/helloworld/ExampleCustomSettingConfig.java new file mode 100644 index 00000000..dc2409af --- /dev/null +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/ExampleCustomSettingConfig.java @@ -0,0 +1,32 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.sdk.sample.helloworld; + +import org.opensearch.common.settings.Setting; +import org.opensearch.common.settings.Setting.RegexValidator; +import org.opensearch.common.settings.Setting.Property; + +/** + * {@link ExampleCustomSettingConfig} contains the custom settings value and their static declarations. + */ +public class ExampleCustomSettingConfig { + private static final String FORBIDDEN_REGEX = "forbidden"; + + /** + * A string setting. If the string setting matches the FORBIDDEN_REGEX string, the validation will fail. + */ + public static final Setting VALIDATED_SETTING = Setting.simpleString( + "custom.validate", + new RegexValidator(FORBIDDEN_REGEX), + Property.NodeScope, + Property.Dynamic + ); + +} diff --git a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java index 13a8273e..faf48e03 100644 --- a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java @@ -15,6 +15,7 @@ import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionResponse; +import org.opensearch.common.settings.Setting; import org.opensearch.sdk.BaseExtension; import org.opensearch.sdk.Extension; import org.opensearch.sdk.ExtensionRestHandler; @@ -63,6 +64,13 @@ public List getExtensionRestHandlers() { return Arrays.asList(new ActionHandler<>(SampleAction.INSTANCE, SampleTransportAction.class)); } + /** + * A list of object that includes a single instance of Validator for Custom Setting + */ + public List> getSettings() { + return Arrays.asList(ExampleCustomSettingConfig.VALIDATED_SETTING); + } + /** * Entry point to execute an extension. * diff --git a/src/test/java/org/opensearch/sdk/sample/helloworld/TestHelloWorldExtension.java b/src/test/java/org/opensearch/sdk/sample/helloworld/TestHelloWorldExtension.java index ffbb7bf4..0d7ca099 100644 --- a/src/test/java/org/opensearch/sdk/sample/helloworld/TestHelloWorldExtension.java +++ b/src/test/java/org/opensearch/sdk/sample/helloworld/TestHelloWorldExtension.java @@ -44,6 +44,8 @@ import com.google.inject.Injector; import com.google.inject.Key; +import static org.opensearch.sdk.sample.helloworld.ExampleCustomSettingConfig.VALIDATED_SETTING; + public class TestHelloWorldExtension extends OpenSearchTestCase { private HelloWorldExtension extension; @@ -237,4 +239,16 @@ public void onFailure(Exception e) { assertEquals("failed to find action [" + UnregisteredAction.INSTANCE + "] to execute", ex.getMessage()); } + + public void testValidatedSettings() { + final String expected = randomAlphaOfLengthBetween(1, 5); + final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build()); + assertEquals(expected, actual); + + final IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build()) + ); + assertEquals("Setting must not contain [forbidden]", exception.getMessage()); + } }