Search code examples
azure-sdk-python

Extract Returned values from 'VirtualNetworkPeering object


How Can I extract the atrribute values returned from list of peered virtual networks. I executed this command and I need to extract the Network ID

list_all = network_client.virtual_network_peerings.list(
    GROUP_NAME,
    VNET_NAME
)

for peer in list_all:

print(peer)

and I get this value for from the print above:

{'additional_properties': {'type': 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings'}, 
'id': '/subscriptions/c70b9b-efd6-497d-98d8-e1e1d497425/resourceGroups/azure-sample-group-virtual-machines/providers/Microsoft.Network/virtualNetworks/azure-sample-vnet/virtualNetworkPeerings/sample-vnetpeer', 
'allow_virtual_network_access': True, 
'allow_forwarded_traffic': True, 
'allow_gateway_transit': False, 
'use_remote_gateways': False, 
'remote_virtual_network': <azure.mgmt.network.v2018_08_01.models.sub_resource_py3.SubResource object at 0x048D6950>, 
'remote_address_space': <azure.mgmt.network.v2018_08_01.models.address_space_py3.AddressSpace object at 0x048D68D0>,
 'peering_state': 'Initiated', 
'provisioning_state': 'Succeeded',
 'name': 'sample-vnetpeer',
 'etag': 'W/"653f7f94-3c4e-4275-bfdf-0bbbd9beb6e4"'}

How can I get this value "remote_virtual_network"?


Solution

  • My feeling is that your question is actually more a Python question than an Azure question. Assuming in your application this field is set with values, then remote_virtual_network is a SubResource meaning it only has one attribute: id

    for peer in list_all:
        remote_virtual_network_id = peer.remote_virtual_network.id
    

    This guy is an actual virtual network, so if you want details about it you need to get it with network_client.virtual_networks.get: https://learn.microsoft.com/en-us/python/api/azure-mgmt-network/azure.mgmt.network.v2018_08_01.operations.virtualnetworksoperations?view=azure-python#get

    The tricky part is you get an ID, but VNet get asks for a RG name and VNet name, you can use the ARM ID parser for that:

    https://learn.microsoft.com/en-us/python/api/msrestazure/msrestazure.tools?view=azure-python#parse-resource-id