blob: 11636673ea4d8fa5f7c2ab710e8961b7198f4bf4 [file] [log] [blame]
Tim Millerc04e9752014-01-15 16:15:47 -08001package gophercloud
2
3import (
Ben Broderick Phillips906b1df2014-02-27 02:42:01 -08004 "errors"
Tim Millerc04e9752014-01-15 16:15:47 -08005 "fmt"
6 "github.com/racker/perigee"
7)
8
9func (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
27func (gsp *genericServersProvider) CreateFloatingIp(pool string) (FloatingIp, error) {
Ben Broderick Phillips906b1df2014-02-27 02:42:01 -080028 fip := new(FloatingIp)
Tim Millerc04e9752014-01-15 16:15:47 -080029
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 Phillips906b1df2014-02-27 02:42:01 -080046 if fip.Ip == "" {
47 return *fip, errors.New("Error creating floating IP")
48 }
49
Tim Millerc04e9752014-01-15 16:15:47 -080050 return *fip, err
51}
52
53func (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
69func (gsp *genericServersProvider) DeleteFloatingIp(ip FloatingIp) error {
70 return gsp.context.WithReauth(gsp.access, func() error {
Ben Broderick Phillips906b1df2014-02-27 02:42:01 -080071 ep := fmt.Sprintf("%s/os-floating-ips/%d", gsp.endpoint, ip.Id)
Tim Millerc04e9752014-01-15 16:15:47 -080072 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
82type FloatingIp struct {
Ben Broderick Phillips906b1df2014-02-27 02:42:01 -080083 Id int `json:"id"`
Tim Millerc04e9752014-01-15 16:15:47 -080084 Pool string `json:"pool"`
85 Ip string `json:"ip"`
86 FixedIp string `json:"fixed_ip"`
87 InstanceId string `json:"instance_id"`
88}