0

我正在研究 azure python sdk,我尝试将磁盘附加到处于停止状态的实例,例如附加到处于停止状态的虚拟机的磁盘,我知道managedby属性会给我没有附加实例的磁盘,但我无法获得任何 api 或属性来检查磁盘当前是否在使用中。

是否有任何方法可以将磁盘附加到已停止的实例(如虚拟机)?

4

3 回答 3

1

你可以这样做:

compute_client = ComputeManagementClient(credentials, subscription_id)
# you can also list by subscription
# https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2017_03_30.operations.disks_operations.disksoperations?view=azure-python#list-custom-headers-none--raw-false----operation-config-
disks = compute_client.disks.list_by_resource_group('resourcegroupname')
for disk in disks:
    print disk

这将为您提供资源组中的所有磁盘。没有办法获得所有“孤儿”磁盘。我认为您最好的选择是获取所有磁盘并查看它们是否连接到某些东西

其他示例:https ://github.com/Azure/azure-sdk-for-python/wiki/Managed-Disk

于 2019-02-01T06:51:27.447 回答
1

Python SDK 中没有直接列出附加到 VM 的磁盘的功能,您只能按组或订阅列出托管磁盘。但是您可以在 VM 属性中获取附加到 VM 的磁盘。

例如,您可以这样列出数据盘:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient

TENANT_ID = "xxxxx"
CLIENT_ID = "xxxxx"
KEY = "secret"

cred = ServicePrincipalCredentials(client_id = CLIENT_ID,
        secret = KEY,
        tenant = TENANT_ID)

subscription_id = "xxxxx"

compute_client = ComputeManagementClient(cred, subscription_id)

rg = "resourceGroupName"
vm_name = "vmName"

vm_info = compute_client.virtual_machines.get(rg, vm_name)

for disk in vm_info.storage_profile.data_disks:
    print disk.managed_disk.id
于 2019-02-01T07:58:21.027 回答
0

我正在检查对象的所有字段,disk我发现可以从name字段中提取关联 VM 的名称。例如,我在TestVM2_OsDisk_1_834968b1cdc341c78bfbc227c9ccacda这里找到TestVM2的名称是连接磁盘的虚拟机的名称。

所以,我name通过使用正则表达式从字段中获取 VM 的名称,re.split('_OsDisk', disk.name)并使用 azure apis 为 VM 检查 VM 状态,如果它给我,VM deallocated那么它处于停止状态,或者它会给我VM running

这可能不是一个好方法,但这目前正在起作用。

于 2019-02-05T13:48:37.747 回答