0

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)?

Joey Marianer
  • 728
  • 1
  • 5
  • 18
  • Hello, if the answer is helpful, could you please accept it as answer:). Thanks. – Ivan Glasenberg Dec 18 '20 at 01:16
  • Sorry it took me so long to check it out; I was busy with other parts of my project. Unfortunately it doesn't seem to have solved my main issue, which was getting the IP address. – Joey Marianer Dec 18 '20 at 21:09

1 Answers1

0

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

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Interestingly, while you're correct that it's the correct type, when I inspect the value of `i.ip_configurations[0].public_ip_address.ip_address` it is None. However, when I get the IP address from `client.public_ip_addresses.get(...)`, it gives me the correct IP address. – Joey Marianer Dec 18 '20 at 21:08
  • @JoeyMarianer, it's really weird. When I open `fiddler`, I can see everything including the `public_ip_address` is there. I need some time to figure it out and update to you later. – Ivan Glasenberg Dec 21 '20 at 03:14
  • @JoeyMarianer, it's actually a bug in the sdk. If you open fiddler, you can see all the data are returned correctly. Some people already raised [an issue](https://github.com/Azure/azure-sdk-for-python/issues/10735#) in github to track it. And before it's fixed, you should use any workaround to fetch the public_ip_address. – Ivan Glasenberg Dec 21 '20 at 03:43
  • Updated my answer with this bug. And if the answer is helpful, could you please [accept it as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work?answertab=active#tab-top)? Thanks:). – Ivan Glasenberg Dec 22 '20 at 01:58