Microsoft Azure Compute Management Client Library for programmatic management of Azure compute resources including virtual machines, scale sets, disks, and related infrastructure.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive lifecycle management of Azure Virtual Machines including creation, configuration, monitoring, scaling, and deletion. The VirtualMachinesOperations class provides complete VM management capabilities with support for both Windows and Linux VMs, extensive customization options, and integration with Azure's security and monitoring services.
Core operations for managing virtual machine lifecycle from creation to deletion.
def begin_create_or_update(
resource_group_name: str,
vm_name: str,
parameters: VirtualMachine
) -> LROPoller[VirtualMachine]:
"""
Create or update a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
parameters: Virtual machine configuration
Returns:
Long-running operation poller for VirtualMachine
"""
def begin_delete(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Delete a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""
def get(resource_group_name: str, vm_name: str) -> VirtualMachine:
"""
Get virtual machine details.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Virtual machine details
"""
def list(resource_group_name: str) -> Iterable[VirtualMachine]:
"""
List virtual machines in a resource group.
Args:
resource_group_name: Name of the resource group
Returns:
Iterable of virtual machines
"""
def list_all() -> Iterable[VirtualMachine]:
"""
List all virtual machines in the subscription.
Returns:
Iterable of virtual machines across all resource groups
"""Operations for controlling virtual machine power state.
def begin_start(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Start a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""
def begin_restart(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Restart a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""
def begin_power_off(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Power off a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""
def begin_deallocate(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Deallocate a virtual machine to release compute resources.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""Operations for retrieving virtual machine status and diagnostic information.
def instance_view(resource_group_name: str, vm_name: str) -> VirtualMachineInstanceView:
"""
Get instance view of a virtual machine including power state and status.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Virtual machine instance view with current status
"""Operations for resizing and updating virtual machine configuration.
def begin_update(
resource_group_name: str,
vm_name: str,
parameters: VirtualMachineUpdate
) -> LROPoller[VirtualMachine]:
"""
Update virtual machine configuration.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
parameters: Update parameters
Returns:
Long-running operation poller for updated VirtualMachine
"""
def begin_redeploy(resource_group_name: str, vm_name: str) -> LROPoller[None]:
"""
Redeploy a virtual machine to a different physical host.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
Returns:
Long-running operation poller
"""Execute commands on virtual machines remotely.
def begin_run_command(
resource_group_name: str,
vm_name: str,
parameters: RunCommandInput
) -> LROPoller[RunCommandResult]:
"""
Run a command on a virtual machine.
Args:
resource_group_name: Name of the resource group
vm_name: Name of the virtual machine
parameters: Command parameters including script and arguments
Returns:
Long-running operation poller for command result
"""class VirtualMachine:
"""Describes a Virtual Machine."""
id: Optional[str]
name: Optional[str]
type: Optional[str]
location: str
tags: Optional[Dict[str, str]]
plan: Optional[Plan]
hardware_profile: Optional[HardwareProfile]
storage_profile: Optional[StorageProfile]
additional_capabilities: Optional[AdditionalCapabilities]
os_profile: Optional[OSProfile]
network_profile: Optional[NetworkProfile]
security_profile: Optional[SecurityProfile]
diagnostics_profile: Optional[DiagnosticsProfile]
availability_set: Optional[SubResource]
virtual_machine_scale_set: Optional[SubResource]
proximity_placement_group: Optional[SubResource]
priority: Optional[VirtualMachinePriorityTypes]
eviction_policy: Optional[VirtualMachineEvictionPolicyTypes]
billing_profile: Optional[BillingProfile]
host: Optional[SubResource]
host_group: Optional[SubResource]
provisioning_state: Optional[str]
instance_view: Optional[VirtualMachineInstanceView]
license_type: Optional[str]
vm_id: Optional[str]
extensions_time_budget: Optional[str]
platform_fault_domain: Optional[int]
class HardwareProfile:
"""Specifies the hardware settings for the virtual machine."""
vm_size: Optional[VirtualMachineSizeTypes]
vm_size_properties: Optional[VMSizeProperties]
class StorageProfile:
"""Specifies the storage settings for the virtual machine disks."""
image_reference: Optional[ImageReference]
os_disk: Optional[OSDisk]
data_disks: Optional[List[DataDisk]]
disk_controller_type: Optional[DiskControllerTypes]
class OSProfile:
"""Specifies the operating system settings for the virtual machine."""
computer_name: Optional[str]
admin_username: Optional[str]
admin_password: Optional[str]
custom_data: Optional[str]
windows_configuration: Optional[WindowsConfiguration]
linux_configuration: Optional[LinuxConfiguration]
secrets: Optional[List[VaultSecretGroup]]
allow_extension_operations: Optional[bool]
require_guest_provision_signal: Optional[bool]
class NetworkProfile:
"""Specifies the network interfaces of the virtual machine."""
network_interfaces: Optional[List[NetworkInterfaceReference]]
network_api_version: Optional[NetworkApiVersion]
network_interface_configurations: Optional[List[VirtualMachineNetworkInterfaceConfiguration]]
class VirtualMachineInstanceView:
"""The instance view of a virtual machine."""
platform_update_domain: Optional[int]
platform_fault_domain: Optional[int]
computer_name: Optional[str]
os_name: Optional[str]
os_version: Optional[str]
hyper_v_generation: Optional[HyperVGenerationType]
rdp_thumb_print: Optional[str]
vm_agent: Optional[VirtualMachineAgentInstanceView]
maintenance_redeploy_status: Optional[MaintenanceRedeployStatus]
disks: Optional[List[DiskInstanceView]]
extensions: Optional[List[VirtualMachineExtensionInstanceView]]
vm_health: Optional[VirtualMachineHealthStatus]
boot_diagnostics: Optional[BootDiagnosticsInstanceView]
assigned_host: Optional[str]
statuses: Optional[List[InstanceViewStatus]]
patch_status: Optional[VirtualMachinePatchStatus]
class VirtualMachineUpdate:
"""Describes a Virtual Machine Update."""
tags: Optional[Dict[str, str]]
plan: Optional[Plan]
hardware_profile: Optional[HardwareProfile]
storage_profile: Optional[StorageProfile]
additional_capabilities: Optional[AdditionalCapabilities]
os_profile: Optional[OSProfile]
network_profile: Optional[NetworkProfile]
security_profile: Optional[SecurityProfile]
diagnostics_profile: Optional[DiagnosticsProfile]
availability_set: Optional[SubResource]
virtual_machine_scale_set: Optional[SubResource]
proximity_placement_group: Optional[SubResource]
priority: Optional[VirtualMachinePriorityTypes]
eviction_policy: Optional[VirtualMachineEvictionPolicyTypes]
billing_profile: Optional[BillingProfile]
host: Optional[SubResource]
host_group: Optional[SubResource]
license_type: Optional[str]
extensions_time_budget: Optional[str]
class RunCommandInput:
"""Captures the command input for running command on Virtual Machine."""
command_id: str
script: Optional[List[str]]
parameters: Optional[List[RunCommandInputParameter]]
protected_parameters: Optional[List[RunCommandInputParameter]]
run_as_user: Optional[str]
run_as_password: Optional[str]
async_execution: Optional[bool]
output_blob_uri: Optional[str]
error_blob_uri: Optional[str]
timeout_in_seconds: Optional[int]
class RunCommandResult:
"""Contains the results of running a command on a Virtual Machine."""
value: Optional[List[InstanceViewStatus]]from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, "subscription-id")
vm_parameters = {
'location': 'East US',
'hardware_profile': {
'vm_size': 'Standard_B1ms'
},
'storage_profile': {
'image_reference': {
'publisher': 'Canonical',
'offer': '0001-com-ubuntu-server-focal',
'sku': '20_04-lts-gen2',
'version': 'latest'
},
'os_disk': {
'create_option': 'FromImage',
'managed_disk': {
'storage_account_type': 'Premium_LRS'
}
}
},
'os_profile': {
'computer_name': 'myvm',
'admin_username': 'azureuser',
'disable_password_authentication': True,
'linux_configuration': {
'ssh': {
'public_keys': [{
'path': '/home/azureuser/.ssh/authorized_keys',
'key_data': 'ssh-rsa AAAAB3NzaC1yc2E...'
}]
}
}
},
'network_profile': {
'network_interfaces': [{
'id': '/subscriptions/.../networkInterfaces/mynic',
'properties': {'primary': True}
}]
}
}
operation = compute_client.virtual_machines.begin_create_or_update(
'myResourceGroup',
'myVM',
vm_parameters
)
vm = operation.result()
print(f"VM {vm.name} created successfully")# Get detailed instance view
instance_view = compute_client.virtual_machines.instance_view(
'myResourceGroup',
'myVM'
)
print(f"Computer Name: {instance_view.computer_name}")
print(f"OS Name: {instance_view.os_name}")
# Check power state
for status in instance_view.statuses:
if status.code.startswith('PowerState'):
print(f"Power State: {status.display_status}")
# Check provisioning state
vm = compute_client.virtual_machines.get('myResourceGroup', 'myVM')
print(f"Provisioning State: {vm.provisioning_state}")run_command_parameters = {
'command_id': 'RunShellScript', # For Linux VMs
'script': [
'df -h',
'free -m',
'uname -a'
]
}
operation = compute_client.virtual_machines.begin_run_command(
'myResourceGroup',
'myVM',
run_command_parameters
)
result = operation.result()
for status in result.value:
print(f"Message: {status.message}")Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-compute