Skip to content

Commit c1a3b0e

Browse files
committed
feat: export/publish built-in managed gateway apis
1 parent 3a9cc89 commit c1a3b0e

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

tools/code/common/Gateway.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace common;
1515

1616
public sealed record GatewayName : ResourceName
1717
{
18+
public static GatewayName Managed { get; } = From("managed");
19+
1820
private GatewayName(string value) : base(value) { }
1921

2022
public static GatewayName From(string value) => new(value);
@@ -119,6 +121,15 @@ public static Option<GatewayInformationFile> TryParse(FileInfo? file, Management
119121

120122
public sealed record GatewayDto
121123
{
124+
public static GatewayDto Managed { get; } = new()
125+
{
126+
Properties = new GatewayContract
127+
{
128+
Description = null,
129+
LocationData = null
130+
}
131+
};
132+
122133
[JsonPropertyName("properties")]
123134
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
124135
public required GatewayContract Properties { get; init; }

tools/code/extractor/Gateway.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@ await list(cancellationToken)
3434

3535
private async ValueTask ExtractGateway(GatewayName name, GatewayDto dto, CancellationToken cancellationToken)
3636
{
37-
await writeArtifacts(name, dto, cancellationToken);
37+
if (name != GatewayName.Managed)
38+
{
39+
await writeArtifacts(name, dto, cancellationToken);
40+
}
3841
await extractGatewayApis(name, cancellationToken);
3942
}
4043
}
4144

4245
file sealed class ListGatewaysHandler(ManagementServiceUri serviceUri, HttpPipeline pipeline)
4346
{
4447
public IAsyncEnumerable<(GatewayName, GatewayDto)> Handle(CancellationToken cancellationToken) =>
45-
GatewaysUri.From(serviceUri).List(pipeline, cancellationToken);
48+
GatewaysUri.From(serviceUri).List(pipeline, cancellationToken)
49+
.Append((GatewayName.Managed, GatewayDto.Managed));
4650
}
4751

4852
file sealed class ShouldExtractGatewayHandler(ShouldExtractFactory shouldExtractFactory)

tools/code/publisher/Api.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace publisher;
2525

2626
internal delegate Option<PublisherAction> FindApiAction(FileInfo file);
2727

28-
file delegate Option<ApiName> TryParseApiName(FileInfo file);
28+
internal delegate Option<ApiName> TryParseApiName(FileInfo file);
2929

3030
file delegate ValueTask ProcessApi(ApiName name, CancellationToken cancellationToken);
3131

tools/code/publisher/Common.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
using Microsoft.IdentityModel.JsonWebTokens;
1414
using System;
1515
using System.Collections.Frozen;
16+
using System.Collections.Generic;
1617
using System.Diagnostics;
1718
using System.IO;
19+
using System.Linq;
1820
using System.Reflection;
1921
using System.Threading;
2022
using System.Threading.Tasks;
@@ -53,19 +55,43 @@ namespace publisher;
5355

5456
file delegate ValueTask<Option<BinaryData>> TryGetFileContentsInCommit(FileInfo fileInfo, CommitId commitId, CancellationToken cancellationToken);
5557

56-
file sealed class GetPublisherFilesHandler(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory)
58+
file sealed class GetPublisherFilesHandler(TryGetCommitId tryGetCommitId, TryParseApiName tryParseApiName, ManagementServiceDirectory serviceDirectory)
5759
{
58-
private readonly Lazy<FrozenSet<FileInfo>> lazy = new(() => GetPublisherFiles(tryGetCommitId, serviceDirectory));
60+
private readonly Lazy<FrozenSet<FileInfo>> lazy = new(() => GetPublisherFiles(tryGetCommitId, tryParseApiName, serviceDirectory));
5961

6062
public FrozenSet<FileInfo> Handle() => lazy.Value;
6163

62-
private static FrozenSet<FileInfo> GetPublisherFiles(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory) =>
64+
private static FrozenSet<FileInfo> GetPublisherFiles(TryGetCommitId tryGetCommitId, TryParseApiName tryParseApiName,
65+
ManagementServiceDirectory serviceDirectory) =>
6366
tryGetCommitId()
64-
.Map(commitId => GetPublisherFiles(commitId, serviceDirectory))
65-
.IfNone(serviceDirectory.GetFilesRecursively);
67+
.Map(commitId => GetPublisherFiles(commitId, tryParseApiName, serviceDirectory))
68+
.IfNone(() =>
69+
serviceDirectory.GetFilesRecursively().ToList()
70+
.Concat(GetManagedGatewayApis(serviceDirectory))
71+
.ToFrozenSet(x => x.FullName)
72+
);
73+
74+
private static IEnumerable<FileInfo> GetManagedGatewayApis(ManagementServiceDirectory serviceDirectory) =>
75+
ApisDirectory.From(serviceDirectory).ToDirectoryInfo()
76+
.ListDirectories("*")
77+
.Map(info =>
78+
GatewayApiInformationFile.From(ApiName.From(info.Name), GatewayName.Managed, serviceDirectory)
79+
.ToFileInfo());
80+
81+
private static FrozenSet<FileInfo> GetPublisherFiles(CommitId commitId, TryParseApiName tryParseApiName,
82+
ManagementServiceDirectory serviceDirectory)
83+
{
84+
var changedFiles = Git.GetChangedFilesInCommit(serviceDirectory.ToDirectoryInfo(), commitId).ToList();
85+
return changedFiles
86+
.Concat(GetChangedManagedGatewayApis(changedFiles, tryParseApiName, serviceDirectory))
87+
.ToFrozenSet(x => x.FullName);
88+
}
6689

67-
private static FrozenSet<FileInfo> GetPublisherFiles(CommitId commitId, ManagementServiceDirectory serviceDirectory) =>
68-
Git.GetChangedFilesInCommit(serviceDirectory.ToDirectoryInfo(), commitId);
90+
private static IEnumerable<FileInfo> GetChangedManagedGatewayApis(List<FileInfo> changedFiles, TryParseApiName tryParseApiName, ManagementServiceDirectory serviceDirectory) =>
91+
changedFiles.Map(x => tryParseApiName(x))
92+
.Filter(x => x.IsSome)
93+
.Map(x => GatewayApiInformationFile.From(ApiName.GetRootName(x.ValueUnsafe()), GatewayName.Managed, serviceDirectory)
94+
.ToFileInfo());
6995
}
7096

7197
file sealed class GetArtifactFilesHandler(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory)

tools/code/publisher/Gateway.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public async ValueTask Handle(GatewayName name, CancellationToken cancellationTo
9393

9494
private async ValueTask HandleInner(GatewayName name, CancellationToken cancellationToken)
9595
{
96+
if (name == GatewayName.Managed)
97+
{
98+
return;
99+
}
100+
96101
if (isNameInSourceControl(name))
97102
{
98103
await put(name, cancellationToken);

tools/code/publisher/GatewayApi.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public async ValueTask Handle(ApiName name, GatewayApiDto dto, GatewayName gatew
163163
}
164164
}
165165

166-
file sealed class DeleteGatewayApiHandler(DeleteGatewayApiFromApim deleteFromApim) : IDisposable
166+
file sealed class DeleteGatewayApiHandler(PutApi putApi, DeleteGatewayApiFromApim deleteFromApim) : IDisposable
167167
{
168168
private readonly GatewayApiSemaphore semaphore = new();
169169

@@ -172,6 +172,11 @@ public async ValueTask Handle(ApiName name, GatewayName gatewayName, Cancellatio
172172

173173
private async ValueTask Delete(ApiName name, GatewayName gatewayName, CancellationToken cancellationToken)
174174
{
175+
if (gatewayName == GatewayName.Managed)
176+
{
177+
await putApi(name, cancellationToken);
178+
}
179+
175180
await deleteFromApim(name, gatewayName, cancellationToken);
176181
}
177182

0 commit comments

Comments
 (0)