Skip to content

Commit e7c24ca

Browse files
authored
ElasticsearchOptions fallback to dotnet secrets (#2513)
* ElasticsearchOptions fallback to dotnet secrets * update default index name
1 parent e29d277 commit e7c24ca

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/api/Elastic.Documentation.Api.Infrastructure/Adapters/Search/ElasticsearchOptions.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,34 @@
66

77
namespace Elastic.Documentation.Api.Infrastructure.Adapters.Search;
88

9-
public class ElasticsearchOptions(IConfiguration configuration)
9+
public class ElasticsearchOptions
1010
{
11+
public ElasticsearchOptions(IConfiguration configuration)
12+
{
13+
// Build a new ConfigurationBuilder to read user secrets
14+
var configBuilder = new ConfigurationBuilder();
15+
_ = configBuilder.AddUserSecrets("72f50f33-6fb9-4d08-bff3-39568fe370b3");
16+
var userSecretsConfig = configBuilder.Build();
17+
var elasticUrlFromSecret = userSecretsConfig["Parameters:DocumentationElasticUrl"];
18+
var elasticApiKeyFromSecret = userSecretsConfig["Parameters:DocumentationElasticApiKey"];
19+
20+
Url = GetEnv("DOCUMENTATION_ELASTIC_URL", elasticUrlFromSecret);
21+
ApiKey = GetEnv("DOCUMENTATION_ELASTIC_APIKEY", elasticApiKeyFromSecret);
22+
IndexName = configuration["DOCUMENTATION_ELASTIC_INDEX"] ?? "semantic-docs-dev-latest";
23+
}
24+
25+
private static string GetEnv(string name, string? defaultValue = null)
26+
{
27+
var value = Environment.GetEnvironmentVariable(name);
28+
if (!string.IsNullOrEmpty(value))
29+
return value;
30+
if (defaultValue != null)
31+
return defaultValue;
32+
throw new ArgumentException($"Environment variable '{name}' not found.");
33+
}
34+
1135
// Read from environment variables (set by Terraform from SSM at deploy time)
12-
public string Url { get; } = configuration["DOCUMENTATION_ELASTIC_URL"]
13-
?? throw new InvalidOperationException("DOCUMENTATION_ELASTIC_URL not configured");
14-
public string ApiKey { get; } = configuration["DOCUMENTATION_ELASTIC_APIKEY"]
15-
?? throw new InvalidOperationException("DOCUMENTATION_ELASTIC_APIKEY not configured");
16-
public string IndexName { get; } = configuration["DOCUMENTATION_ELASTIC_INDEX"]
17-
?? "documentation-latest";
36+
public string Url { get; }
37+
public string ApiKey { get; }
38+
public string IndexName { get; }
1839
}

0 commit comments

Comments
 (0)