2016-07-15 75 views
2

我正在使用azure-sdk-for-python来创建和删除虚拟机。如何使用azure-sdk-for-python删除磁盘?

https://github.com/Azure/azure-sdk-for-python

http://azure-sdk-for-python.readthedocs.io/en/latest/

我已经成功地设法编写代码来创建和使用资源管理器方法(而不是经典)删除我的虚拟机。

基本创建一个虚拟机可以在这里看到: http://azure-sdk-for-python.readthedocs.io/en/latest/resourcemanagementcomputenetwork.html

我不担心删除资源组存储账户,因为我使用了相同的我所有的虚拟机。

要删除创建的VM我有这样的事情:

# 1. Delete the virtual machine 
result = compute_client.virtual_machines.delete(
    group_name, 
    vm_name    
) 
result.wait() 
# 2. Delete the network interface 
result = network_client.network_interfaces.delete(
    group_name, 
    network_interface_name 
) 
result.wait() 
# 3. Delete the ip 
result = network_client.public_ip_addresses.delete(
    group_name, 
    public_ip_address_name 
) 
result.wait() 

正如一些知道数据磁盘没有与它的虚拟机一起被删除。 我知道它可以与Azure的CLI来完成: https://azure.microsoft.com/en-us/documentation/articles/storage-azure-cli/

azure storage blob delete -a <storage_account_name> -k <storage_account_key> -q vhds <data_disk>.vhd 

但我不知道如何与Azure的SDK换蟒蛇做编程。我不想依赖Azure CLI,因为我的代码的其余部分正在使用python sdk。

我将不胜感激一些如何做到这一点的帮助。

感谢

回答

1

这里有一个小更多的代码:

storage_account = <name of storage account> 

storage_client = StorageManagementClient(...) 

keys = storage_client.storage_accounts.list_keys(...) 

for key in keys.keys: 
    # Use the first key; adjust accordingly if your set up is different 
    break 

block_blob_service = BlockBlobService(
    account_name=storage_account, account_key=key.value) 

for blob in block_blob_service.list_blobs(container): 
    print blob.name 

我希望你发现这很有用。感谢Laurent的指点。

+1

感谢这个有用的代码。一个注意:对于list_keys,appid需要配置为资源组的“所有者” – arved