Skip to content

Commit

Permalink
Refactor code and add ShowDiffEnvironmentsCommand
Browse files Browse the repository at this point in the history
Refactored various manifest related code blocks, including renaming fields and improving the manifest loading process. Added the ShowDiffEnvironmentsCommand to show differences between two Creatio instances. Additionally, improved associated tests and extracted repetitive logic into a helper method.

Signed-off-by: Kirill Krylov <[email protected]>
  • Loading branch information
kirillkrylov committed May 31, 2024
1 parent b5feb69 commit 9008aca
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 72 deletions.
32 changes: 10 additions & 22 deletions clio.tests/Command/ShowDiffEnvironmentsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using YamlDotNet.Serialization;

namespace Clio.Tests.Command
Expand All @@ -27,34 +28,21 @@ protected override MockFileSystem CreateFs() {
return mockFS;
}


[Test]
public void Execute_ShouldReturnZero() {
ILogger loggerMock = Substitute.For<ILogger>();
var container = GetContainer();
ShowDiffEnvironmentsCommand diffCommand = new(container.Resolve<IEnvironmentManager>(), container.Resolve<IDataProvider>(), loggerMock);
diffCommand.Execute(new ShowDiffEnvironmentsOptions() {
Source = "SourceEnv",
Origin = "OriginEnv"
});
Assert.AreEqual("{}", _fileSystem.File.ReadAllText("diff-SourceEnv-OriginEnv.yaml").Trim());
}

[Test]
public void MergeManifest_FindPackages() {
[TestCase("packages-source-env-manifest.yaml","packages-target-env-manifest.yaml","packages-diff.yaml")]
[TestCase("syssettings-source-env-manifest.yaml","syssettings-target-env-manifest.yaml","syssettings-diff.yaml")]
[TestCase("features-source-env-manifest.yaml","features-target-env-manifest.yaml","features-diff.yaml")]
public void MergeManifest_FindPackages(string sourceManifestFileName, string targetManifestFileName,string diffManifestFileName) {
ILogger loggerMock = Substitute.For<ILogger>();
var container = GetContainer();
var environmentManager = container.Resolve<IEnvironmentManager>();
var sourceManifestFileName = "packages-source-env-manifest.yaml";
var targetManifestFileName = "packages-target-env-manifest.yaml";
var diffManifestFileName = "packages-diff.yaml";
var sourceManifest = environmentManager.GetEnvironmentFromManifest(sourceManifestFileName);
var targetManifest = environmentManager.GetEnvironmentFromManifest(targetManifestFileName);
var expectedDiffManifest = environmentManager.GetEnvironmentFromManifest(diffManifestFileName);
var sourceManifest = environmentManager.LoadEnvironmentManifestFromFile(sourceManifestFileName);
var targetManifest = environmentManager.LoadEnvironmentManifestFromFile(targetManifestFileName);
EnvironmentManifest expectedDiffManifest = environmentManager.LoadEnvironmentManifestFromFile(diffManifestFileName);
var actualDiffManifest = environmentManager.GetDiffManifest(sourceManifest, targetManifest);
Assert.AreEqual(expectedDiffManifest, actualDiffManifest);
expectedDiffManifest.Should().BeEquivalentTo(actualDiffManifest);
}


private IContainer GetContainer() {
return MockDataContainer.GetContainer(_fileSystem);
}
Expand Down
8 changes: 4 additions & 4 deletions clio.tests/Examples/diff-manifest/features-diff.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
features:
- name: Feature2
enabled: "true"
- name: Feature3
enabled: "false"
- code: Feature2
value: "true"
- code: Feature3
value: "false"
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ environment:
platformversion: "8.1.1"
platformtype: "NET6" # "NET6" or "NETFramework"

syssettings:
- name: SysSettings1
settings:
- code: SysSettings1
value: Value1
- name: SysSettings2
- code: SysSettings2
value: Value2

features:
- name: Feature1
enabled: "true"
- name: Feature2
enabled: "true"
- name: Feature3
enabled: "false"
- code: Feature1
value: "true"
- code: Feature2
value: "true"
- code: Feature3
value: "false"

webservices:
- name: WebService1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ environment:
platformversion: "8.1.1"
platformtype: "NET6" # "NET6" or "NETFramework"

syssettings:
- name: SysSettings1
settings:
- code: SysSettings1
value: Value1
- name: SysSettings2
- code: SysSettings2
value: Value2

features:
- name: Feature1
enabled: "true"
- name: Feature2
enabled: "false"
- code: Feature1
value: "true"
- code: Feature2
value: "false"

webservices:
- name: WebService1
Expand Down
4 changes: 2 additions & 2 deletions clio.tests/Examples/diff-manifest/syssettings-diff.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syssettings:
settings:
- name: SysSettings1
value: Value1
- name: SysSettings2
value: Value2
value: Value2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
platformversion: "8.1.1"
platformtype: "NET6" # "NET6" or "NETFramework"

syssettings:
settings:
- name: SysSettings1
value: Value1
- name: SysSettings2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
platformversion: "8.1.1"
platformtype: "NET6" # "NET6" or "NETFramework"

syssettings:
settings:
- name: SysSettings1
value: Value3

Expand Down
4 changes: 4 additions & 0 deletions clio.tests/clio.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<Content Include="Examples\odata_data_examples\syssettings-odata.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="Examples\diff-manifest\packages-diff.yaml" />
<Content Include="Examples\diff-manifest\packages-diff.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions clio/BindingsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public IContainer Register(EnvironmentSettings settings = null, bool registerNul
containerBuilder.RegisterType<SysSettingsManager>();
}
#endregion

containerBuilder.RegisterInstance(new CreatioSdkOnline()).As<ICreatioSdk>();
containerBuilder.RegisterInstance(deserializer).As<IDeserializer>();
containerBuilder.RegisterInstance(serializer).As<ISerializer>();
Expand Down Expand Up @@ -191,6 +192,7 @@ public IContainer Register(EnvironmentSettings settings = null, bool registerNul
containerBuilder.RegisterType<PackageHotFixCommand>();
containerBuilder.RegisterType<PackageEditableMutator>();
containerBuilder.RegisterType<SaveSettingsToManifestCommand>();
containerBuilder.RegisterType<ShowDiffEnvironmentsCommand>();
additionalRegistrations?.Invoke(containerBuilder);
return containerBuilder.Build();
}
Expand Down
24 changes: 21 additions & 3 deletions clio/Command/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private IEnumerable<AppHubInfo> GetAppHubsFromManifest(string manifestFilePath){
return envManiifest.AppHubs;
}

private EnvironmentManifest LoadEnvironmentManifestFromFile(string manifestFilePath){
public EnvironmentManifest LoadEnvironmentManifestFromFile(string manifestFilePath){
var manifest = fileSystem.File.ReadAllText(manifestFilePath);
var envManifest = yamlDesirializer.Deserialize<EnvironmentManifest>(manifest);

Expand Down Expand Up @@ -97,6 +97,8 @@ public EnvironmentSettings GetEnvironmentFromManifest(string manifestFilePath) {
var envManifest = yamlDesirializer.Deserialize<EnvironmentManifest>(manifest);
return envManifest.EnvironmentSettings;
}



public IEnumerable<Feature> GetFeaturesFromManifest(string manifestFilePath){
var manifest = fileSystem.File.ReadAllText(manifestFilePath);
Expand Down Expand Up @@ -142,8 +144,23 @@ public void SaveManifestToFile(string manifestFileName, EnvironmentManifest envM
fileSystem.File.WriteAllText(manifestFileName, manifestContent);
}

public EnvironmentManifest GetDiffManifest(EnvironmentSettings sourceManifest, EnvironmentSettings targetManifest) {
public EnvironmentManifest GetDiffManifest(EnvironmentManifest sourceManifest, EnvironmentManifest targetManifest) {
var diffManifest = new EnvironmentManifest();

diffManifest.Packages = sourceManifest.Packages
.Where(p => !targetManifest.Packages.Any(sp => sp.Name == p.Name))
.ToList();

diffManifest.Settings = sourceManifest.Settings
.Where(p => !targetManifest.Settings.Any(sp => sp.Code == p.Code && sp.Value == p.Value))
.ToList();

diffManifest.Features = sourceManifest.Features
.Where(p => !targetManifest.Features.Any(sp => sp.Code == p.Code && sp.Value == p.Value))
.ToList();



return diffManifest;
}
}
Expand All @@ -152,6 +169,7 @@ public interface IEnvironmentManager
{
List<SysInstalledApp> GetApplicationsFromManifest(string manifestFilePath);

EnvironmentManifest LoadEnvironmentManifestFromFile(string manifestFilePath);
int ApplyManifest(string manifestFilePath);

List<SysInstalledApp> FindApplicationsInAppHub(string manifestFilePath);
Expand All @@ -162,7 +180,7 @@ public interface IEnvironmentManager
IEnumerable<CreatioManifestWebService> GetWebServicesFromManifest(string manifestFilePath);
List<CreatioManifestPackage> GetPackagesGromManifest(string manifestFileName);
void SaveManifestToFile(string manifestFileName, EnvironmentManifest envManifest, bool overwrite = false);
EnvironmentManifest GetDiffManifest(EnvironmentSettings sourceManifest, EnvironmentSettings targetManifest);
EnvironmentManifest GetDiffManifest(EnvironmentManifest sourceManifest, EnvironmentManifest targetManifest);
}

public class CreatioManifestSetting
Expand Down
7 changes: 6 additions & 1 deletion clio/Command/SaveSettingsToManifestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal class SaveSettingsToManifestOptions : EnvironmentNameOptions
HelpText = "Overwrite manifest file if exists", Default = true)]
public bool Overwrite { get; internal set; }

public bool SkipDone { get; set; }

#endregion

}
Expand Down Expand Up @@ -91,7 +93,10 @@ public override int Execute(SaveSettingsToManifestOptions options){
}
_logger.WriteInfo($"Saving file {options.ManifestFileName}");
_environmentManager.SaveManifestToFile(options.ManifestFileName, environmentManifest, options.Overwrite);
_logger.WriteInfo("Done");

