Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,114 @@
import asyncio
from azure.appconfiguration.provider.aio import load
from azure.appconfiguration.provider import SettingSelector
from sample_utilities import get_client_modifications
from azure.appconfiguration.aio import AzureAppConfigurationClient
from azure.appconfiguration import ConfigurationSettingsFilter, ConfigurationSetting, FeatureFlagConfigurationSetting
from sample_utilities import get_authority, get_credential, get_client_modifications
import os
import uuid


async def main():
endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING")
authority = get_authority(endpoint)
credential = get_credential(authority, is_async=True)
kwargs = get_client_modifications()
connection_string = os.environ["APPCONFIGURATION_CONNECTION_STRING"]

# Loading configuration settings from a snapshot
# Note: The snapshot must already exist in your App Configuration store
snapshot_name = "my-snapshot-name"
# Step 1: Create a snapshot
# First, we'll create some configuration settings and then create a snapshot containing them
async with AzureAppConfigurationClient(endpoint, credential) as client:
# Create sample configuration settings (these will be included in the snapshot)
sample_settings = [
ConfigurationSetting(key="app/settings/message", value="Hello from snapshot!"),
ConfigurationSetting(key="app/settings/fontSize", value="14"),
ConfigurationSetting(key="app/settings/backgroundColor", value="#FFFFFF"),
]

# Create a feature flag (also included in the snapshot)
sample_feature_flag = FeatureFlagConfigurationSetting(
feature_id="Beta",
enabled=True,
description="Beta feature flag from snapshot sample",
)

# Override settings with "prod" label (used in mixed selects, not in snapshot)
override_settings = [
ConfigurationSetting(key="override.message", value="Production override!", label="prod"),
ConfigurationSetting(key="override.fontSize", value="16", label="prod"),
]

print("Creating sample configuration settings...")
for setting in sample_settings:
await client.set_configuration_setting(setting)
print(f" Created: {setting.key} = {setting.value}")

# Create the feature flag
await client.set_configuration_setting(sample_feature_flag)
print(f" Created feature flag: {sample_feature_flag.feature_id} = {sample_feature_flag.enabled}")

for setting in override_settings:
await client.set_configuration_setting(setting)
print(f" Created: {setting.key} = {setting.value} (label: {setting.label})")

# Generate a unique snapshot name
snapshot_name = f"sample-snapshot-{uuid.uuid4().hex[:8]}"

# Create snapshot with filters for app settings and feature flags (retention_period=3600 seconds = 1 hour)
snapshot_filters = [
ConfigurationSettingsFilter(key="app/*"),
ConfigurationSettingsFilter(key=".appconfig.featureflag/*"),
]

try:
poller = await client.begin_create_snapshot(
name=snapshot_name, filters=snapshot_filters, retention_period=3600
)
created_snapshot = await poller.result()
print(f"Created snapshot: {created_snapshot.name} with status: {created_snapshot.status}")
except Exception as e:
print(f"Error creating snapshot: {e}")
print("Make sure you have configuration settings with keys starting with 'app/' in your store.")
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
raise

# Step 2: Loading configuration settings from the snapshot
snapshot_selects = [SettingSelector(snapshot_name=snapshot_name)]
config = await load(connection_string=connection_string, selects=snapshot_selects, **kwargs)
config = await load(endpoint=endpoint, credential=credential, selects=snapshot_selects, **kwargs)

print("Configuration settings from snapshot:")
for key, value in config.items():
print(f"{key}: {value}")
await config.close()

# You can also combine snapshot-based selectors with regular selectors
# The snapshot settings and filtered settings will be merged, with later selectors taking precedence
# Step 3: Combine snapshot with regular selectors (later selectors take precedence)
mixed_selects = [
SettingSelector(snapshot_name=snapshot_name), # Load all settings from snapshot
SettingSelector(key_filter="override.*", label_filter="prod"), # Also load specific override settings
]
config_mixed = await load(connection_string=connection_string, selects=mixed_selects, **kwargs)
config_mixed = await load(endpoint=endpoint, credential=credential, selects=mixed_selects, **kwargs)

print("\nMixed configuration (snapshot + filtered settings):")
for key, value in config_mixed.items():
print(f"{key}: {value}")
await config_mixed.close()

