Azure Management - Policy Insights

1.0.0 · active · verified Thu Apr 09

The Azure Management Policy Insights client library for Python (version 1.0.0) provides functionality to query and analyze Azure Policy compliance. It allows retrieving policy states, events, and remediation details to understand resource compliance with defined policies. As part of the broader Azure SDK for Python, it follows a regular release cadence, primarily focusing on stability for 1.x versions and aligning with Azure API updates.

Warnings

Install

Imports

Quickstart

Initializes the PolicyInsightsClient using DefaultAzureCredential and attempts to list the first policy event for the given subscription scope. This demonstrates client setup and a basic API interaction.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.policyinsights import PolicyInsightsClient

# Get subscription ID from environment variable
# Ensure AZURE_SUBSCRIPTION_ID is set, e.g., in your shell or .env file.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if subscription_id == "YOUR_SUBSCRIPTION_ID":
    print("WARNING: Please set the AZURE_SUBSCRIPTION_ID environment variable for actual API calls.")
    print("Using a dummy subscription ID for client initialization. API calls will likely fail.")
    subscription_id = "00000000-0000-0000-0000-000000000000"

try:
    # Authenticate using DefaultAzureCredential
    # This tries various methods: environment variables, managed identity, Azure CLI, etc.
    credential = DefaultAzureCredential()

    # Create a PolicyInsights client
    client = PolicyInsightsClient(credential, subscription_id)
    print(f"\nPolicyInsightsClient initialized for subscription: {subscription_id}")

    # Example: Attempt to list a few policy events
    # For actual data, you would typically add more filters (e.g., by time, resource).
    print("\nAttempting to list first few policy events (may require permissions and valid subscription ID)...\n")
    events_iterator = client.policy_events.list_query_results_for_subscription(
        scope=f"/subscriptions/{subscription_id}",
        policy_events_resource="latest",
        top=1 # Limit to 1 for quickstart output
    )

    found_event = False
    for event in events_iterator:
        print(f"  Policy Assignment ID: {event.policy_assignment_id}")
        print(f"  Policy Definition ID: {event.policy_definition_id}")
        print(f"  Resource ID: {event.resource_id}")
        found_event = True
        break # Only show the first one

    if not found_event:
        print("  No policy events found or unable to retrieve. Check your AZURE_SUBSCRIPTION_ID and Azure RBAC permissions.")

except Exception as e:
    print(f"\nAn error occurred during client initialization or API call: {e}")
    print("Please ensure you have authenticated with Azure (e.g., `az login` or environment variables) and have the necessary permissions (e.g., 'Reader' role).")

view raw JSON →