Skip to content

Commit 5540aca

Browse files
authored
Merge pull request #293991 from maud-lv/ml-azcredpython
Add Microsoft Entra ID tab to Python quickstart
2 parents fc49047 + ba3bb60 commit 5540aca

File tree

1 file changed

+170
-122
lines changed

1 file changed

+170
-122
lines changed

articles/azure-app-configuration/quickstart-python.md

Lines changed: 170 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: azure-app-configuration
77
ms.devlang: python
88
ms.topic: sample
99
ms.custom: devx-track-python, mode-other, engagement-fy23, py-fresh-zinc
10-
ms.date: 11/20/2023
10+
ms.date: 02/03/2025
1111
ms.author: malev
1212
#Customer intent: As a Python developer, I want to use the Azure SDK for Python to access my data in Azure App Configuration.
1313
---
@@ -70,76 +70,6 @@ Add the following key-value to the App Configuration store and leave **Label** a
7070
> [!NOTE]
7171
> The code snippets in this example will help you get started with the App Configuration client library for Python. For your application, you should also consider handling exceptions according to your needs. To learn more about exception handling, please refer to our [Python SDK documentation](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/appconfiguration/azure-appconfiguration).
7272

73-
## Configure your App Configuration connection string
74-
75-
1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command:
76-
77-
### [Windows command prompt](#tab/windowscommandprompt)
78-
79-
To run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
80-
81-
```cmd
82-
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
83-
```
84-
85-
### [PowerShell](#tab/powershell)
86-
87-
If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
88-
89-
```azurepowershell
90-
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>"
91-
```
92-
93-
### [macOS](#tab/unix)
94-
95-
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
96-
97-
```console
98-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
99-
```
100-
101-
### [Linux](#tab/linux)
102-
103-
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
104-
105-
```console
106-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
107-
```
108-
109-
1. Print out the value of the environment variable to validate that it is set properly with the command below.
110-
111-
### [Windows command prompt](#tab/windowscommandprompt)
112-
113-
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
114-
115-
```cmd
116-
echo %AZURE_APPCONFIG_CONNECTION_STRING%
117-
```
118-
119-
### [PowerShell](#tab/powershell)
120-
121-
If you use Windows PowerShell, run the following command:
122-
123-
```azurepowershell
124-
$Env:AZURE_APPCONFIG_CONNECTION_STRING
125-
```
126-
127-
### [macOS](#tab/unix)
128-
129-
If you use macOS, run the following command:
130-
131-
```console
132-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
133-
```
134-
135-
### [Linux](#tab/linux)
136-
137-
If you use Linux, run the following command:
138-
139-
```console
140-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
141-
```
142-
14373
## Code samples
14474

14575
The sample code snippets in this section show you how to perform common operations with the App Configuration client library for Python. Add these code snippets to the `try` block in *app-configuration-example.py* file you created earlier.
@@ -160,21 +90,37 @@ Learn below how to:
16090

16191
### Connect to an App Configuration store
16292

163-
The following code snippet creates an instance of **AzureAppConfigurationClient** using the connection string stored in your environment variables.
93+
The following code snippet creates an instance of **AzureAppConfigurationClient**. You can connect to your App Configuration store using Microsoft Entra ID (recommended), or a connection string.
94+
95+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
96+
97+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
16498

16599
```python
166-
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
167-
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
100+
from azure.identity import DefaultAzureCredential
101+
102+
credential = DefaultAzureCredential()
103+
104+
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
105+
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
106+
```
107+
108+
### [Connection string](#tab/connection-string)
109+
110+
```python
111+
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
112+
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
168113
```
114+
---
169115

170116
### Get a configuration setting
171117

172118
The following code snippet retrieves a configuration setting by `key` name.
173119

174120
```python
175-
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
176-
print("\nRetrieved configuration setting:")
177-
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
121+
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
122+
print("\nRetrieved configuration setting:")
123+
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
178124
```
179125

