Merge pull request #10909 from aleph-zero/issues/9706

Read configuration file with .yaml suffix
This commit is contained in:
Britta Weber 2015-05-28 11:54:05 +02:00
commit 334763acef
5 changed files with 30 additions and 16 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.node.internal;
import com.google.common.collect.ImmutableList;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.Names;
import org.elasticsearch.common.Strings;
@ -27,6 +28,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.FailedToResolveConfigException;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.Strings.cleanPath;
@ -37,6 +39,8 @@ import static org.elasticsearch.common.settings.Settings.settingsBuilder;
*/
public class InternalSettingsPreparer {
static final List<String> ALLOWED_SUFFIXES = ImmutableList.of(".yml", ".yaml", ".json", ".properties");
public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, boolean loadConfigSettings) {
// ignore this prefixes when getting properties from es. and elasticsearch.
String[] ignorePrefixes = new String[]{"es.default.", "elasticsearch.default."};
@ -72,22 +76,12 @@ public class InternalSettingsPreparer {
}
}
if (loadFromEnv) {
try {
settingsBuilder.loadFromUrl(environment.resolveConfig("elasticsearch.yml"));
} catch (FailedToResolveConfigException e) {
// ignore
} catch (NoClassDefFoundError e) {
// ignore, no yaml
}
try {
settingsBuilder.loadFromUrl(environment.resolveConfig("elasticsearch.json"));
} catch (FailedToResolveConfigException e) {
// ignore
}
try {
settingsBuilder.loadFromUrl(environment.resolveConfig("elasticsearch.properties"));
} catch (FailedToResolveConfigException e) {
// ignore
for (String allowedSuffix : ALLOWED_SUFFIXES) {
try {
settingsBuilder.loadFromUrl(environment.resolveConfig("elasticsearch" + allowedSuffix));
} catch (FailedToResolveConfigException e) {
// ignore
}
}
}
}

View File

@ -61,4 +61,16 @@ public class InternalSettingsPreparerTests extends ElasticsearchTestCase {
// Should use setting from the system property
assertThat(tuple.v1().get("node.zone"), equalTo("bar"));
}
@Test
public void testAlternateConfigFileSuffixes() {
// test that we can read config files with .yaml, .json, and .properties suffixes
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(settingsBuilder()
.put("config.ignore_system_properties", true)
.build(), true);
assertThat(tuple.v1().get("yaml.config.exists"), equalTo("true"));
assertThat(tuple.v1().get("json.config.exists"), equalTo("true"));
assertThat(tuple.v1().get("properties.config.exists"), equalTo("true"));
}
}

View File

@ -0,0 +1,3 @@
{
"json.config.exists" : "true"
}

View File

@ -0,0 +1,2 @@
properties.config.exists: true

View File

@ -0,0 +1,3 @@
yaml.config.exists: true