Skip to content

Commit

Permalink
feat: masa stack config support dcc (#456)
Browse files Browse the repository at this point in the history
* feat: add dcc storage

* feat: update masa config

* feat: masa stack config support dcc

* feat: code review

* feat: update appsettings

* feat: test

* feat: update InitializeMasaStackConfiguration

* feat: update test

* feat: update test

* feat: update test

* feat: update test

* feat: code review

* feat: code review

---------

Co-authored-by: yanpengju <[email protected]>
  • Loading branch information
codding-y and yanpengju authored Feb 16, 2023
1 parent 3e10b50 commit 2b5969f
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public interface IMasaStackConfig

void SetValue(string key, string value);

public List<string> ProjectList();
void SetValues(Dictionary<string, string> configMap);

List<string> GetProjectList();
}
11 changes: 11 additions & 0 deletions src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.StackSdks.Config;

internal static class Constants
{
internal const string DEFAULT_PUBLIC_ID = "public-$Config";

internal const string DEFAULT_CONFIG_NAME = "$public.DefaultConfig";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\Configuration\Masa.BuildingBlocks.Configuration\Masa.BuildingBlocks.Configuration.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\StackSdks\Masa.BuildingBlocks.StackSdks.Config\Masa.BuildingBlocks.StackSdks.Config.csproj" />
<ProjectReference Include="..\..\Configuration\ConfigurationApi\Masa.Contrib.Configuration.ConfigurationApi.Dcc\Masa.Contrib.Configuration.ConfigurationApi.Dcc.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace Masa.Contrib.StackSdks.Config;

public class MasaStackConfig : IMasaStackConfig
{
private readonly IOptions<MasaStackConfigOptions> _options;

public MasaStackConfig(IOptions<MasaStackConfigOptions> options)
public MasaStackConfig(IConfigurationApiClient client)
{
_options = options;
var configs = client.GetAsync<Dictionary<string, string>>(
Environment,
Cluster,
DEFAULT_PUBLIC_ID,
DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult();

MasaStackConfigOptions.SetValues(configs);
}

public RedisModel RedisModel
Expand Down Expand Up @@ -52,9 +56,11 @@ public ElasticModel ElasticModel

public bool SingleSsoClient { get; }

public List<string> ProjectList() => this.GetAllServer().Keys.ToList();
public List<string> GetProjectList() => this.GetAllServer().Keys.ToList();

public string GetValue(string key) => MasaStackConfigOptions.GetValue(key);

public string GetValue(string key) => _options.Value.GetValue(key);
public void SetValue(string key, string value) => MasaStackConfigOptions.SetValue(key, value);

public void SetValue(string key, string value) => _options.Value.SetValue(key, value);
public void SetValues(Dictionary<string, string> configMap) => MasaStackConfigOptions.SetValues(configMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,28 @@ public static string GetWebId(this IMasaStackConfig masaStackConfig, string proj
return obj?[service]?.ToString() ?? "";
}

public static T GetDccMiniOptions<T>(this IMasaStackConfig masaStackConfig)
public static DccOptions GetDefaultDccOptions(this IMasaStackConfig masaStackConfig)
{
var dccServerAddress = GetDccServiceDomain(masaStackConfig);
var redis = masaStackConfig.RedisModel ?? throw new Exception("redis options can not null");

var stringBuilder = new System.Text.StringBuilder(@"{""ManageServiceAddress"":");
stringBuilder.Append($"\"{dccServerAddress}\",");
stringBuilder.Append(@"""RedisOptions"": {""Servers"": [{""Host"": ");
stringBuilder.Append($"\"{redis.RedisHost}\",");
stringBuilder.Append(@$"""Port"":{redis.RedisPort}");
stringBuilder.Append("}],");
stringBuilder.Append(@$"""DefaultDatabase"":{redis.RedisDb},");
stringBuilder.Append(@$"""Password"": ""{redis.RedisPassword}""");
stringBuilder.Append(@"},");
stringBuilder.Append(@"""ConfigObjectSecret"":");
stringBuilder.Append($"\"{masaStackConfig.DccSecret}\",");
stringBuilder.Append(@"""PublicSecret"":");
stringBuilder.Append($"\"{masaStackConfig.DccSecret}\"");
stringBuilder.Append(@"}");
return JsonSerializer.Deserialize<T>(stringBuilder.ToString()) ?? throw new JsonException();
var options = new DccOptions
{
ManageServiceAddress = dccServerAddress,
RedisOptions = new Caching.Distributed.StackExchangeRedis.RedisConfigurationOptions
{
Servers = new List<Caching.Distributed.StackExchangeRedis.RedisServerOptions>
{
new Caching.Distributed.StackExchangeRedis.RedisServerOptions(redis.RedisHost,redis.RedisPort)
},
DefaultDatabase = redis.RedisDb,
Password = redis.RedisPassword
},
Secret = masaStackConfig.DccSecret,
PublicSecret = masaStackConfig.DccSecret
};

return options;
}

public static Guid GetDefaultUserId(this IMasaStackConfig masaStackConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@

namespace Masa.Contrib.StackSdks.Config;

public class MasaStackConfigOptions
public static class MasaStackConfigOptions
{
private ConcurrentDictionary<string, string> ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase);
private static ConcurrentDictionary<string, string> ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase);

public string GetValue(string key) => GetValue(key, () => string.Empty);
public static string GetValue(string key) => GetValue(key, () => string.Empty);

public string GetValue(string key, Func<string> defaultFunc)
public static string GetValue(string key, Func<string> defaultFunc)
{
if (ConfigMap.ContainsKey(key)) return ConfigMap[key];
return defaultFunc.Invoke();
}

public void SetValue(string key, string value) => ConfigMap[key] = value;
public static void SetValue(string key, string value) => ConfigMap[key] = value;

public static void SetValues(Dictionary<string, string> configMap)
{
foreach (var config in configMap)
{
SetValue(config.Key, config.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,76 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
private static void InitializeMasaStackConfiguration(this IServiceCollection services)
private static async Task InitializeMasaStackConfiguration(this IServiceCollection services)
{
services.Configure<MasaStackConfigOptions>(masaStackConfig =>
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var configurationApiManage = serviceProvider.GetRequiredService<IConfigurationApiManage>();
var configurationApiClient = serviceProvider.GetRequiredService<IConfigurationApiClient>();

var configs = new Dictionary<string, string>()
{
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();

masaStackConfig.SetValue(MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION));
masaStackConfig.SetValue(MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString());
masaStackConfig.SetValue(MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME));
masaStackConfig.SetValue(MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE));
masaStackConfig.SetValue(MasaStackConfigConstant.TLS_NAME, configuration.GetValue<string>(MasaStackConfigConstant.TLS_NAME));
masaStackConfig.SetValue(MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER));
masaStackConfig.SetValue(MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL));
masaStackConfig.SetValue(MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS));
masaStackConfig.SetValue(MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING));
masaStackConfig.SetValue(MasaStackConfigConstant.MASA_SERVER, configuration.GetValue<string>(MasaStackConfigConstant.MASA_SERVER));
masaStackConfig.SetValue(MasaStackConfigConstant.MASA_UI, configuration.GetValue<string>(MasaStackConfigConstant.MASA_UI));
masaStackConfig.SetValue(MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC));
masaStackConfig.SetValue(MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT));
masaStackConfig.SetValue(MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD));
masaStackConfig.SetValue(MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET));
});
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION) },
{ MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME) },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE) },
{ MasaStackConfigConstant.TLS_NAME, configuration.GetValue<string>(MasaStackConfigConstant.TLS_NAME) },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER) },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL) },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS) },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING) },
{ MasaStackConfigConstant.MASA_SERVER, configuration.GetValue<string>(MasaStackConfigConstant.MASA_SERVER) },
{ MasaStackConfigConstant.MASA_UI, configuration.GetValue<string>(MasaStackConfigConstant.MASA_UI) },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC) },
{ MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT) },
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD) },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET) }
};

try
{
var remoteConfigs = await configurationApiClient.GetAsync<Dictionary<string, string>>(
configs[MasaStackConfigConstant.ENVIRONMENT],
configs[MasaStackConfigConstant.CLUSTER],
DEFAULT_PUBLIC_ID,
DEFAULT_CONFIG_NAME);

if (remoteConfigs != null)
{
await configurationApiManage.UpdateAsync(
configs[MasaStackConfigConstant.ENVIRONMENT],
configs[MasaStackConfigConstant.CLUSTER],
DEFAULT_PUBLIC_ID,
DEFAULT_CONFIG_NAME,
configs);
}
}
catch (ArgumentException)
{
await configurationApiManage.AddAsync(
configs[MasaStackConfigConstant.ENVIRONMENT],
configs[MasaStackConfigConstant.CLUSTER],
DEFAULT_PUBLIC_ID,
new Dictionary<string, string>
{
{ DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) }
});
}
}

public static IServiceCollection AddMasaStackConfig(this IServiceCollection services, bool init = true)
public static IServiceCollection AddMasaStackConfig(this IServiceCollection services, bool init = false)
{
services.TryAddSingleton<IMasaStackConfig, MasaStackConfig>();
if (init)
{
InitializeMasaStackConfiguration(services);
InitializeMasaStackConfiguration(services).ConfigureAwait(false);
}

services.TryAddScoped<IMasaStackConfig>(serviceProvider =>
{
var client = serviceProvider.GetRequiredService<IConfigurationApiClient>();
return new MasaStackConfig(client);
});

return services;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Configuration;
global using Masa.BuildingBlocks.StackSdks.Config;
global using Masa.BuildingBlocks.StackSdks.Config.Models;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Options;
global using Masa.Contrib.StackSdks.Config;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.Extensions.Options;
global using System.Collections.Concurrent;
global using System.Globalization;
global using System.Security.Cryptography;
global using System.Text;
global using System.Text.Json;
global using System.Text.Json.Nodes;
global using static Masa.Contrib.StackSdks.Config.Constants;
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.Configuration;
using Moq;

namespace Masa.Contrib.StackSdks.Config.Tests;

[TestClass]
public class MasaStackConfigTest
{
private IMasaStackConfig _stackConfig;
private MasaStackConfig _stackConfig;

[TestInitialize]
public void Initialize()
{
var builder = WebApplication.CreateBuilder();
var configuration = builder.Configuration.AddJsonFile("appsettings.json", true, true).Build();
var configs = new Dictionary<string, string>()
{
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION) },
{ MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME) },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE) },
{ MasaStackConfigConstant.TLS_NAME, configuration.GetValue<string>(MasaStackConfigConstant.TLS_NAME) },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER) },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL) },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS) },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING) },
{ MasaStackConfigConstant.MASA_SERVER, configuration.GetValue<string>(MasaStackConfigConstant.MASA_SERVER) },
{ MasaStackConfigConstant.MASA_UI, configuration.GetValue<string>(MasaStackConfigConstant.MASA_UI) },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC) },
{ MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT) },
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD) },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET) }
};

Mock<IConfigurationApiClient> dccClient = new();

dccClient.Setup(aa => aa.GetAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Action<Dictionary<string, string>>>()!))
.ReturnsAsync(configs);

_stackConfig = new MasaStackConfig(dccClient.Object);

builder.Services.AddMasaStackConfig();
_stackConfig = builder.Services.BuildServiceProvider().GetRequiredService<IMasaStackConfig>();
}

[TestMethod]
Expand All @@ -26,9 +57,9 @@ public void TestGetAllServers()
}

[TestMethod]
public void TestGetMiniDccOptions()
public void TestGetDefaultDccOptions()
{
var dccOptions = _stackConfig.GetDccMiniOptions<DccOptions>();
var dccOptions = _stackConfig.GetDefaultDccOptions();

Assert.IsNotNull(dccOptions?.RedisOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.StackSdks.Config;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Options;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Configuration;
global using Microsoft.VisualStudio.TestTools.UnitTesting;

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"MASA_SERVER": "{\"pm\":{\"server\":\"masa-pm-server\",\"ui\":\"masa-pm-ui\"},\"dcc\":{\"server\":\"masa-dcc-server\",\"ui\":\"masa-dcc-ui\"},\"auth\":{\"server\":\"masa-auth-server\",\"ui\":\"masa-auth-ui\",\"sso\":\"masa-auth-sso\"},\"mc\":{\"server\":\"masa-mc-server\",\"ui\":\"masa-mc-web\"},\"scheduler\":{\"server\":\"masa-scheduler-server\",\"ui\":\"masa-scheduler-ui\",\"worker\":\"masa-scheduler-worker\"},\"tsc\":{\"server\":\"masa-tsc-server\",\"ui\":\"masa-tsc-web\"},\"alert\":{\"server\":\"masa-alert-server\",\"ui\":\"masa-alert-web\"}}",
"MASA_UI": "{\"pm\":{\"server\":\"masa-pm-server\",\"ui\":\"masa-pm-ui-demo\"},\"dcc\":{\"server\":\"masa-dcc-server\",\"ui\":\"masa-dcc-ui-demo\"},\"auth\":{\"server\":\"masa-auth-server\",\"ui\":\"masa-auth-ui-demo\",\"sso\":\"masa-auth-sso-demo\"},\"mc\":{\"server\":\"masa-mc-server\",\"ui\":\"masa-mc-web-demo\"},\"scheduler\":{\"server\":\"masa-scheduler-server\",\"ui\":\"masa-scheduler-ui-demo\",\"worker\":\"masa-scheduler-worker\"},\"tsc\":{\"server\":\"masa-tsc-server\",\"ui\":\"masa-tsc-web-demo\"},\"alert\":{\"server\":\"masa-alert-server\",\"ui\":\"masa-alert-web-demo\"}}",
"OTLP_URL": "otel-collector.masastack:9013",
"REDIS": "{\"RedisHost\": \"masastack-redis.masastack\", \"RedisPort\": 6379, \"RedisDb\": 0,\"RedisPassword\": \"p@ssw0rd\"}",
"REDIS": "{\"RedisHost\": \"localhost\", \"RedisPort\": 6379, \"RedisDb\": 0,\"RedisPassword\": \"\"}",
"TLS_NAME": "masastack",
"VERSION": "1.0-Preview1",
"ENVIRONMENT": "Development",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@ public EventMiddlewareTest()
options.Mapping(nameof(MasaUser.Account), "ACCOUNT");
});

builder.Services.AddMasaStackConfig();
Mock<IConfigurationApiClient> dccClient = new();
var configs = new Dictionary<string, string>()
{
{ MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() }
};
dccClient.Setup(aa => aa.GetAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Action<Dictionary<string, string>>>()!))
.ReturnsAsync(configs);

builder.Services.AddSingleton<IMasaStackConfig>(serviceProvider =>
{
return new MasaStackConfig(dccClient.Object);
});
builder.Services.AddTestEventBus(new Assembly[1] { Assembly.GetExecutingAssembly() }, ServiceLifetime.Scoped);
builder.Services.AddStackMiddleware();

Expand Down
Loading

0 comments on commit 2b5969f

Please sign in to comment.