From 1df2c15c88cf081e709e4cdb3173225853fa4c01 Mon Sep 17 00:00:00 2001 From: Daniel Lavoie Date: Mon, 23 Oct 2017 15:10:36 -0400 Subject: [PATCH] Add support for default label on native environment repository (#826) --- .../EnvironmentRepositoryConfiguration.java | 11 ++++++++++- .../NativeEnvironmentRepository.java | 18 ++++++++++++++---- .../NativeEnvironmentRepositoryTests.java | 9 +++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java index b74e6b56..88f92179 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java @@ -41,6 +41,7 @@ import org.springframework.web.client.RestTemplate; /** * @author Dave Syer * @author Ryan Baxter + * @author Daniel Lavoie * */ @Configuration @@ -109,10 +110,18 @@ class NativeRepositoryConfiguration { @Autowired private ConfigurableEnvironment environment; + + @Autowired + private ConfigServerProperties configServerProperties; @Bean public NativeEnvironmentRepository nativeEnvironmentRepository() { - return new NativeEnvironmentRepository(this.environment); + NativeEnvironmentRepository repository = new NativeEnvironmentRepository( + this.environment); + + repository.setDefaultLabel(configServerProperties.getDefaultLabel()); + + return repository; } } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index 0987819f..b989c6b9 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -49,13 +49,15 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Roy Clarkson * @author Venil Noronha + * @author Daniel Lavoie */ @ConfigurationProperties("spring.cloud.config.server.native") -public class NativeEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, Ordered { +public class NativeEnvironmentRepository + implements EnvironmentRepository, SearchPathLocator, Ordered { private static Log logger = LogFactory.getLog(NativeEnvironmentRepository.class); - - private static final String DEFAULT_LABEL = "master"; + + private String defaultLabel = "master"; /** * Locations to search for configuration files. Defaults to the same as a Spring Boot @@ -106,7 +108,11 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc } public String getDefaultLabel() { - return DEFAULT_LABEL; + return defaultLabel; + } + + public void setDefaultLabel(String defaultLabel) { + this.defaultLabel = defaultLabel; } @Override @@ -143,6 +149,10 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc locations = DEFAULT_LOCATIONS; } Collection output = new LinkedHashSet(); + + if (label == null) { + label = defaultLabel; + } for (String location : locations) { String[] profiles = new String[] { profile }; if (profile != null) { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index 354ccfb0..655e6e53 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -29,6 +29,7 @@ import org.springframework.context.ConfigurableApplicationContext; * @author Dave Syer * @author Spencer Gibb * @author Venil Noronha + * @author Daniel Lavoie */ public class NativeEnvironmentRepositoryTests { @@ -40,6 +41,7 @@ public class NativeEnvironmentRepositoryTests { NativeEnvironmentRepositoryTests.class).web(false).run(); this.repository = new NativeEnvironmentRepository(context.getEnvironment()); this.repository.setVersion("myversion"); + this.repository.setDefaultLabel(null); context.close(); } @@ -187,5 +189,12 @@ public class NativeEnvironmentRepositoryTests { Locations locations = this.repository.getLocations("foo", "dev", null); assertEquals(1, locations.getLocations().length); } + + @Test + public void testDefaultLabel() { + this.repository.setDefaultLabel("test"); + assertEquals("test_bar", this.repository.findOne("foo", "default", null) + .getPropertySources().get(0).getSource().get("foo")); + } }