Microsoft Azure Reservations Client Library for Python
The `azure-mgmt-reservations` library provides a Python interface for managing Azure Reservations. This allows users to programmatically interact with Azure Reservations, such as purchasing, viewing, and managing reserved instances. It is part of the new generation of Azure SDKs, following consistent design guidelines, and the current stable version is 2.3.0. The library typically follows the Azure SDK's release cadence, with updates often coinciding with API version changes or new feature rollouts.
Warnings
- breaking The credential system was completely revamped in versions around 1.0.0b1/1.0.0, moving away from `azure.common.credentials` or `msrestazure.azure_active_directory`. Use `azure-identity` classes (e.g., `DefaultAzureCredential`) instead. The client constructor's `credentials` parameter was renamed to `credential`.
- breaking Starting with versions around 1.0.0, operations returning a long-running poller now return `azure.core.polling.LROPoller` instead of `msrest.polling.LROPoller` and are typically prefixed with `begin_`. Additionally, `CloudError` was removed, and most exceptions are now `azure.core.exceptions.HttpResponseError`.
- breaking Import paths for client classes and models changed, particularly around v0.4.0. Importing `AzureReservationAPI` or models from submodules (e.g., `azure.mgmt.reservations.azure_reservation_api` or `azure.mgmt.reservations.models.my_class`) is no longer supported; they should be imported directly from the `azure.mgmt.reservations` or `azure.mgmt.reservations.models` package respectively.
- gotcha Authentication with `DefaultAzureCredential` relies on specific environment variables (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) for service principal authentication, and `AZURE_SUBSCRIPTION_ID` is crucial for client instantiation. Misconfiguration is a common source of authentication errors.
- gotcha Permissions for managing Azure Reservations are distinct and do not inherit from Azure subscription permissions. Users need specific Reservation RBAC roles (e.g., Reservations Administrator, Contributor, Reader) or relevant Billing Admin roles to view and manage reservations.
- gotcha Python 2.7 support for Azure SDK Python packages, including `azure-mgmt-reservations`, officially ended on January 1, 2022. The library now requires Python 3.7+.
Install
-
pip install azure-mgmt-reservations azure-identity
Imports
- AzureReservationAPI
from azure.mgmt.reservations import AzureReservationAPI
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.reservations import AzureReservationAPI
# Set these environment variables or replace with your actual values for a production setup
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET for DefaultAzureCredential (Service Principal)
# AZURE_SUBSCRIPTION_ID for the Reservations client
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "your_subscription_id_here")
# Authenticate using DefaultAzureCredential
# This credential type tries various authentication methods, like environment variables,
# managed identity, or Azure CLI login.
credential = DefaultAzureCredential()
# Create a Reservations Management client
client = AzureReservationAPI(credential=credential, subscription_id=subscription_id)
# List all reservation orders
print("Listing reservation orders:")
for order in client.reservation_order.list():
print(f" Order ID: {order.id}, Display Name: {order.display_name}, Status: {order.provisioning_state}")
# You can also list reservations within an order, for example:
# if client.reservation_order.list():
# first_order_id = client.reservation_order.list().next().name # Assuming at least one order exists
# print(f"\nListing reservations for order {first_order_id}:")
# for reservation in client.reservation.list(reservation_order_id=first_order_id):
# print(f" Reservation ID: {reservation.id}, Scope: {reservation.scope}, Quantity: {reservation.quantity}")