180126
### Add a configuration setting
@@ -183,64 +129,63 @@ The following code snippet creates a `ConfigurationSetting` object with `key` an
183129
This method will throw an exception if you try to add a configuration setting that already exists in your store. If you want to avoid this exception, the [set_configuration_setting](#update-a-configuration-setting) method can be used instead.
184130

185131
```python
186-
config_setting = ConfigurationSetting(
187-
key='TestApp:Settings:NewSetting',
188-
value='New setting value'
189-
)
190-
added_config_setting = app_config_client.add_configuration_setting(config_setting)
191-
print("\nAdded configuration setting:")
192-
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
132+
config_setting = ConfigurationSetting(
133+
key='TestApp:Settings:NewSetting',
134+
value='New setting value'
135+
)
136+
added_config_setting = app_config_client.add_configuration_setting(config_setting)
137+
print("\nAdded configuration setting:")
138+
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
193139
```
194140

195141
### Get a list of configuration settings
196142

197143
The following code snippet retrieves a list of configuration settings. The `key_filter` and `label_filter` arguments can be provided to filter key-values based on `key` and `label` respectively. For more information on filtering, see how to [query configuration settings](./concept-key-value.md#query-key-values).
198144

199145
```python
200-
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
201-
print("\nRetrieved list of configuration settings:")
202-
for item in filtered_settings_list:
203-
print("Key: " + item.key + ", Value: " + item.value)
146+
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
147+
print("\nRetrieved list of configuration settings:")
148+
for item in filtered_settings_list:
149+
print("Key: " + item.key + ", Value: " + item.value)
204150
```
205151

206152
### Lock a configuration setting
207153

208154
The lock status of a key-value in App Configuration is denoted by the `read_only` attribute of the `ConfigurationSetting` object. If `read_only` is `True`, the setting is locked. The `set_read_only` method can be invoked with `read_only=True` argument to lock the configuration setting.
209155

210156
```python
211-
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
212-
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
157+
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
158+
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
213159
```
214160

215161
### Unlock a configuration setting
216162

217163
If the `read_only` attribute of a `ConfigurationSetting` is `False`, the setting is unlocked. The `set_read_only` method can be invoked with `read_only=False` argument to unlock the configuration setting.
218164

219165
```python
220-
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
221-
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
166+
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
167+
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
222168
```
223169

224170
### Update a configuration setting
225171

226172
The `set_configuration_setting` method can be used to update an existing setting or create a new setting. The following code snippet changes the value of an existing configuration setting.
227173

228174
```python
229-
added_config_setting.value = "Value has been updated!"
230-
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
231-
print("\nUpdated configuration setting:")
232-
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
175+
added_config_setting.value = "Value has been updated!"
176+
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
177+
print("\nUpdated configuration setting:")
178+
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
233179
```
234180

235181
### Delete a configuration setting
236182

237183
The following code snippet deletes a configuration setting by `key` name.
238184

239185
```python
240-
241-
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
242-
print("\nDeleted configuration setting:")
243-
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
186+
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
187+
print("\nDeleted configuration setting:")
188+
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
244189
```
245190

246191
## Run the app
@@ -249,16 +194,20 @@ In this example, you created a Python app that uses the Azure App Configuration
249194

250195
At this point, your *app-configuration-example.py* file should have the following code:
251196

197+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
198+
252199
```python
253200
import os
201+
from azure.identity import DefaultAzureCredential
254202
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
255203

256204
try:
257205
print("Azure App Configuration - Python example")
258206
# Example code goes here
259207

260-
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
261-
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
208+
credential = DefaultAzureCredential()
209+
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
210+
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
262211

263212
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
264213
print("\nRetrieved configuration setting:")
@@ -297,38 +246,137 @@ except Exception as ex:
297246
print(ex)
298247
```
299248

300-
In your console window, navigate to the directory containing the *app-configuration-example.py* file and execute the following Python command to run the app:
249+
### [Connection string](#tab/connection-string)
301250

302-
```console
303-
python app-configuration-example.py
304-
```
251+
```python
252+
import os
253+
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
305254

306-
You should see the following output:
255+
try:
256+
print("Azure App Configuration - Python example")
257+
# Example code goes here
307258

308-
```output
309-
Azure App Configuration - Python example
259+
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
260+
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
310261

311-
Retrieved configuration setting:
312-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
262+
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
263+
print("\nRetrieved configuration setting:")
264+
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
313265

314-
Added configuration setting:
315-
Key: TestApp:Settings:NewSetting, Value: New setting value
266+
config_setting = ConfigurationSetting(
267+
key='TestApp:Settings:NewSetting',
268+
value='New setting value'
269+
)
270+
added_config_setting = app_config_client.add_configuration_setting(config_setting)
271+
print("\nAdded configuration setting:")
272+
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
316273

317-
Retrieved list of configuration settings:
318-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
319-
Key: TestApp:Settings:NewSetting, Value: New setting value
274+
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
275+
print("\nRetrieved list of configuration settings:")
276+
for item in filtered_settings_list:
277+
print("Key: " + item.key + ", Value: " + item.value)
278+
279+
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
280+
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
320281

321-
Read-only status for TestApp:Settings:NewSetting: True
282+
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
283+
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
322284

323-
Read-only status for TestApp:Settings:NewSetting: False
285+
added_config_setting.value = "Value has been updated!"
286+
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
287+
print("\nUpdated configuration setting:")
288+
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
324289

325-
Updated configuration setting:
326-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
290+
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
291+
print("\nDeleted configuration setting:")
292+
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
327293

328-
Deleted configuration setting:
329-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
294+
except Exception as ex:
295+
print('Exception:')
296+
print(ex)
330297
```
331298

299+
---
300+
301+
1. Configure an environment variable
302+
303+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
304+
305+
Set the environment variable named **AZURE_APPCONFIG_ENDPOINT** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
306+
307+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
308+
309+
```cmd
310+
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
311+
```
312+
313+
If you use PowerShell, run the following command:
314+
315+
```powershell
316+
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
317+
```
318+
319+
If you use macOS or Linux, run the following command:
320+
321+
```bash
322+
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
323+
```
324+
325+
### [Connection string](#tab/connection-string)
326+
327+
Set the environment variable named **AZURE_APPCONFIG_CONNECTION_STRING** to the read-only connection string of your App Configuration store found under *Access keys* of your store in the Azure portal.
328+
329+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
330+
331+
```cmd
332+
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
333+
```
334+
335+
If you use PowerShell, run the following command:
336+
337+
```powershell
338+
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
339+
```
340+
341+
If you use macOS or Linux, run the following command:
342+
343+
```bash
344+
export AZURE_APPCONFIG_CONNECTION_STRING='<connection-string-of-your-app-configuration-store>'
345+
```
346+
---
347+
348+
1. After the environment variable is properly set, in your console window, navigate to the directory containing the *app-configuration-example.py* file and execute the following Python command to run the app:
349+
350+
```console
351+
python app-configuration-example.py
352+
```
353+
354+
You should see the following output:
355+
356+
```output
357+
Azure App Configuration - Python example
358+
359+
Retrieved configuration setting:
360+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
361+
362+
Added configuration setting:
363+
Key: TestApp:Settings:NewSetting, Value: New setting value
364+
365+
Retrieved list of configuration settings:
366+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
367+
Key: TestApp:Settings:NewSetting, Value: New setting value
368+
369+
Read-only status for TestApp:Settings:NewSetting: True
370+
371+
Read-only status for TestApp:Settings:NewSetting: False
372+
373+
Updated configuration setting:
374+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
375+
376+
Deleted configuration setting:
377+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
378+
```
379+
332380
## Clean up resources
333381
334382
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]

0 commit comments

Comments
 (0)