Started to add CloudFoundry configuration setting support
Still more to do!
This commit is contained in:
Родитель
f8fc070ab7
Коммит
91f5b6f60a
|
@ -28,6 +28,8 @@ namespace Spring.Extensions.Configuration.Server
|
||||||
public static class ConfigServerConfigurationExtensions
|
public static class ConfigServerConfigurationExtensions
|
||||||
{
|
{
|
||||||
private const string SPRING_APPLICATION_PREFIX = "spring:application";
|
private const string SPRING_APPLICATION_PREFIX = "spring:application";
|
||||||
|
private const string VCAP_SERVICES_PREFIX = "vcap:services";
|
||||||
|
private const string VCAP_SERVICES_CONFIGSERVER_PREFIX = "vcap:services:p-config-server:0";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The prefix (<see cref="IConfigurationSection"/> under which all Spring Cloud Config Server
|
/// The prefix (<see cref="IConfigurationSection"/> under which all Spring Cloud Config Server
|
||||||
|
@ -94,21 +96,20 @@ namespace Spring.Extensions.Configuration.Server
|
||||||
{
|
{
|
||||||
|
|
||||||
var clientConfigsection = root.GetSection(PREFIX);
|
var clientConfigsection = root.GetSection(PREFIX);
|
||||||
var appSection = root.GetSection(SPRING_APPLICATION_PREFIX);
|
|
||||||
|
|
||||||
settings.Name = ResovlePlaceholders(GetApplicationName(clientConfigsection, appSection), root);
|
settings.Name = ResovlePlaceholders(GetApplicationName(clientConfigsection, root), root);
|
||||||
settings.Environment = ResovlePlaceholders(GetEnvironment(clientConfigsection, environment), root);
|
settings.Environment = ResovlePlaceholders(GetEnvironment(clientConfigsection, environment), root);
|
||||||
settings.Label = ResovlePlaceholders(GetLabel(clientConfigsection), root);
|
settings.Label = ResovlePlaceholders(GetLabel(clientConfigsection), root);
|
||||||
settings.Username = ResovlePlaceholders(GetUsername(clientConfigsection), root);
|
settings.Username = ResovlePlaceholders(GetUsername(clientConfigsection), root);
|
||||||
settings.Password = ResovlePlaceholders(GetPassword(clientConfigsection), root);
|
settings.Password = ResovlePlaceholders(GetPassword(clientConfigsection), root);
|
||||||
settings.Uri = ResovlePlaceholders(GetUri(clientConfigsection), root);
|
settings.Uri = ResovlePlaceholders(GetUri(clientConfigsection, root), root);
|
||||||
settings.Enabled = GetEnabled(clientConfigsection, root);
|
settings.Enabled = GetEnabled(clientConfigsection, root);
|
||||||
settings.FailFast = GetFailFast(clientConfigsection, root);
|
settings.FailFast = GetFailFast(clientConfigsection, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool GetFailFast(IConfigurationSection section, ConfigurationRoot root)
|
private static bool GetFailFast(IConfigurationSection configServerSection, ConfigurationRoot root)
|
||||||
{
|
{
|
||||||
var failFast = section["failFast"];
|
var failFast = configServerSection["failFast"];
|
||||||
if (!string.IsNullOrEmpty(failFast))
|
if (!string.IsNullOrEmpty(failFast))
|
||||||
{
|
{
|
||||||
bool result;
|
bool result;
|
||||||
|
@ -119,9 +120,9 @@ namespace Spring.Extensions.Configuration.Server
|
||||||
return ConfigServerClientSettings.DEFAULT_FAILFAST;
|
return ConfigServerClientSettings.DEFAULT_FAILFAST;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool GetEnabled(IConfigurationSection section, ConfigurationRoot root)
|
private static bool GetEnabled(IConfigurationSection configServerSection, ConfigurationRoot root)
|
||||||
{
|
{
|
||||||
var enabled = section["enabled"];
|
var enabled = configServerSection["enabled"];
|
||||||
if (!string.IsNullOrEmpty(enabled))
|
if (!string.IsNullOrEmpty(enabled))
|
||||||
{
|
{
|
||||||
bool result;
|
bool result;
|
||||||
|
@ -133,42 +134,54 @@ namespace Spring.Extensions.Configuration.Server
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetUri(IConfigurationSection section)
|
private static string GetUri(IConfigurationSection configServerSection, ConfigurationRoot root)
|
||||||
{
|
{
|
||||||
var uri = section["uri"];
|
// First check for spring:cloud:config:uri
|
||||||
|
var uri = configServerSection["uri"];
|
||||||
if (!string.IsNullOrEmpty(uri))
|
if (!string.IsNullOrEmpty(uri))
|
||||||
{
|
{
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Next check for cloudfoundry binding vcap:services:p-config-server:0:credentials:uri
|
||||||
|
var vcapConfigServerSection = root.GetSection(VCAP_SERVICES_CONFIGSERVER_PREFIX);
|
||||||
|
uri = vcapConfigServerSection["credentials:uri"];
|
||||||
|
if (!string.IsNullOrEmpty(uri))
|
||||||
|
{
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take default if none of above
|
||||||
return ConfigServerClientSettings.DEFAULT_URI;
|
return ConfigServerClientSettings.DEFAULT_URI;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetPassword(IConfigurationSection section)
|
private static string GetPassword(IConfigurationSection configServerSection)
|
||||||
{
|
{
|
||||||
return section["password"];
|
return configServerSection["password"];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetUsername(IConfigurationSection section)
|
private static string GetUsername(IConfigurationSection configServerSection)
|
||||||
{
|
{
|
||||||
return section["username"];
|
return configServerSection["username"];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetLabel(IConfigurationSection section)
|
private static string GetLabel(IConfigurationSection configServerSection)
|
||||||
{
|
{
|
||||||
// TODO: multi label support
|
// TODO: multi label support
|
||||||
return section["label"];
|
return configServerSection["label"];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetApplicationName(IConfigurationSection section, IConfigurationSection appSection)
|
private static string GetApplicationName(IConfigurationSection configServerSection, ConfigurationRoot root)
|
||||||
{
|
{
|
||||||
// if spring:cloud:config:name present, use it
|
// if spring:cloud:config:name present, use it
|
||||||
var name = section["name"];
|
var name = configServerSection["name"];
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if spring:application:name present, use it
|
// if spring:application:name present, use it
|
||||||
|
var appSection = root.GetSection(SPRING_APPLICATION_PREFIX);
|
||||||
name = appSection["name"];
|
name = appSection["name"];
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
|
@ -181,14 +194,14 @@ namespace Spring.Extensions.Configuration.Server
|
||||||
|
|
||||||
private static string GetEnvironment(IConfigurationSection section, IHostingEnvironment environment)
|
private static string GetEnvironment(IConfigurationSection section, IHostingEnvironment environment)
|
||||||
{
|
{
|
||||||
// if spring:cloud:config:environment present, use it
|
// if spring:cloud:config:env present, use it
|
||||||
var env = section["env"];
|
var env = section["env"];
|
||||||
if (!string.IsNullOrEmpty(env))
|
if (!string.IsNullOrEmpty(env))
|
||||||
{
|
{
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise use frameworks defined value (its default is Production)
|
// Otherwise use ASP.NET 5 defined value (i.e. ASPNET_ENV or Hosting:Environment) (its default is 'Production')
|
||||||
return environment.EnvironmentName;
|
return environment.EnvironmentName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -339,6 +339,108 @@ namespace Spring.Extensions.Configuration.Server.Test
|
||||||
Assert.Equal("myPassword", settings.Password);
|
Assert.Equal("myPassword", settings.Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddConfigService_WithCloudfoundryEnvironment_ConfiguresClientCorrectly()
|
||||||
|
{
|
||||||
|
|
||||||
|
// Arrange
|
||||||
|
var VCAP_APPLICATION = @"
|
||||||
|
{
|
||||||
|
'vcap': {
|
||||||
|
'application':
|
||||||
|
{
|
||||||
|
'application_id': 'fa05c1a9-0fc1-4fbd-bae1-139850dec7a3',
|
||||||
|
'application_name': 'my-app',
|
||||||
|
'application_uris': [
|
||||||
|
'my-app.10.244.0.34.xip.io'
|
||||||
|
],
|
||||||
|
'application_version': 'fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca',
|
||||||
|
'limits': {
|
||||||
|
'disk': 1024,
|
||||||
|
'fds': 16384,
|
||||||
|
'mem': 256
|
||||||
|
},
|
||||||
|
'name': 'my-app',
|
||||||
|
'space_id': '06450c72-4669-4dc6-8096-45f9777db68a',
|
||||||
|
'space_name': 'my-space',
|
||||||
|
'uris': [
|
||||||
|
'my-app.10.244.0.34.xip.io',
|
||||||
|
'my-app2.10.244.0.34.xip.io'
|
||||||
|
],
|
||||||
|
'users': null,
|
||||||
|
'version': 'fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
|
var VCAP_SERVICES = @"
|
||||||
|
{
|
||||||
|
'vcap': {
|
||||||
|
'services': {
|
||||||
|
'p-config-server': [
|
||||||
|
{
|
||||||
|
'credentials': {
|
||||||
|
'access_token_uri': 'https://p-spring-cloud-services.uaa.wise.com/oauth/token',
|
||||||
|
'client_id': 'p-config-server-a74fc0a3-a7c3-43b6-81f9-9eb6586dd3ef',
|
||||||
|
'client_secret': 'e8KF1hXvAnGd',
|
||||||
|
'uri': 'https://config-ba6b6079-163b-45d2-8932-e2eca0d1e49a.wise.com'
|
||||||
|
},
|
||||||
|
'label': 'p-config-server',
|
||||||
|
'name': 'My Config Server',
|
||||||
|
'plan': 'standard',
|
||||||
|
'tags': [
|
||||||
|
'configuration',
|
||||||
|
'spring-cloud'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
|
var appsettings = @"
|
||||||
|
{
|
||||||
|
'spring': {
|
||||||
|
'application': {
|
||||||
|
'name': '${vcap:application:name?foobar}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
|
var appsettingsPath = ConfigServerTestHelpers.CreateTempFile(appsettings);
|
||||||
|
var vcapAppPath = ConfigServerTestHelpers.CreateTempFile(VCAP_APPLICATION);
|
||||||
|
var vcapServicesPath = ConfigServerTestHelpers.CreateTempFile(VCAP_SERVICES);
|
||||||
|
var environment = new HostingEnvironment();
|
||||||
|
|
||||||
|
var configurationBuilder = new ConfigurationBuilder();
|
||||||
|
configurationBuilder.AddJsonFile(appsettingsPath);
|
||||||
|
configurationBuilder.AddJsonFile(vcapAppPath);
|
||||||
|
configurationBuilder.AddJsonFile(vcapServicesPath);
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
configurationBuilder.AddConfigServer(environment);
|
||||||
|
IConfigurationRoot root = configurationBuilder.Build();
|
||||||
|
|
||||||
|
// Find our provider so we can check settings
|
||||||
|
ConfigServerConfigurationProvider configServerProvider = null;
|
||||||
|
foreach (IConfigurationProvider provider in configurationBuilder.Providers)
|
||||||
|
{
|
||||||
|
configServerProvider = provider as ConfigServerConfigurationProvider;
|
||||||
|
if (configServerProvider != null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Assert.NotNull(configServerProvider);
|
||||||
|
|
||||||
|
// Check settings
|
||||||
|
ConfigServerClientSettings settings = configServerProvider.Settings;
|
||||||
|
Assert.True(settings.Enabled);
|
||||||
|
Assert.False(settings.FailFast);
|
||||||
|
Assert.Equal("https://config-ba6b6079-163b-45d2-8932-e2eca0d1e49a.wise.com", settings.Uri);
|
||||||
|
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, settings.Environment);
|
||||||
|
Assert.Equal("my-app", settings.Name);
|
||||||
|
Assert.Null(settings.Label);
|
||||||
|
Assert.Null(settings.Username);
|
||||||
|
Assert.Null(settings.Password);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче