File: sample_config.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (141 lines) | stat: -rw-r--r-- 6,314 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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: sample_config.py

DESCRIPTION:
    This sample demonstrates how to use a Configuration Store with AzureApp and AzureInfrastructure.

    This sample includes provisioning the following Azure resources which may incur charges:
    - Azure Resource Group
    - Azure User-Assigned Managed Identity
    - Azure Storage (SKU: Standard_GRS)
    - Azure App Configuration (SKU: Free)


    See pricing: https://azure.microsoft.com/pricing/.

USAGE:
    python sample_config.py

    Running the samples requires that Azure Developer CLI be installed and authenticated:
    For more information: https://learn.microsoft.com/azure/developer/azure-developer-cli/
"""
import asyncio
import time
import logging

from azure.projects import deprovision

unique_suffix = int(time.time())


async def main():
    from azure.storage.blob import BlobServiceClient
    from azure.projects import AzureApp

    class SampleApp(AzureApp):
        data: BlobServiceClient

    # The AzureApp base class has a default field called 'config_store', of type Mapping[str, Any].
    # An Azure Projects deployment will by default include an Azure App Configuration resource
    # that will be automatically populated with the resource settings and passed into the AzureApp
    # instance. Note that this is used only for endpoint and resource name/id information, and no secrets
    # are stored there.
    with SampleApp.provision(location="eastus") as app:
        print("\n01. App configuration:")
        for key, value in app.config_store.items():
            print(f"{key}={value}")

    # When calling the load() class method, a config_store Mapping can be passed in to be used for the
    # construction of any client instances. If provided, this will replace any configuration store automatically
    # derived from the deployment infrastructure. In addition to this, when building clients, the constructor will
    # also attempt to load an AZD .env file from a previously provisioned deployment for this app, and read from
    # environment variables.
    # If the settings required to build the clients cannot be resolved, a RuntimeError will be raised.
    with SampleApp.load(config_store={"AZURE_BLOBS_ENDPOINT": "https://alternative.blob.core.windows.net/"}) as app:
        print("\n02. App configuration:")
        for key, value in app.config_store.items():
            print(f"{key}={value}")

    # The default Mapping instance used is a static dict loaded from the config store.
    # If you wish to make more extensive use of the config store beyond resource endpoints, you may
    # could use a dynamic mapping with automatic refresh capabilities, like the
    # AzureAppConfigurationProvider. To change the config_store, just overwrite the field (though note
    # that any change to the type must still support the Mapping protocol).
    from azure.storage.blob.aio import BlobServiceClient as AsyncBlobServiceClient
    from azure.appconfiguration.provider.aio import AzureAppConfigurationProvider

    class SampleApp(AzureApp):  # type: ignore[no-redef]
        config_store: AzureAppConfigurationProvider
        data: AsyncBlobServiceClient

    async with SampleApp.load() as app:
        print("\n03. App configuration:")
        for key, value in app.config_store.items():
            print(f"{key}={value}")

        deprovision(app, purge=True)

    # If you wish to update the default deployment for the ConfigStore, or add additional settings,
    # this can be done by specifying an AzureInfrastructure definition.
    # You can either overwrite the field definition on the class, or pass in your own custom instance.
    from azure.projects import AzureInfrastructure, field
    from azure.projects.resources.storage.blobs import BlobStorage
    from azure.projects.resources.appconfig import ConfigStore

    class SampleInfra(AzureInfrastructure):
        storage: BlobStorage = BlobStorage()

    # You can use an existing App Configuration Store, though in order to add settings to this store
    # a tole assignment needs to be added to the resource group - so you will need permissions to do that
    # in order for the deployment to succeed.
    # If you don't have such permisisons, you can still use the existing config store, but it will need to
    # be added as a separate field to the Infrastructure definition, and new settings will not be able to be
    # deployed to it.
    infra = SampleInfra(config_store=ConfigStore.reference(name="MyAppConfig", resource_group="MyResourceGroup"))

    # You can also use the config store for other settings relevant to your app
    from azure.projects.resources.appconfig.setting import ConfigSetting

    class SampleInfraLogging(AzureInfrastructure):
        log_level: str = field()
        debug_setting: ConfigSetting = ConfigSetting(name="LOGGING_LEVEL", value=log_level)
        storage: BlobStorage = BlobStorage()

    infra_with_logging = SampleInfraLogging(log_level="INFO")

    async with SampleApp.provision(infra_with_logging) as app:
        print("\n04. App configuration:")
        for key, value in app.config_store.items():
            print(f"{key}={value}")

        logger = logging.getLogger()
        logger.setLevel(app.config_store["LOGGING_LEVEL"])

        deprovision(app, purge=True)

    # If you wish to disable to default Configuration Store resource, this can be done by specifying
    # an AzureInfrastructure defintion, and either overwriting the default field for config_store, or
    # setting the value to None at construction.
    class SampleInfra(AzureInfrastructure):  # type: ignore[no-redef]
        storage: BlobStorage = BlobStorage()

    infra = SampleInfra(config_store=None)

    with SampleApp.provision(infra) as app:
        print("\n05.App configuration:", app.config_store)
        for key, value in app.config_store.items():
            print(f"{key}={value}")

        deprovision(app, purge=True)


if __name__ == "__main__":
    asyncio.run(main())