if(!options.SkipDone) {
_logger.WriteInfo("Done");
}
return 0;
}

Expand Down
96 changes: 77 additions & 19 deletions clio/Command/ShowDiffEnvironmentsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,101 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Clio.UserEnvironment;
using DocumentFormat.OpenXml.Drawing;
using k8s.Models;
using YamlDotNet.Serialization;

namespace Clio.Command
{

[Verb("show-diff", Aliases = new[] { "diff", "compare" }, HelpText = "Show difference in settings for two Creatio intances")]
[Verb("show-diff", Aliases = new[] {"diff", "compare"},
HelpText = "Show difference in settings for two Creatio intances")]
internal class ShowDiffEnvironmentsOptions : EnvironmentNameOptions
{

[Option("source", Required = true, HelpText = "Source environment name")]
public string Source { get; internal set; }
public string Origin { get; internal set; }

[Option("target", Required = true, HelpText = "Target environment name")]
public string Target { get; internal set; }

[Option("file", Required = false, HelpText = "Diff file name")]
public string FileName { get; internal set; }

[Option("overwrite", Required = false, HelpText = "Overwrite existing file", Default = true)]
public bool Overwrite { get; internal set; }
}

}

internal class ShowDiffEnvironmentsCommand : BaseDataContextCommand<ShowDiffEnvironmentsOptions>
{
private IEnvironmentManager _environmentManager;
private readonly IEnvironmentManager _environmentManager;
private readonly IWorkingDirectoriesProvider _workingDirectoriesProvider;
private readonly ISerializer _serializer;
private readonly ISettingsRepository _settingsRepository;

public ShowDiffEnvironmentsCommand(IEnvironmentManager environmentManager, IDataProvider provider, ILogger logger) : base(provider, logger) {
public ShowDiffEnvironmentsCommand(IEnvironmentManager environmentManager, IDataProvider provider,
ILogger logger, IWorkingDirectoriesProvider workingDirectoriesProvider,
ISerializer serializer, ISettingsRepository settingsRepository)
: base(provider, logger){
_environmentManager = environmentManager;
_workingDirectoriesProvider = workingDirectoriesProvider;
_serializer = serializer;
_settingsRepository = settingsRepository;
}

public ShowDiffEnvironmentsCommand(IEnvironmentManager environmentManager, IDataProvider provider, ILogger logger, IApplicationClient applicationClient, EnvironmentSettings environmentSettings) : base(provider, logger, applicationClient, environmentSettings) {
_environmentManager = environmentManager;
}

public override int Execute(ShowDiffEnvironmentsOptions options) {
_logger.WriteInfo($"Operating on environment: {options.Uri}");
_logger.WriteInfo("Loading information about webservices");
var manifestFileName = options.FileName ?? $"diff-{options.Source}-{options.Origin}.yaml";
var diffManifest = new EnvironmentManifest();
diffManifest.Packages = new List<CreatioManifestPackage>();
_environmentManager.SaveManifestToFile(manifestFileName, diffManifest, options.Overwrite);
public override int Execute(ShowDiffEnvironmentsOptions options){
if(options.Target == options.Source) {
_logger.WriteInfo("No differences found.");
return 0;
}
var manifestFileName = options.FileName ?? $"diff-{options.Source}-{options.Target}.yaml";
string sourceName = $"source-{options.Source}-manifest.yaml";
string targetName = $"target-{options.Target}-manifest.yaml";

_workingDirectoriesProvider.CreateTempDirectory(tempDirectory => {

var sourceFilePath = System.IO.Path.Combine(tempDirectory, sourceName);
var targetFilePath = System.IO.Path.Combine(tempDirectory, targetName);

SaveEnvironmentManifest(options.Source, sourceFilePath);
SaveEnvironmentManifest(options.Target, targetFilePath);

EnvironmentManifest sourceManifest = _environmentManager.LoadEnvironmentManifestFromFile(sourceFilePath);
EnvironmentManifest targetManifest = _environmentManager.LoadEnvironmentManifestFromFile(targetFilePath);
EnvironmentManifest diffManifest = _environmentManager.GetDiffManifest(sourceManifest, targetManifest);

if(string.IsNullOrEmpty(options.FileName) ){
_logger.WriteInfo("Result diff manifest:");
var result = _serializer.Serialize(diffManifest);
if(string.IsNullOrEmpty(result) || result.Trim() == "{}") {
_logger.WriteInfo("No differences found.");
}else {
_logger.WriteInfo(_serializer.Serialize(diffManifest));
}
}
else{
_logger.WriteInfo($"Diff manifest saved to {manifestFileName}");
_environmentManager.SaveManifestToFile(manifestFileName, diffManifest, options.Overwrite);
}
});
_logger.WriteInfo("Done");
return 0;
}
}

}
private void SaveEnvironmentManifest(string environmentName, string manifestFilePath){
_logger.WriteInfo($"Loading environments manifest from {environmentName}");
var sourceEnv = _settingsRepository.GetEnvironment(environmentName);
var container = new BindingsModule().Register(sourceEnv);
var command = container.Resolve<SaveSettingsToManifestCommand>();
command.Execute(new SaveSettingsToManifestOptions() {
EnvironmentName = environmentName,
ManifestFileName = manifestFilePath,
Overwrite = true,
SkipDone = true
});
}

}
}
4 changes: 1 addition & 3 deletions clio/Common/WorkingDirectoriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public string BaseTempDirectory {
string path = Path.Combine(string.IsNullOrEmpty(tempDir)
? Path.GetTempPath()
: tempDir, "clio");
#if DEBUG
_logger.WriteInfo($"Clio temptDir path: {path}");
#endif

return path;
}
}
Expand Down
2 changes: 2 additions & 0 deletions clio/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ private static int ConvertPackage(ConvertOptions opts)
typeof(ApplyEnvironmentManifestOptions),
typeof(GetWebServiceUrlOptions),
typeof(SaveSettingsToManifestOptions),
typeof(ShowDiffEnvironmentsOptions),

};
public static Func<object, int> ExecuteCommandWithOption = (instance) => {
Expand Down Expand Up @@ -742,6 +743,7 @@ private static int ConvertPackage(ConvertOptions opts)
ApplyEnvironmentManifestOptions opts => ResolveEnvSettings<ApplyEnvironmentManifestCommand>(opts).Execute(opts),
GetWebServiceUrlOptions opts => Resolve<GetWebServiceUrlCommand>(opts).Execute(opts),
SaveSettingsToManifestOptions opts => Resolve<SaveSettingsToManifestCommand>(opts).Execute(opts),
ShowDiffEnvironmentsOptions opts => Resolve<ShowDiffEnvironmentsCommand>(opts).Execute(opts),
_ => 1,
};
};
Expand Down

0 comments on commit 9008aca

Please sign in to comment.