Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 1 | package floatingips |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // List returns a Pager that allows you to iterate over a collection of FloatingIPs. |
| 9 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 10 | return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 11 | return FloatingIPPage{pagination.SinglePageBase(r)} |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 12 | }) |
| 13 | } |
| 14 | |
| 15 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 16 | // CreateOpts struct in this package does. |
| 17 | type CreateOptsBuilder interface { |
| 18 | ToFloatingIPCreateMap() (map[string]interface{}, error) |
| 19 | } |
| 20 | |
| 21 | // CreateOpts specifies a Floating IP allocation request |
| 22 | type CreateOpts struct { |
| 23 | // Pool is the pool of floating IPs to allocate one from |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 24 | Pool string `json:"pool" required:"true"` |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 27 | // ToFloatingIPCreateMap constructs a request body from CreateOpts. |
| 28 | func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 29 | return gophercloud.BuildRequestBody(opts, "") |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 32 | // Create requests the creation of a new floating IP |
| 33 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 34 | var r CreateResult |
| 35 | b, err := opts.ToFloatingIPCreateMap() |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 36 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 37 | r.Err = err |
| 38 | return r |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 39 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 40 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 41 | OkCodes: []int{200}, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 42 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 43 | return r |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Get returns data about a previously created FloatingIP. |
| 47 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | var r GetResult |
| 49 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 50 | return r |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Delete requests the deletion of a previous allocated FloatingIP. |
| 54 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 55 | var r DeleteResult |
| 56 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 57 | return r |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 60 | // AssociateOptsBuilder is the interface types must satfisfy to be used as |
| 61 | // Associate options |
| 62 | type AssociateOptsBuilder interface { |
| 63 | ToFloatingIPAssociateMap() (map[string]interface{}, error) |
| 64 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 65 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 66 | // AssociateOpts specifies the required information to associate a floating IP with an instance |
| 67 | type AssociateOpts struct { |
| 68 | // FloatingIP is the floating IP to associate with an instance |
| 69 | FloatingIP string `json:"address" required:"true"` |
| 70 | // FixedIP is an optional fixed IP address of the server |
| 71 | FixedIP string `json:"fixed_address,omitempty"` |
| 72 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 73 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 74 | // ToFloatingIPAssociateMap constructs a request body from AssociateOpts. |
| 75 | func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) { |
| 76 | return gophercloud.BuildRequestBody(opts, "addFloatingIp") |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 79 | // AssociateInstance pairs an allocated floating IP with an instance. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 80 | func AssociateInstance(client *gophercloud.ServiceClient, serverID string, opts AssociateOptsBuilder) AssociateResult { |
| 81 | var r AssociateResult |
| 82 | b, err := opts.ToFloatingIPAssociateMap() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 83 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 84 | r.Err = err |
| 85 | return r |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 86 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 87 | _, r.Err = client.Post(associateURL(client, serverID), b, nil, nil) |
| 88 | return r |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 91 | // DisassociateOptsBuilder is the interface types must satfisfy to be used as |
| 92 | // Disassociate options |
| 93 | type DisassociateOptsBuilder interface { |
| 94 | ToFloatingIPDisassociateMap() (map[string]interface{}, error) |
| 95 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 96 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 97 | // DisassociateOpts specifies the required information to disassociate a floating IP with an instance |
| 98 | type DisassociateOpts struct { |
| 99 | FloatingIP string `json:"address" required:"true"` |
| 100 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 101 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 102 | // ToFloatingIPDisassociateMap constructs a request body from AssociateOpts. |
| 103 | func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) { |
| 104 | return gophercloud.BuildRequestBody(opts, "removeFloatingIp") |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 105 | } |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 106 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 107 | // DisassociateInstance decouples an allocated floating IP from an instance |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 108 | func DisassociateInstance(client *gophercloud.ServiceClient, serverID string, opts DisassociateOptsBuilder) DisassociateResult { |
| 109 | var r DisassociateResult |
| 110 | b, err := opts.ToFloatingIPDisassociateMap() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 111 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 112 | r.Err = err |
| 113 | return r |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 114 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 115 | _, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil) |
| 116 | return r |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 117 | } |