1212using Tilework . Persistence . LoadBalancing . Models ;
1313using Tilework . LoadBalancing . Haproxy ;
1414
15- using Tilework . CertificateManagement . Interfaces ;
1615using Tilework . CertificateManagement . Models ;
17-
16+ using Tilework . Monitoring . Services ;
1817using 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 ( )
0 commit comments