blob: 4c273474b0d7bcba9355d53ad46278e9ec8e9c6e [file] [log] [blame]
package gophercloud
import (
"fmt"
"github.com/racker/perigee"
)
func (gsp *genericServersProvider) ListFloatingIps() ([]FloatingIp, error) {
var fips []FloatingIp
err := gsp.context.WithReauth(gsp.access, func() error {
url := gsp.endpoint + "/os-floating-ips"
return perigee.Get(url, perigee.Options{
CustomClient: gsp.context.httpClient,
Results: &struct {
FloatingIps *[]FloatingIp `json:"floating_ips"`
}{&fips},
MoreHeaders: map[string]string{
"X-Auth-Token": gsp.access.AuthToken(),
},
})
})
return fips, err
}
func (gsp *genericServersProvider) CreateFloatingIp(pool string) (FloatingIp, error) {
var fip *FloatingIp
err := gsp.context.WithReauth(gsp.access, func() error {
url := gsp.endpoint + "/os-floating-ips"
return perigee.Post(url, perigee.Options{
CustomClient: gsp.context.httpClient,
ReqBody: map[string]string{
"pool": pool,
},
Results: &struct {
FloatingIp **FloatingIp `json:"floating_ip"`
}{&fip},
MoreHeaders: map[string]string{
"X-Auth-Token": gsp.access.AuthToken(),
},
})
})
return *fip, err
}
func (gsp *genericServersProvider) AssociateFloatingIp(serverId string, ip FloatingIp) error {
return gsp.context.WithReauth(gsp.access, func() error {
ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, serverId)
return perigee.Post(ep, perigee.Options{
CustomClient: gsp.context.httpClient,
ReqBody: map[string](map[string]string){
"addFloatingIp": map[string]string{"address": ip.Ip},
},
MoreHeaders: map[string]string{
"X-Auth-Token": gsp.access.AuthToken(),
},
OkCodes: []int{202},
})
})
}
func (gsp *genericServersProvider) DeleteFloatingIp(ip FloatingIp) error {
return gsp.context.WithReauth(gsp.access, func() error {
ep := fmt.Sprintf("%s/os-floating-ips/%s", gsp.endpoint, ip.Id)
return perigee.Delete(ep, perigee.Options{
CustomClient: gsp.context.httpClient,
MoreHeaders: map[string]string{
"X-Auth-Token": gsp.access.AuthToken(),
},
OkCodes: []int{202},
})
})
}
type FloatingIp struct {
Id string `json:"id"`
Pool string `json:"pool"`
Ip string `json:"ip"`
FixedIp string `json:"fixed_ip"`
InstanceId string `json:"instance_id"`
}