Azure SQL Virtual Machine Management Client Library

0.5.0 · active · verified Thu Apr 09

The `azure-mgmt-sqlvirtualmachine` library provides a client for managing Azure SQL Virtual Machines, allowing you to create, update, delete, and list SQL VMs and their associated resources (like SQL VM groups and availability groups). It is currently at version `0.5.0` (as of late 2019) and follows the typical Azure SDK release cadence for management plane libraries, though updates for this specific library have been infrequent.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and list all SQL Virtual Machines within a specified subscription. Ensure the `AZURE_SUBSCRIPTION_ID` environment variable is set and your Azure principal has sufficient permissions (e.g., Reader role) to view resources.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.sqlvirtualmachine import SqlVirtualMachineManagementClient

# Retrieve subscription ID from environment variable
# You can get your subscription ID using Azure CLI: az account show --query id -o tsv
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if not subscription_id or subscription_id == "YOUR_SUBSCRIPTION_ID":
    raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID' in the code.")

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

# Create a SQL Virtual Machine Management Client
client = SqlVirtualMachineManagementClient(credential, subscription_id)

print(f"Listing SQL Virtual Machines in subscription: {subscription_id}...")
try:
    # Iterate through all SQL Virtual Machines in the subscription
    sql_vms = client.sql_virtual_machines.list()
    
    found_vms = False
    for vm in sql_vms:
        found_vms = True
        print(f"  - Name: {vm.name}")
        print(f"    Resource Group: {vm.id.split('/resourceGroups/')[1].split('/')[0]}")
        print(f"    Location: {vm.location}")
        print(f"    Provisioning State: {vm.provisioning_state}")
        if vm.sql_virtual_machine_group_id:
            print(f"    SQL VM Group ID: {vm.sql_virtual_machine_group_id}")
        print("-" * 20)

    if not found_vms:
        print("No SQL Virtual Machines found in this subscription.")
    else:
        print("\nSuccessfully listed SQL Virtual Machines.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure AZURE_SUBSCRIPTION_ID is set and your credential has 'Reader' permissions (at least) on the subscription.")

view raw JSON →