Skip to content

[python-skills] the generated code uses from_connection_string instead of DAC. #288

@xiangyan99

Description

@xiangyan99

Samples:

"""
Azure App Configuration Manager
Demonstrates CRUD operations for configuration settings and feature flags.
"""

import os
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting, FeatureFlagConfigurationSetting
from azure.core.exceptions import HttpResponseError, ResourceNotFoundError


def main():
    # Get connection string from environment variable
    connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
    
    if not connection_string:
        raise ValueError(
            "AZURE_APPCONFIG_CONNECTION_STRING environment variable is not set.\n"
            "Please set it to your App Configuration connection string."
        )
    
    try:
        # 1. Create an AzureAppConfigurationClient
        print("Creating Azure App Configuration client...")
        client = AzureAppConfigurationClient.from_connection_string(connection_string)
        print("✓ Client created successfully\n")
        
        # 2. Set a configuration setting with key "app:Settings:FontSize" and value "24"
        print("Setting configuration: app:Settings:FontSize = 24")
        config_setting = ConfigurationSetting(
            key="app:Settings:FontSize",
            value="24"
        )
        client.set_configuration_setting(config_setting)
        print("✓ Configuration setting created\n")
        
        # 3. Set a setting with label "Production"
        print("Setting configuration with label 'Production'...")
        config_setting_with_label = ConfigurationSetting(
            key="app:Settings:FontSize",
            label="Production",
            value="18"
        )
        client.set_configuration_setting(config_setting_with_label)
        print("✓ Configuration setting with label created\n")
        
        # 4. Get the setting by key and print its value
        print("Retrieving configuration setting by key...")
        retrieved_setting = client.get_configuration_setting(key="app:Settings:FontSize")
        print(f"✓ Retrieved setting:")
        print(f"  Key: {retrieved_setting.key}")
        print(f"  Value: {retrieved_setting.value}")
        print(f"  Label: {retrieved_setting.label or '(no label)'}")
        print(f"  Content Type: {retrieved_setting.content_type or '(not set)'}")
        print()
        
        # Also retrieve the one with Production label
        print("Retrieving configuration setting with 'Production' label...")
        retrieved_prod_setting = client.get_configuration_setting(
            key="app:Settings:FontSize",
            label="Production"
        )
        print(f"✓ Retrieved setting:")
        print(f"  Key: {retrieved_prod_setting.key}")
        print(f"  Value: {retrieved_prod_setting.value}")
        print(f"  Label: {retrieved_prod_setting.label}")
        print()
        
        # 5. List all settings matching the key filter "app:Settings:*"
        print("Listing all settings matching 'app:Settings:*'...")
        settings = client.list_configuration_settings(key_filter="app:Settings:*")
        print("✓ Settings found:")
        for setting in settings:
            print(f"  - Key: {setting.key}, Value: {setting.value}, Label: {setting.label or '(no label)'}")
        print()
        
        # 6. Create a FeatureFlagConfigurationSetting for "BetaFeature" that is enabled
        print("Creating feature flag 'BetaFeature'...")
        feature_flag = FeatureFlagConfigurationSetting(
            feature_id="BetaFeature",
            enabled=True,
            description="Beta feature for testing new functionality"
        )
        client.set_configuration_setting(feature_flag)
        print("✓ Feature flag created and enabled\n")
        
        # Verify the feature flag was created
        print("Retrieving feature flag...")
        retrieved_feature = client.get_configuration_setting(key=feature_flag.key)
        if isinstance(retrieved_feature, FeatureFlagConfigurationSetting):
            print(f"✓ Feature flag retrieved:")
            print(f"  Feature ID: {retrieved_feature.feature_id}")
            print(f"  Enabled: {retrieved_feature.enabled}")
            print(f"  Description: {retrieved_feature.description}")
        print()
        
        # 7. Delete the setting by key
        print("Deleting configuration setting 'app:Settings:FontSize' (no label)...")
        client.delete_configuration_setting(key="app:Settings:FontSize")
        print("✓ Configuration setting deleted\n")
        
        print("Deleting configuration setting 'app:Settings:FontSize' (Production label)...")
        client.delete_configuration_setting(key="app:Settings:FontSize", label="Production")
        print("✓ Configuration setting with Production label deleted\n")
        
        print("Deleting feature flag 'BetaFeature'...")
        client.delete_configuration_setting(key=feature_flag.key)
        print("✓ Feature flag deleted\n")
        
        # Verify deletion
        print("Verifying deletion...")
        try:
            client.get_configuration_setting(key="app:Settings:FontSize")
            print("⚠ Warning: Setting still exists")
        except ResourceNotFoundError:
            print("✓ Confirmed: Setting 'app:Settings:FontSize' has been deleted")
        
        print("\n" + "="*60)
        print("All operations completed successfully!")
        print("="*60)
        
    except HttpResponseError as e:
        print(f"\n❌ HTTP Error occurred:")
        print(f"Status Code: {e.status_code}")
        print(f"Error Code: {e.error.code if hasattr(e, 'error') else 'N/A'}")
        print(f"Message: {e.message}")
        raise
    
    except ResourceNotFoundError as e:
        print(f"\n❌ Resource not found:")
        print(f"Message: {e.message}")
        raise
    
    except Exception as e:
        print(f"\n❌ Unexpected error occurred:")
        print(f"Type: {type(e).__name__}")
        print(f"Message: {str(e)}")
        raise


if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions