Как отобразить azure ресурсов в табличной форме python SDK - PullRequest
0 голосов
/ 01 мая 2020

Я создал ResourceManagementClient, используя azure python sdk:

  • resource_client = ResourceManagementClient(service_credential, subscription_id)

вывод производится через ...

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

... трудно читать, и он не в действительном формате json.

{'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}

Может ли кто-нибудь помочь мне распечатать этот вывод в виде таблицы display()?

1 Ответ

1 голос
/ 01 мая 2020

Не знаете, зачем вам это нужно, вы можете распечатать все нужные вам свойства item напрямую.

Что-то вроде:

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

enter image description here

Или если вы хотите действительный json, вы можете настроить свойства, которые вы хотите, как показано ниже, выходные данные являются действительными 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)

Если вы хотите сделать вывод легко читаемым, вы можете использовать JSON Formatter Tool.

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...