0

When I try run this code it is giving me null as the IP address for every resource, not sure why. Trying to return the IP Addresses for each resource and will also return null if the resource does not have an IP Address.

from ipaddress import ip_address
import logging
import json
import os

from azure.mgmt.resource import ResourceManagementClient

This function return the resources in a resource group. This is where I need to return the IP address.

def process_r_instance(resource):
"""
Get the resources from a ResourceGroup instance.
"""
return {
    "Name": resource.name,
    "Type": resource.type,
    "Location": resource.location,
    "Tags": resource.tags,
    "IP Address: ": resource.properties.ipAddress \
            if resource.properties and resource.properties.ipAddress else None,
    
    
        
}

This function returns the resource groups in a subscription.

def process_rg_instance(group):
"""
Get the relevant pieces of information from a ResourceGroup 
instance.
"""
return {
    "Name": group.name,
    "Id": group.id,
    "Location": group.location,
    "Type": group.type,
    "Tags": group.tags,
    "Properties: Provisioning State: ": group.properties.provisioning_state \
        if group.properties and group.properties.provisioning_state else None,
           
     }

This function gets the list of resource groups for the subscription ID Passed, and returns the list.

async def list_rgs(credentials, subscription_id):
"""
Get list of resource groups for the subscription id passed.
"""
list_of_resource_groups = []

with ResourceManagementClient(credentials, subscription_id) as 
  rg_client:
    try:
        for i in rg_client.resource_groups.list():
            list_of_resource_groups.append(process_rg_instance(i)),
        
        for i in rg_client.resources.list():
            list_of_resource_groups.append(process_r_instance(i)),

                     
    except Exception as e:
        logging.error("encountered: {0}".format(str(e)))


return json.dumps(list_of_resource_groups)
Evan Reen
  • 1
  • 1

1 Answers1

0

One of the workaround,

To return IP addresses using python you may need to use regex pattern inside the code to extract the ip address as on below example:-

Example Of Code:-

pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')

For more information please refer this SO THREAD|Is there any python API which can get the IP address (internal or external) of Virtual machine in Azure.

And to get IP address from Azure Function Http request with Python please refer this SO THREAD workaround .

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15