Skip to content

Commit 759bf7d

Browse files
authored
Merge pull request #66 from tileworkdev/develop
SonarQube code cleanups
2 parents d696a7f + e18eb16 commit 759bf7d

File tree

11 files changed

+139
-139
lines changed

11 files changed

+139
-139
lines changed

tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingMonitorData.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Reflection;
2-
using Tilework.Core.Attributes;
3-
using Tilework.LoadBalancing.Enums;
1+
using Tilework.Monitoring.Models;
42

53
namespace Tilework.LoadBalancing.Models;
64

tilework.core/Models/LoadBalancing/Monitoring/LoadBalancingStatusData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Reflection;
2-
using Tilework.Core.Attributes;
1+
using Tilework.Monitoring.Models;
32
using Tilework.LoadBalancing.Enums;
43

54
namespace Tilework.LoadBalancing.Models;

tilework.core/Models/Monitoring/BaseMonitorData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
namespace Tilework.Monitoring.Models;
2+
13
public class BaseMonitorData
24
{
35
public DateTimeOffset Timestamp { get; set; }

tilework.core/Providers/LoadBalancingProviders/HAProxy/HAProxyConfigurator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
using Tilework.Core.Interfaces;
1414
using Tilework.Core.Enums;
1515
using Tilework.Core.Models;
16+
using Tilework.Core.Services;
1617

1718
using Tilework.CertificateManagement.Interfaces;
18-
using Tilework.CertificateManagement.Enums;
1919
using Tilework.Monitoring.Enums;
2020
using Tilework.Monitoring.Models;
2121
using Tilework.Persistence.LoadBalancing.Models;

tilework.core/Providers/MonitoringProviders/Influxdb/Influxdb2DataPersistence.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Globalization;
2-
using System.Linq;
32
using System.Reflection;
43
using System.Text.RegularExpressions;
54

@@ -9,8 +8,6 @@
98
using AutoMapper;
109
using InfluxDB.Client;
1110
using InfluxDB.Client.Api.Domain;
12-
using InfluxDB.Client.Core;
13-
using InfluxDB.Client.Writes;
1411

1512
using Tilework.Core.Interfaces;
1613
using Tilework.Core.Models;

tilework.core/Providers/MonitoringProviders/Telegraf/TelegrafDataCollector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Tilework.Core.Interfaces;
99
using Tilework.Core.Models;
1010
using Tilework.Core.Enums;
11+
using Tilework.Core.Services;
1112
using Tilework.Monitoring.Interfaces;
1213
using Tilework.Monitoring.Models;
1314
using Tilework.Monitoring.Enums;

tilework.core/Providers/Shared/BaseContainerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.Extensions.Logging;
2-
using System.Linq;
32

43

54
using Tilework.Core.Interfaces;
65
using Tilework.Core.Models;
76
using Tilework.Core.Enums;
87

8+
namespace Tilework.Core.Services;
99

1010
public abstract class BaseContainerProvider
1111
{

tilework.core/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public static IServiceCollection AddLoadBalancing(this IServiceCollection servic
8787

8888
services.AddScoped<ILoadBalancerService, LoadBalancerService>();
8989
services.AddScoped<HAProxyConfigurator>();
90-
services.AddScoped<HAProxyMonitor>();
9190

9291
services.AddHostedService<LoadBalancingInitializer>();
9392

tilework.core/Services/LoadBalancing/LoadBalancerService.cs

Lines changed: 127 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
using Tilework.Persistence.LoadBalancing.Models;
1313
using Tilework.LoadBalancing.Haproxy;
1414

15-
using Tilework.CertificateManagement.Interfaces;
1615
using Tilework.CertificateManagement.Models;
17-
16+
using Tilework.Monitoring.Services;
1817
using Tilework.Core.Persistence;
1918

2019

@@ -55,103 +54,103 @@ private ILoadBalancingConfigurator LoadConfigurator(IServiceProvider serviceProv
5554
};
5655
}
5756

58-
private static void ValidateRulePriority(ICollection<Rule> rules, int newPriority)
59-
{
60-
if (newPriority < 0)
61-
{
62-
throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, "Rule priority cannot be negative.");
57+
private static void ValidateRulePriority(ICollection<Rule> rules, int newPriority)
58+
{
59+
if (newPriority < 0)
60+
{
61+
throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, "Rule priority cannot be negative.");
6362
}
6463

6564
var maxPriority = rules.Count == 0 ? -1 : rules.Max(r => r.Priority);
6665
if (newPriority > maxPriority + 1)
6766
{
68-
throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, $"Rule priority cannot be greater than {maxPriority + 1}.");
69-
}
70-
}
71-
72-
private static void EnsureRuleConditionsAllowed(LoadBalancer balancer, RuleDTO rule)
73-
{
74-
var invalid = rule.Conditions
75-
.Select(condition => condition.Type)
76-
.Distinct()
77-
.Where(conditionType => !LoadBalancerConditionRules.IsAllowed(balancer.Protocol, conditionType))
78-
.ToList();
79-
80-
if (invalid.Count == 0)
81-
{
82-
return;
83-
}
84-
85-
var invalidList = string.Join(", ", invalid);
86-
throw new ArgumentException(
87-
$"Rule contains condition types not supported by {balancer.Protocol}: {invalidList}.",
88-
nameof(rule));
89-
}
90-
91-
private static void EnsureRuleActionAllowed(LoadBalancer balancer, RuleDTO rule)
92-
{
93-
if (rule.Action == null)
94-
{
95-
rule.Action = new RuleAction();
96-
}
97-
98-
if (!LoadBalancerActionRules.IsAllowed(balancer.Type, rule.Action.Type))
99-
{
100-
throw new ArgumentException(
101-
$"Rule action {rule.Action.Type} is not valid for load balancer type {balancer.Type}",
102-
nameof(rule));
103-
}
104-
105-
switch (rule.Action.Type)
106-
{
107-
case RuleActionType.Forward:
108-
if (rule.TargetGroup == null || rule.TargetGroup == Guid.Empty)
109-
{
110-
throw new ArgumentException("Forward actions require a target group.", nameof(rule));
111-
}
112-
break;
113-
case RuleActionType.Redirect:
114-
rule.TargetGroup = null;
115-
if (string.IsNullOrWhiteSpace(rule.Action.RedirectUrl))
116-
{
117-
throw new ArgumentException("Redirect actions require a destination URL.", nameof(rule));
118-
}
119-
if (rule.Action.RedirectStatusCode == null)
120-
{
121-
rule.Action.RedirectStatusCode = 302;
122-
}
123-
else if (rule.Action.RedirectStatusCode < 300 || rule.Action.RedirectStatusCode > 399)
124-
{
125-
throw new ArgumentException("Redirect status code must be in the 300-399 range.", nameof(rule));
126-
}
127-
break;
128-
case RuleActionType.FixedResponse:
129-
rule.TargetGroup = null;
130-
if (rule.Action.FixedResponseStatusCode == null)
131-
{
132-
rule.Action.FixedResponseStatusCode = 200;
133-
}
134-
else if (rule.Action.FixedResponseStatusCode < 100 || rule.Action.FixedResponseStatusCode > 599)
135-
{
136-
throw new ArgumentException("Fixed response status code must be in the 100-599 range.", nameof(rule));
137-
}
138-
if (string.IsNullOrWhiteSpace(rule.Action.FixedResponseContentType))
139-
{
140-
rule.Action.FixedResponseContentType = "text/plain";
141-
}
142-
break;
143-
case RuleActionType.Reject:
144-
rule.TargetGroup = null;
145-
break;
146-
default:
147-
throw new ArgumentOutOfRangeException(nameof(rule.Action.Type), rule.Action.Type, "Unknown rule action type.");
148-
}
149-
}
150-
151-
private static bool RequiresCertificate(LoadBalancer balancer)
152-
{
153-
return balancer.Protocol == LoadBalancerProtocol.HTTPS || balancer.Protocol == LoadBalancerProtocol.TLS;
154-
}
67+
throw new ArgumentOutOfRangeException(nameof(newPriority), newPriority, $"Rule priority cannot be greater than {maxPriority + 1}.");
68+
}
69+
}
70+
71+
private static void EnsureRuleConditionsAllowed(LoadBalancer balancer, RuleDTO rule)
72+
{
73+
var invalid = rule.Conditions
74+
.Select(condition => condition.Type)
75+
.Distinct()
76+
.Where(conditionType => !LoadBalancerConditionRules.IsAllowed(balancer.Protocol, conditionType))
77+
.ToList();
78+
79+
if (invalid.Count == 0)
80+
{
81+
return;
82+
}
83+
84+
var invalidList = string.Join(", ", invalid);
85+
throw new ArgumentException(
86+
$"Rule contains condition types not supported by {balancer.Protocol}: {invalidList}.",
87+
nameof(rule));
88+
}
89+
90+
private static void EnsureRuleActionAllowed(LoadBalancer balancer, RuleDTO rule)
91+
{
92+
if (rule.Action == null)
93+
{
94+
rule.Action = new RuleAction();
95+
}
96+
97+
if (!LoadBalancerActionRules.IsAllowed(balancer.Type, rule.Action.Type))
98+
{
99+
throw new ArgumentException(
100+
$"Rule action {rule.Action.Type} is not valid for load balancer type {balancer.Type}",
101+
nameof(rule));
102+
}
103+
104+
switch (rule.Action.Type)
105+
{
106+
case RuleActionType.Forward:
107+
if (rule.TargetGroup == null || rule.TargetGroup == Guid.Empty)
108+
{
109+
throw new ArgumentException("Forward actions require a target group.", nameof(rule));
110+
}
111+
break;
112+
case RuleActionType.Redirect:
113+
rule.TargetGroup = null;
114+
if (string.IsNullOrWhiteSpace(rule.Action.RedirectUrl))
115+
{
116+
throw new ArgumentException("Redirect actions require a destination URL.", nameof(rule));
117+
}
118+
if (rule.Action.RedirectStatusCode == null)
119+
{
120+
rule.Action.RedirectStatusCode = 302;
121+
}
122+
else if (rule.Action.RedirectStatusCode < 300 || rule.Action.RedirectStatusCode > 399)
123+
{
124+
throw new ArgumentException("Redirect status code must be in the 300-399 range.", nameof(rule));
125+
}
126+
break;
127+
case RuleActionType.FixedResponse:
128+
rule.TargetGroup = null;
129+
if (rule.Action.FixedResponseStatusCode == null)
130+
{
131+
rule.Action.FixedResponseStatusCode = 200;
132+
}
133+
else if (rule.Action.FixedResponseStatusCode < 100 || rule.Action.FixedResponseStatusCode > 599)
134+
{
135+
throw new ArgumentException("Fixed response status code must be in the 100-599 range.", nameof(rule));
136+
}
137+
if (string.IsNullOrWhiteSpace(rule.Action.FixedResponseContentType))
138+
{
139+
rule.Action.FixedResponseContentType = "text/plain";
140+
}
141+
break;
142+
case RuleActionType.Reject:
143+
rule.TargetGroup = null;
144+
break;
145+
default:
146+
throw new ArgumentOutOfRangeException(nameof(rule.Action.Type), rule.Action.Type, "Unknown rule action type.");
147+
}
148+
}
149+
150+
private static bool RequiresCertificate(LoadBalancer balancer)
151+
{
152+
return balancer.Protocol == LoadBalancerProtocol.HTTPS || balancer.Protocol == LoadBalancerProtocol.TLS;
153+
}
155154

