Skip to content

Commit

Permalink
[~] Upsert AppZone when appserver in zone are deleted or added
Browse files Browse the repository at this point in the history
[~] Some fixes prod state(WIP) CactuseSecurity#2597
(contains test data)
  • Loading branch information
SolidProgramming committed Nov 27, 2024
1 parent 8c1b70e commit d76bb8d
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 119 deletions.
17 changes: 17 additions & 0 deletions roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,22 @@ public ModellingAppZone(ModellingAppZone appZone) : base(appZone)
AppServers = appZone.AppServers;
Area = appZone.Area;
}

public ModellingAppZone(NetworkObject nwObj, ModellingNamingConvention? namCon = null) : base(nwObj, namCon)
{
Comment = nwObj.Comment;
CreationDate = nwObj.CreateTime.Time;
AppServers = ConvertNwObjectsToAppServers(nwObj.ObjectGroupFlats);
}

private static List<ModellingAppServerWrapper> ConvertNwObjectsToAppServers(GroupFlat<NetworkObject>[] groupFlats)
{
List<ModellingAppServerWrapper> appServers = [];
foreach (var obj in groupFlats.Where(x => x.Object?.IP != null && x.Object?.IP != "").ToList())
{
appServers.Add(new ModellingAppServerWrapper() { Content = obj.Object != null ? new(obj.Object) : new() });
}
return appServers;
}
}
}
1 change: 1 addition & 0 deletions roles/lib/files/FWO.Services/AppZoneStateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace FWO.Services
{
//Luca test
internal static class AppZoneStateHelper
{
//int = mgtId = Management Id
Expand Down
135 changes: 106 additions & 29 deletions roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FWO.Api.Client.Queries;
using FWO.Api.Data;
using FWO.Config.Api;
using System.Linq;
using System.Text.Json;

namespace FWO.Services
Expand All @@ -11,7 +12,7 @@ public class ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig use
{
private ModellingNamingConvention NamingConvention = new();

public async Task CreateAppZones(string extAppId)
public async Task CreateAppZone(string extAppId)
{
List<FwoOwner> owners = await apiConnection.SendQueryAsync<List<FwoOwner>>(OwnerQueries.getOwners);

Expand All @@ -28,7 +29,7 @@ public async Task CreateAppZones(string extAppId)
await CreateAppZone(owner);
}

public async Task CreateAppZones(int appId)
public async Task CreateAppZone(int appId)
{
List<FwoOwner> owners = await apiConnection.SendQueryAsync<List<FwoOwner>>(OwnerQueries.getOwners);

Expand All @@ -45,7 +46,7 @@ public async Task CreateAppZones(int appId)
await CreateAppZone(owner);
}

public async Task<ModellingAppZone>? CreateAppZone(FwoOwner owner)
public async Task<ModellingAppZone?> CreateAppZone(FwoOwner owner)
{
//await DeleteExistingAppZones(owner.Id);

Expand All @@ -67,11 +68,72 @@ public async Task CreateAppZones(int appId)

appZone.Id = appZoneId;

await AddAppServersToAppZone(appZone);
await AddAppServersToAppZone(appZoneId, appZone.AppServers);

return appZone;
}

public async Task<ModellingAppZone?> UpsertAppZone(FwoOwner owner)
{
ModellingAppZone? appZone;

List<ModellingAppServer> tempAppServers = await apiConnection.SendQueryAsync<List<ModellingAppServer>>(ModellingQueries.getAppServers, new { appId = owner.Id });
List<ModellingAppServerWrapper> allAppServers = [];

foreach (ModellingAppServer appServer in tempAppServers)
{
allAppServers.Add(new ModellingAppServerWrapper() { Content = appServer });
}

appZone = await GetExistingAppZone(owner.Id);

if (appZone is null)
{
appZone = new()
{
AppId = owner.Id
};

ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone);

appZone.AppServers.AddRange(allAppServers);

int newAppZoneId = await AddAppZoneToDb(appZone);
appZone.Id = newAppZoneId;
}
else
{
List<ModellingAppServerWrapper>? removedAppServers = FindRemovedAppServers(appZone, allAppServers);

if (removedAppServers.Count > 0)
{
await RemoveAppServersFromAppZone(appZone.Id, removedAppServers);

appZone.AppServers.RemoveAll(_ => removedAppServers.Contains(_));
}

List<ModellingAppServerWrapper>? newAppServers = FindNewAppServers(appZone, allAppServers);

if (newAppServers.Count > 0)
{
await AddAppServersToAppZone(appZone.Id, newAppServers);
appZone.AppServers.AddRange(newAppServers);
}
}

return appZone;
}

private static List<ModellingAppServerWrapper> FindNewAppServers(ModellingAppZone existingAppZone, List<ModellingAppServerWrapper> allAppServers)
{
return allAppServers.Except(existingAppZone.AppServers, new AppServerComparer()).ToList();
}

private static List<ModellingAppServerWrapper> FindRemovedAppServers(ModellingAppZone existingAppZone, List<ModellingAppServerWrapper> allAppServers)
{
return existingAppZone.AppServers.Except(allAppServers, new AppServerComparer()).ToList();
}

public async Task<ModellingAppZone?> CreateAppZone(ModellingConnection conn, FwoOwner owner)
{
if (conn is null || ( conn.SourceAppServers.Count == 0 && conn.DestinationAppServers.Count == 0 ))
Expand All @@ -84,8 +146,6 @@ public async Task CreateAppZones(int appId)

ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone);

//await DeleteExistingAppZones(appZone.AppId);

foreach (ModellingAppServerWrapper srcAppServer in conn.SourceAppServers)
{
appZone.AppServers.Add(srcAppServer);
Expand All @@ -100,48 +160,44 @@ public async Task CreateAppZones(int appId)

appZone.Id = appZoneId;

await AddAppServersToAppZone(appZone);
await AddAppServersToAppZone(appZoneId, appZone.AppServers);

return appZone;
}

private async Task DeleteExistingAppZones(int? ownerId)
private async Task DeleteExistingAppZone(int ownerId)
{
(bool success, List<ModellingAppZone>? existingAppZones) = await GetExistingAppZones(ownerId);
ModellingAppZone? existingAppZone = await GetExistingAppZone(ownerId);

if (success && existingAppZones is not null)
if (existingAppZone is not null)
{
foreach (ModellingAppZone existingAppZone in existingAppZones)
try
{
try
{
await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.deleteNwGroup, new { id = existingAppZone.Id });

await LogChange(ModellingTypes.ChangeType.Delete, ModellingTypes.ModObjectType.AppZone, existingAppZone.Id, $"Delete App Zone: {existingAppZone.Display()}", ownerId);
}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("delete_app_zone"), userConfig.GetText("E9201"), true);
}
await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.deleteNwGroup, new { id = existingAppZone.Id });

await LogChange(ModellingTypes.ChangeType.Delete, ModellingTypes.ModObjectType.AppZone, existingAppZone.Id, $"Delete App Zone: {existingAppZone.Display()}", ownerId);
}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("delete_app_zone"), userConfig.GetText("E9201"), true);
}
}
}

