[英]Get resource group name from resource object Azure SDK Python
只是想知道是否有一種簡單的方法可以從 Azure 資源對象(即磁盤、快照對象)中獲取資源組名稱
目前我使用資源 URI 並從中派生資源組:
# ID example: '/subscriptions/123/resourceGroups/MyRG/providers/Microsoft.Compute/disks/disk-a
resource_group = [value for value in str(disk.id).split("/") if value][3]
輸出
>> MyRG
我似乎看不到做以下事情的方法:
disk.resource_group
查看MSDN ,我不相信有resource_group
屬性。
我通常做的是創建一個方法(類似於您所做的)來從資源 ID 中獲取資源組:
def get_resource_group_from_id(resource_id):
return resource_id.lstrip("/").split("/")[3]
然后我只是在需要獲取資源組的任何地方使用此方法:
from azure.mgmt.compute import ComputeManagementClient
from azure.common.client_factory import get_client_from_cli_profile
compute_client = get_client_from_cli_profile(ComputeManagementClient)
for disk in compute_client.disks.list():
resource_group = get_resource_group_from_id(resource_id=disk.id)
但是,我不喜歡這個,因為我習慣於直接從資源中獲取它。
您可以使用azure-sdk-for-python提出功能請求,以獲取包含在對象中的此屬性。 Azure PowerShell 和 Azure CLI 包含此屬性,所以我不明白為什么 Azure Python SDK 不應該包含它。
另一種選擇是包含一個resource_group
標簽,然后您可以直接從資源中獲取它。
以下是適用於 Python 的 Azure SDK:
https://docs.microsoft.com/en-us/python/api/overview/azure/?view=azure-python
我覺得你的需求需要具體到具體的服務,而且一般來說大范圍可以具體到小范圍,但是每個服務的具體對象好像沒有定義獲取對應資源組的方法。 我想你的想法可能無法實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.