Azure python SDK - 从资源组中删除或取消分配VM

时间:2016-03-09 10:50:56

标签: python azure azure-sdk-python

我想使用Azure sdk为python从资源组中释放VM。 我已经使用sdk(compute_client.virtual_machines.create_or_update)创建了一个VM,但我无法找到任何可以停止或取消分配VM的特定方法。

谢谢

1 个答案:

答案 0 :(得分:2)

您可以参考Azure SDK for Python的文档,找到deallocate的方法VirtualMachinesOperations,请参阅http://azure-sdk-for-python.readthedocs.org/en/latest/ref/azure.mgmt.compute.operations.html#azure.mgmt.compute.operations.VirtualMachinesOperations.deallocate

以下是代码作为参考。

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration

credentials = ServicePrincipalCredentials(
    client_id = '<client-id>',
    secret = '<key>',
    tenant = '<tenant-id>'
)

subscription_id = '<subscription-id>'

compute_config = ComputeManagementClientConfiguration(credentials, subscription_id, api_version='2015-05-01-preview')
compute_client = ComputeManagementClient(compute_config)
resource_group_name = '<resource-group>'
vm_name = '<vm-name>'
result = compute_client.virtual_machines.deallocate(resource_group_name, vm_name)