Refactor VaultServerInstanceProvider into interface.

We now provide a VaultServerInstanceProvider interface with a default implementation based on DiscoveryClient as bean.

See gh-186.
This commit is contained in:
Mark Paluch
2018-01-23 14:55:33 +01:00
parent 847539afc9
commit 8545578a3d
3 changed files with 53 additions and 17 deletions

View File

@@ -50,18 +50,21 @@ public class DiscoveryClientVaultBootstrapConfiguration {
private final VaultProperties vaultProperties;
private final VaultServerInstanceProvider instanceProvider;
public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties,
DiscoveryClient discoveryClient) {
public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties) {
this.vaultProperties = vaultProperties;
this.instanceProvider = new VaultServerInstanceProvider(discoveryClient);
}
@Bean
@ConditionalOnMissingBean
public VaultEndpointProvider vaultEndpointProvider() {
public VaultServiceInstanceProvider vaultServerInstanceProvider(
DiscoveryClient discoveryClient) {
return new DiscoveryClientVaultServiceInstanceProvider(discoveryClient);
}
@Bean
@ConditionalOnMissingBean
public VaultEndpointProvider vaultEndpointProvider(
VaultServiceInstanceProvider instanceProvider) {
final String serviceId = this.vaultProperties.getDiscovery().getServiceId();
@@ -86,12 +89,6 @@ public class DiscoveryClientVaultBootstrapConfiguration {
vaultEndpoint.setScheme(server.isSecure() ? "https" : fallbackScheme);
}
return new VaultEndpointProvider() {
@Override
public VaultEndpoint getVaultEndpoint() {
return vaultEndpoint;
}
};
return () -> vaultEndpoint;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 the original author or authors.
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,11 +31,13 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
*/
@CommonsLog
@RequiredArgsConstructor
class VaultServerInstanceProvider {
public class DiscoveryClientVaultServiceInstanceProvider implements
VaultServiceInstanceProvider {
private final DiscoveryClient client;
ServiceInstance getVaultServerInstance(String serviceId) {
@Override
public ServiceInstance getVaultServerInstance(String serviceId) {
log.debug("Locating Vault server (" + serviceId + ") via discovery");

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.vault.config;
import org.springframework.cloud.client.ServiceInstance;
/**
* Provider interface to obtain a {@link ServiceInstance} to look up the Vault service.
*
* @author Mark Paluch
* @since 1.1
*/
@FunctionalInterface
public interface VaultServiceInstanceProvider {
/**
* Lookup {@link ServiceInstance} by {@code serviceId}.
*
* @param serviceId the service Id.
* @return {@link ServiceInstance} for the given {@code serviceId}.
* @throws IllegalStateException if no service with {@code serviceId} was found.
*/
ServiceInstance getVaultServerInstance(String serviceId);
}