Search in sources :

Example 81 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class PropertiesResultSetExtractor method findOne.

@Override
public Environment findOne(String application, String profile, String label) {
    String config = application;
    if (StringUtils.isEmpty(label)) {
        label = "master";
    }
    if (StringUtils.isEmpty(profile)) {
        profile = "default";
    }
    if (!profile.startsWith("default")) {
        profile = "default," + profile;
    }
    String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
    Environment environment = new Environment(application, profiles, label, null, null);
    if (!config.startsWith("application")) {
        config = "application," + config;
    }
    List<String> applications = new ArrayList<String>(new LinkedHashSet<>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(config))));
    List<String> envs = new ArrayList<String>(new LinkedHashSet<>(Arrays.asList(profiles)));
    Collections.reverse(applications);
    Collections.reverse(envs);
    for (String app : applications) {
        for (String env : envs) {
            Map<String, String> next = (Map<String, String>) jdbc.query(this.sql, new Object[] { app, env, label }, this.extractor);
            if (!next.isEmpty()) {
                environment.add(new PropertySource(app + "-" + env, next));
            }
        }
    }
    return environment;
}
Also used : ArrayList(java.util.ArrayList) Environment(org.springframework.cloud.config.environment.Environment) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 82 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class VaultEnvironmentRepository method findOne.

@Override
public Environment findOne(String application, String profile, String label) {
    String state = request.getHeader(STATE_HEADER);
    String newState = this.watch.watch(state);
    String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
    List<String> scrubbedProfiles = scrubProfiles(profiles);
    List<String> keys = findKeys(application, scrubbedProfiles);
    Environment environment = new Environment(application, profiles, label, null, newState);
    for (String key : keys) {
        // read raw 'data' key from vault
        String data = read(key);
        if (data != null) {
            // data is in json format of which, yaml is a superset, so parse
            final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ByteArrayResource(data.getBytes()));
            Properties properties = yaml.getObject();
            if (!properties.isEmpty()) {
                environment.add(new PropertySource("vault:" + key, properties));
            }
        }
    }
    return environment;
}
Also used : Environment(org.springframework.cloud.config.environment.Environment) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 83 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method basedirExists.

@Test
public void basedirExists() throws Exception {
    assertTrue(this.basedir.mkdirs());
    assertTrue(new File(this.basedir, ".nothing").createNewFile());
    this.repository.setBasedir(this.basedir);
    this.repository.findOne("bar", "staging", "master");
    Environment environment = this.repository.findOne("bar", "staging", "master");
    assertEquals(2, environment.getPropertySources().size());
    assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
    assertVersion(environment);
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.cloud.config.environment.Environment) File(java.io.File) Test(org.junit.Test)

Example 84 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class MultipleJGitEnvironmentApplicationPlaceholderRepositoryTests method otherMappingRepo.

@Test
public void otherMappingRepo() {
    Environment environment = this.repository.findOne("test2-config-repo", "staging", "master");
    assertEquals(1, environment.getPropertySources().size());
    assertEquals(getUri("*").replace("{application}", "test2-config-repo") + "/application.properties", environment.getPropertySources().get(0).getName());
    assertVersion(environment);
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 85 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class MultipleJGitEnvironmentApplicationPlaceholderRepositoryTests method defaultRepo.

@Test
public void defaultRepo() {
    Environment environment = this.repository.findOne("bar", "staging", "master");
    assertEquals(2, environment.getPropertySources().size());
    assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
    assertVersion(environment);
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Aggregations

Environment (org.springframework.cloud.config.environment.Environment)118 Test (org.junit.Test)104 StandardEnvironment (org.springframework.core.env.StandardEnvironment)37 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)20 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)19 PropertySource (org.springframework.cloud.config.environment.PropertySource)12 LinkedHashMap (java.util.LinkedHashMap)9 File (java.io.File)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)7 FileOutputStream (java.io.FileOutputStream)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 EnvironmentPropertySource.prepareEnvironment (org.springframework.cloud.config.server.support.EnvironmentPropertySource.prepareEnvironment)4 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)4 HttpEntity (org.springframework.http.HttpEntity)4 RestTemplate (org.springframework.web.client.RestTemplate)4 Git (org.eclipse.jgit.api.Git)3 Matchers.anyString (org.mockito.Matchers.anyString)3