Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | package floatingip |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // List returns a Pager that allows you to iterate over a collection of FloatingIPs. |
| 11 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 12 | return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { |
| 13 | return FloatingIPsPage{pagination.SinglePageBase(r)} |
| 14 | }) |
| 15 | } |
| 16 | |
| 17 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 18 | // CreateOpts struct in this package does. |
| 19 | type CreateOptsBuilder interface { |
| 20 | ToFloatingIPCreateMap() (map[string]interface{}, error) |
| 21 | } |
| 22 | |
| 23 | // CreateOpts specifies a Floating IP allocation request |
| 24 | type CreateOpts struct { |
| 25 | // Pool is the pool of floating IPs to allocate one from |
| 26 | Pool string |
| 27 | } |
| 28 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 29 | // AssociateOpts specifies the required information to associate or disassociate a floating IP to an instance |
| 30 | type AssociateOpts struct { |
| 31 | // ServerID is the UUID of the server |
| 32 | ServerID string |
| 33 | |
| 34 | // FixedIP is an optional fixed IP address of the server |
| 35 | FixedIP string |
| 36 | |
| 37 | // FloatingIP is the floating IP to associate with an instance |
| 38 | FloatingIP string |
| 39 | } |
| 40 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 41 | // ToFloatingIPCreateMap constructs a request body from CreateOpts. |
| 42 | func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) { |
| 43 | if opts.Pool == "" { |
| 44 | return nil, errors.New("Missing field required for floating IP creation: Pool") |
| 45 | } |
| 46 | |
| 47 | return map[string]interface{}{"pool": opts.Pool}, nil |
| 48 | } |
| 49 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 50 | // ToAssociateMap constructs a request body from AssociateOpts. |
| 51 | func (opts AssociateOpts) ToAssociateMap() (map[string]interface{}, error) { |
| 52 | if opts.ServerID == "" { |
| 53 | return nil, errors.New("Required field missing for floating IP association: ServerID") |
| 54 | } |
| 55 | |
| 56 | if opts.FloatingIP == "" { |
| 57 | return nil, errors.New("Required field missing for floating IP association: FloatingIP") |
| 58 | } |
| 59 | |
| 60 | associateInfo := map[string]interface{}{ |
| 61 | "serverId": opts.ServerID, |
| 62 | "floatingIp": opts.FloatingIP, |
| 63 | "fixedIp": opts.FixedIP, |
| 64 | } |
| 65 | |
| 66 | return associateInfo, nil |
| 67 | |
| 68 | } |
| 69 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 70 | // Create requests the creation of a new floating IP |
| 71 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 72 | var res CreateResult |
| 73 | |
| 74 | reqBody, err := opts.ToFloatingIPCreateMap() |
| 75 | if err != nil { |
| 76 | res.Err = err |
| 77 | return res |
| 78 | } |
| 79 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 80 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 81 | OkCodes: []int{200}, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 82 | }) |
| 83 | return res |
| 84 | } |
| 85 | |
| 86 | // Get returns data about a previously created FloatingIP. |
| 87 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 88 | var res GetResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 89 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 90 | return res |
| 91 | } |
| 92 | |
| 93 | // Delete requests the deletion of a previous allocated FloatingIP. |
| 94 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 95 | var res DeleteResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 96 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 97 | return res |
| 98 | } |
| 99 | |
| 100 | // association / disassociation |
| 101 | |
| 102 | // Associate pairs an allocated floating IP with an instance |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame^] | 103 | // Deprecated. Use AssociateInstance. |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 104 | func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult { |
| 105 | var res AssociateResult |
| 106 | |
| 107 | addFloatingIp := make(map[string]interface{}) |
| 108 | addFloatingIp["address"] = fip |
| 109 | reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp} |
| 110 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 111 | _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 112 | return res |
| 113 | } |
| 114 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame^] | 115 | // AssociateInstance pairs an allocated floating IP with an instance. |
| 116 | func AssociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) AssociateResult { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 117 | var res AssociateResult |
| 118 | |
| 119 | associateInfo, err := opts.ToAssociateMap() |
| 120 | if err != nil { |
| 121 | res.Err = err |
| 122 | return res |
| 123 | } |
| 124 | |
| 125 | addFloatingIp := make(map[string]interface{}) |
| 126 | addFloatingIp["address"] = associateInfo["floatingIp"].(string) |
| 127 | |
| 128 | // fixedIp is not required |
| 129 | if associateInfo["fixedIp"] != "" { |
| 130 | addFloatingIp["fixed_address"] = associateInfo["fixedIp"].(string) |
| 131 | } |
| 132 | |
| 133 | serverId := associateInfo["serverId"].(string) |
| 134 | |
| 135 | reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp} |
| 136 | _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil) |
| 137 | return res |
| 138 | } |
| 139 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 140 | // Disassociate decouples an allocated floating IP from an instance |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame^] | 141 | // Deprecated. Use DisassociateInstance. |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 142 | func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult { |
| 143 | var res DisassociateResult |
| 144 | |
| 145 | removeFloatingIp := make(map[string]interface{}) |
| 146 | removeFloatingIp["address"] = fip |
| 147 | reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp} |
| 148 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 149 | _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 150 | return res |
| 151 | } |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 152 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame^] | 153 | // DisassociateInstance decouples an allocated floating IP from an instance |
| 154 | func DisassociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) DisassociateResult { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 155 | var res DisassociateResult |
| 156 | |
| 157 | associateInfo, err := opts.ToAssociateMap() |
| 158 | if err != nil { |
| 159 | res.Err = err |
| 160 | return res |
| 161 | } |
| 162 | |
| 163 | removeFloatingIp := make(map[string]interface{}) |
| 164 | removeFloatingIp["address"] = associateInfo["floatingIp"].(string) |
| 165 | reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp} |
| 166 | |
| 167 | serverId := associateInfo["serverId"].(string) |
| 168 | |
| 169 | _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil) |
| 170 | return res |
| 171 | } |