Azure Log Analytics Management Client
The `azure-mgmt-loganalytics` library is the Microsoft Azure Log Analytics Management Client Library for Python. It allows developers to programmatically manage Log Analytics workspaces, solutions, linked services, and other related resources within Azure. As part of the wider Azure SDK for Python, it adheres to the unified API guidelines and current authentication patterns. The current version is 13.1.1, and Azure SDKs generally follow a frequent release cadence, often monthly or bi-monthly, to keep pace with Azure service updates.
Warnings
- breaking Authentication with `msrestazure` based credentials (e.g., `ServicePrincipalCredentials`, `MSIAuthentication`) is deprecated or removed in recent major versions (v12+). The modern and recommended approach is to use `azure-identity`'s `DefaultAzureCredential` or other specific credential classes.
- gotcha This library (`azure-mgmt-loganalytics`) is for *managing* Log Analytics workspaces (creating, updating, deleting workspaces, managing linked services, etc.). It is NOT for *querying* data within a workspace or performing data plane operations.
- gotcha When upgrading between major versions of `azure-mgmt-loganalytics` (e.g., from v12 to v13), there might be breaking changes in API surface areas, such as parameter names, method signatures, or model object structures. This is due to updates in the underlying Azure REST API.
Install
-
pip install azure-mgmt-loganalytics azure-identity
Imports
- LogAnalyticsManagementClient
from azure.mgmt.loganalytics import LogAnalyticsManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.loganalytics import LogAnalyticsManagementClient
# Ensure you have AZURE_SUBSCRIPTION_ID and other Azure credentials set in your environment
# E.g., AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for service principal
# Or login via `az login` for DefaultAzureCredential to pick up user credentials
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
resource_group_name = os.environ.get("AZURE_RESOURCE_GROUP", "my-loganalytics-rg")
if not subscription_id or subscription_id == "YOUR_SUBSCRIPTION_ID":
raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
credential = DefaultAzureCredential()
client = LogAnalyticsManagementClient(credential, subscription_id)
print(f"Listing Log Analytics workspaces in subscription {subscription_id}...")
# Example: List all workspaces in a specific resource group
# For simplicity, this example just lists the first 5 if available
workspaces = client.workspaces.list_by_resource_group(resource_group_name)
found_any = False
for i, workspace in enumerate(workspaces):
if i >= 5: # Limit output for quickstart
break
print(f"- Workspace: {workspace.name} (Location: {workspace.location})")
found_any = True
if not found_any:
print(f"No Log Analytics workspaces found in resource group '{resource_group_name}'.")