|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 |
|
3 |
| -using Amazon.Runtime.Internal.Endpoints.StandardLibrary; |
4 | 3 | using Aspire.Hosting.ApplicationModel;
|
5 | 4 | using Aspire.Hosting.AWS;
|
6 | 5 | using Aspire.Hosting.AWS.Lambda;
|
| 6 | +using Aspire.Hosting.AWS.Utils.Internal; |
7 | 7 | using Aspire.Hosting.Lifecycle;
|
8 |
| -using Microsoft.Extensions.DependencyInjection; |
9 |
| -using System.Collections.Immutable; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
10 | 10 | using System.Diagnostics;
|
11 | 11 | using System.Net.Sockets;
|
12 | 12 | using System.Runtime.Versioning;
|
@@ -97,32 +97,62 @@ public static class LambdaExtensions
|
97 | 97 | return resource;
|
98 | 98 | }
|
99 | 99 |
|
100 |
| - private static ExecutableResource AddOrGetLambdaServiceEmulatorResource(IDistributedApplicationBuilder builder) |
101 |
| - { |
102 |
| - if (builder.Resources.FirstOrDefault(x => x.TryGetAnnotationsOfType<LambdaEmulatorAnnotation>(out _)) is not ExecutableResource serviceEmulator) |
| 100 | + /// <summary> |
| 101 | + /// Add the Lambda service emulator resource. The AddAWSLambdaFunction method will automatically add the Lambda service emulator if it hasn't |
| 102 | + /// already been added. This method only needs to be called if the emulator needs to be customized with the LambdaEmulatorOptions. If |
| 103 | + /// this method is called it must be called only once and before any AddAWSLambdaFunction calls. |
| 104 | + /// </summary> |
| 105 | + /// <param name="builder"></param> |
| 106 | + /// <param name="options">The options to configure the emulator with.</param> |
| 107 | + /// <returns></returns> |
| 108 | + /// <exception cref="InvalidOperationException">Thrown if the Lambda service emulator has already been added.</exception> |
| 109 | + public static IResourceBuilder<LambdaEmulatorResource> AddAWSLambdaServiceEmulator(this IDistributedApplicationBuilder builder, LambdaEmulatorOptions? options = null) |
| 110 | + { |
| 111 | + if (builder.Resources.FirstOrDefault(x => x.TryGetAnnotationsOfType<LambdaEmulatorAnnotation>(out _)) is ExecutableResource serviceEmulator) |
103 | 112 | {
|
104 |
| - var serviceEmulatorBuilder = builder.AddExecutable($"Lambda-ServiceEmulator", |
105 |
| - "dotnet-lambda-test-tool", |
106 |
| - Environment.CurrentDirectory, |
107 |
| - "--no-launch-window") |
108 |
| - .ExcludeFromManifest(); |
| 113 | + throw new InvalidOperationException("A Lambda service emulator has already been added. The AddAWSLambdaFunction will add the emulator " + |
| 114 | + "if it hasn't already been added. This method must be called before AddAWSLambdaFunction if the Lambda service emulator needs to be customized."); |
| 115 | + } |
109 | 116 |
|
110 |
| - var annotation = new EndpointAnnotation( |
111 |
| - protocol: ProtocolType.Tcp, |
112 |
| - uriScheme: "http"); |
| 117 | + builder.Services.TryAddSingleton<IProcessCommandService, ProcessCommandService>(); |
113 | 118 |
|
114 |
| - serviceEmulatorBuilder.WithAnnotation(annotation); |
115 |
| - var endpointReference = new EndpointReference(serviceEmulatorBuilder.Resource, annotation); |
| 119 | + var lambdaEmulator = builder.AddResource(new LambdaEmulatorResource("LambdaServiceEmulator")).ExcludeFromManifest(); |
| 120 | + lambdaEmulator.WithArgs(context => |
| 121 | + { |
| 122 | + lambdaEmulator.Resource.AddCommandLineArguments(context.Args); |
| 123 | + }); |
116 | 124 |
|
117 |
| - serviceEmulatorBuilder.WithAnnotation(new LambdaEmulatorAnnotation(endpointReference)); |
| 125 | + var annotation = new EndpointAnnotation( |
| 126 | + protocol: ProtocolType.Tcp, |
| 127 | + uriScheme: "http"); |
118 | 128 |
|
119 |
| - serviceEmulatorBuilder.WithAnnotation(new EnvironmentCallbackAnnotation(context => |
120 |
| - { |
121 |
| - context.EnvironmentVariables[Constants.IsAspireHostedEnvVariable] = "true"; |
122 |
| - context.EnvironmentVariables["LAMBDA_RUNTIME_API_PORT"] = endpointReference.Property(EndpointProperty.TargetPort); |
123 |
| - })); |
| 129 | + lambdaEmulator.WithAnnotation(annotation); |
| 130 | + var endpointReference = new EndpointReference(lambdaEmulator.Resource, annotation); |
124 | 131 |
|
125 |
| - serviceEmulator = serviceEmulatorBuilder.Resource; |
| 132 | + lambdaEmulator.WithAnnotation(new LambdaEmulatorAnnotation(endpointReference) |
| 133 | + { |
| 134 | + DisableAutoInstall = options?.DisableAutoInstall ?? false, |
| 135 | + OverrideMinimumInstallVersion = options?.OverrideMinimumInstallVersion, |
| 136 | + AllowDowngrade = options?.AllowDowngrade ?? false, |
| 137 | + }); |
| 138 | + |
| 139 | + lambdaEmulator.WithAnnotation(new EnvironmentCallbackAnnotation(context => |
| 140 | + { |
| 141 | + context.EnvironmentVariables[Constants.IsAspireHostedEnvVariable] = "true"; |
| 142 | + context.EnvironmentVariables["LAMBDA_RUNTIME_API_PORT"] = endpointReference.Property(EndpointProperty.TargetPort); |
| 143 | + })); |
| 144 | + |
| 145 | + serviceEmulator = lambdaEmulator.Resource; |
| 146 | + builder.Services.TryAddLifecycleHook<LambdaLifecycleHook>(); |
| 147 | + |
| 148 | + return lambdaEmulator; |
| 149 | + } |
| 150 | + |
| 151 | + private static ExecutableResource AddOrGetLambdaServiceEmulatorResource(IDistributedApplicationBuilder builder) |
| 152 | + { |
| 153 | + if (builder.Resources.FirstOrDefault(x => x.TryGetAnnotationsOfType<LambdaEmulatorAnnotation>(out _)) is not ExecutableResource serviceEmulator) |
| 154 | + { |
| 155 | + serviceEmulator = builder.AddAWSLambdaServiceEmulator().Resource; |
126 | 156 | }
|
127 | 157 |
|
128 | 158 | return serviceEmulator;
|
|
0 commit comments