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