Skip to content

Commit

Permalink
feat: export/publish built-in managed gateway apis
Browse files Browse the repository at this point in the history
  • Loading branch information
daviian committed Jul 10, 2024
1 parent 3a9cc89 commit c384212
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
11 changes: 11 additions & 0 deletions tools/code/common/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace common;

public sealed record GatewayName : ResourceName
{
public static GatewayName Managed { get; } = From("managed");

private GatewayName(string value) : base(value) { }

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

public sealed record GatewayDto
{
public static GatewayDto Managed { get; } = new()
{
Properties = new GatewayContract
{
Description = null,
LocationData = null
}
};

[JsonPropertyName("properties")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public required GatewayContract Properties { get; init; }
Expand Down
8 changes: 6 additions & 2 deletions tools/code/extractor/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ await list(cancellationToken)

private async ValueTask ExtractGateway(GatewayName name, GatewayDto dto, CancellationToken cancellationToken)
{
await writeArtifacts(name, dto, cancellationToken);
if (name != GatewayName.Managed)
{
await writeArtifacts(name, dto, cancellationToken);
}
await extractGatewayApis(name, cancellationToken);
}
}

file sealed class ListGatewaysHandler(ManagementServiceUri serviceUri, HttpPipeline pipeline)
{
public IAsyncEnumerable<(GatewayName, GatewayDto)> Handle(CancellationToken cancellationToken) =>
GatewaysUri.From(serviceUri).List(pipeline, cancellationToken);
GatewaysUri.From(serviceUri).List(pipeline, cancellationToken)
.Append((GatewayName.Managed, GatewayDto.Managed));
}

file sealed class ShouldExtractGatewayHandler(ShouldExtractFactory shouldExtractFactory)
Expand Down
2 changes: 1 addition & 1 deletion tools/code/publisher/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace publisher;

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

file delegate Option<ApiName> TryParseApiName(FileInfo file);
internal delegate Option<ApiName> TryParseApiName(FileInfo file);

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

Expand Down
39 changes: 32 additions & 7 deletions tools/code/publisher/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.Frozen;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,19 +54,43 @@ namespace publisher;

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

file sealed class GetPublisherFilesHandler(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory)
file sealed class GetPublisherFilesHandler(TryGetCommitId tryGetCommitId, TryParseApiName tryParseApiName, ManagementServiceDirectory serviceDirectory)
{
private readonly Lazy<FrozenSet<FileInfo>> lazy = new(() => GetPublisherFiles(tryGetCommitId, serviceDirectory));
private readonly Lazy<FrozenSet<FileInfo>> lazy = new(() => GetPublisherFiles(tryGetCommitId, tryParseApiName, serviceDirectory));

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

private static FrozenSet<FileInfo> GetPublisherFiles(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory) =>
private static FrozenSet<FileInfo> GetPublisherFiles(TryGetCommitId tryGetCommitId, TryParseApiName tryParseApiName,
ManagementServiceDirectory serviceDirectory) =>
tryGetCommitId()
.Map(commitId => GetPublisherFiles(commitId, serviceDirectory))
.IfNone(serviceDirectory.GetFilesRecursively);
.Map(commitId => GetPublisherFiles(commitId, tryParseApiName, serviceDirectory))
.IfNone(() =>
{
var managedGatewayApis = ApisDirectory.From(serviceDirectory).ToDirectoryInfo()
.ListDirectories("*")
.Map(info =>
GatewayApiInformationFile.From(ApiName.From(info.Name), GatewayName.Managed, serviceDirectory)
.ToFileInfo());

return serviceDirectory.GetFilesRecursively().ToList()
.Concat(managedGatewayApis)
.ToFrozenSet(x => x.FullName);
});

private static FrozenSet<FileInfo> GetPublisherFiles(CommitId commitId, TryParseApiName tryParseApiName,
ManagementServiceDirectory serviceDirectory)
{
var changedFiles = Git.GetChangedFilesInCommit(serviceDirectory.ToDirectoryInfo(), commitId).ToList();

private static FrozenSet<FileInfo> GetPublisherFiles(CommitId commitId, ManagementServiceDirectory serviceDirectory) =>
Git.GetChangedFilesInCommit(serviceDirectory.ToDirectoryInfo(), commitId);
var managedGatewayApis = changedFiles.Map(x => tryParseApiName(x))
.Filter(x => x.IsSome)
.Map(x => GatewayApiInformationFile.From(ApiName.GetRootName(x.ValueUnsafe()), GatewayName.Managed, serviceDirectory)
.ToFileInfo());

return changedFiles
.Concat(managedGatewayApis)
.ToFrozenSet(x => x.FullName);
}
}

file sealed class GetArtifactFilesHandler(TryGetCommitId tryGetCommitId, ManagementServiceDirectory serviceDirectory)
Expand Down
5 changes: 5 additions & 0 deletions tools/code/publisher/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public async ValueTask Handle(GatewayName name, CancellationToken cancellationTo

private async ValueTask HandleInner(GatewayName name, CancellationToken cancellationToken)
{
if (name == GatewayName.Managed)
{
return;
}

if (isNameInSourceControl(name))
{
await put(name, cancellationToken);
Expand Down
7 changes: 6 additions & 1 deletion tools/code/publisher/GatewayApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async ValueTask Handle(ApiName name, GatewayApiDto dto, GatewayName gatew
}
}

file sealed class DeleteGatewayApiHandler(DeleteGatewayApiFromApim deleteFromApim) : IDisposable
file sealed class DeleteGatewayApiHandler(PutApi putApi, DeleteGatewayApiFromApim deleteFromApim) : IDisposable
{
private readonly GatewayApiSemaphore semaphore = new();

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

private async ValueTask Delete(ApiName name, GatewayName gatewayName, CancellationToken cancellationToken)
{
if (gatewayName == GatewayName.Managed)
{
await putApi(name, cancellationToken);
}

await deleteFromApim(name, gatewayName, cancellationToken);
}

Expand Down

0 comments on commit c384212

Please sign in to comment.