-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathGateway.cs
More file actions
143 lines (118 loc) · 5.97 KB
/
Gateway.cs
File metadata and controls
143 lines (118 loc) · 5.97 KB
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
using Azure.Core.Pipeline;
using common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace extractor;
public delegate ValueTask ExtractGateways(CancellationToken cancellationToken);
public delegate IAsyncEnumerable<(GatewayName Name, GatewayDto Dto)> ListGateways(CancellationToken cancellationToken);
public delegate ValueTask WriteGatewayArtifacts(GatewayName name, GatewayDto dto, CancellationToken cancellationToken);
public delegate ValueTask WriteGatewayInformationFile(GatewayName name, GatewayDto dto, CancellationToken cancellationToken);
internal static class GatewayModule
{
public static void ConfigureExtractGateways(IHostApplicationBuilder builder)
{
ConfigureListGateways(builder);
ConfigureWriteGatewayArtifacts(builder);
GatewayApiModule.ConfigureExtractGatewayApis(builder);
builder.Services.TryAddSingleton(GetExtractGateways);
}
private static ExtractGateways GetExtractGateways(IServiceProvider provider)
{
var list = provider.GetRequiredService<ListGateways>();
var writeArtifacts = provider.GetRequiredService<WriteGatewayArtifacts>();
var extractGatewayApis = provider.GetRequiredService<ExtractGatewayApis>();
var activitySource = provider.GetRequiredService<ActivitySource>();
var logger = provider.GetRequiredService<ILogger>();
return async cancellationToken =>
{
using var _ = activitySource.StartActivity(nameof(ExtractGateways));
logger.LogInformation("Extracting gateways...");
await list(cancellationToken)
.IterParallel(async resource => await extractGateway(resource.Name, resource.Dto, cancellationToken),
cancellationToken);
};
async ValueTask extractGateway(GatewayName name, GatewayDto dto, CancellationToken cancellationToken)
{
if (name != GatewayName.Managed)
{
await writeArtifacts(name, dto, cancellationToken);
}
await extractGatewayApis(name, cancellationToken);
}
}
private static void ConfigureListGateways(IHostApplicationBuilder builder)
{
ConfigurationModule.ConfigureFindConfigurationNamesFactory(builder);
AzureModule.ConfigureManagementServiceUri(builder);
AzureModule.ConfigureHttpPipeline(builder);
builder.Services.TryAddSingleton(GetListGateways);
}
private static ListGateways GetListGateways(IServiceProvider provider)
{
var findConfigurationNamesFactory = provider.GetRequiredService<FindConfigurationNamesFactory>();
var serviceUri = provider.GetRequiredService<ManagementServiceUri>();
var pipeline = provider.GetRequiredService<HttpPipeline>();
var findConfigurationNames = findConfigurationNamesFactory.Create<GatewayName>();
return cancellationToken =>
findConfigurationNames()
.Map(names => listFromSet(names, cancellationToken))
.IfNone(() => listAll(cancellationToken));
IAsyncEnumerable<(GatewayName, GatewayDto)> listFromSet(IEnumerable<GatewayName> names, CancellationToken cancellationToken) =>
names.Select(name => GatewayUri.From(name, serviceUri))
.ToAsyncEnumerable()
.Choose(async uri =>
{
var dtoOption = await uri.TryGetDto(pipeline, cancellationToken);
return dtoOption.Map(dto => (uri.Name, dto));
})
// Handle scenarios where the SKU doesn't support gateways
.Catch((HttpRequestException exception) =>
exception.StatusCode == HttpStatusCode.InternalServerError
&& exception.Message.Contains("Request processing failed", StringComparison.OrdinalIgnoreCase)
? AsyncEnumerable.Empty<(GatewayName, GatewayDto)>()
: throw exception);
IAsyncEnumerable<(GatewayName, GatewayDto)> listAll(CancellationToken cancellationToken)
{
var gatewaysUri = GatewaysUri.From(serviceUri);
return gatewaysUri.List(pipeline, cancellationToken)
.Append((GatewayName.Managed, GatewayDto.Managed));
}
}
private static void ConfigureWriteGatewayArtifacts(IHostApplicationBuilder builder)
{
ConfigureWriteGatewayInformationFile(builder);
builder.Services.TryAddSingleton(GetWriteGatewayArtifacts);
}
private static WriteGatewayArtifacts GetWriteGatewayArtifacts(IServiceProvider provider)
{
var writeInformationFile = provider.GetRequiredService<WriteGatewayInformationFile>();
return async (name, dto, cancellationToken) =>
await writeInformationFile(name, dto, cancellationToken);
}
private static void ConfigureWriteGatewayInformationFile(IHostApplicationBuilder builder)
{
AzureModule.ConfigureManagementServiceDirectory(builder);
builder.Services.TryAddSingleton(GetWriteGatewayInformationFile);
}
private static WriteGatewayInformationFile GetWriteGatewayInformationFile(IServiceProvider provider)
{
var serviceDirectory = provider.GetRequiredService<ManagementServiceDirectory>();
var logger = provider.GetRequiredService<ILogger>();
return async (name, dto, cancellationToken) =>
{
var informationFile = GatewayInformationFile.From(name, serviceDirectory);
logger.LogInformation("Writing gateway information file {GatewayInformationFile}...", informationFile);
await informationFile.WriteDto(dto, cancellationToken);
};
}
}