Azure Resource Health Management Client Library for Python
The Azure Resource Health Management Client Library for Python enables developers to programmatically interact with Azure Resource Health, a service that provides information about the current and past health of Azure resources. It helps diagnose and get support for issues impacting resources, whether caused by Azure platform events or user actions. The current version is 1.0.0b6 and it's part of the broader Azure SDK for Python, which sees frequent updates across its various sub-packages.
Common errors
-
azure.identity.CredentialUnavailableError: DefaultAzureCredential failed to retrieve a token from the following sources:...
cause The `DefaultAzureCredential` could not find valid credentials in the environment variables, Azure CLI, or other configured sources.fixEnsure you are logged in via Azure CLI (`az login`), or that required environment variables like `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_CLIENT_SECRET` are set for a service principal. Also, ensure `AZURE_SUBSCRIPTION_ID` is set. -
HttpResponseError: Operation returned an invalid status 'Bad Request' (or 'NotFound')
cause This generic error often indicates an issue with the request parameters, such as an incorrect or malformed resource URI, an unsupported API version for the requested operation, or insufficient permissions.fixDouble-check the resource URI for accuracy. Verify the `api-version` implicitly used by the client is supported for the operation. Ensure the authenticated identity has the necessary 'Reader' or 'Monitoring Reader' permissions for Azure Resource Health. -
AttributeError: 'ResourceHealthMgmtClient' object has no attribute 'some_old_method'
cause An API method or property name has changed or been removed between library versions, especially common in beta releases or due to the 'latest API version' breaking change.fixConsult the official `azure-mgmt-resourcehealth` documentation for the `1.0.0b6` version to identify the correct method/property names and usage. If necessary, pin to an older working version or adapt your code to the new API surface.
Warnings
- breaking Starting with version 1.0.0b6, this package targets only the latest API-Version available on Azure and removes APIs for other API-Versions. If your application relies on a specific, non-latest API-Version, you may need to pin to an older package version.
- gotcha Authentication with Azure SDKs often relies on `DefaultAzureCredential`, which attempts multiple authentication methods. Common issues arise if required environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`) are not set, or if `az login` (for Azure CLI) is not performed for local development.
- gotcha As a beta package, the API surface (`ResourceHealthMgmtClient` methods, parameter names, model structures) is subject to change without strict backward compatibility between minor beta versions. This is common in `b` releases within the Azure SDK.
- deprecated While `azure-mgmt-resourcehealth` is actively maintained, some older `azure-mgmt-*` client libraries have been refactored or deprecated in favor of newer `azure-resourcemanager-*` or specific `azure-mgmt-resource-*` packages. This can cause confusion if you're mixing new and old styles of Azure SDKs for other resource types.
Install
-
pip install azure-mgmt-resourcehealth==1.0.0b6 azure-identity
Imports
- ResourceHealthMgmtClient
from azure.mgmt.resourcehealth import ResourceHealthMgmtClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resourcehealth import ResourceHealthMgmtClient
# Set your Azure Subscription ID as an environment variable, e.g., AZURE_SUBSCRIPTION_ID
# For local development, ensure you are logged in via Azure CLI (`az login`)
# or configure environment variables for service principal authentication.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace the placeholder.")
# Authenticate using DefaultAzureCredential
# This credential provider chain will attempt various authentication methods.
credential = DefaultAzureCredential()
# Create the Resource Health Management Client
resource_health_client = ResourceHealthMgmtClient(
credential=credential,
subscription_id=subscription_id
)
# Example: List service health events in the subscription
print(f"Listing service health events for subscription ID: {subscription_id}")
for event in resource_health_client.events.list_by_subscription_id():
print(f" Event ID: {event.event_id}, Status: {event.impacted_resource_type}, Severity: {event.severity}, Status: {event.status}")
# Example: Get availability status for a specific resource (replace with your resource URI)
# resource_uri = "/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/{resource_provider}/{resource_type}/{resource_name}"
# print(f"\nGetting availability status for resource: {resource_uri}")
# try:
# availability_status = resource_health_client.availability_statuses.get_by_resource(resource_uri=resource_uri)
# print(f" Availability State: {availability_status.availability_state}")
# print(f" Summary: {availability_status.summary}")
# except Exception as e:
# print(f" Could not retrieve availability status: {e}")