public async Task<(bool, List<ModellingAppZone>?)> GetExistingAppZones(int? appId)
public async Task<ModellingAppZone?> GetExistingAppZone(int appId)
{
try
{
List<ModellingAppZone> existingAppZones = await apiConnection.SendQueryAsync<List<ModellingAppZone>>(ModellingQueries.getAppZonesByAppId, new { appId = appId });
List<ModellingAppZone>? existingAppZones = await apiConnection.SendQueryAsync<List<ModellingAppZone>>(ModellingQueries.getAppZonesByAppId, new { appId = appId });

if (existingAppZones is not null)
return (true, existingAppZones);
return existingAppZones.FirstOrDefault();
}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), userConfig.GetText("E9203"), true);
}

return (false, default);
return default;
}

private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone)
Expand Down Expand Up @@ -179,14 +235,14 @@ private async Task<int> AddAppZoneToDb(ModellingAppZone appZone)
return -1;
}

public async Task AddAppServersToAppZone(ModellingAppZone appZone)
public async Task AddAppServersToAppZone(long appZoneId, List<ModellingAppServerWrapper> appServers)
{
foreach (ModellingAppServerWrapper appServer in appZone.AppServers)
foreach (ModellingAppServerWrapper appServer in appServers)
{
var nwobject_nwgroupVars = new
{
nwObjectId = appServer.Content.Id,
nwGroupId = appZone.Id
nwGroupId = appZoneId
};

try
Expand All @@ -199,5 +255,26 @@ public async Task AddAppServersToAppZone(ModellingAppZone appZone)
}
}
}

public async Task RemoveAppServersFromAppZone(long appZoneId, List<ModellingAppServerWrapper> appServers)
{
foreach (ModellingAppServer appServer in ModellingAppServerWrapper.Resolve(appServers))
{
var nwobject_nwgroupVars = new
{
nwObjectId = appServer.Id,
nwGroupId = appZoneId
};

try
{
await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.removeNwObjectFromNwGroup, nwobject_nwgroupVars);
}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), userConfig.GetText("E9204"), true);
}
}
}
}
}
Loading

0 comments on commit d76bb8d

Please sign in to comment.