Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vincent Lesierse
committed
Apr 10, 2024
1 parent
bd911ab
commit 25e34ef
Showing
31 changed files
with
271 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Aspire.Hosting.AWS/CDK/CDKApplicationExecutionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Amazon.CDK; | ||
|
||
namespace Aspire.Hosting.AWS.CDK; | ||
|
||
internal sealed class CDKApplicationExecutionContext(App app) | ||
{ | ||
public App App { get; } = app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using Amazon.CDK; | ||
using Aspire.Hosting.ApplicationModel; | ||
using Constructs; | ||
|
||
namespace Aspire.Hosting.AWS.CDK; | ||
|
||
internal sealed class CDKProvisionerContext(DistributedApplicationModel model, ResourceNotificationService notificationService) | ||
{ | ||
|
||
public DistributedApplicationModel AppModel { get; } = model; | ||
|
||
public IEnumerable<StackResource> StackResources { get; } = model.Resources.OfType<StackResource>(); | ||
|
||
public IEnumerable<ConstructResource> ConstructResources { get; } = model.Resources.OfType<ConstructResource>(); | ||
|
||
private Lazy<IImmutableDictionary<IStackResource, IEnumerable<IConstructResource>>> ConstructResourcesInStack { get; } = new(model.Resources.GetResourcesGroupedByParent<IStackResource, IConstructResource>()); | ||
|
||
public async Task PublishUpdateStateAsync(IStackResource resource, string status, ImmutableArray<ResourcePropertySnapshot>? properties = null) | ||
{ | ||
if (properties == null) | ||
{ | ||
properties = ImmutableArray.Create<ResourcePropertySnapshot>(); | ||
} | ||
|
||
await notificationService.PublishUpdateAsync(resource, state => state with | ||
{ | ||
ResourceType = GetResourceType<Stack>(resource), | ||
State = status, | ||
Properties = state.Properties.AddRange(properties) | ||
}).ConfigureAwait(false); | ||
await Task.WhenAll( | ||
ConstructResourcesInStack.Value[resource].Select(cr => notificationService.PublishUpdateAsync(cr, state => state with | ||
{ | ||
ResourceType = GetResourceType<Construct>(cr), | ||
State = status | ||
})) | ||
).ConfigureAwait(false); | ||
} | ||
|
||
private static string GetResourceType<T>(IResourceWithConstruct constructResource) | ||
where T : Construct | ||
{ | ||
var constructType = constructResource.Construct.GetType(); | ||
var baseConstructType = typeof(T); | ||
return constructType == baseConstructType ? baseConstructType.Name : $"{constructType.Name}({baseConstructType.Name})"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
using Amazon.CDK; | ||
using Constructs; | ||
using Aspire.Hosting.AWS.Utils; | ||
|
||
namespace Aspire.Hosting.AWS.CDK; | ||
|
||
|
File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
src/Aspire.Hosting.AWS/CDK/DistributedApplicationModelExcentions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Collections.Immutable; | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace Aspire.Hosting.AWS.CDK; | ||
|
||
internal static class DistributedApplicationModelExtentions | ||
{ | ||
public static IResourceBuilder<TDestination> AddResource<TSource, TDestination>(this IResourceBuilder<TSource> builder, Func<TSource, TDestination> resource) | ||
where TSource : IResource | ||
where TDestination : IResourceWithConstruct | ||
{ | ||
return builder.ApplicationBuilder.AddResource(resource(builder.Resource)); | ||
} | ||
|
||
public static IResourceBuilder<T>? ResourceBuilderFor<T>(this IResourceBuilder<IResourceWithParent> builder) | ||
where T : IResource | ||
{ | ||
var parentResource = builder.Resource.Parent; | ||
return parentResource switch | ||
{ | ||
T resultResource => builder.ApplicationBuilder.CreateResourceBuilder(resultResource), | ||
IResourceWithParent parent => ResourceBuilderFor<T>(builder.ApplicationBuilder.CreateResourceBuilder(parent)), | ||
_ => default | ||
}; | ||
} | ||
|
||
public static T? TryFindResource<T>(this IResourceWithParent resource) | ||
where T : IResource | ||
{ | ||
var parentResource = resource.Parent; | ||
return parentResource switch | ||
{ | ||
T resultResource => resultResource, | ||
IResourceWithParent parent => FindResource<T>(parent), | ||
_ => default | ||
}; | ||
} | ||
|
||
public static T FindResource<T>(this IResourceWithParent resource) | ||
where T : IResource | ||
{ | ||
return resource.TryFindResource<T>() ?? | ||
throw new ArgumentException($@"Resource with parent '{resource.GetType().FullName}' not found", | ||
nameof(resource)); | ||
} | ||
|
||
public static IImmutableDictionary<TParent, IEnumerable<TChildren>> GetResourcesGroupedByParent<TParent, TChildren>(this IResourceCollection resources) | ||
where TParent : IResource | ||
where TChildren : IResourceWithParent | ||
{ | ||
return resources.OfType<TChildren>().Where(resource => resource.Parent is TParent).GroupBy(resource => resource.FindResource<TParent>()).ToImmutableDictionary(group => group.Key, group => group.AsEnumerable()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.