Azure CDN Management Client Library
The Azure CDN Management Client Library for Python provides programmatic access to manage Azure Content Delivery Network (CDN) resources. It allows users to create, configure, update, and delete CDN profiles, endpoints, and custom domains. Part of the broader Azure SDK for Python, it follows the SDK's consistent design principles and release cadence, typically receiving updates as new Azure API versions are released or significant changes occur in the underlying service, often with minor version bumps and major version updates for breaking changes.
Warnings
- breaking Major version 13.0.0 introduced significant breaking changes, primarily standardizing on `azure-identity` for authentication and `azure-core` for common client primitives. If upgrading from versions prior to 13.0.0, expect changes to authentication mechanisms and potentially to resource model classes and method signatures.
- gotcha Operations that create, update, or delete resources (e.g., `profiles.begin_create`) are often long-running operations and return a 'poller' object (e.g., `LROPoller`). You must call `.result()` on the poller to wait for the operation to complete and retrieve the final resource object or status.
- gotcha Listing operations (e.g., `client.profiles.list()`, `client.endpoints.list_by_profile()`) return an iterable object (e.g., `ItemPaged`), not a direct list. This object automatically handles pagination internally. Attempting to index directly or treat it as a static list without iterating might lead to unexpected behavior or incomplete data.
- gotcha `DefaultAzureCredential` attempts multiple authentication methods. While convenient, it can sometimes be unclear which method is being used or why authentication is failing. For production environments or specific scenarios (e.g., CI/CD, Managed Identity), it's often more robust to explicitly use a specific credential type.
Install
-
pip install azure-mgmt-cdn
Imports
- CdnManagementClient
from azure.mgmt.cdn import CdnManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.cdn import CdnManagementClient
# --- Configuration ---
# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables.
# You can also set AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET for service principal auth.
AZURE_SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
if not AZURE_SUBSCRIPTION_ID:
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
exit(1)
print(f"Using Subscription ID: {AZURE_SUBSCRIPTION_ID}")
# Authenticate with Azure using DefaultAzureCredential
# This credential chain attempts to authenticate via various methods:
# Environment variables, Managed Identity, Visual Studio Code, Azure CLI, Azure PowerShell.
credential = DefaultAzureCredential()
# Create a CDN management client
# All CDN operations are performed through this client object.
client = CdnManagementClient(credential, AZURE_SUBSCRIPTION_ID)
print("\nListing CDN Profiles in the subscription...")
# List all CDN profiles. This returns an iterator (ItemPaged).
# Iterating directly fetches pages as needed.
cdn_profiles = client.profiles.list()
profile_count = 0
for profile in cdn_profiles:
profile_count += 1
print(f" - Profile Name: {profile.name}, Location: {profile.location}, Sku: {profile.sku.name}")
if profile_count == 0:
print(" No CDN profiles found in this subscription.")
else:
print(f"Found {profile_count} CDN profile(s).")
# Example of a long-running operation (creation of a CDN profile)
# This would typically involve a begin_ method and a poller.
# resource_group_name = "myResourceGroup"
# profile_name = "myUniqueCdnProfile"
# profile_parameters = {
# "location": "westus",
# "sku": {"name": "Standard_Microsoft"}
# }
# print(f"\nStarting creation of CDN Profile '{profile_name}'...")
# poller = client.profiles.begin_create(resource_group_name, profile_name, profile_parameters)
# profile = poller.result() # Wait for the operation to complete
# print(f"CDN Profile '{profile.name}' created successfully.")