Explicitly list profiles and label in Environment

Fixes gh-111
This commit is contained in:
Dave Syer
2015-03-20 13:13:52 +00:00
parent 2845b26eed
commit 5a80ce9e3a
6 changed files with 38 additions and 9 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.config.environment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -34,15 +35,23 @@ public class Environment {
private String name;
private String[] profiles = new String[0];
private String label;
private List<PropertySource> propertySources = new ArrayList<PropertySource>();
public Environment(String name, String... profiles) {
this(name, profiles, "master");
}
@JsonCreator
public Environment(@JsonProperty("name") String name,
@JsonProperty("profiles") String[] profiles,
@JsonProperty("label") String label) {
super();
this.name = name;
this.profiles = profiles;
this.label = label;
}
@@ -61,15 +70,31 @@ public class Environment {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String[] getProfiles() {
return profiles;
}
public void setProfiles(String[] profiles) {
this.profiles = profiles;
}
@Override
public String toString() {
return "Environment [name=" + name + ", label=" + label + ", propertySources="
+ propertySources + "]";
return "Environment [name=" + name + ", profiles=" + Arrays.asList(profiles)
+ ", label=" + label + ", propertySources=" + propertySources + "]";
}
}

View File

@@ -144,7 +144,7 @@ public abstract class AbstractScmEnvironmentRepository implements EnvironmentRep
}
protected Environment clean(Environment value) {
Environment result = new Environment(value.getName(), value.getLabel());
Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel());
for (PropertySource source : value.getPropertySources()) {
String name = source.getName().replace(
getWorkingDirectory().toURI().toString(), "");

View File

@@ -223,7 +223,7 @@ public class EncryptionController {
}
public Environment decrypt(Environment environment) {
Environment result = new Environment(environment.getName(),
Environment result = new Environment(environment.getName(), environment.getProfiles(),
environment.getLabel());
for (PropertySource source : environment.getPropertySources()) {
Map<Object, Object> map = new LinkedHashMap<Object, Object>(

View File

@@ -112,7 +112,9 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
pull(git, label, ref);
}
environment.setSearchLocations(getSearchLocations(getWorkingDirectory()));
return clean(environment.findOne(application, profile, ""));
Environment result = environment.findOne(application, profile, "");
result.setLabel(label);
return clean(result);
}
private Ref checkout(Git git, String label) throws GitAPIException {

View File

@@ -26,6 +26,7 @@ import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.StandardServletEnvironment;
/**
@@ -53,7 +54,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository {
@Override
public Environment findOne(String application, String env, String label) {
Environment result = new Environment(env, label);
Environment result = new Environment(application, StringUtils.commaDelimitedListToStringArray(env), label);
for (org.springframework.core.env.PropertySource<?> source : environment.getPropertySources()) {
String name = source.getName();
if (!standardSources.contains(name) && source instanceof MapPropertySource) {

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.config.server;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.File;
@@ -31,9 +32,6 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.ConfigServerConfiguration;
import org.springframework.cloud.config.server.ConfigServerTestUtils;
import org.springframework.cloud.config.server.EnvironmentRepository;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -74,6 +72,9 @@ public class JGitEnvironmentRepositoryIntegrationTests {
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
assertEquals("bar", environment.getName());
assertArrayEquals(new String[] {"staging"}, environment.getProfiles());
assertEquals("master", environment.getLabel());
}
@Test