Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | package floatingip |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // List returns a Pager that allows you to iterate over a collection of FloatingIPs. |
| 11 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 12 | return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { |
| 13 | return FloatingIPsPage{pagination.SinglePageBase(r)} |
| 14 | }) |
| 15 | } |
| 16 | |
| 17 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 18 | // CreateOpts struct in this package does. |
| 19 | type CreateOptsBuilder interface { |
| 20 | ToFloatingIPCreateMap() (map[string]interface{}, error) |
| 21 | } |
| 22 | |
| 23 | // CreateOpts specifies a Floating IP allocation request |
| 24 | type CreateOpts struct { |
| 25 | // Pool is the pool of floating IPs to allocate one from |
| 26 | Pool string |
| 27 | } |
| 28 | |
| 29 | // ToFloatingIPCreateMap constructs a request body from CreateOpts. |
| 30 | func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) { |
| 31 | if opts.Pool == "" { |
| 32 | return nil, errors.New("Missing field required for floating IP creation: Pool") |
| 33 | } |
| 34 | |
| 35 | return map[string]interface{}{"pool": opts.Pool}, nil |
| 36 | } |
| 37 | |
| 38 | // Create requests the creation of a new floating IP |
| 39 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 40 | var res CreateResult |
| 41 | |
| 42 | reqBody, err := opts.ToFloatingIPCreateMap() |
| 43 | if err != nil { |
| 44 | res.Err = err |
| 45 | return res |
| 46 | } |
| 47 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame^] | 48 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 49 | OkCodes: []int{200}, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 50 | }) |
| 51 | return res |
| 52 | } |
| 53 | |
| 54 | // Get returns data about a previously created FloatingIP. |
| 55 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 56 | var res GetResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame^] | 57 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 58 | return res |
| 59 | } |
| 60 | |
| 61 | // Delete requests the deletion of a previous allocated FloatingIP. |
| 62 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 63 | var res DeleteResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame^] | 64 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 65 | return res |
| 66 | } |
| 67 | |
| 68 | // association / disassociation |
| 69 | |
| 70 | // Associate pairs an allocated floating IP with an instance |
| 71 | func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult { |
| 72 | var res AssociateResult |
| 73 | |
| 74 | addFloatingIp := make(map[string]interface{}) |
| 75 | addFloatingIp["address"] = fip |
| 76 | reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp} |
| 77 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame^] | 78 | _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 79 | return res |
| 80 | } |
| 81 | |
| 82 | // Disassociate decouples an allocated floating IP from an instance |
| 83 | func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult { |
| 84 | var res DisassociateResult |
| 85 | |
| 86 | removeFloatingIp := make(map[string]interface{}) |
| 87 | removeFloatingIp["address"] = fip |
| 88 | reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp} |
| 89 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame^] | 90 | _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 91 | return res |
| 92 | } |