-
Notifications
You must be signed in to change notification settings - Fork 882
Expand file tree
/
Copy pathElasticsearchBuilderExtensions.cs
More file actions
121 lines (110 loc) · 5.77 KB
/
ElasticsearchBuilderExtensions.cs
File metadata and controls
121 lines (110 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Elasticsearch;
using Aspire.Hosting.Utils;
namespace Aspire.Hosting;
/// <summary>
/// Provides extension methods for adding Elasticsearch resources to the application model.
/// </summary>
public static class ElasticsearchBuilderExtensions
{
private const int ElasticsearchPort = 9200;
private const int ElasticsearchInternalPort = 9300;
/// <summary>
/// Adds an Elasticsearch container resource to the application model.
/// </summary>
/// <remarks>
/// The default image is "elasticsearch" and the tag is "8.14.0".
/// </remarks>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
/// <param name="port">The host port to bind the underlying container to.</param>
/// <param name="password">The parameter used to provide the superuser password for the elasticsearch. If <see langword="null"/> a random password will be generated.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
/// <example>
/// Add an Elasticsearch container to the application model and reference it in a .NET project.
/// <code lang="csharp">
/// var builder = DistributedApplication.CreateBuilder(args);
///
/// var elasticsearch = builder.AddElasticsearch("elasticsearch");
/// var api = builder.AddProject<Projects.Api>("api")
/// .WithReference(elasticsearch);
///
/// builder.Build().Run();
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(
this IDistributedApplicationBuilder builder,
string name,
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");
var elasticsearch = new ElasticsearchResource(name, passwordParameter);
return builder.AddResource(elasticsearch)
.WithImage(ElasticsearchContainerImageTags.Image, ElasticsearchContainerImageTags.Tag)
.WithImageRegistry(ElasticsearchContainerImageTags.Registry)
.WithHttpEndpoint(targetPort: ElasticsearchPort, port: port, name: ElasticsearchResource.PrimaryEndpointName)
.WithEndpoint(targetPort: ElasticsearchInternalPort, port: port, name: ElasticsearchResource.InternalEndpointName)
.WithEnvironment("discovery.type", "single-node")
.WithEnvironment("xpack.security.enabled", "true")
.WithEnvironment(context =>
{
context.EnvironmentVariables["ELASTIC_PASSWORD"] = elasticsearch.PasswordParameter;
});
}
/// <summary>
/// Adds a named volume for the data folder to a Elasticsearch container resource.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="name">The name of the volume. Defaults to an auto-generated name based on the application and resource names.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
/// <example>
/// Add an Elasticsearch container to the application model and reference it in a .NET project. Additionally, in this
/// example a data volume is added to the container to allow data to be persisted across container restarts.
/// <code lang="csharp">
/// var builder = DistributedApplication.CreateBuilder(args);
///
/// var elasticsearch = builder.AddElasticsearch("elasticsearch")
/// .WithDataVolume();
/// var api = builder.AddProject<Projects.Api>("api")
/// .WithReference(elasticsearch);
///
/// builder.Build().Run();
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResourceBuilder<ElasticsearchResource> builder, string? name = null)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data");
}
/// <summary>
/// Adds a bind mount for the data folder to a Elasticsearch container resource.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="source">The source directory on the host to mount into the container.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
/// <example>
/// Add an Elasticsearch container to the application model and reference it in a .NET project. Additionally, in this
/// example a bind mount is added to the container to allow data to be persisted across container restarts.
/// <code lang="csharp">
/// var builder = DistributedApplication.CreateBuilder(args);
///
/// var elasticsearch = builder.AddElasticsearch("elasticsearch")
/// .WithDataBindMount("./data/elasticsearch/data");
/// var api = builder.AddProject<Projects.Api>("api")
/// .WithReference(elasticsearch);
///
/// builder.Build().Run();
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataBindMount(this IResourceBuilder<ElasticsearchResource> builder, string source)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);
return builder.WithBindMount(source, "/usr/share/elasticsearch/data");
}
}