digitalocean / godo

DigitalOcean Go API client
https://pkg.go.dev/github.com/digitalocean/godo
Other
1.43k stars 299 forks source link

Add floating IP info to the droplet #343

Open roidelapluie opened 4 years ago

roidelapluie commented 4 years ago

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

andrewsomething commented 4 years ago

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.

roidelapluie commented 4 years ago

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

ChiefMateStarbuck commented 1 year ago

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.