-
Notifications
You must be signed in to change notification settings - Fork 56
Added support for AppConfig Feature Flag profiles #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,51 @@ namespace CustomParameterProcessorExample | |
} | ||
``` | ||
|
||
### Simple AppConfig FeatureFlag Sample | ||
```csharp | ||
// | ||
// AppConfigFeatureFlag.cs | ||
// | ||
public class AppConfigFeatureFlag | ||
{ | ||
[JsonPropertyName("enabled")] | ||
public bool Enabled { get; set; } | ||
} | ||
|
||
// | ||
// Program.cs | ||
// | ||
// Map feature flag configuration | ||
builder.Services.Configure<Dictionary<string, AppConfigFeatureFlag>>(builder.Configuration.GetSection("FeatureFlags")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of injecting |
||
|
||
// Add AppConfig configuration source | ||
var awsAppConfigClient = new AmazonAppConfigDataClient(); | ||
builder.Configuration | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | ||
.AddAppConfig(new AppConfigConfigurationSource() | ||
{ | ||
ApplicationId = "application", | ||
EnvironmentId = "Default", | ||
ConfigProfileId = "my-appconfig-feature-profile", | ||
AwsOptions = new AWSOptions(), | ||
ReloadAfter = new TimeSpan(0, 0, 30), | ||
WrapperNodeName = "FeatureFlags" // Nest AppConfig FeatureFlag data under this node | ||
}); | ||
|
||
// | ||
// TestApi.cs | ||
// | ||
public class TestApi : ITestApi | ||
{ | ||
private readonly IOptionsMonitor<Dictionary<string, AppConfigFeatureFlag>> _FeatureFlags; | ||
public TestApi(IOptionsMonitor<Dictionary<string, AppConfigFeatureFlag>> featureFlags) | ||
{ | ||
_FeatureFlags = featureFlags; | ||
} | ||
} | ||
``` | ||
|
||
For more complete examples, take a look at sample projects available in [samples directory](https://github.com/aws/aws-dotnet-extensions-configuration/tree/master/samples). | ||
|
||
# Configuring Systems Manager Client | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,19 @@ public async Task<IDictionary<string, string>> GetDataAsync() | |
?? new Dictionary<string, string>(); | ||
} | ||
} | ||
|
||
private async Task AddWrapperNodeAsync(Stream configStream, Stream wrappedStream) | ||
{ | ||
string wrappedConfig; | ||
using (var reader = new StreamReader(configStream)) | ||
{ | ||
wrappedConfig = $"{{\"{Source.WrapperNodeName}\":{await reader.ReadToEndAsync().ConfigureAwait(false)}}}"; | ||
} | ||
|
||
var wrappedConfigBytes = System.Text.Encoding.UTF8.GetBytes(wrappedConfig); | ||
await wrappedStream.WriteAsync(wrappedConfigBytes, 0, wrappedConfigBytes.Length).ConfigureAwait(false); | ||
wrappedStream.Position = 0; | ||
} | ||
|
||
private async Task<IDictionary<string,string>> GetDataFromLambdaExtensionAsync() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was thinking about this comment. i couldn't think of a reason to not add the same logic here. [Question] Can/should we add the new logic you added here too (or is there a specific reason you didn't) |
||
{ | ||
|
@@ -134,7 +147,18 @@ private async Task<IDictionary<string, string>> GetDataFromServiceAsync() | |
// so only attempt to parse the AppConfig response when it is not empty | ||
if (response.ContentLength > 0) | ||
{ | ||
LastConfig = ParseConfig(response.ContentType, response.Configuration); | ||
if (string.IsNullOrWhiteSpace(Source.WrapperNodeName)) | ||
{ | ||
LastConfig = ParseConfig(response.ContentType, response.Configuration); | ||
} | ||
else | ||
{ | ||
using (var wrappedConfiguration = new MemoryStream()) | ||
{ | ||
await AddWrapperNodeAsync(response.Configuration, wrappedConfiguration).ConfigureAwait(false); | ||
LastConfig = ParseConfig(response.ContentType, wrappedConfiguration); | ||
} | ||
} | ||
} | ||
} | ||
finally | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Amazon.Extensions.Configuration.SystemsManager.AppConfig; | ||
using Amazon.Extensions.NETCore.Setup; | ||
using Xunit; | ||
|
||
namespace Amazon.Extensions.Configuration.SystemsManager.Tests | ||
{ | ||
public class AppConfigProcessorTests | ||
{ | ||
[Fact] | ||
public async Task AddWrapperNode_WithWrapperNodeName_WrapsConfigurationCorrectly() | ||
{ | ||
// Arrange | ||
var source = new AppConfigConfigurationSource | ||
{ | ||
ApplicationId = "appId", | ||
EnvironmentId = "envId", | ||
ConfigProfileId = "profileId", | ||
WrapperNodeName = "FeatureFlags", | ||
AwsOptions = new AWSOptions() | ||
}; | ||
|
||
var processor = new AppConfigProcessor(source); | ||
var inputJson = "{\"test-flag-1\":{\"enabled\":false},\"test-flag-2\":{\"enabled\":true}}"; | ||
using var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputJson)); | ||
using var outputStream = new MemoryStream(); | ||
|
||
// Act | ||
await InvokeAddWrapperNode(processor, inputStream, outputStream); | ||
|
||
// Assert | ||
string resultJson; | ||
using (var reader = new StreamReader(outputStream)) | ||
{ | ||
resultJson = reader.ReadToEnd(); | ||
} | ||
|
||
var expectedJson = "{\"FeatureFlags\":{\"test-flag-1\":{\"enabled\":false},\"test-flag-2\":{\"enabled\":true}}}"; | ||
Assert.Equal(expectedJson, resultJson); | ||
} | ||
|
||
// Helper method to invoke the private AddWrapperNode method using reflection | ||
private async Task InvokeAddWrapperNode(AppConfigProcessor processor, Stream configuration, Stream wrappedConfig) | ||
{ | ||
var method = typeof(AppConfigProcessor).GetMethod("AddWrapperNodeAsync", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
await (Task)method.Invoke(processor, new object[] { configuration, wrappedConfig }); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add a change file for this change https://github.com/aws/aws-dotnet-extensions-configuration/blob/main/CONTRIBUTING.md#adding-a-change-file-to-your-contribution-branch