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 |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 33 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 34 | b, err := opts.ToFloatingIPCreateMap() |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 35 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 36 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 37 | return |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 38 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 39 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 40 | OkCodes: []int{200}, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 41 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 42 | return |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Get returns data about a previously created FloatingIP. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 46 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 47 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 48 | return |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Delete requests the deletion of a previous allocated FloatingIP. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 52 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 53 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 54 | return |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 57 | // AssociateOptsBuilder is the interface types must satfisfy to be used as |
| 58 | // Associate options |
| 59 | type AssociateOptsBuilder interface { |
| 60 | ToFloatingIPAssociateMap() (map[string]interface{}, error) |
| 61 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 62 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 63 | // AssociateOpts specifies the required information to associate a floating IP with an instance |
| 64 | type AssociateOpts struct { |
| 65 | // FloatingIP is the floating IP to associate with an instance |
| 66 | FloatingIP string `json:"address" required:"true"` |
| 67 | // FixedIP is an optional fixed IP address of the server |
| 68 | FixedIP string `json:"fixed_address,omitempty"` |
| 69 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 70 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 71 | // ToFloatingIPAssociateMap constructs a request body from AssociateOpts. |
| 72 | func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) { |
| 73 | return gophercloud.BuildRequestBody(opts, "addFloatingIp") |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 76 | // AssociateInstance pairs an allocated floating IP with an instance. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 77 | func AssociateInstance(client *gophercloud.ServiceClient, serverID string, opts AssociateOptsBuilder) (r AssociateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 78 | b, err := opts.ToFloatingIPAssociateMap() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 79 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 80 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 81 | return |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 82 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 83 | _, r.Err = client.Post(associateURL(client, serverID), b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 84 | return |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 87 | // DisassociateOptsBuilder is the interface types must satfisfy to be used as |
| 88 | // Disassociate options |
| 89 | type DisassociateOptsBuilder interface { |
| 90 | ToFloatingIPDisassociateMap() (map[string]interface{}, error) |
| 91 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 92 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 93 | // DisassociateOpts specifies the required information to disassociate a floating IP with an instance |
| 94 | type DisassociateOpts struct { |
| 95 | FloatingIP string `json:"address" required:"true"` |
| 96 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 97 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 98 | // ToFloatingIPDisassociateMap constructs a request body from AssociateOpts. |
| 99 | func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) { |
| 100 | return gophercloud.BuildRequestBody(opts, "removeFloatingIp") |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 101 | } |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 102 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 103 | // DisassociateInstance decouples an allocated floating IP from an instance |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 104 | func DisassociateInstance(client *gophercloud.ServiceClient, serverID string, opts DisassociateOptsBuilder) (r DisassociateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 105 | b, err := opts.ToFloatingIPDisassociateMap() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 106 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 107 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 108 | return |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 109 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 110 | _, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 111 | return |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 112 | } |