Tim Miller | c04e975 | 2014-01-15 16:15:47 -0800 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/racker/perigee" |
| 6 | ) |
| 7 | |
| 8 | func (gsp *genericServersProvider) ListFloatingIps() ([]FloatingIp, error) { |
| 9 | var fips []FloatingIp |
| 10 | |
| 11 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 12 | url := gsp.endpoint + "/os-floating-ips" |
| 13 | return perigee.Get(url, perigee.Options{ |
| 14 | CustomClient: gsp.context.httpClient, |
| 15 | Results: &struct { |
| 16 | FloatingIps *[]FloatingIp `json:"floating_ips"` |
| 17 | }{&fips}, |
| 18 | MoreHeaders: map[string]string{ |
| 19 | "X-Auth-Token": gsp.access.AuthToken(), |
| 20 | }, |
| 21 | }) |
| 22 | }) |
| 23 | return fips, err |
| 24 | } |
| 25 | |
| 26 | func (gsp *genericServersProvider) CreateFloatingIp(pool string) (FloatingIp, error) { |
| 27 | var fip *FloatingIp |
| 28 | |
| 29 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 30 | url := gsp.endpoint + "/os-floating-ips" |
| 31 | return perigee.Post(url, perigee.Options{ |
| 32 | CustomClient: gsp.context.httpClient, |
| 33 | ReqBody: map[string]string{ |
| 34 | "pool": pool, |
| 35 | }, |
| 36 | Results: &struct { |
| 37 | FloatingIp **FloatingIp `json:"floating_ip"` |
| 38 | }{&fip}, |
| 39 | MoreHeaders: map[string]string{ |
| 40 | "X-Auth-Token": gsp.access.AuthToken(), |
| 41 | }, |
| 42 | }) |
| 43 | }) |
| 44 | |
| 45 | return *fip, err |
| 46 | } |
| 47 | |
| 48 | func (gsp *genericServersProvider) AssociateFloatingIp(serverId string, ip FloatingIp) error { |
| 49 | return gsp.context.WithReauth(gsp.access, func() error { |
| 50 | ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, serverId) |
| 51 | return perigee.Post(ep, perigee.Options{ |
| 52 | CustomClient: gsp.context.httpClient, |
| 53 | ReqBody: map[string](map[string]string){ |
| 54 | "addFloatingIp": map[string]string{"address": ip.Ip}, |
| 55 | }, |
| 56 | MoreHeaders: map[string]string{ |
| 57 | "X-Auth-Token": gsp.access.AuthToken(), |
| 58 | }, |
| 59 | OkCodes: []int{202}, |
| 60 | }) |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | func (gsp *genericServersProvider) DeleteFloatingIp(ip FloatingIp) error { |
| 65 | return gsp.context.WithReauth(gsp.access, func() error { |
| 66 | ep := fmt.Sprintf("%s/os-floating-ips/%s", gsp.endpoint, ip.Id) |
| 67 | return perigee.Delete(ep, perigee.Options{ |
| 68 | CustomClient: gsp.context.httpClient, |
| 69 | MoreHeaders: map[string]string{ |
| 70 | "X-Auth-Token": gsp.access.AuthToken(), |
| 71 | }, |
| 72 | OkCodes: []int{202}, |
| 73 | }) |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | type FloatingIp struct { |
| 78 | Id string `json:"id"` |
| 79 | Pool string `json:"pool"` |
| 80 | Ip string `json:"ip"` |
| 81 | FixedIp string `json:"fixed_ip"` |
| 82 | InstanceId string `json:"instance_id"` |
| 83 | } |