Skip to content

Commit

Permalink
Add mappings for MongoDb and Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHess committed Nov 3, 2023
1 parent b48a048 commit 213cab3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@

namespace Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding;

internal sealed class MongoDbLegacyConnectorPostProcessor : IConfigurationPostProcessor
{
internal const string BindingTypeKey = "mongodb";

public void PostProcessConfiguration(PostProcessorConfigurationProvider provider, IDictionary<string, string> configData)
{
if (!provider.IsBindingTypeEnabled(BindingTypeKey))
{
return;
}

configData.Filter(ServiceBindingConfigurationProvider.KubernetesBindingsPrefix, ServiceBindingConfigurationProvider.TypeKey, BindingTypeKey).ForEach(
bindingNameKey =>
{
var mapper = new ServiceBindingMapper(configData, bindingNameKey, "mongodb", "client");

// Spring -> spring.data.mongodb....
// Steeltoe -> mongodb:client:...
mapper.MapFromTo("host", "server");
mapper.MapFromTo("port", "port");
mapper.MapFromTo("database", "database");
mapper.MapFromTo("username", "username");
mapper.MapFromTo("password", "password");
});
}
}

internal sealed class MySqlLegacyConnectorPostProcessor : IConfigurationPostProcessor
{
internal const string BindingTypeKey = "mysql";
Expand Down Expand Up @@ -100,3 +127,28 @@ public void PostProcessConfiguration(PostProcessorConfigurationProvider provider
});
}
}

internal sealed class RedisLegacyConnectorPostProcessor : IConfigurationPostProcessor
{
internal const string BindingTypeKey = "redis";

public void PostProcessConfiguration(PostProcessorConfigurationProvider provider, IDictionary<string, string> configData)
{
if (!provider.IsBindingTypeEnabled(BindingTypeKey))
{
return;
}

configData.Filter(ServiceBindingConfigurationProvider.KubernetesBindingsPrefix, ServiceBindingConfigurationProvider.TypeKey, BindingTypeKey).ForEach(
bindingNameKey =>
{
var mapper = new ServiceBindingMapper(configData, bindingNameKey, "redis", "client");

mapper.MapFromTo("host", "host");
mapper.MapFromTo("port", "port");
mapper.MapFromTo("ssl", "ssl");
mapper.MapFromTo("password", "password");
mapper.MapFromTo("client-name", "clientName");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ namespace Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding.Test;

public class LegacyConnectorsPostProcessorsTest : BasePostProcessorsTest
{
[Fact]
public void MongoDbTest_BindingTypeEnabled()
{
var postProcessor = new MongoDbLegacyConnectorPostProcessor();

Dictionary<string, string> configData = GetConfigData(_testBindingName, MongoDbPostProcessor.BindingTypeKey, Tuple.Create("database", "test-database"), Tuple.Create("host", "test-host"), Tuple.Create("password", "test-password"), Tuple.Create("port", "test-port"), Tuple.Create("username", "test-username"));

// BindingType enabled
postProcessor.PostProcessConfiguration(GetConfigurationProvider(postProcessor, MongoDbPostProcessor.BindingTypeKey, true), configData);
Assert.Equal("test-host", configData["mongodb:client:server"]);
Assert.Equal("test-port", configData["mongodb:client:port"]);
Assert.Equal("test-database", configData["mongodb:client:database"]);
Assert.Equal("test-username", configData["mongodb:client:username"]);
Assert.Equal("test-password", configData["mongodb:client:password"]);
}

[Fact]
public void MySqlTest_BindingTypeEnabled()
{
Expand Down Expand Up @@ -63,4 +79,20 @@ public void RabbitMQTest_BindingTypeEnabled()
Assert.Equal("test-username", configData["rabbitmq:client:username"]);
Assert.Equal("test-virtual-host", configData["rabbitmq:client:virtualhost"]);
}

[Fact]
public void RedisTest_BindingTypeEnabled()
{
var postProcessor = new RedisLegacyConnectorPostProcessor();

Dictionary<string, string> configData = GetConfigData(_testBindingName, RedisPostProcessor.BindingTypeKey, Tuple.Create("host", "test-host"), Tuple.Create("password", "test-password"), Tuple.Create("port", "test-port"), Tuple.Create("username", "test-username"), Tuple.Create("client-name", "test-client"), Tuple.Create("ssl", "true"));

// BindingType enabled
postProcessor.PostProcessConfiguration(GetConfigurationProvider(postProcessor, RedisPostProcessor.BindingTypeKey, true), configData);
Assert.Equal("test-host", configData["redis:client:host"]);
Assert.Equal("test-port", configData["redis:client:port"]);
Assert.Equal("true", configData["redis:client:ssl"]);
Assert.Equal("test-password", configData["redis:client:password"]);
Assert.Equal("test-client", configData["redis:client:clientName"]);
}
}

0 comments on commit 213cab3

Please sign in to comment.