簡體   English   中英

如何使用 python 在 azure 磁盤中創建標簽?

[英]how to create a tags in azure disk using python?

我想使用 python 在 Azure 磁盤中添加或創建一個新標簽,但無法執行此操作,請幫助我使用 python sdk/代碼。

for disk in compute_client.disks.list():
   if disk.as_dict()["name"] == "test_disk_rohit":
      tags = target_disk.tags["DetachedTime"] = datetime.now()
      compute_client.disks.begin_create_or_update(resrc,disk.as_dict()["name"],disk)

這就是我嘗試為我的 azure 磁盤添加/創建一個名為“test_disk_rohit”的新標簽。 任何人幫我這個..

您可以使用create_or_update而不是使用begin_create_or_update

我已經按照下面的代碼片段我可以創建/更新辦公桌上的標簽

AZURE_TENANT_ID= '<Tenent ID>'
AZURE_CLIENT_ID='<Client ID>'
AZURE_CLIENT_SECRET='<Client Secret>'
AZURE_SUBSCRIPTION_ID='<Sub_ID>'

credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)  

resource_client = ResourceManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
compute_client = ComputeManagementClient(credentials,AZURE_SUBSCRIPTION_ID)

Diskdetails = compute_client.disks.create_or_update(
    '<ResourceGroupName>',
    '<Disk Name>',
    {
        'location': 'eastasia',
        'creation_data': {
            'create_option': DiskCreateOption.copy,
            'source_resource_id': <Source Resource ID>

},
        "tags": {
            "tagtest": "testtagGanesh"
        },
    }
)

disk_resource = Diskdetails.result()
#get Disk details
disk = compute_client.disks.get('<ResourceGroupName>','<Disk Name>')

print(disk.sku)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM