blob: b36aeba59c3e5ad69db7e34c53a756fbec950f09 [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 })
jrperritt29ae6b32016-04-13 12:59:37 -050042 return
Joe Topjiandee32222015-02-09 23:56:26 +000043}
44
45// Get returns data about a previously created FloatingIP.
Jon Perritt3860b512016-03-29 12:01:48 -050046func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060047 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050048 return
Joe Topjiandee32222015-02-09 23:56:26 +000049}
50
51// Delete requests the deletion of a previous allocated FloatingIP.
Jon Perritt3860b512016-03-29 12:01:48 -050052func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050054 return
Joe Topjiandee32222015-02-09 23:56:26 +000055}
56
Jon Perrittdb0ae142016-03-13 00:33:41 -060057// AssociateOptsBuilder is the interface types must satfisfy to be used as
58// Associate options
59type AssociateOptsBuilder interface {
60 ToFloatingIPAssociateMap() (map[string]interface{}, error)
61}
Joe Topjiandee32222015-02-09 23:56:26 +000062
Jon Perrittdb0ae142016-03-13 00:33:41 -060063// AssociateOpts specifies the required information to associate a floating IP with an instance
64type 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 Topjiandee32222015-02-09 23:56:26 +000070
Jon Perrittdb0ae142016-03-13 00:33:41 -060071// ToFloatingIPAssociateMap constructs a request body from AssociateOpts.
72func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) {
73 return gophercloud.BuildRequestBody(opts, "addFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +000074}
75
Joe Topjian94e4cc52016-01-05 17:01:18 +000076// AssociateInstance pairs an allocated floating IP with an instance.
Jon Perritt3860b512016-03-29 12:01:48 -050077func AssociateInstance(client *gophercloud.ServiceClient, serverID string, opts AssociateOptsBuilder) (r AssociateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060078 b, err := opts.ToFloatingIPAssociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +000079 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060080 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050081 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +000082 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060083 _, r.Err = client.Post(associateURL(client, serverID), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050084 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +000085}
86
Jon Perrittdb0ae142016-03-13 00:33:41 -060087// DisassociateOptsBuilder is the interface types must satfisfy to be used as
88// Disassociate options
89type DisassociateOptsBuilder interface {
90 ToFloatingIPDisassociateMap() (map[string]interface{}, error)
91}
Joe Topjiandee32222015-02-09 23:56:26 +000092
Jon Perrittdb0ae142016-03-13 00:33:41 -060093// DisassociateOpts specifies the required information to disassociate a floating IP with an instance
94type DisassociateOpts struct {
95 FloatingIP string `json:"address" required:"true"`
96}
Joe Topjiandee32222015-02-09 23:56:26 +000097
Jon Perrittdb0ae142016-03-13 00:33:41 -060098// ToFloatingIPDisassociateMap constructs a request body from AssociateOpts.
99func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) {
100 return gophercloud.BuildRequestBody(opts, "removeFloatingIp")
Joe Topjiandee32222015-02-09 23:56:26 +0000101}
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000102
Joe Topjian94e4cc52016-01-05 17:01:18 +0000103// DisassociateInstance decouples an allocated floating IP from an instance
Jon Perritt3860b512016-03-29 12:01:48 -0500104func DisassociateInstance(client *gophercloud.ServiceClient, serverID string, opts DisassociateOptsBuilder) (r DisassociateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600105 b, err := opts.ToFloatingIPDisassociateMap()
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000106 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600107 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500108 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000109 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600110 _, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500111 return
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000112}