In the end I was able to figure it with the help of a colleague and lots of searches. Posting this here in the hope that others will benefit from the answer.
Greets to @Charles Xu for getting me started with this answer how-could-i-list-azure-virtual-machines-using-python
It was difficult for me to find specific and relevant documentation from MS to address my issue.
NOTE: THis is a work in progress and I am sure that there is lots of room for improvement. I don't think the resulting json is perfect but json2html isn't complaining.
And now for the code....
#!/usr/bin/env python
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
import json
from json2html import *
# Tenant ID for your Azure subscription
TENANT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your service principal App ID
CLIENT = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your service principal password
KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your Azure Subscription ID
subscription_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
credentials = ServicePrincipalCredentials(
client_id=CLIENT,
secret=KEY,
tenant=TENANT_ID
)
result = []
# Create a Resource Management client
compute_client = ComputeManagementClient(credentials, subscription_id)
vm_list = compute_client.virtual_machines.list_all()
for vm in vm_list:
resource_group = vm.id.split("/")[4]
vm_name = vm.name
details = compute_client.virtual_machines.instance_view(resource_group, vm_name, expand='instanceView')
status = len(details.statuses) >= 2 and details.statuses[1]
if status and status.code == 'PowerState/running':
tagz = compute_client.virtual_machines.get(resource_group, vm_name).tags
if not tagz:
tagz = ""
admin = ''
if hasattr(details, 'os_profile'):
admin = details.os_profile.admin_username
row = {"ComputerName": vm_name, "ResourceGroup": resource_group, "Admin": admin,
"Status": status.code, "Tags": tagz}
result.append(row)
data = json.dumps(result)
html = json2html.convert(json=data)
print(html)