Microsoft Azure Maps Client Library for Python
The `azure-mgmt-maps` library provides Python client APIs for managing Azure Maps accounts and resources. It enables programmatic interaction with Azure Maps, allowing operations such as creating, updating, deleting, and listing Maps accounts, as well as managing account keys. The current stable version is 2.1.0, released in September 2023, and it follows the Azure SDK for Python's release cadence, with major versions indicating potential breaking changes.
Warnings
- breaking In `azure-mgmt-maps` version 2.0.0, the credential system was completely revamped. Old authentication methods (e.g., `azure.common.credentials` or `msrestazure.azure_active_directory`) are no longer supported. Users must migrate to `azure-identity` classes (e.g., `DefaultAzureCredential`). Additionally, the `credentials` parameter in client constructors was renamed to `credential`.
- breaking Version 2.0.0 introduced several parameter renames for Maps account operations. Specifically, `maps_account_id` was renamed to `account_id` in methods like `accounts.delete`, `accounts.get`, `accounts.list_by_resource_group`, and `accounts.list_keys`. Similarly, `maps_account` was renamed to `account` in `accounts.create_or_update`.
- breaking As of `azure-mgmt-maps` version 2.0.0, long-running operations (LROs) that previously returned `msrest.polling.LROPoller` now return `azure.core.polling.LROPoller` and are consistently prefixed with `begin_`. The `CloudError` exception has also been removed; most exceptions are now `azure.core.exceptions.HttpResponseError`.
- breaking A beta version `3.0.0b1` introduces significant breaking changes related to 'hybrid models'. Many instance variables (e.g., `provisioning_state`, `storage_units`, `linked_resources`, `cors`, `encryption`) have moved under a `properties` object within models like `CreatorUpdateParameters` and `MapsAccountUpdateParameters`. Enum values like `Kind.GEN1` and `Name.S0`, `Name.S1` were also deleted or renamed.
- gotcha To manage Azure Maps resources, the authenticated identity (e.g., via `DefaultAzureCredential`) must have appropriate Azure RBAC permissions. Roles like 'Maps Contributor' or 'Owner' are typically required to create, update, or delete Maps accounts.
- gotcha When creating or updating an Azure Maps account, the `sku` parameter is mandatory. It defines the pricing tier and capabilities (e.g., 'G2' for Gen2, 'S1' for Gen1). Omitting or providing an invalid SKU will result in a deployment error.
Install
-
pip install azure-mgmt-maps -
pip install azure-identity
Imports
- MapsManagementClient
from azure.mgmt.maps import MapsManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.maps import MapsManagementClient
# Set your Azure Subscription ID as an environment variable (AZURE_SUBSCRIPTION_ID)
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
resource_group_name = "myMapsResourceGroup" # Replace with your resource group name
maps_account_name = "myUniqueMapsAccount" # Replace with your desired Maps account name
location = "eastus" # Replace with your desired Azure region
if not subscription_id:
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
else:
# Authenticate using DefaultAzureCredential
# This credential will attempt to authenticate via several mechanisms,
# including environment variables, managed identity, and Azure CLI/PowerShell.
credential = DefaultAzureCredential()
# Create a MapsManagementClient
maps_client = MapsManagementClient(credential, subscription_id)
print(f"Listing existing Maps accounts in subscription '{subscription_id}'...")
for account in maps_client.accounts.list_by_subscription():
print(f" - {account.name} (Location: {account.location}, SKU: {account.sku.name})")
print(f"\nCreating or updating Maps account '{maps_account_name}' in resource group '{resource_group_name}'...")
try:
# Ensure the resource group exists before creating a Maps account.
# This example assumes the resource group already exists.
maps_account = maps_client.accounts.begin_create_or_update(
resource_group_name=resource_group_name,
account_name=maps_account_name,
account={
"location": location,
"sku": {"name": "G2"}, # 'S1' (Gen1) or 'G2' (Gen2) are common SKUs
"tags": {"environment": "development", "project": "maps-demo"}
}
).result()
print(f"Successfully created/updated Maps account: {maps_account.name} (SKU: {maps_account.sku.name})")
print(f"\nGetting keys for Maps account '{maps_account_name}'...")
keys = maps_client.accounts.list_keys(resource_group_name, maps_account_name)
print(f" - Primary Key: {keys.primary_key}")
# Optional: Delete the Maps account
# print(f"\nDeleting Maps account '{maps_account_name}'...")
# maps_client.accounts.begin_delete(resource_group_name, maps_account_name).result()
# print(f"Maps account '{maps_account_name}' deleted successfully.")
except Exception as e:
print(f"An error occurred: {e}")