获取使用Python SDK连接到已停止的实例盘

问题描述 投票:0回答:3

我的工作在蔚蓝的蟒蛇SDK,我试图连接到实例在连接到虚拟机处于停止状态如磁盘停止状态GET盘,我知道managedby属性会给我的磁盘,不附加任何情况下,但我没能获得任何API或属性来检查是否磁盘在目前使用与否。

有没有什么方法来获取连接到像虚拟机已停止的实例盘?

azure azure-disk
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


1
投票

有没有直接的作用列出连接到虚拟机在Python SDK中的磁盘,你可以按组或订阅列表管理的盘。但是你可以连接到虚拟机的虚拟机性能的磁盘。

例如,你可以列出这样的数据磁盘:

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

0
投票

我被检查对象disk的各个领域,我发现相关联的VM的这个名字可以从name现场提取。对于如我发现名称TestVM2_OsDisk_1_834968b1cdc341c78bfbc227c9ccacda这里TestVM2是与磁盘连接虚拟机的名称。

所以,我用正则表达式name和使用蔚蓝的API为虚拟机VM检查取状态的虚拟机的名字从re.split('_OsDisk', disk.name)字段,如果它给了我VM deallocated那么它是处于停止状态,或者它会给我VM running

这可能不是一个好办法,但是这是工作现在。

© www.soinside.com 2019 - 2024. All rights reserved.