No project description provided
Project description
pydantic-settings-azure-app-configuration
Introduction
https://learn.microsoft.com/en-us/azure/azure-app-configuration/overview
Installation
pip install pydantic-settings-azure-app-configuration
Usage
By default, it loads all the values from Azure App Configuration, but you can assign a prefix to your application (e.g. my_api__
) and load only the values for it using the select_key
method. You can also use the trim_key_prefix
method to remove the prefix from the value names.
Furthermore, if some value references a secret stored in Azure Key Vault, you can use the configure_key_vault
method to retrieve it.
If you use Entra ID authentication, you can use the role App Configuration Data Reader
to access the configurations and Key Vault Secrets User
to access the secrets.
To nest models you have to define a env_nested_delimiter
(e.g. __
), either in the source constructor or in the model_config
class.
The configuration of this settings source is almost idental to the the provided by ASP.NET Core, in case you want to read the official documentation to inform you about more complex uses, best practices, etc.
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
class SqlServer(BaseModel):
password: str
host: str
class AzureKeyVaultSettings(BaseSettings):
logging_level: str
sql_server: SqlServer
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
azure_credential = DefaultAzureCredential()
azure_app_configuration = AzureAppConfigurationSettingsSource(
settings_cls,
lambda app_configuration_options: app_configuration_options.connect_with_url(
os.environ["AZURE_APP_CONFIGURATION_URL"], azure_credential
)
.select_key("my_api__*")
.trim_key_prefix("my_api__")
.configure_key_vault(
lambda key_vault_options: key_vault_options.set_credential(
azure_credential
)
),
env_nested_delimiter="__",
)
return (
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
azure_app_configuration,
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_settings_azure_app_configuration-1.0.0.tar.gz
.
File metadata
- Download URL: pydantic_settings_azure_app_configuration-1.0.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f311088947e6e3d6afd9521cfc6b81e92af70929a2661a4af1d87fc9b85793 |
|
MD5 | e5213fb4f9b4d0b2b8ef4269fdae1557 |
|
BLAKE2b-256 | 0cc13bd7a0660f4b8006f6b12ebad0f97d044b8164826501d44a71f07e5c2897 |
File details
Details for the file pydantic_settings_azure_app_configuration-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_settings_azure_app_configuration-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4a34960a2e2edef26376f904112e0a19f02f3b1b4bb5d7a0a2426ae9a42c8cc |
|
MD5 | f73f27274745380857a37370483e1b6b |
|
BLAKE2b-256 | f7fc2493aae1b074cbe5c295a46e5dd0a825a6ec2fa7972075c6cbeed8f94303 |