"Dereference" a sub-resource in the Azure Python SDK return value

128 Views Asked by At

I would like to retrieve the public IP address associated with a given network interface. I need to do something like

client = NetworkManagementClient(...)
interface = client.network_interfaces.get('rg', 'nic-name')
ip_config_id = interface[0].public_ip_address.id
ip_config = some_magic(ip_config_id)  # What goes here?
return ip_config.ip_address

This question suggests that in order to implement the some_magic routine, I should parse the ID (by splitting on slashes) and call client.public_ip_addresses.get(). This question indicates that I can call resource_client.resources.get_by_uid, but that doesn't return a PublicIPAddress object (I know I can call as_dict on it and get the data that way).

Is there a way to get an object of the appropriate type (in this case PublicIPAddress) from an object's ID in Azure (without manually parsing the ID)?

1

There are 1 best solutions below

5
On BEST ANSWER

Update:

Due to this issue: public_ip_address method within NetworkManagementClient will not return values, we cannot fetch the ip address from PublicIPAddress.

So currently, you can use any other workaround, For example:

myip = client.public_ip_addresses.get(" resource_group_name","public_ip_address_name")
print(myip.ip_address)

You can change this line of code ip_config_id = interface[0].public_ip_address.id to something like my_public_ip_address = interface.ip_configurations[0].public_ip_address, then the return type is PublicIPAddress.

For example:

enter image description here