Azure Network Management Client Library
The Microsoft Azure Network Management Client Library for Python provides programmatic access to manage network resources within Azure subscriptions, including virtual networks, public IPs, network security groups, and more. It is part of the broader Azure SDK for Python and is currently at version 30.2.0, with regular updates aligning with Azure service API changes.
Warnings
- breaking Authentication method changes: The Azure SDK for Python (v20.0.0 and later) deprecated legacy credential classes like `ServicePrincipalCredentials` in `azure.common.credentials`. Modern authentication exclusively uses classes from `azure.identity` (e.g., `DefaultAzureCredential`).
- gotcha Long-Running Operations (LROs) now return a poller object. Operations like `create_or_update`, `delete`, and `begin_xyz` (prefixed) for resource provisioning or modification are asynchronous. They return an `LROPoller` object, not the final resource or `None` directly.
- breaking Significant API changes and refactoring occurred around v20.0.0. This includes changes to client method signatures, model classes, and how collection methods (`list`) are handled. Direct access to `.value` on list results is no longer necessary; iterators are returned.
Install
-
pip install azure-mgmt-network azure-identity
Imports
- NetworkManagementClient
from azure.mgmt.network import NetworkManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.network import NetworkManagementClient
# Retrieve subscription ID from environment variable or replace with your ID
# You must set AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
# (or use other authentication methods supported by DefaultAzureCredential)
SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if SUBSCRIPTION_ID == "YOUR_SUBSCRIPTION_ID":
print("Warning: Please set the AZURE_SUBSCRIPTION_ID environment variable.")
print("Or replace 'YOUR_SUBSCRIPTION_ID' in the code with your actual ID.")
exit(1)
# Authenticate using DefaultAzureCredential (recommended for most scenarios)
try:
credential = DefaultAzureCredential()
# For local development, ensure Azure CLI is logged in, or relevant env vars are set
credential.get_token("https://management.azure.com/.default") # Test token acquisition
except Exception as e:
print(f"Authentication failed: {e}")
print("Please ensure you are logged into Azure CLI, have appropriate environment variables set,")
print("or use a different credential type.")
exit(1)
# Create a NetworkManagementClient instance
network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID)
# Example: List all Public IP Addresses in the subscription
print("Listing Public IP Addresses...")
public_ips = network_client.public_ip_addresses.list_all()
for ip in public_ips:
print(f" - Name: {ip.name}, IP Address: {ip.ip_address or 'N/A'}, Resource Group: {ip.id.split('/')[4]}")
print("Public IP Address listing complete.")