blob: 2f5cd79fbb1824e71fca60b82c400a505899a2c0 [file] [log] [blame]
Jon Perritt12395212016-02-24 10:41:17 -06001package floatingips
Joe Topjiandee32222015-02-09 23:56:26 +00002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Joe Topjiandee32222015-02-09 23:56:26 +00006)
7
8// List returns a Pager that allows you to iterate over a collection of FloatingIPs.
9func List(client *gophercloud.ServiceClient) pagination.Pager {
10 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060011 return FloatingIPPage{pagination.SinglePageBase(r)}
Joe Topjiandee32222015-02-09 23:56:26 +000012 })
13}
14
15// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
16// CreateOpts struct in this package does.
17type CreateOptsBuilder interface {
18 ToFloatingIPCreateMap() (map[string]interface{}, error)
19}
20
21// CreateOpts specifies a Floating IP allocation request
22type CreateOpts struct {
23 // Pool is the pool of floating IPs to allocate one from
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 Pool string `json:"pool" required:"true"`
Joe Topjiand97fe9b2015-09-17 02:08:38 +000025}
26
Joe Topjiandee32222015-02-09 23:56:26 +000027// ToFloatingIPCreateMap constructs a request body from CreateOpts.
28func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 return gophercloud.BuildRequestBody(opts, "")
Joe Topjiand97fe9b2015-09-17 02:08:38 +000030}
31
Joe Topjiandee32222015-02-09 23:56:26 +000032// Create requests the creation of a new floating IP
Jon Perritt3860b512016-03-29 12:01:48 -050033func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060034 b, err := opts.ToFloatingIPCreateMap()
Joe Topjiandee32222015-02-09 23:56:26 +000035 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050037 return
Joe Topjiandee32222015-02-09 23:56:26 +000038 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060039 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010040 OkCodes: []int{200},
Joe Topjiandee32222015-02-09 23:56:26 +000041 })
Joe Topjiandee32222015-02-09 23:56:26 +000042}
43
44// Get returns data about a previously created FloatingIP.
Jon Perritt3860b512016-03-29 12:01:48 -050045func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
Joe Topjiandee32222015-02-09 23:56:26 +000047}
48
49// Delete requests the deletion of a previous allocated FloatingIP.
Jon Perritt3860b512016-03-29 12:01:48 -050050func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 _, r.Err = client.Delete(deleteURL(client, id), nil)
Joe Topjiandee32222015-02-09 23:56:26 +000052}
53
Jon Perrittdb0ae142016-03-13 00:33:41 -060054// AssociateOptsBuilder is the interface types must satfisfy to be used as
55// Associate options
56type AssociateOptsBuilder interface {
57 ToFloatingIPAssociateMap() (map[string]interface{}, error)
58}
Joe Topjiandee32222015-02-09 23:56:26 +000059
Jon Perrittdb0ae142016-03-13 00:33:41 -060060// AssociateOpts specifies the required information to associate a floating IP with an instance
61type AssociateOpts struct {
62 // FloatingIP is the floating IP to associate with an instance
63 FloatingIP string `json:"address" required:"true"`
64 // FixedIP is an optional fixed IP address of the server
65 FixedIP string `json:"fixed_address,omitempty"`
66}
Joe Topjiandee32222015-02-09 23:56:26 +000067
Jon Perrittdb0ae142016-03-13 00:33:41 -060068// ToFloatingIPAssociateMap constructs a request body from AssociateOpts.
69func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) {
70 return gophercloud.BuildRequestBody(opts, "addFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +000071}
72
Joe Topjian94e4cc52016-01-05 17:01:18 +000073// AssociateInstance pairs an allocated floating IP with an instance.
Jon Perritt3860b512016-03-29 12:01:48 -050074func AssociateInstance(client *gophercloud.ServiceClient, serverID string, opts AssociateOptsBuilder) (r AssociateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060075 b, err := opts.ToFloatingIPAssociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +000076 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050078 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +000079 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060080 _, r.Err = client.Post(associateURL(client, serverID), b, nil, nil)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000081}
82
Jon Perrittdb0ae142016-03-13 00:33:41 -060083// DisassociateOptsBuilder is the interface types must satfisfy to be used as
84// Disassociate options
85type DisassociateOptsBuilder interface {
86 ToFloatingIPDisassociateMap() (map[string]interface{}, error)
87}
Joe Topjiandee32222015-02-09 23:56:26 +000088
Jon Perrittdb0ae142016-03-13 00:33:41 -060089// DisassociateOpts specifies the required information to disassociate a floating IP with an instance
90type DisassociateOpts struct {
91 FloatingIP string `json:"address" required:"true"`
92}
Joe Topjiandee32222015-02-09 23:56:26 +000093
Jon Perrittdb0ae142016-03-13 00:33:41 -060094// ToFloatingIPDisassociateMap constructs a request body from AssociateOpts.
95func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) {
96 return gophercloud.BuildRequestBody(opts, "removeFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +000097}
Joe Topjiand97fe9b2015-09-17 02:08:38 +000098
Joe Topjian94e4cc52016-01-05 17:01:18 +000099// DisassociateInstance decouples an allocated floating IP from an instance
Jon Perritt3860b512016-03-29 12:01:48 -0500100func DisassociateInstance(client *gophercloud.ServiceClient, serverID string, opts DisassociateOptsBuilder) (r DisassociateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600101 b, err := opts.ToFloatingIPDisassociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000102 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500104 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000105 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600106 _, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000107}