如何在Azure中使用Azure-SDK-for-Python获取磁盘的所有快照列表

问题描述

我正在尝试找到一种通过azure-sdk-for-python获取Azure磁盘快照列表的方法快照磁盘类没有用于此目的的任何方法或字段。

但是在Web客户端中,您可以在快照概述页面enter image description here

中找到源磁盘参数。

解决方法

请我回答这个愚蠢的问题。我总是将 creation_data 读为 creation_date 。 :(

解决方案是:

from typing import List

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import Disk
from azure.mgmt.compute.models import Snapshot

    
def get_disk_snapshot_list(disk: Disk,resource_group_name: str) -> List[Snapshot]
    disk_snapshot_list = list()
    snapshot_list = compute_client.snapshots.list_by_resource_group(resource_group_name=resource_group_name)
    
    for snapshot in snapshot_list:
        if snapshot.creation_data.source_unique_id != disk.unique_id:
            continue
        
        disk_snapshot_list.append(snapshot)
        
    return disk_snapshot_list
    
def main():
    credential = ClientSecretCredential(
        tenant_id='your_tenant_id',client_id='your_client_id',client_secret='your_client_secret'
    )
    
    resource_group_name = 'your_resource_group_name'
    
    compute_client = ComputeManagementClient(credential=credential,subscription_id='your_subscription_id')
    disk = compute_client.disks.get(resource_group_name=resource_group_name,disk_name='your_disk_name')
    disk_snapshot_list = get_disk_snapshot_list(disk=disk,resource_group_name=resource_group_name)
    
if __name__ == '__main__':
    main()