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 |
| 24 | Pool string |
| 25 | } |
| 26 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 27 | // AssociateOpts specifies the required information to associate or disassociate a floating IP to an instance |
| 28 | type AssociateOpts struct { |
| 29 | // ServerID is the UUID of the server |
| 30 | ServerID string |
| 31 | |
| 32 | // FixedIP is an optional fixed IP address of the server |
| 33 | FixedIP string |
| 34 | |
| 35 | // FloatingIP is the floating IP to associate with an instance |
| 36 | FloatingIP string |
| 37 | } |
| 38 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 39 | // ToFloatingIPCreateMap constructs a request body from CreateOpts. |
| 40 | func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) { |
| 41 | if opts.Pool == "" { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 42 | err := gophercloud.ErrMissingInput{} |
| 43 | err.Function = "floatingips.ToFloatingIPCreateMap" |
| 44 | err.Argument = "floatingips.CreateOpts.Pool" |
| 45 | return nil, err |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return map[string]interface{}{"pool": opts.Pool}, nil |
| 49 | } |
| 50 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 51 | // ToAssociateMap constructs a request body from AssociateOpts. |
| 52 | func (opts AssociateOpts) ToAssociateMap() (map[string]interface{}, error) { |
| 53 | if opts.ServerID == "" { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 54 | err := gophercloud.ErrMissingInput{} |
| 55 | err.Function = "floatingips.ToAssociateMap" |
| 56 | err.Argument = "floatingips.AssociateOpts.ServerID" |
| 57 | return nil, err |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if opts.FloatingIP == "" { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 61 | err := gophercloud.ErrMissingInput{} |
| 62 | err.Function = "floatingips.ToAssociateMap" |
| 63 | err.Argument = "floatingips.AssociateOpts.FloatingIP" |
| 64 | return nil, err |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | associateInfo := map[string]interface{}{ |
| 68 | "serverId": opts.ServerID, |
| 69 | "floatingIp": opts.FloatingIP, |
| 70 | "fixedIp": opts.FixedIP, |
| 71 | } |
| 72 | |
| 73 | return associateInfo, nil |
| 74 | |
| 75 | } |
| 76 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 77 | // Create requests the creation of a new floating IP |
| 78 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 79 | var res CreateResult |
| 80 | |
| 81 | reqBody, err := opts.ToFloatingIPCreateMap() |
| 82 | if err != nil { |
| 83 | res.Err = err |
| 84 | return res |
| 85 | } |
| 86 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 87 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 88 | OkCodes: []int{200}, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 89 | }) |
| 90 | return res |
| 91 | } |
| 92 | |
| 93 | // Get returns data about a previously created FloatingIP. |
| 94 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 95 | var res GetResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 96 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 97 | return res |
| 98 | } |
| 99 | |
| 100 | // Delete requests the deletion of a previous allocated FloatingIP. |
| 101 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 102 | var res DeleteResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 103 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 104 | return res |
| 105 | } |
| 106 | |
| 107 | // association / disassociation |
| 108 | |
| 109 | // Associate pairs an allocated floating IP with an instance |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 110 | // Deprecated. Use AssociateInstance. |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 111 | func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult { |
| 112 | var res AssociateResult |
| 113 | |
| 114 | addFloatingIp := make(map[string]interface{}) |
| 115 | addFloatingIp["address"] = fip |
| 116 | reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp} |
| 117 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 118 | _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 119 | return res |
| 120 | } |
| 121 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 122 | // AssociateInstance pairs an allocated floating IP with an instance. |
| 123 | func AssociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) AssociateResult { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 124 | var res AssociateResult |
| 125 | |
| 126 | associateInfo, err := opts.ToAssociateMap() |
| 127 | if err != nil { |
| 128 | res.Err = err |
| 129 | return res |
| 130 | } |
| 131 | |
| 132 | addFloatingIp := make(map[string]interface{}) |
| 133 | addFloatingIp["address"] = associateInfo["floatingIp"].(string) |
| 134 | |
| 135 | // fixedIp is not required |
| 136 | if associateInfo["fixedIp"] != "" { |
| 137 | addFloatingIp["fixed_address"] = associateInfo["fixedIp"].(string) |
| 138 | } |
| 139 | |
| 140 | serverId := associateInfo["serverId"].(string) |
| 141 | |
| 142 | reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp} |
| 143 | _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil) |
| 144 | return res |
| 145 | } |
| 146 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 147 | // Disassociate decouples an allocated floating IP from an instance |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 148 | // Deprecated. Use DisassociateInstance. |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 149 | func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult { |
| 150 | var res DisassociateResult |
| 151 | |
| 152 | removeFloatingIp := make(map[string]interface{}) |
| 153 | removeFloatingIp["address"] = fip |
| 154 | reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp} |
| 155 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 156 | _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 157 | return res |
| 158 | } |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 159 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 160 | // DisassociateInstance decouples an allocated floating IP from an instance |
| 161 | func DisassociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) DisassociateResult { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 162 | var res DisassociateResult |
| 163 | |
| 164 | associateInfo, err := opts.ToAssociateMap() |
| 165 | if err != nil { |
| 166 | res.Err = err |
| 167 | return res |
| 168 | } |
| 169 | |
| 170 | removeFloatingIp := make(map[string]interface{}) |
| 171 | removeFloatingIp["address"] = associateInfo["floatingIp"].(string) |
| 172 | reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp} |
| 173 | |
| 174 | serverId := associateInfo["serverId"].(string) |
| 175 | |
| 176 | _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil) |
| 177 | return res |
| 178 | } |