Skip to content

Commit c384212

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

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Frozen;
1616
using System.Diagnostics;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Reflection;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -53,19 +54,43 @@ namespace publisher;
5354

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

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

6061
public FrozenSet<FileInfo> Handle() => lazy.Value;
6162

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

67-
private static FrozenSet<FileInfo> GetPublisherFiles(CommitId commitId, ManagementServiceDirectory serviceDirectory) =>
68-
Git.GetChangedFilesInCommit(serviceDirectory.ToDirectoryInfo(), commitId);
85+
var managedGatewayApis = changedFiles.Map(x => tryParseApiName(x))
86+
.Filter(x => x.IsSome)
87+
.Map(x => GatewayApiInformationFile.From(ApiName.GetRootName(x.ValueUnsafe()), GatewayName.Managed, serviceDirectory)
88+
.ToFileInfo());
89+
90+
return changedFiles
91+
.Concat(managedGatewayApis)
92+
.ToFrozenSet(x => x.FullName);
93+
}
6994
}
7095

7196
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)