|
| 1 | +#if FALSE // TODO: Re-enable and update to Aspire 13 eventing model (IDistributedApplicationEventingSubscriber) |
1 | 2 | using System.Text; |
2 | | -using Aspire.Hosting.ApplicationModel; |
| 3 | +using Aspire.Hosting.Lifecycle; |
3 | 4 |
|
4 | 5 | namespace Aspire.Hosting; |
5 | 6 |
|
6 | | -internal static class KibanaConfigWriterExtensions |
| 7 | +internal class KibanaConfigWriterHook : IDistributedApplicationLifecycleHook |
7 | 8 | { |
8 | | - public static IResourceBuilder<KibanaResource> ConfigureElasticsearchHosts( |
9 | | - this IResourceBuilder<KibanaResource> builder, |
10 | | - IEnumerable<ElasticsearchResource> elasticsearchResources) |
| 9 | + public async Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken) |
11 | 10 | { |
12 | | - builder.WithAnnotation(new EnvironmentCallbackAnnotation(async context => |
13 | | - { |
14 | | - var hostsVariableBuilder = new StringBuilder(); |
| 11 | + if (appModel.Resources.OfType<KibanaResource>().SingleOrDefault() is not { } kibanaResource) |
| 12 | + return; |
15 | 13 |
|
16 | | - foreach (var elasticsearchInstance in elasticsearchResources) |
17 | | - { |
18 | | - if (elasticsearchInstance.PrimaryEndpoint.IsAllocated) |
19 | | - { |
20 | | - if (hostsVariableBuilder.Length > 0) |
21 | | - hostsVariableBuilder.Append(","); |
22 | | - |
23 | | - var endpoint = elasticsearchInstance.PrimaryEndpoint; |
24 | | - hostsVariableBuilder.Append(endpoint.Scheme) |
25 | | - .Append("://") |
26 | | - .Append(endpoint.Host) |
27 | | - .Append(":") |
28 | | - .Append(endpoint.Port); |
29 | | - } |
30 | | - } |
| 14 | + var elasticsearchInstances = appModel.Resources.OfType<ElasticsearchResource>(); |
| 15 | + |
| 16 | + if (!elasticsearchInstances.Any()) |
| 17 | + return; |
31 | 18 |
|
32 | | - if (hostsVariableBuilder.Length > 0) |
| 19 | + var hostsVariableBuilder = new StringBuilder(); |
| 20 | + |
| 21 | + foreach (var elasticsearchInstance in elasticsearchInstances) |
| 22 | + { |
| 23 | + if (elasticsearchInstance.PrimaryEndpoint.IsAllocated) |
33 | 24 | { |
34 | | - context.EnvironmentVariables["ELASTICSEARCH_HOSTS"] = hostsVariableBuilder.ToString(); |
| 25 | + var connectionString = await elasticsearchInstance.GetConnectionStringAsync(); |
| 26 | + if (hostsVariableBuilder.Length > 0) |
| 27 | + hostsVariableBuilder.Append(","); |
| 28 | + hostsVariableBuilder.Append(elasticsearchInstance.PrimaryEndpoint.Scheme).Append("://").Append(elasticsearchInstance.PrimaryEndpoint.Host).Append(":").Append(elasticsearchInstance.PrimaryEndpoint.Port); |
35 | 29 | } |
| 30 | + } |
36 | 31 |
|
37 | | - await Task.CompletedTask; |
| 32 | + kibanaResource.Annotations.Add(new EnvironmentCallbackAnnotation(context => |
| 33 | + { |
| 34 | + context.EnvironmentVariables.Add("ELASTICSEARCH_HOSTS", hostsVariableBuilder.ToString()); |
38 | 35 | })); |
39 | | - |
40 | | - return builder; |
41 | 36 | } |
42 | 37 | } |
| 38 | +#endif |
0 commit comments