Kaashika Prajaapat
Kaashika Prajaapat

Reputation: 1

Setting/Adding Feature flag in Azure App Configuration

Is there a way to set/add feature flags from Service Fabric Application. As far as I have searched, the only way to change and add feature flags is to do it via the portal. Is there a way to do it through the code itself.

Upvotes: 0

Views: 2437

Answers (3)

vikas
vikas

Reputation: 1

You can create a feature flag by using the following code snippet

  var client = new ConfigurationClient(_configuration.GetConnectionString("AppConnectionString"));
  var settingToCreate = new ConfigurationSetting(Key, Value);
  settingToCreate.ContentType = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8";
  client.SetConfigurationSetting(settingToCreate);

Please note the ContentType property on ConfigurationSetting is of feature flag same as the one provided in code snippet.

Key = .appconfig.featureflag/"your feature flag name"

Value = {
    "id": "your feature flag name",
    "description": "",
    "enabled": true,
    "conditions": {
        "client_filters": [
            {
                "name": "Microsoft.Targeting",
                "parameters": {
                    "Audience": {
                        "Users": [],
                        "Groups": [],
                        "DefaultRolloutPercentage": 50
                    }
                }
            }
        ]
    }
}

Find Medium Post here for entire end-to-end implementation

Upvotes: 0

hpsanampudi
hpsanampudi

Reputation: 679

To get and set azure feature flag status programatically (Useful in automation tests)

The feature flag json key value is in below format.

    {
        "id": "Feature-EnableClientRegistration",
        "description": "Enables client registration journey if selected otherwise disables.",
        "enabled": false,
        "conditions": null
    }
  1. Create a model to store this Json data
  2. Create a generic service to access feature flag status
    public class FeatureManagementService : IFeatureManagementService
        {
            private const string AZURE_APP_CONFIG_KEY_IDENTIFIER = ".appconfig.featureflag";
    
            private const string AZURE_FEATURE_MANAGEMENT_CONTENT_TYPE = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8";
    
            internal class FeatureFlagModel
            {
                [JsonPropertyName("id")]
                public string Id { get; set; }
    
                [JsonPropertyName("description")]
                public string Description { get; set; }
    
                [JsonPropertyName("enabled")]
                public bool Enabled { get; set; }
    
                [JsonPropertyName("conditions")]
                public object Conditions { get; set; }
            }
    
            public string Label { get; init; }
    
            private readonly ConfigurationClient client;
    
            public FeatureManagementService(string connectionStr, string label)
            {
                client = new ConfigurationClient(connectionStr);
    
                Label = label;
            }
    
            public bool GetFlagStatus(string featureFlagId)
            {
                var key = GetAppConfigFeatureFlagKey(featureFlagId);
    
                var configSettings = GetAppConfigFeatureFlagSetting(key, string.Empty, Label);
    
                var response = client.GetConfigurationSetting(configSettings);
    
                var model = JsonSerializer.Deserialize<FeatureFlagModel>(response.Value.Value);
    
                return model.Enabled;
            }
    
            public void SetFlagStatus(string featureFlagId, bool isEnabled)
            {
                var key = GetAppConfigFeatureFlagKey(featureFlagId);
    
                var model = new FeatureFlagModel
                {
                    Id = featureFlagId,
                    Description = FeatureConstants.GetDescription(featureFlagId),
                    Enabled = isEnabled,
                    Conditions = default
                };
    
                var value = JsonSerializer.Serialize(model);
    
                var configSettings = GetAppConfigFeatureFlagSetting(key, value, Label);
    
                client.SetConfigurationSetting(configSettings);
            }
    
            private static string GetAppConfigFeatureFlagKey(string featureFlagId) =>
                $"{AZURE_APP_CONFIG_KEY_IDENTIFIER}/{featureFlagId}";
    
            private static ConfigurationSetting GetAppConfigFeatureFlagSetting(
                string key, string value, string label) =>
                new(key, value, label)
                {
                    ContentType = AZURE_FEATURE_MANAGEMENT_CONTENT_TYPE
                };
        }   
  1. Create a constants class
    public static class FeatureConstants
    {
        [Description("Enables client registration journey if selected otherwise disables.")]
        public const string FeatureEnableClientRegistration = "Feature-EnableClientRegistration";
    
        public static string GetDescription(string featureFlagId)
        {
            var comparer = StringComparison.InvariantCultureIgnoreCase;
    
            var featureFieldInfo = typeof(FeatureConstants)
                .GetFields(BindingFlags.Public | BindingFlags.Static)
                .SingleOrDefault(f => featureFlagId.Equals((string)f.GetValue(null), comparer));
    
            var featureDescription = featureFieldInfo?
                .GetCustomAttribute<DescriptionAttribute>(true)?
                .Description ?? string.Empty;
    
            return featureDescription;
        }
    }
  1. We can consume by passing azure appconfig store connection string as shown below:
    var connectionStr = "Endpoint=https://*<placeholder>*.azconfig.io;Id=*<placeholder>*;Secret=*<placeholder>*";
    
    var service = new FeatureManagementService(connectionStr, "Stagging");
    
    var isEnabled = service.GetFlagStatus("Feature-EnableClientRegistration");
    
    service.SetFlagStatus("Feature-EnableClientRegistration", !isEnabled);

Upvotes: 0

Abhilash Arora
Abhilash Arora

Reputation: 277

The Azure App Configuration SDK can be used to create and update feature flags from a Service Fabric application. Each feature flag is stored as a key-value with the key prefix .appconfig.featureflag/. The SDK for .NET can be found here. The conventions and schema for feature flags are documented here.

Upvotes: 2

Related Questions