godo icon indicating copy to clipboard operation
godo copied to clipboard

Add floating IP info to the droplet

Open roidelapluie opened this issue 5 years ago • 3 comments

The API returns the Floating IP's but there is no way to get them via godo. That would be useful.

roidelapluie avatar Jul 01 '20 19:07 roidelapluie

Hi @roidelapluie,

The Droplet.PublicIPv4() method is a helper that returns the first public IPv4 address for a Droplet. You can dig into the Droplet's networks to find additional IP addresses. Droplet.Networks.v4 is a slice of NetworkV4

https://godoc.org/github.com/digitalocean/godo#NetworkV4

In practice, currently a Droplet can only have two public IPv4 addresses, it's assigned address and a floating IP. So you could get at it using something like:

func getFloatingIP(droplet *godo.Droplet) (string, error) {
	if droplet.Networks == nil {
		return "", errors.New("no networks found")
	}

	publicIP, err := droplet.PublicIPv4()
	if err != nil {
		return "", err
	}

	for _, v4 := range droplet.Networks.V4 {
		if v4.Type == "public" && v4.IPAddress != publicIP {
			return v4.IPAddress, nil
		}
	}

	return "", nil
}

It could be nice to expose something like that in godo itself.

andrewsomething avatar Jul 01 '20 19:07 andrewsomething

Yes I did find out but is that guaranteed by the API docs that the second will always be the floating IP?

roidelapluie avatar Jul 01 '20 20:07 roidelapluie

hello @roidelapluie,

I was able to talk to the internal droplets team. We do not guarantee an order of ips. That said, @andrewsomething's suggestion works great for this use case and we will review and assist with any PR's that add this functionality to the droplets file. This would be a great first issue for any newcomers.

Thank you for your patience.

ChiefMateStarbuck avatar May 05 '23 14:05 ChiefMateStarbuck