I want to get information about Virtual Machine from Azure Using Azure-SDK for Python . I'm able to get information of VM by providing resource-group and virtual-machine name in computeClient
compute_client = ComputeManagementClient(
credentials,
SUBSCRIPTION_ID
)
compute_client.virtual_machines.get(GROUP_NAME, VM_NAME, expand='instanceView')
But above code doesn't give me Vnet information
Can someone guide me on this
For your requirements, all you can get from the ComputeManagementClient SDK are network interfaces fo the VM, no such as Vnet, subnet. It's the configuration of the network interfaces.
So what you need to do is to get the information of the network interface, and then it will show you the config of the Nic, it contains the subnet which the Nic in.
I assume you only know the VM info, then you can get the Vnet info like this:
from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
subscription_Id = "xxxxxxxxx"
tenant_Id = "xxxxxxxxx"
client_Id = "xxxxxxxxx"
secret = "xxxxxxxxx"
credential = ServicePrincipalCredentials(
client_id=client_Id,
secret=secret,
tenant=tenant_Id
)
compute_client = ComputeManagementClient(credential, subscription_Id)
group_name = 'xxxxxxxxx'
vm_name = 'xxxxxxxxx'
vm = compute_client.virtual_machines.get(group_name, vm_name)
nic_name = vm.network_profile.network_interfaces[0].id.split('/')[-1]
nic_group = vm.network_profile.network_interfaces[0].id.split('/')[-5]
network_client = NetworkManagementClient(credential, subscription_Id)
nic = network_client.network_interfaces.get(nic_group, nic_name)
vnet_name = nic.ip_configurations[0].subnet.id.split('/')[-3]
vnet_group = nic.ip_configurations[0].subnet.id.split('/')[-7]
vnet = network_client.virtual_networks.get(vnet_group, vnet_name)
All the above code, I assume the VM only has one Nic, and Nic only has one configuration. If the VM has multiple Nics and each Nic has multiple configs. You can get them one by one inside a for each loop. Finally, you got the information about the Vnet that you want.