From d59195063856daa73923a10661539190e73d7222 Mon Sep 17 00:00:00 2001 From: Alexandros Nikolopoulos Date: Fri, 6 Mar 2026 20:17:59 +0000 Subject: [PATCH] Code cleanup --- .../Monitoring/LoadBalancingMonitorData.cs | 4 +- .../Monitoring/LoadBalancingStatusData.cs | 3 +- .../Models/Monitoring/BaseMonitorData.cs | 2 + .../HAProxy/HAProxyConfigurator.cs | 2 +- .../Influxdb/Influxdb2DataPersistence.cs | 3 - .../Telegraf/TelegrafDataCollector.cs | 1 + .../Providers/Shared/BaseContainerProvider.cs | 2 +- tilework.core/ServiceCollectionExtensions.cs | 1 - .../LoadBalancing/LoadBalancerService.cs | 255 +++++++++--------- .../Services/Monitoring/MonitoringService.cs | 3 + .../Components/Validators/ValidatePort.cs | 2 + 11 files changed, 139 insertions(+), 139 deletions(-) diff --git a/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingMonitorData.cs b/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingMonitorData.cs index 1f07200..c6a54df 100644 --- a/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingMonitorData.cs +++ b/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingMonitorData.cs @@ -1,6 +1,4 @@ -using System.Reflection; -using Tilework.Core.Attributes; -using Tilework.LoadBalancing.Enums; +using Tilework.Monitoring.Models; namespace Tilework.LoadBalancing.Models; diff --git a/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingStatusData.cs b/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingStatusData.cs index f70168c..84e34af 100644 --- a/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingStatusData.cs +++ b/tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingStatusData.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using Tilework.Core.Attributes; +using Tilework.Monitoring.Models; using Tilework.LoadBalancing.Enums; namespace Tilework.LoadBalancing.Models; diff --git a/tilework.core/Models/Monitoring/BaseMonitorData.cs b/tilework.core/Models/Monitoring/BaseMonitorData.cs index 8fdeab5..dffea7b 100644 --- a/tilework.core/Models/Monitoring/BaseMonitorData.cs +++ b/tilework.core/Models/Monitoring/BaseMonitorData.cs @@ -1,3 +1,5 @@ +namespace Tilework.Monitoring.Models; + public class BaseMonitorData { public DateTimeOffset Timestamp { get; set; } diff --git a/tilework.core/Providers/LoadBalancingProviders/HAProxy/HAProxyConfigurator.cs b/tilework.core/Providers/LoadBalancingProviders/HAProxy/HAProxyConfigurator.cs index 7a33a29..46dd09f 100644 --- a/tilework.core/Providers/LoadBalancingProviders/HAProxy/HAProxyConfigurator.cs +++ b/tilework.core/Providers/LoadBalancingProviders/HAProxy/HAProxyConfigurator.cs @@ -13,9 +13,9 @@ using Tilework.Core.Interfaces; using Tilework.Core.Enums; using Tilework.Core.Models; +using Tilework.Core.Services; using Tilework.CertificateManagement.Interfaces; -using Tilework.CertificateManagement.Enums; using Tilework.Monitoring.Enums; using Tilework.Monitoring.Models; using Tilework.Persistence.LoadBalancing.Models; diff --git a/tilework.core/Providers/MonitoringProviders/Influxdb/Influxdb2DataPersistence.cs b/tilework.core/Providers/MonitoringProviders/Influxdb/Influxdb2DataPersistence.cs index 804c7b2..1a04128 100644 --- a/tilework.core/Providers/MonitoringProviders/Influxdb/Influxdb2DataPersistence.cs +++ b/tilework.core/Providers/MonitoringProviders/Influxdb/Influxdb2DataPersistence.cs @@ -1,5 +1,4 @@ using System.Globalization; -using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -9,8 +8,6 @@ using AutoMapper; using InfluxDB.Client; using InfluxDB.Client.Api.Domain; -using InfluxDB.Client.Core; -using InfluxDB.Client.Writes; using Tilework.Core.Interfaces; using Tilework.Core.Models; diff --git a/tilework.core/Providers/MonitoringProviders/Telegraf/TelegrafDataCollector.cs b/tilework.core/Providers/MonitoringProviders/Telegraf/TelegrafDataCollector.cs index 4fb44cb..1f6f888 100644 --- a/tilework.core/Providers/MonitoringProviders/Telegraf/TelegrafDataCollector.cs +++ b/tilework.core/Providers/MonitoringProviders/Telegraf/TelegrafDataCollector.cs @@ -8,6 +8,7 @@ using Tilework.Core.Interfaces; using Tilework.Core.Models; using Tilework.Core.Enums; +using Tilework.Core.Services; using Tilework.Monitoring.Interfaces; using Tilework.Monitoring.Models; using Tilework.Monitoring.Enums; diff --git a/tilework.core/Providers/Shared/BaseContainerProvider.cs b/tilework.core/Providers/Shared/BaseContainerProvider.cs index c7f47fd..00fffd9 100644 --- a/tilework.core/Providers/Shared/BaseContainerProvider.cs +++ b/tilework.core/Providers/Shared/BaseContainerProvider.cs @@ -1,11 +1,11 @@ using Microsoft.Extensions.Logging; -using System.Linq; using Tilework.Core.Interfaces; using Tilework.Core.Models; using Tilework.Core.Enums; +namespace Tilework.Core.Services; public abstract class BaseContainerProvider { diff --git a/tilework.core/ServiceCollectionExtensions.cs b/tilework.core/ServiceCollectionExtensions.cs index 7a18b6a..28e898d 100644 --- a/tilework.core/ServiceCollectionExtensions.cs +++ b/tilework.core/ServiceCollectionExtensions.cs @@ -87,7 +87,6 @@ public static IServiceCollection AddLoadBalancing(this IServiceCollection servic services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddHostedService(); diff --git a/tilework.core/Services/LoadBalancing/LoadBalancerService.cs b/tilework.core/Services/LoadBalancing/LoadBalancerService.cs index 1309097..9a1e81c 100644 --- a/tilework.core/Services/LoadBalancing/LoadBalancerService.cs +++ b/tilework.core/Services/LoadBalancing/LoadBalancerService.cs @@ -12,9 +12,8 @@ using Tilework.Persistence.LoadBalancing.Models; using Tilework.LoadBalancing.Haproxy; -using Tilework.CertificateManagement.Interfaces; using Tilework.CertificateManagement.Models; - +using Tilework.Monitoring.Services; using Tilework.Core.Persistence; @@ -55,103 +54,103 @@ private ILoadBalancingConfigurator LoadConfigurator(IServiceProvider serviceProv }; } - private static void ValidateRulePriority(ICollection rules, int newPriority) - { - if (newPriority < 0) - { - throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, "Rule priority cannot be negative."); + private static void ValidateRulePriority(ICollection rules, int newPriority) + { + if (newPriority < 0) + { + throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, "Rule priority cannot be negative."); } var maxPriority = rules.Count == 0 ? -1 : rules.Max(r => r.Priority); if (newPriority > maxPriority + 1) { - throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, $"Rule priority cannot be greater than {maxPriority + 1}."); - } - } - - private static void EnsureRuleConditionsAllowed(LoadBalancer balancer, RuleDTO rule) - { - var invalid = rule.Conditions - .Select(condition => condition.Type) - .Distinct() - .Where(conditionType => !LoadBalancerConditionRules.IsAllowed(balancer.Protocol, conditionType)) - .ToList(); - - if (invalid.Count == 0) - { - return; - } - - var invalidList = string.Join(", ", invalid); - throw new ArgumentException( - $"Rule contains condition types not supported by {balancer.Protocol}: {invalidList}.", - nameof(rule)); - } - - private static void EnsureRuleActionAllowed(LoadBalancer balancer, RuleDTO rule) - { - if (rule.Action == null) - { - rule.Action = new RuleAction(); - } - - if (!LoadBalancerActionRules.IsAllowed(balancer.Type, rule.Action.Type)) - { - throw new ArgumentException( - $"Rule action {rule.Action.Type} is not valid for load balancer type {balancer.Type}", - nameof(rule)); - } - - switch (rule.Action.Type) - { - case RuleActionType.Forward: - if (rule.TargetGroup == null || rule.TargetGroup == Guid.Empty) - { - throw new ArgumentException("Forward actions require a target group.", nameof(rule)); - } - break; - case RuleActionType.Redirect: - rule.TargetGroup = null; - if (string.IsNullOrWhiteSpace(rule.Action.RedirectUrl)) - { - throw new ArgumentException("Redirect actions require a destination URL.", nameof(rule)); - } - if (rule.Action.RedirectStatusCode == null) - { - rule.Action.RedirectStatusCode = 302; - } - else if (rule.Action.RedirectStatusCode < 300 || rule.Action.RedirectStatusCode > 399) - { - throw new ArgumentException("Redirect status code must be in the 300-399 range.", nameof(rule)); - } - break; - case RuleActionType.FixedResponse: - rule.TargetGroup = null; - if (rule.Action.FixedResponseStatusCode == null) - { - rule.Action.FixedResponseStatusCode = 200; - } - else if (rule.Action.FixedResponseStatusCode < 100 || rule.Action.FixedResponseStatusCode > 599) - { - throw new ArgumentException("Fixed response status code must be in the 100-599 range.", nameof(rule)); - } - if (string.IsNullOrWhiteSpace(rule.Action.FixedResponseContentType)) - { - rule.Action.FixedResponseContentType = "text/plain"; - } - break; - case RuleActionType.Reject: - rule.TargetGroup = null; - break; - default: - throw new ArgumentOutOfRangeException(nameof(rule.Action.Type), rule.Action.Type, "Unknown rule action type."); - } - } - - private static bool RequiresCertificate(LoadBalancer balancer) - { - return balancer.Protocol == LoadBalancerProtocol.HTTPS || balancer.Protocol == LoadBalancerProtocol.TLS; - } + throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, $"Rule priority cannot be greater than {maxPriority + 1}."); + } + } + + private static void EnsureRuleConditionsAllowed(LoadBalancer balancer, RuleDTO rule) + { + var invalid = rule.Conditions + .Select(condition => condition.Type) + .Distinct() + .Where(conditionType => !LoadBalancerConditionRules.IsAllowed(balancer.Protocol, conditionType)) + .ToList(); + + if (invalid.Count == 0) + { + return; + } + + var invalidList = string.Join(", ", invalid); + throw new ArgumentException( + $"Rule contains condition types not supported by {balancer.Protocol}: {invalidList}.", + nameof(rule)); + } + + private static void EnsureRuleActionAllowed(LoadBalancer balancer, RuleDTO rule) + { + if (rule.Action == null) + { + rule.Action = new RuleAction(); + } + + if (!LoadBalancerActionRules.IsAllowed(balancer.Type, rule.Action.Type)) + { + throw new ArgumentException( + $"Rule action {rule.Action.Type} is not valid for load balancer type {balancer.Type}", + nameof(rule)); + } + + switch (rule.Action.Type) + { + case RuleActionType.Forward: + if (rule.TargetGroup == null || rule.TargetGroup == Guid.Empty) + { + throw new ArgumentException("Forward actions require a target group.", nameof(rule)); + } + break; + case RuleActionType.Redirect: + rule.TargetGroup = null; + if (string.IsNullOrWhiteSpace(rule.Action.RedirectUrl)) + { + throw new ArgumentException("Redirect actions require a destination URL.", nameof(rule)); + } + if (rule.Action.RedirectStatusCode == null) + { + rule.Action.RedirectStatusCode = 302; + } + else if (rule.Action.RedirectStatusCode < 300 || rule.Action.RedirectStatusCode > 399) + { + throw new ArgumentException("Redirect status code must be in the 300-399 range.", nameof(rule)); + } + break; + case RuleActionType.FixedResponse: + rule.TargetGroup = null; + if (rule.Action.FixedResponseStatusCode == null) + { + rule.Action.FixedResponseStatusCode = 200; + } + else if (rule.Action.FixedResponseStatusCode < 100 || rule.Action.FixedResponseStatusCode > 599) + { + throw new ArgumentException("Fixed response status code must be in the 100-599 range.", nameof(rule)); + } + if (string.IsNullOrWhiteSpace(rule.Action.FixedResponseContentType)) + { + rule.Action.FixedResponseContentType = "text/plain"; + } + break; + case RuleActionType.Reject: + rule.TargetGroup = null; + break; + default: + throw new ArgumentOutOfRangeException(nameof(rule.Action.Type), rule.Action.Type, "Unknown rule action type."); + } + } + + private static bool RequiresCertificate(LoadBalancer balancer) + { + return balancer.Protocol == LoadBalancerProtocol.HTTPS || balancer.Protocol == LoadBalancerProtocol.TLS; + } private static void EnsureCertificatesPresentIfRequired(LoadBalancer balancer) { @@ -174,21 +173,21 @@ public async Task> GetLoadBalancers() return _mapper.Map(entity); } - public async Task AddLoadBalancer(LoadBalancerDTO balancer) - { - EnsureProtocolAllowed(balancer); - var entity = _mapper.Map(balancer); + public async Task AddLoadBalancer(LoadBalancerDTO balancer) + { + EnsureProtocolAllowed(balancer); + var entity = _mapper.Map(balancer); await _dbContext.LoadBalancers.AddAsync(entity); await _dbContext.SaveChangesAsync(); return _mapper.Map(entity); } - public async Task UpdateLoadBalancer(LoadBalancerDTO balancer) - { - EnsureProtocolAllowed(balancer); - var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id); - entity = _mapper.Map(balancer, entity); + public async Task UpdateLoadBalancer(LoadBalancerDTO balancer) + { + EnsureProtocolAllowed(balancer); + var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id); + entity = _mapper.Map(balancer, entity); _dbContext.LoadBalancers.Update(entity); await _dbContext.SaveChangesAsync(); @@ -263,12 +262,12 @@ public async Task> GetRules(LoadBalancerDTO balancer) public async Task AddRule(LoadBalancerDTO balancer, RuleDTO rule) { var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id); - if (entity == null) - throw new ArgumentNullException(nameof(balancer)); - - ValidateRulePriority(entity.Rules, rule.Priority); - EnsureRuleConditionsAllowed(entity, rule); - EnsureRuleActionAllowed(entity, rule); + if (entity == null) + throw new ArgumentNullException(nameof(balancer)); + + ValidateRulePriority(entity.Rules, rule.Priority); + EnsureRuleConditionsAllowed(entity, rule); + EnsureRuleActionAllowed(entity, rule); foreach (var existingRule in entity.Rules.Where(r => r.Priority >= rule.Priority)) { @@ -287,12 +286,12 @@ public async Task UpdateRule(LoadBalancerDTO balancer, RuleDTO rule) throw new ArgumentNullException(nameof(balancer)); var ruleEntity = entity.Rules.FirstOrDefault(t => t.Id == rule.Id); - if (ruleEntity == null) - return; - - ValidateRulePriority(entity.Rules, rule.Priority); - EnsureRuleConditionsAllowed(entity, rule); - EnsureRuleActionAllowed(entity, rule); + if (ruleEntity == null) + return; + + ValidateRulePriority(entity.Rules, rule.Priority); + EnsureRuleConditionsAllowed(entity, rule); + EnsureRuleActionAllowed(entity, rule); await using var tx = await _dbContext.Database.BeginTransactionAsync(); @@ -393,24 +392,24 @@ public async Task AddCertificate(LoadBalancerDTO balancer, Guid certificateId) await _dbContext.SaveChangesAsync(); } - public async Task RemoveCertificate(LoadBalancerDTO balancer, Guid certificateId) + public async Task RemoveCertificate(LoadBalancerDTO balancer, Guid certificateId) { var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id); var c = entity.Certificates.FirstOrDefault(t => t.Id == certificateId); entity.Certificates.Remove(c); _dbContext.LoadBalancers.Update(entity); - await _dbContext.SaveChangesAsync(); - } - - private static void EnsureProtocolAllowed(LoadBalancerDTO balancer) - { - if (!LoadBalancerProtocolRules.IsAllowed(balancer.Type, balancer.Protocol)) - { - throw new ArgumentException( - $"Protocol {balancer.Protocol} is not valid for load balancer type {balancer.Type}", - nameof(balancer)); - } - } + await _dbContext.SaveChangesAsync(); + } + + private static void EnsureProtocolAllowed(LoadBalancerDTO balancer) + { + if (!LoadBalancerProtocolRules.IsAllowed(balancer.Type, balancer.Protocol)) + { + throw new ArgumentException( + $"Protocol {balancer.Protocol} is not valid for load balancer type {balancer.Type}", + nameof(balancer)); + } + } public async Task> GetTargetGroups() diff --git a/tilework.core/Services/Monitoring/MonitoringService.cs b/tilework.core/Services/Monitoring/MonitoringService.cs index 7a467c2..65fe9e0 100644 --- a/tilework.core/Services/Monitoring/MonitoringService.cs +++ b/tilework.core/Services/Monitoring/MonitoringService.cs @@ -1,6 +1,9 @@ using Microsoft.Extensions.Logging; using Tilework.Monitoring.Interfaces; +using Tilework.Monitoring.Models; + +namespace Tilework.Monitoring.Services; public class MonitoringService { diff --git a/tilework.ui/Components/Validators/ValidatePort.cs b/tilework.ui/Components/Validators/ValidatePort.cs index f415876..223ec3a 100644 --- a/tilework.ui/Components/Validators/ValidatePort.cs +++ b/tilework.ui/Components/Validators/ValidatePort.cs @@ -1,3 +1,5 @@ +namespace Tilework.Ui.Components.Validators; + public static partial class Validators { public static string? ValidatePort(int? value)