/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ using System; using Amazon.Extensions.Configuration.SystemsManager; using Amazon.Extensions.Configuration.SystemsManager.Internal; using Amazon.Extensions.NETCore.Setup; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.Configuration { /// /// Extension methods for registering with . /// public static class SystemsManagerExtensions { /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// used to create an AWS Systems Manager Client connection /// The path that variable names must start with. The path will be removed from the variable names. /// Whether the AWS Systems Manager Parameters are optional. /// Initiate reload after TimeSpan /// cannot be null /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, AWSOptions awsOptions, bool optional, TimeSpan reloadAfter) { if (path == null) throw new ArgumentNullException(nameof(path)); if (awsOptions == null) throw new ArgumentNullException(nameof(awsOptions)); return builder.AddSystemsManager(ConfigureSource(path, awsOptions, optional, reloadAfter)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// used to create an AWS Systems Manager Client connection /// The path that variable names must start with. The path will be removed from the variable names. /// Whether the AWS Systems Manager Parameters are optional. /// cannot be null /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, AWSOptions awsOptions, bool optional) { if (path == null) throw new ArgumentNullException(nameof(path)); if (awsOptions == null) throw new ArgumentNullException(nameof(awsOptions)); return builder.AddSystemsManager(ConfigureSource(path, awsOptions, optional)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// used to create an AWS Systems Manager Client connection /// The path that variable names must start with. The path will be removed from the variable names. /// Initiate reload after TimeSpan /// cannot be null /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, AWSOptions awsOptions, TimeSpan reloadAfter) { if (path == null) throw new ArgumentNullException(nameof(path)); if (awsOptions == null) throw new ArgumentNullException(nameof(awsOptions)); return builder.AddSystemsManager(ConfigureSource(path, awsOptions, reloadAfter: reloadAfter)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// used to create an AWS Systems Manager Client connection /// The path that variable names must start with. The path will be removed from the variable names. /// cannot be null /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, AWSOptions awsOptions) { if (path == null) throw new ArgumentNullException(nameof(path)); if (awsOptions == null) throw new ArgumentNullException(nameof(awsOptions)); return builder.AddSystemsManager(ConfigureSource(path, awsOptions)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// The path that variable names must start with. The path will be removed from the variable names. /// Whether the AWS Systems Manager Parameters are optional. /// Initiate reload after TimeSpan /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, bool optional, TimeSpan reloadAfter) { if (path == null) throw new ArgumentNullException(nameof(path)); return builder.AddSystemsManager(ConfigureSource(path, null, optional, reloadAfter)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// The path that variable names must start with. The path will be removed from the variable names. /// Whether the AWS Systems Manager Parameters are optional. /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, bool optional) { if (path == null) throw new ArgumentNullException(nameof(path)); return builder.AddSystemsManager(ConfigureSource(path, null, optional)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// The path that variable names must start with. The path will be removed from the variable names. /// Initiate reload after TimeSpan /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path, TimeSpan reloadAfter) { if (path == null) throw new ArgumentNullException(nameof(path)); return builder.AddSystemsManager(ConfigureSource(path, null, reloadAfter: reloadAfter)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter Store with a specified path. /// /// The to add to. /// The path that variable names must start with. The path will be removed from the variable names. /// cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, string path) { if (path == null) throw new ArgumentNullException(nameof(path)); return builder.AddSystemsManager(ConfigureSource(path, null)); } /// /// Adds an that reads configuration values from AWS Systems Manager Parameter variables with a specified path. /// /// The to add to. /// Configures the source. /// cannot be null /// . cannot be null /// The . public static IConfigurationBuilder AddSystemsManager(this IConfigurationBuilder builder, Action configureSource) { if (configureSource == null) throw new ArgumentNullException(nameof(configureSource)); var source = new SystemsManagerConfigurationSource(); configureSource(source); if (string.IsNullOrWhiteSpace(source.Path)) throw new ArgumentNullException(nameof(source.Path)); if (source.AwsOptions != null) return builder.Add(source); source.AwsOptions = AwsOptionsProvider.GetAwsOptions(builder); return builder.Add(source); } private static Action ConfigureSource(string path, AWSOptions awsOptions, bool optional = false, TimeSpan? reloadAfter = null) { return configurationSource => { configurationSource.Path = path; configurationSource.AwsOptions = awsOptions; configurationSource.Optional = optional; configurationSource.ReloadAfter = reloadAfter; }; } } }