# Loading feature flags from a snapshot
# To load feature flags from a snapshot, include the snapshot selector in the 'selects' parameter and set feature_flag_enabled=True.
# Step 4: Load feature flags from the snapshot (requires feature_flag_enabled=True)
feature_flag_selects = [SettingSelector(snapshot_name=snapshot_name)]
config_with_flags = await load(
connection_string=connection_string,
endpoint=endpoint,
credential=credential,
selects=feature_flag_selects,
feature_flag_enabled=True,
**kwargs,
)
print(
f"\nConfiguration includes feature flags: {any(key.startswith('.appconfig.featureflag/') for key in config_with_flags.keys())}"
)

print(f"\nFeature flags loaded: {'feature_management' in config_with_flags}")
if "feature_management" in config_with_flags:
feature_flags = config_with_flags["feature_management"].get("feature_flags", [])
for flag in feature_flags:
print(f" {flag['id']}: enabled={flag['enabled']}")

await config_with_flags.close()
await credential.close()


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,88 @@
# -------------------------------------------------------------------------

from azure.appconfiguration.provider import load, SettingSelector
from azure.appconfiguration import (
AzureAppConfigurationClient,
ConfigurationSettingsFilter,
ConfigurationSnapshot,
ConfigurationSetting,
FeatureFlagConfigurationSetting,
)
from sample_utilities import get_authority, get_credential, get_client_modifications
import os
import uuid

endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING")
authority = get_authority(endpoint)
credential = get_credential(authority)
kwargs = get_client_modifications()

# Connecting to Azure App Configuration using AAD
config = load(endpoint=endpoint, credential=credential, **kwargs)
# Step 1: Create a snapshot
# First, we'll create some configuration settings and then create a snapshot containing them
client = AzureAppConfigurationClient(endpoint, credential)


# Create sample configuration settings (these will be included in the snapshot)
sample_settings = [
ConfigurationSetting(key="app/settings/message", value="Hello from snapshot!"),
ConfigurationSetting(key="app/settings/fontSize", value="14"),
ConfigurationSetting(key="app/settings/backgroundColor", value="#FFFFFF"),
]

# Create a feature flag (also included in the snapshot)
sample_feature_flag = FeatureFlagConfigurationSetting(
feature_id="Beta",
enabled=True,
description="Beta feature flag from snapshot sample",
)

# Override settings with "prod" label (used in mixed selects, not in snapshot)
override_settings = [
ConfigurationSetting(key="override.message", value="Production override!", label="prod"),
ConfigurationSetting(key="override.fontSize", value="16", label="prod"),
]

print("Creating sample configuration settings...")
for setting in sample_settings:
# client.set_configuration_setting(setting)
print(f" Created: {setting.key} = {setting.value}")

# Create the feature flag
# client.set_configuration_setting(sample_feature_flag)
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
print(f" Created feature flag: {sample_feature_flag.feature_id} = {sample_feature_flag.enabled}")

for setting in override_settings:
# client.set_configuration_setting(setting)
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
print(f" Created: {setting.key} = {setting.value} (label: {setting.label})")

# Generate a unique snapshot name
snapshot_name = f"sample-snapshot-{uuid.uuid4().hex[:8]}"

# Loading configuration settings from a snapshot
# Note: The snapshot must already exist in your App Configuration store
snapshot_name = "my-snapshot-name"
# Create snapshot with filters for app settings and feature flags (retention_period=3600 seconds = 1 hour)
snapshot_filters = [
ConfigurationSettingsFilter(key="app/*"),
ConfigurationSettingsFilter(key=".appconfig.featureflag/*"),
]

try:
created_snapshot = client.begin_create_snapshot(
name=snapshot_name, filters=snapshot_filters, retention_period=3600
).result()
print(f"Created snapshot: {created_snapshot.name} with status: {created_snapshot.status}")
except Exception as e:
print(f"Error creating snapshot: {e}")
print("Make sure you have configuration settings with keys starting with 'app/' in your store.")
raise

# Step 2: Loading configuration settings from the snapshot
snapshot_selects = [SettingSelector(snapshot_name=snapshot_name)]
config = load(endpoint=endpoint, credential=credential, selects=snapshot_selects, **kwargs)

print("Configuration settings from snapshot:")
for key, value in config.items():
print(f"{key}: {value}")