156155
private static void EnsureCertificatesPresentIfRequired(LoadBalancer balancer)
157156
{
@@ -174,21 +173,21 @@ public async Task<List<LoadBalancerDTO>> GetLoadBalancers()
174173
return _mapper.Map<LoadBalancerDTO>(entity);
175174
}
176175

177-
public async Task<LoadBalancerDTO> AddLoadBalancer(LoadBalancerDTO balancer)
178-
{
179-
EnsureProtocolAllowed(balancer);
180-
var entity = _mapper.Map<LoadBalancer>(balancer);
176+
public async Task<LoadBalancerDTO> AddLoadBalancer(LoadBalancerDTO balancer)
177+
{
178+
EnsureProtocolAllowed(balancer);
179+
var entity = _mapper.Map<LoadBalancer>(balancer);
181180

182181
await _dbContext.LoadBalancers.AddAsync(entity);
183182
await _dbContext.SaveChangesAsync();
184183
return _mapper.Map<LoadBalancerDTO>(entity);
185184
}
186185

187-
public async Task<LoadBalancerDTO> UpdateLoadBalancer(LoadBalancerDTO balancer)
188-
{
189-
EnsureProtocolAllowed(balancer);
190-
var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id);
191-
entity = _mapper.Map(balancer, entity);
186+
public async Task<LoadBalancerDTO> UpdateLoadBalancer(LoadBalancerDTO balancer)
187+
{
188+
EnsureProtocolAllowed(balancer);
189+
var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id);
190+
entity = _mapper.Map(balancer, entity);
192191

193192
_dbContext.LoadBalancers.Update(entity);
194193
await _dbContext.SaveChangesAsync();
@@ -263,12 +262,12 @@ public async Task<List<RuleDTO>> GetRules(LoadBalancerDTO balancer)
263262
public async Task AddRule(LoadBalancerDTO balancer, RuleDTO rule)
264263
{
265264
var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id);
266-
if (entity == null)
267-
throw new ArgumentNullException(nameof(balancer));
268-
269-
ValidateRulePriority(entity.Rules, rule.Priority);
270-
EnsureRuleConditionsAllowed(entity, rule);
271-
EnsureRuleActionAllowed(entity, rule);
265+
if (entity == null)
266+
throw new ArgumentNullException(nameof(balancer));
267+
268+
ValidateRulePriority(entity.Rules, rule.Priority);
269+
EnsureRuleConditionsAllowed(entity, rule);
270+
EnsureRuleActionAllowed(entity, rule);
272271

273272
foreach (var existingRule in entity.Rules.Where(r => r.Priority >= rule.Priority))
274273
{
@@ -287,12 +286,12 @@ public async Task UpdateRule(LoadBalancerDTO balancer, RuleDTO rule)
287286
throw new ArgumentNullException(nameof(balancer));
288287

289288
var ruleEntity = entity.Rules.FirstOrDefault(t => t.Id == rule.Id);
290-
if (ruleEntity == null)
291-
return;
292-
293-
ValidateRulePriority(entity.Rules, rule.Priority);
294-
EnsureRuleConditionsAllowed(entity, rule);
295-
EnsureRuleActionAllowed(entity, rule);
289+
if (ruleEntity == null)
290+
return;
291+
292+
ValidateRulePriority(entity.Rules, rule.Priority);
293+
EnsureRuleConditionsAllowed(entity, rule);
294+
EnsureRuleActionAllowed(entity, rule);
296295

297296
await using var tx = await _dbContext.Database.BeginTransactionAsync();
298297

@@ -393,24 +392,24 @@ public async Task AddCertificate(LoadBalancerDTO balancer, Guid certificateId)
393392
await _dbContext.SaveChangesAsync();
394393
}
395394

396-
public async Task RemoveCertificate(LoadBalancerDTO balancer, Guid certificateId)
395+
public async Task RemoveCertificate(LoadBalancerDTO balancer, Guid certificateId)
397396
{
398397
var entity = await _dbContext.LoadBalancers.FindAsync(balancer.Id);
399398
var c = entity.Certificates.FirstOrDefault(t => t.Id == certificateId);
400399
entity.Certificates.Remove(c);
401400
_dbContext.LoadBalancers.Update(entity);
402-
await _dbContext.SaveChangesAsync();
403-
}
404-
405-
private static void EnsureProtocolAllowed(LoadBalancerDTO balancer)
406-
{
407-
if (!LoadBalancerProtocolRules.IsAllowed(balancer.Type, balancer.Protocol))
408-
{
409-
throw new ArgumentException(
410-
$"Protocol {balancer.Protocol} is not valid for load balancer type {balancer.Type}",
411-
nameof(balancer));
412-
}
413-
}
401+
await _dbContext.SaveChangesAsync();
402+
}
403+
404+
private static void EnsureProtocolAllowed(LoadBalancerDTO balancer)
405+
{
406+
if (!LoadBalancerProtocolRules.IsAllowed(balancer.Type, balancer.Protocol))
407+
{
408+
throw new ArgumentException(
409+
$"Protocol {balancer.Protocol} is not valid for load balancer type {balancer.Type}",
410+
nameof(balancer));
411+
}
412+
}
414413

415414

416415
public async Task<List<TargetGroupDTO>> GetTargetGroups()

tilework.core/Services/Monitoring/MonitoringService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Microsoft.Extensions.Logging;
22

33
using Tilework.Monitoring.Interfaces;
4+
using Tilework.Monitoring.Models;
5+
6+
namespace Tilework.Monitoring.Services;
47

58
public class MonitoringService
69
{

0 commit comments

Comments
 (0)