Microsoft Azure Reservations Client Library for Python

2.3.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with `DefaultAzureCredential` and list Azure Reservation Orders using the `azure-mgmt-reservations` library. Ensure `AZURE_SUBSCRIPTION_ID` and other necessary Azure AD environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) are set for `DefaultAzureCredential` to function correctly.

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}")

view raw JSON →