File: snapshot_sample.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (54 lines) | stat: -rw-r--r-- 2,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------

from azure.appconfiguration.provider import load, SettingSelector
from sample_utilities import get_authority, get_credential, get_client_modifications
import os

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)

# Loading configuration settings from a snapshot
# Note: The snapshot must already exist in your App Configuration store
snapshot_name = "my-snapshot-name"
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
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 = 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}")

# 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`.
feature_flag_selects = [SettingSelector(snapshot_name=snapshot_name)]
config_with_flags = load(
    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())}"
)