blob: 8137d76c8976abf9f7e8bf7f6e4a9d48bd12bbc2 [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
33func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060034 var r CreateResult
35 b, err := opts.ToFloatingIPCreateMap()
Joe Topjiandee32222015-02-09 23:56:26 +000036 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060037 r.Err = err
38 return r
Joe Topjiandee32222015-02-09 23:56:26 +000039 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060040 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010041 OkCodes: []int{200},
Joe Topjiandee32222015-02-09 23:56:26 +000042 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060043 return r
Joe Topjiandee32222015-02-09 23:56:26 +000044}
45
46// Get returns data about a previously created FloatingIP.
47func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060048 var r GetResult
49 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
50 return r
Joe Topjiandee32222015-02-09 23:56:26 +000051}
52
53// Delete requests the deletion of a previous allocated FloatingIP.
54func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060055 var r DeleteResult
56 _, r.Err = client.Delete(deleteURL(client, id), nil)
57 return r
Joe Topjiandee32222015-02-09 23:56:26 +000058}
59
Jon Perrittdb0ae142016-03-13 00:33:41 -060060// AssociateOptsBuilder is the interface types must satfisfy to be used as
61// Associate options
62type AssociateOptsBuilder interface {
63 ToFloatingIPAssociateMap() (map[string]interface{}, error)
64}
Joe Topjiandee32222015-02-09 23:56:26 +000065
Jon Perrittdb0ae142016-03-13 00:33:41 -060066// AssociateOpts specifies the required information to associate a floating IP with an instance
67type 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 Topjiandee32222015-02-09 23:56:26 +000073
Jon Perrittdb0ae142016-03-13 00:33:41 -060074// ToFloatingIPAssociateMap constructs a request body from AssociateOpts.
75func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) {
76 return gophercloud.BuildRequestBody(opts, "addFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +000077}
78
Joe Topjian94e4cc52016-01-05 17:01:18 +000079// AssociateInstance pairs an allocated floating IP with an instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -060080func AssociateInstance(client *gophercloud.ServiceClient, serverID string, opts AssociateOptsBuilder) AssociateResult {
81 var r AssociateResult
82 b, err := opts.ToFloatingIPAssociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +000083 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060084 r.Err = err
85 return r
Joe Topjiand97fe9b2015-09-17 02:08:38 +000086 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060087 _, r.Err = client.Post(associateURL(client, serverID), b, nil, nil)
88 return r
Joe Topjiand97fe9b2015-09-17 02:08:38 +000089}
90
Jon Perrittdb0ae142016-03-13 00:33:41 -060091// DisassociateOptsBuilder is the interface types must satfisfy to be used as
92// Disassociate options
93type DisassociateOptsBuilder interface {
94 ToFloatingIPDisassociateMap() (map[string]interface{}, error)
95}
Joe Topjiandee32222015-02-09 23:56:26 +000096
Jon Perrittdb0ae142016-03-13 00:33:41 -060097// DisassociateOpts specifies the required information to disassociate a floating IP with an instance
98type DisassociateOpts struct {
99 FloatingIP string `json:"address" required:"true"`
100}
Joe Topjiandee32222015-02-09 23:56:26 +0000101
Jon Perrittdb0ae142016-03-13 00:33:41 -0600102// ToFloatingIPDisassociateMap constructs a request body from AssociateOpts.
103func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) {
104 return gophercloud.BuildRequestBody(opts, "removeFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +0000105}
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000106
Joe Topjian94e4cc52016-01-05 17:01:18 +0000107// DisassociateInstance decouples an allocated floating IP from an instance
Jon Perrittdb0ae142016-03-13 00:33:41 -0600108func DisassociateInstance(client *gophercloud.ServiceClient, serverID string, opts DisassociateOptsBuilder) DisassociateResult {
109 var r DisassociateResult
110 b, err := opts.ToFloatingIPDisassociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000111 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600112 r.Err = err
113 return r
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000114 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600115 _, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil)
116 return r
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000117}