Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

namespace Masa.Dcc.Infrastructure.Domain.Services;

public class InitConfigObjectDomainService(
IDomainEventBus eventBus,
IConfigObjectReleaseRepository configObjectReleaseRepository,
IConfigObjectRepository configObjectRepository,
IPublicConfigRepository publicConfigRepository,
IMultilevelCacheClient memoryCacheClient,
IPmClient pmClient,
IMasaStackConfig masaStackConfig,
ILogger<InitConfigObjectDomainService> logger,
IUnitOfWork unitOfWork) : DomainService(eventBus)

Check warning on line 15 in Masa.Dcc.Infrastructure.Domain/Services/InitConfigObjectDomainService.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Constructor has 8 new parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=masastack_MASA.DCC&issues=AZzlG7AiS4YnTyer1eBR&open=AZzlG7AiS4YnTyer1eBR&pullRequest=656
{
private readonly IConfigObjectReleaseRepository _configObjectReleaseRepository = configObjectReleaseRepository;
private readonly IConfigObjectRepository _configObjectRepository = configObjectRepository;
private readonly IPublicConfigRepository _publicConfigRepository = publicConfigRepository;
private readonly IMultilevelCacheClient _memoryCacheClient = memoryCacheClient;
private readonly IPmClient _pmClient = pmClient;
private readonly IMasaStackConfig _masaStackConfig = masaStackConfig;
private readonly IUnitOfWork _unitOfWork = unitOfWork;
private readonly ILogger _logger = logger;

private string EncryptContent(string content)
{
var secret = _masaStackConfig.DccSecret;

var encryptContent = AesUtils.Encrypt(content, secret, FillType.Left);
return encryptContent;
}

private async Task AddConfigObjectReleaseAsync(AddConfigObjectReleaseDto dto)
{
var configObject = await _configObjectRepository.FindAsync(configObject => configObject.Id == dto.ConfigObjectId);
if (configObject == null)
throw new Exception($"Config object ConfigObjectId:{dto.ConfigObjectId} does not exist");

Check warning on line 38 in Masa.Dcc.Infrastructure.Domain/Services/InitConfigObjectDomainService.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.Exception' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=masastack_MASA.DCC&issues=AZzlG7AiS4YnTyer1eBS&open=AZzlG7AiS4YnTyer1eBS&pullRequest=656

configObject.AddContent(configObject.Content, configObject.Content);
await _configObjectRepository.UpdateAsync(configObject);

var configObjectRelease = new ConfigObjectRelease(
dto.ConfigObjectId,
dto.Name,
dto.Comment,
configObject.Content);
await _configObjectReleaseRepository.AddAsync(configObjectRelease);

//add redis cache
var key = $"{dto.EnvironmentName}-{dto.ClusterName}-{dto.Identity}-{configObject.Name}";
if (configObject.Encryption)
{
dto.Content = EncryptContent(dto.Content);
}
var releaseContent = new PublishReleaseModel
{
Content = dto.Content,
FormatLabelCode = configObject.FormatLabelCode,
Encryption = configObject.Encryption
};
await _memoryCacheClient.SetAsync(key.ToLower(), releaseContent);
}

public async Task InitConfigObjectAsync(
string environmentName,
string clusterName,
string appId,
Dictionary<string, string> configObjects,
ConfigObjectType configObjectType = ConfigObjectType.App,
bool isEncryption = false)
{
var envs = await _pmClient.EnvironmentService.GetListAsync();
var env = envs.FirstOrDefault(e => e.Name.ToLower() == environmentName.ToLower()) ?? throw new UserFriendlyException("Environment does not exist");
var clusters = await _pmClient.ClusterService.GetListByEnvIdAsync(env.Id);
var cluster = clusters.FirstOrDefault(c => c.Name.ToLower() == clusterName.ToLower()) ?? throw new UserFriendlyException("Cluster does not exist");

var publicConfig = await _publicConfigRepository.FindAsync(publicConfig => publicConfig.Identity == appId);
if (publicConfig == null)
throw new ArgumentException($"dcc init failed: Identity {appId} is not exists in PublicConfigs");

foreach (var configObject in configObjects)
{
var configObjectName = configObject.Key;
string content = configObject.Value;
if (isEncryption)
content = EncryptContent(content);

var newConfigObject = await _configObjectRepository.FindAsync(x => x.Name == configObjectName && x.Type == configObjectType);

if (newConfigObject == null)
{
newConfigObject = new ConfigObject(
configObjectName,
"JSON",
configObjectType,
content,
"{}",
encryption: isEncryption);

newConfigObject.SetConfigObjectType(ConfigObjectType.App);
newConfigObject.SetPublicConfigObject(publicConfig.Id, cluster.EnvironmentClusterId);

await _configObjectRepository.AddAsync(newConfigObject);
await _unitOfWork.SaveChangesAsync();
}

var key = $"{environmentName}-{clusterName}-{appId}-{configObjectName}".ToLower();
var redisData = await _memoryCacheClient.GetAsync<PublishReleaseModel?>(key);
if (redisData != null)
continue;

_logger.LogInformation("InitConfigObjectAsync add redis cache key:{Key}", key);

var releaseModel = new AddConfigObjectReleaseDto
{
Type = ReleaseType.MainRelease,
ConfigObjectId = newConfigObject.Id,
Name = "通过Sdk发布",
EnvironmentName = environmentName,
ClusterName = clusterName,
Identity = appId,
Content = configObject.Value
};

await AddConfigObjectReleaseAsync(releaseModel);
}
}
}
1 change: 1 addition & 0 deletions Masa.Dcc.Infrastructure.Domain/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
global using Masa.Dcc.Infrastructure.Repository.Label;
global using Masa.Utils.Security.Cryptography;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.Logging;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(DotnetVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(DotnetVersion)" />
<PackageReference Include="Masa.Contrib.Data.EFCore.SqlServer" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="$(DotnetVersion)">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="$(DotnetVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -23,5 +23,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>

</Project>
Loading