Updates to use updated ConfigData apis

This commit is contained in:
spencergibb
2020-10-16 15:15:24 -04:00
parent b58dd4d80e
commit 46f29724ad
4 changed files with 34 additions and 29 deletions

View File

@@ -24,9 +24,10 @@ import org.apache.commons.logging.Log;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
public class ConsulConfigDataLoader implements ConfigDataLoader<ConsulConfigDataLocation> {
public class ConsulConfigDataLoader implements ConfigDataLoader<ConsulConfigDataResource> {
private final Log log;
@@ -35,17 +36,17 @@ public class ConsulConfigDataLoader implements ConfigDataLoader<ConsulConfigData
}
@Override
public ConfigData load(ConfigDataLoaderContext context, ConsulConfigDataLocation location) {
public ConfigData load(ConfigDataLoaderContext context, ConsulConfigDataResource resource) {
try {
ConsulClient consul = getBean(context, ConsulClient.class);
ConsulConfigIndexes indexes = getBean(context, ConsulConfigIndexes.class);
ConsulPropertySource propertySource = location.getConsulPropertySources().createPropertySource(
location.getContext(), location.isOptional(), consul, indexes.getIndexes()::put);
ConsulPropertySource propertySource = resource.getConsulPropertySources().createPropertySource(
resource.getContext(), resource.isOptional(), consul, indexes.getIndexes()::put);
return new ConfigData(Collections.singletonList(propertySource));
}
catch (Exception e) {
throw new ConfigDataLocationNotFoundException(location, e);
throw new ConfigDataLocationNotFoundException(ConfigDataLocation.of(resource.getContext()), e);
}
}

View File

@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
@@ -45,7 +46,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import static org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES;
public class ConsulConfigDataLocationResolver implements ConfigDataLocationResolver<ConsulConfigDataLocation> {
public class ConsulConfigDataLocationResolver implements ConfigDataLocationResolver<ConsulConfigDataResource> {
/**
* Consul ConfigData prefix.
@@ -64,8 +65,8 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
}
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, String location) {
if (!location.startsWith(PREFIX)) {
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
if (!location.hasPrefix(PREFIX)) {
return false;
}
// only bind if correct prefix
@@ -76,14 +77,14 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
}
@Override
public List<ConsulConfigDataLocation> resolve(ConfigDataLocationResolverContext context, String location,
boolean optional) throws ConfigDataLocationNotFoundException {
public List<ConsulConfigDataResource> resolve(ConfigDataLocationResolverContext context,
ConfigDataLocation location) throws ConfigDataLocationNotFoundException {
return Collections.emptyList();
}
@Override
public List<ConsulConfigDataLocation> resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext,
String location, boolean optional, Profiles profiles) throws ConfigDataLocationNotFoundException {
public List<ConsulConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext,
ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException {
UriComponents locationUri = parseLocation(resolverContext, location);
// create consul client
@@ -105,8 +106,8 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
registerAndPromoteBean(resolverContext, ConsulConfigIndexes.class,
InstanceSupplier.from(ConsulConfigDataIndexes::new));
return contexts.stream().map(propertySourceContext -> new ConsulConfigDataLocation(propertySourceContext,
optional, properties, consulPropertySources)).collect(Collectors.toList());
return contexts.stream().map(propertySourceContext -> new ConsulConfigDataResource(propertySourceContext,
location.isOptional(), properties, consulPropertySources)).collect(Collectors.toList());
}
private List<String> getCustomContexts(UriComponents uriComponents, ConsulConfigProperties properties) {
@@ -132,8 +133,9 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
}
@Nullable
protected UriComponents parseLocation(ConfigDataLocationResolverContext context, String location) {
String uri = location.substring(PREFIX.length());
protected UriComponents parseLocation(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
String originalLocation = location.getNonPrefixedValue(PREFIX);
String uri = originalLocation;
if (!StringUtils.hasText(uri)) {
return null;
}
@@ -141,7 +143,7 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
uri = PREFIX + "//" + uri;
}
else {
uri = location;
uri = originalLocation;
}
return UriComponentsBuilder.fromUriString(uri).build();
}

View File

@@ -18,10 +18,10 @@ package org.springframework.cloud.consul.config;
import java.util.Objects;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.core.style.ToStringCreator;
public class ConsulConfigDataLocation extends ConfigDataLocation {
public class ConsulConfigDataResource extends ConfigDataResource {
private final ConsulConfigProperties properties;
@@ -31,7 +31,7 @@ public class ConsulConfigDataLocation extends ConfigDataLocation {
private final ConsulPropertySources consulPropertySources;
public ConsulConfigDataLocation(String context, boolean optional, ConsulConfigProperties properties,
public ConsulConfigDataResource(String context, boolean optional, ConsulConfigProperties properties,
ConsulPropertySources consulPropertySources) {
this.properties = properties;
this.context = context;
@@ -63,7 +63,7 @@ public class ConsulConfigDataLocation extends ConfigDataLocation {
if (o == null || getClass() != o.getClass()) {
return false;
}
ConsulConfigDataLocation that = (ConsulConfigDataLocation) o;
ConsulConfigDataResource that = (ConsulConfigDataResource) o;
return this.optional == that.optional && this.context.equals(that.context);
}

View File

@@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Binder;
@@ -41,18 +42,19 @@ public class ConsulConfigDataLocationResolverTests {
@Test
public void testParseLocation() {
ConsulConfigDataLocationResolver resolver = new ConsulConfigDataLocationResolver(LogFactory.getLog(getClass()));
UriComponents uriComponents = resolver.parseLocation(null, "consul:myhost:8501/mypath1;/mypath2;/mypath3");
UriComponents uriComponents = resolver.parseLocation(null,
ConfigDataLocation.of("consul:myhost:8501/mypath1;/mypath2;/mypath3"));
assertThat(uriComponents.toUri()).hasScheme("consul").hasHost("myhost").hasPort(8501)
.hasPath("/mypath1;/mypath2;/mypath3");
uriComponents = resolver.parseLocation(null, "consul:myhost:8501");
uriComponents = resolver.parseLocation(null, ConfigDataLocation.of("consul:myhost:8501"));
assertThat(uriComponents.toUri()).hasScheme("consul").hasHost("myhost").hasPort(8501).hasPath("");
}
@Test
public void testResolveProfileSpecificWithCustomPaths() {
String location = "consul:myhost:8501/mypath1;/mypath2;/mypath3";
List<ConsulConfigDataLocation> locations = testResolveProfileSpecific(location);
List<ConsulConfigDataResource> locations = testResolveProfileSpecific(location);
assertThat(locations).hasSize(3);
assertThat(toContexts(locations)).containsExactly("/mypath1/", "/mypath2/", "/mypath3/");
}
@@ -60,7 +62,7 @@ public class ConsulConfigDataLocationResolverTests {
@Test
public void testResolveProfileSpecificWithAutomaticPaths() {
String location = "consul:myhost";
List<ConsulConfigDataLocation> locations = testResolveProfileSpecific(location);
List<ConsulConfigDataResource> locations = testResolveProfileSpecific(location);
assertThat(locations).hasSize(4);
assertThat(toContexts(locations)).containsExactly("config/testapp,dev/", "config/testapp/",
"config/application,dev/", "config/application/");
@@ -74,11 +76,11 @@ public class ConsulConfigDataLocationResolverTests {
assertThat(properties.getPort()).isEqualTo(8502);
}
private List<String> toContexts(List<ConsulConfigDataLocation> locations) {
return locations.stream().map(ConsulConfigDataLocation::getContext).collect(Collectors.toList());
private List<String> toContexts(List<ConsulConfigDataResource> locations) {
return locations.stream().map(ConsulConfigDataResource::getContext).collect(Collectors.toList());
}
private List<ConsulConfigDataLocation> testResolveProfileSpecific(String location) {
private List<ConsulConfigDataResource> testResolveProfileSpecific(String location) {
ConsulConfigDataLocationResolver resolver = createResolver();
ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class);
MockEnvironment env = new MockEnvironment();
@@ -86,7 +88,7 @@ public class ConsulConfigDataLocationResolverTests {
when(context.getBinder()).thenReturn(Binder.get(env));
Profiles profiles = mock(Profiles.class);
when(profiles.getAccepted()).thenReturn(Collections.singletonList("dev"));
return resolver.resolveProfileSpecific(context, location, false, profiles);
return resolver.resolveProfileSpecific(context, ConfigDataLocation.of(location), profiles);
}
private ConsulConfigDataLocationResolver createResolver() {