简体   繁体   中英

How to display azure resources in tabular form python SDK

I created a ResourceManagementClient using the azure python sdk:

  • resource_client = ResourceManagementClient(service_credential, subscription_id)

The output produced via...

for item in resource_client.resource_groups.list():
    print(item)

...is difficult to read and its not in valid json format.

{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/azureMaps', 'name': 'azureMaps', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499748>, 'location': 'westus', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/speechToText', 'name': 'speechToText', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba4997f0>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/neo4j-rg', 'name': 'neo4j-rg', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499b00>, 'location': 'westus', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/things', 'name': 'things', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499a90>, 'location': 'westus', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/appInsights', 'name': 'appInsights', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499a20>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/computerVision', 'name': 'computerVision', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499c50>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/dataBricks', 'name': 'dataBricks', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499be0>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/databricks-rg-databricksDEV-v6oygdyuh3sii', 'name': 'databricks-rg-databricksDEV-v6oygdyuh3sii', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499d30>, 'location': 'westus2', 'managed_by': '/subscriptions/mySub/resourceGroups/dataBricks/providers/Microsoft.Databricks/workspaces/databricksDEV', 'tags': {}}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/cool', 'name': 'cool', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499cf8>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/wsFunctionApp', 'name': 'wsFunctionApp', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499e48>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/azureStorage', 'name': 'azureStorage', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499dd8>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/azureSQL', 'name': 'azureSQL', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499d68>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/azureFunction', 'name': 'azureFunction', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499f28>, 'location': 'westus2', 'managed_by': None, 'tags': None}
{'additional_properties': {}, 'id': '/subscriptions/mySub/resourceGroups/NetworkWatcherRG', 'name': 'NetworkWatcherRG', 'properties': <azure.mgmt.resource.resources.v2018_05_01.models.resource_group_properties_py3.ResourceGroupProperties object at 0x7f0fba499f98>, 'location': 'westus2', 'managed_by': None, 'tags': None}

Can someone help me pretty print this output into something like a display() table?

Not sure why you want this, you can print all the properties you want of the item directly.

Something like:

print(item.id)
print(item.location)
print(item.tags)

在此处输入图像描述

Or if you want a valid json, you could custom the properties you want like below, the output is a valid json:

import json

result = []
for item in resource_client.resource_groups.list():
    additional_properties = item.additional_properties
    id = item.id
    location = item.location
    tags = item.tags
    type = item.type
    row = {"additional_properties": additional_properties, "id": id, "location": location, "tags": tags, "type": type}
    result.append(row)
data = json.dumps(result)
print(data)

If you want to make the output easy to read, you could use the JSON Formatter Tool.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM