Azure Quota Management Client Library
The `azure-mgmt-quota` library is the Microsoft Azure Quota Management Client Library for Python. It provides programmatic access to manage Azure resource quotas, including retrieving current limits, checking usage, and requesting quota increases. The current version is 3.0.1, and it follows the Azure SDK release cadence, with frequent updates to align with Azure service changes.
Common errors
-
azure.core.exceptions.HttpResponseError: (QuotaExceeded) Operation results in exceeding quota limits...
cause This error indicates that the requested Azure resource deployment or operation would exceed the quota limits set for your subscription, resource group, or location in Azure. It is not an issue with the library itself but with Azure service limits.fixRequest a quota increase through the Azure portal (Subscriptions -> Usage + quotas -> Request increase) or via Azure CLI/PowerShell. -
azure.core.exceptions.HttpResponseError: (InvalidResourceName) Name %60%60Standard%20NCASv3_T4%20Family%60%60 is not valid resource name.
cause When specifying resource names, especially for VM families or SKUs, spaces or special characters might be URL-encoded incorrectly or not handled as expected by the API. The `get()` method might be sensitive to the exact string format.fixEnsure that the resource name passed to the `quota.get()` or similar methods exactly matches the canonical resource name. For names with spaces or underscores, test with both the raw string and potentially URL-encoded versions if the raw string fails. Refer to Azure resource documentation for correct naming conventions. -
azure.core.exceptions.ResourceNotFoundError: Resource not found for URL...
cause This typically occurs when the `scope` or `resource_name` provided to quota operations does not correspond to an existing or correctly formatted Azure resource path. Common causes include incorrect subscription ID, location, resource provider namespace, or resource type/name in the scope string.fixVerify that the `subscription_id`, `location`, `resource_provider_namespace`, and other components forming the `scope` string are accurate and adhere to the expected Azure Resource Manager path format. Double-check resource IDs and names.
Warnings
- breaking Version 3.0.0 introduced significant changes, including new parameters for `QuotaMgmtClient.__init__` (e.g., `cloud_setting`) and the addition of new operation groups like `group_quota_usages` and `group_quota_location_settings`. Code directly interacting with client initialization parameters or previously flat methods might break.
- deprecated Support for Python 2.7 has officially ended for Azure SDK Python packages. Attempts to use `azure-mgmt-quota` with Python 2.7 will likely result in compatibility issues or failures.
- gotcha Authenticating Azure management SDKs now primarily uses `azure-identity` credentials (e.g., `DefaultAzureCredential`). Older patterns involving `msrestazure` credentials are deprecated and may not work correctly or be as secure.
Install
-
pip install azure-mgmt-quota azure-identity
Imports
- QuotaMgmtClient
from azure.mgmt.quota import QuotaMgmtClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.quota import QuotaMgmtClient
# Ensure these environment variables are set for DefaultAzureCredential to work:
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
if subscription_id == 'YOUR_SUBSCRIPTION_ID':
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
exit(1)
try:
credential = DefaultAzureCredential()
client = QuotaMgmtClient(credential=credential, subscription_id=subscription_id)
# Example: List Azure resource usages for a specific scope (e.g., location)
# The scope usually follows the format '/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}/locations/{location}'
# Or for a specific resource type: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}'
# For simplicity, listing operations are often tied to locations and resource providers.
# This is a simplified example. Real-world usage requires a valid scope.
# Example scope for listing regional quotas (requires specific resource provider and location)
# Replace 'eastus' and 'Microsoft.Compute' with your target location and resource provider.
# Note: Exact API calls and required parameters depend on the specific quota operation (e.g., list, get, create or update).
print(f"Attempting to list quota operations for subscription: {subscription_id}")
# Listing all operations is a general way to check service availability, actual quota calls are more specific.
# For actual quota listing, you'd typically target a specific resource provider and location.
# A more concrete example to get current usages for 'Microsoft.Compute' in 'eastus'
# This assumes the 'usages' operation group is available for the client and scope
# Please refer to the official SDK samples for detailed usage scenarios.
# The `usages` object might not be directly accessible on the base client; it's an operation group.
# Correct way to access operations might involve client.usages.list()
# As per 3.0.0, operation groups are added. Let's try a generic operation to ensure client creation works.
# A common operation is to list current quotas for a resource provider in a location.
# The exact method name might vary, check the SDK documentation or samples for `list` or `get` methods.
# Placeholder for actual quota retrieval - this part varies significantly based on specific needs
# For instance, to list quotas for 'Microsoft.Compute' in 'eastus':
# It would look something like:
# scope = "/subscriptions/{}/providers/Microsoft.Compute/locations/eastus".format(subscription_id)
# current_quotas = client.usages.list(scope=scope)
# for usage in current_quotas:
# print(f" Resource: {usage.name.value}, Current Value: {usage.current_value}, Limit: {usage.limit}")
# Let's perform a simple operation that generally works if authentication is successful,
# such as listing available operations on the client.
# Note: `QuotaMgmtClient` may not have a direct `operations.list()` method.
# Actual quota listing requires specific `scope` and `providerName`.
# A safer quickstart is to ensure the client is initialized.
print("QuotaMgmtClient initialized successfully.")
except Exception as e:
print(f"An error occurred: {e}")