-
Notifications
You must be signed in to change notification settings - Fork 803
/
DynamicConfigBuilder.cs
143 lines (123 loc) · 5.94 KB
/
DynamicConfigBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// <copyright file="DynamicConfigBuilder.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using IslandGateway.Core.Abstractions;
using IslandGateway.Core.ConfigModel;
using IslandGateway.Utilities;
namespace IslandGateway.Core.Service
{
internal class DynamicConfigBuilder : IDynamicConfigBuilder
{
private readonly IBackendsRepo backendsRepo;
private readonly IBackendEndpointsRepo endpointsRepo;
private readonly IRoutesRepo routesRepo;
private readonly IRouteParser routeParser;
private readonly IRouteValidator parsedRouteValidator;
public DynamicConfigBuilder(
IBackendsRepo backendsRepo,
IBackendEndpointsRepo endpointsRepo,
IRoutesRepo routesRepo,
IRouteParser routeParser,
IRouteValidator parsedRouteValidator)
{
Contracts.CheckValue(backendsRepo, nameof(backendsRepo));
Contracts.CheckValue(endpointsRepo, nameof(endpointsRepo));
Contracts.CheckValue(routesRepo, nameof(routesRepo));
Contracts.CheckValue(routeParser, nameof(routeParser));
Contracts.CheckValue(parsedRouteValidator, nameof(parsedRouteValidator));
this.backendsRepo = backendsRepo;
this.endpointsRepo = endpointsRepo;
this.routesRepo = routesRepo;
this.routeParser = routeParser;
this.parsedRouteValidator = parsedRouteValidator;
}
public async Task<Result<DynamicConfigRoot>> BuildConfigAsync(IConfigErrorReporter errorReporter, CancellationToken cancellation)
{
Contracts.CheckValue(errorReporter, nameof(errorReporter));
var backends = await this.GetBackendsWithEndpointsAsync(errorReporter, cancellation);
var routes = await this.GetRoutesAsync(errorReporter, cancellation);
var config = new DynamicConfigRoot
{
Backends = backends,
Routes = routes,
};
return Result.Success(config);
}
private async Task<IList<BackendWithEndpoints>> GetBackendsWithEndpointsAsync(IConfigErrorReporter errorReporter, CancellationToken cancellation)
{
var backends = await this.backendsRepo.GetBackendsAsync(cancellation);
var sortedBackends = new SortedList<string, BackendWithEndpoints>(backends?.Count ?? 0, StringComparer.Ordinal);
if (backends != null)
{
foreach (var backend in backends)
{
if (sortedBackends.ContainsKey(backend.BackendId))
{
errorReporter.ReportError(ConfigErrors.BackendDuplicateId, backend.BackendId, $"Duplicate backend '{backend.BackendId}'.");
continue;
}
var endpoints = await this.GetBackendEndpointsAsync(errorReporter, backend.BackendId, cancellation);
sortedBackends.Add(
backend.BackendId,
new BackendWithEndpoints(
backend: backend.DeepClone(),
endpoints: endpoints)); // `this.GetBackendEndpoints` already returns deep-cloned copies
}
}
return sortedBackends.Values;
}
private async Task<IList<BackendEndpoint>> GetBackendEndpointsAsync(IConfigErrorReporter errorReporter, string backendId, CancellationToken cancellation)
{
var endpoints = await this.endpointsRepo.GetEndpointsAsync(backendId, cancellation);
var backendEndpoints = new SortedList<string, BackendEndpoint>(endpoints?.Count ?? 0, StringComparer.Ordinal);
if (endpoints != null)
{
foreach (var endpoint in endpoints)
{
if (backendEndpoints.ContainsKey(endpoint.EndpointId))
{
errorReporter.ReportError(ConfigErrors.BackendEndpointDuplicateId, endpoint.EndpointId, $"Duplicate endpoint '{endpoint.EndpointId}'.");
continue;
}
backendEndpoints.Add(endpoint.EndpointId, endpoint.DeepClone());
}
}
return backendEndpoints.Values;
}
private async Task<IList<ParsedRoute>> GetRoutesAsync(IConfigErrorReporter errorReporter, CancellationToken cancellation)
{
var routes = await this.routesRepo.GetRoutesAsync(cancellation);
var seenRouteIds = new HashSet<string>();
var sortedRoutes = new SortedList<(int, string), ParsedRoute>(routes?.Count ?? 0);
if (routes != null)
{
foreach (var route in routes)
{
if (seenRouteIds.Contains(route.RouteId))
{
errorReporter.ReportError(ConfigErrors.RouteDuplicateId, route.RouteId, $"Duplicate route '{route.RouteId}'.");
continue;
}
var parsedResult = this.routeParser.ParseRoute(route, errorReporter);
if (!parsedResult.IsSuccess)
{
// routeParser already reported error message
continue;
}
var parsedRoute = parsedResult.Value;
if (!this.parsedRouteValidator.ValidateRoute(parsedRoute, errorReporter))
{
// parsedRouteValidator already reported error message
continue;
}
sortedRoutes.Add((parsedRoute.Priority ?? 0, parsedRoute.RouteId), parsedRoute);
}
}
return sortedRoutes.Values;
}
}
}