# You can also combine snapshot-based selectors with regular selectors
# The snapshot settings and filtered settings will be merged, with later selectors taking precedence
# Step 3: Combine snapshot with regular selectors (later selectors take precedence)
mixed_selects = [
SettingSelector(snapshot_name=snapshot_name), # Load all settings from snapshot
SettingSelector(key_filter="override.*", label_filter="prod"), # Also load specific override settings
Expand All @@ -38,8 +97,7 @@
for key, value in config_mixed.items():
print(f"{key}: {value}")

# Loading feature flags from a snapshot
# To load feature flags from a snapshot, include the snapshot selector in the `selects` parameter and set `feature_flag_enabled=True`.
# Step 4: Load feature flags from the snapshot (requires feature_flag_enabled=True)
feature_flag_selects = [SettingSelector(snapshot_name=snapshot_name)]
config_with_flags = load(
endpoint=endpoint,
Expand All @@ -49,6 +107,8 @@
**kwargs,
)

print(
f"\nConfiguration includes feature flags: {any(key.startswith('.appconfig.featureflag/') for key in config_with_flags.keys())}"
)
print(f"\nFeature flags loaded: {'feature_management' in config_with_flags}")
if "feature_management" in config_with_flags:
feature_flags = config_with_flags["feature_management"].get("feature_flags", [])
for flag in feature_flags:
print(f" {flag['id']}: enabled={flag['enabled']}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: hello_world_sample.py

DESCRIPTION:
This sample demos how to add/update/retrieve/delete configuration settings synchronously.

USAGE: python hello_world_sample.py
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
Comment thread
mrm9084 marked this conversation as resolved.
Outdated

Set the environment variables with your own values before running the sample:
1) APPCONFIGURATION_CONNECTION_STRING: Connection String used to access the Azure App Configuration.
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
Comment thread
mrm9084 marked this conversation as resolved.
Outdated
"""
import os
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential
from azure.appconfiguration import ConfigurationSetting


def main():
# [START create_app_config_client]
Comment thread
avanigupta marked this conversation as resolved.

ENDPOINT = os.environ["APPCONFIGURATION_ENDPOINT"]
credential = DefaultAzureCredential()
# Create app config client
client = AzureAppConfigurationClient(base_url=ENDPOINT, credential=credential)
# [END create_app_config_client]

print("Add new configuration setting")
# [START create_config_setting]
config_setting = ConfigurationSetting(
key="MyKey", label="MyLabel", value="my value", content_type="my content type", tags={"my tag": "my tag value"}
)
added_config_setting = client.add_configuration_setting(config_setting)
# [END create_config_setting]
print("New configuration setting:")
print(added_config_setting)
print("")

print("Set configuration setting")
# [START set_config_setting]
added_config_setting.value = "new value"
added_config_setting.content_type = "new content type"
updated_config_setting = client.set_configuration_setting(added_config_setting)
# [END set_config_setting]
print(updated_config_setting)
print("")

print("Get configuration setting")
# [START get_config_setting]
fetched_config_setting = client.get_configuration_setting(key="MyKey", label="MyLabel")
# [END get_config_setting]
print("Fetched configuration setting:")
print(fetched_config_setting)
print("")

print("Delete configuration setting")
# [START delete_config_setting]
client.delete_configuration_setting(key="MyKey", label="MyLabel")
# [END delete_config_setting]


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
4) AZURE_CLIENT_SECRET: Your application client secret

For Azure Bleu (French Sovereign Cloud):
- Use credential_scopes: ["https://appconfig.sovcloud-api.fr/.default"]
- Use audience: ["https://appconfig.sovcloud-api.fr/"]

DefaultAzureCredential will attempt multiple authentication methods:
- Environment variables (AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
Expand All @@ -33,21 +33,20 @@
- Azure PowerShell
- Interactive browser
"""
import os
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential
from azure.appconfiguration import ConfigurationSetting


def main():
# [START create_app_config_client_entra_id]
import os
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential

ENDPOINT = os.environ["APPCONFIGURATION_ENDPOINT"]

# Create app config client with Entra ID authentication
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(
base_url=ENDPOINT, credential=credential, credential_scopes=["https://appconfig.sovcloud-api.fr/.default"]
base_url=ENDPOINT, credential=credential, audience="https://appconfig.sovcloud-api.fr/"
)
# [END create_app_config_client_entra_id]

Expand Down
Loading