Added support for os-floating-ips extension
This commit adds support for the os-floating-ips extention. This allows
users to allocate and deallocate floating IPs as well as have instances
associate and disassociate floating IPs in a nova-network based cloud.
diff --git a/openstack/compute/v2/extensions/floatingip/doc.go b/openstack/compute/v2/extensions/floatingip/doc.go
new file mode 100644
index 0000000..f74f58c
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/doc.go
@@ -0,0 +1,3 @@
+// Package floatingip provides the ability to manage floating ips through
+// nova-network
+package floatingip
diff --git a/openstack/compute/v2/extensions/floatingip/fixtures.go b/openstack/compute/v2/extensions/floatingip/fixtures.go
new file mode 100644
index 0000000..26f3299
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/fixtures.go
@@ -0,0 +1,174 @@
+// +build fixtures
+
+package floatingip
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// ListOutput is a sample response to a List call.
+const ListOutput = `
+{
+ "floating_ips": [
+ {
+ "fixed_ip": null,
+ "id": 1,
+ "instance_id": null,
+ "ip": "10.10.10.1",
+ "pool": "nova"
+ },
+ {
+ "fixed_ip": "166.78.185.201",
+ "id": 2,
+ "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "ip": "10.10.10.2",
+ "pool": "nova"
+ }
+ ]
+}
+`
+
+// GetOutput is a sample response to a Get call.
+const GetOutput = `
+{
+ "floating_ip": {
+ "fixed_ip": "166.78.185.201",
+ "id": 2,
+ "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "ip": "10.10.10.2",
+ "pool": "nova"
+ }
+}
+`
+
+// CreateOutput is a sample response to a Post call
+const CreateOutput = `
+{
+ "floating_ip": {
+ "fixed_ip": null,
+ "id": 1,
+ "instance_id": null,
+ "ip": "10.10.10.1",
+ "pool": "nova"
+ }
+}
+`
+
+// FirstFloatingIP is the first result in ListOutput.
+var FirstFloatingIP = FloatingIP{
+ ID: "1",
+ IP: "10.10.10.1",
+ Pool: "nova",
+}
+
+// SecondFloatingIP is the first result in ListOutput.
+var SecondFloatingIP = FloatingIP{
+ FixedIP: "166.78.185.201",
+ ID: "2",
+ InstanceID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ IP: "10.10.10.2",
+ Pool: "nova",
+}
+
+// ExpectedFloatingIPsSlice is the slice of results that should be parsed
+// from ListOutput, in the expected order.
+var ExpectedFloatingIPsSlice = []FloatingIP{FirstFloatingIP, SecondFloatingIP}
+
+// CreatedFloatingIP is the parsed result from CreateOutput.
+var CreatedFloatingIP = FloatingIP{
+ ID: "1",
+ IP: "10.10.10.1",
+ Pool: "nova",
+}
+
+// HandleListSuccessfully configures the test server to respond to a List request.
+func HandleListSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, ListOutput)
+ })
+}
+
+// HandleGetSuccessfully configures the test server to respond to a Get request
+// for an existing floating ip
+func HandleGetSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/os-floating-ips/2", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, GetOutput)
+ })
+}
+
+// HandleCreateSuccessfully configures the test server to respond to a Create request
+// for a new floating ip
+func HandleCreateSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestJSONRequest(t, r, `
+{
+ "pool": "nova"
+}
+`)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, CreateOutput)
+ })
+}
+
+// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
+// an existing floating ip
+func HandleDeleteSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/os-floating-ips/1", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
+
+// HandleAssociateSuccessfully configures the test server to respond to a Post request
+// to associate an allocated floating IP
+func HandleAssociateSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestJSONRequest(t, r, `
+{
+ "addFloatingIp": {
+ "address": "10.10.10.2"
+ }
+}
+`)
+
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
+
+// HandleDisassociateSuccessfully configures the test server to respond to a Post request
+// to disassociate an allocated floating IP
+func HandleDisassociateSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestJSONRequest(t, r, `
+{
+ "removeFloatingIp": {
+ "address": "10.10.10.2"
+ }
+}
+`)
+
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
diff --git a/openstack/compute/v2/extensions/floatingip/requests.go b/openstack/compute/v2/extensions/floatingip/requests.go
new file mode 100644
index 0000000..ab681dc
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/requests.go
@@ -0,0 +1,111 @@
+package floatingip
+
+import (
+ "errors"
+
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// List returns a Pager that allows you to iterate over a collection of FloatingIPs.
+func List(client *gophercloud.ServiceClient) pagination.Pager {
+ return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
+ return FloatingIPsPage{pagination.SinglePageBase(r)}
+ })
+}
+
+// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
+// CreateOpts struct in this package does.
+type CreateOptsBuilder interface {
+ ToFloatingIPCreateMap() (map[string]interface{}, error)
+}
+
+// CreateOpts specifies a Floating IP allocation request
+type CreateOpts struct {
+ // Pool is the pool of floating IPs to allocate one from
+ Pool string
+}
+
+// ToFloatingIPCreateMap constructs a request body from CreateOpts.
+func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
+ if opts.Pool == "" {
+ return nil, errors.New("Missing field required for floating IP creation: Pool")
+ }
+
+ return map[string]interface{}{"pool": opts.Pool}, nil
+}
+
+// Create requests the creation of a new floating IP
+func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToFloatingIPCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: reqBody,
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
+
+// Get returns data about a previously created FloatingIP.
+func Get(client *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
+ _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
+
+// Delete requests the deletion of a previous allocated FloatingIP.
+func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
+ _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ OkCodes: []int{202},
+ })
+ return res
+}
+
+// association / disassociation
+
+// Associate pairs an allocated floating IP with an instance
+func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult {
+ var res AssociateResult
+
+ addFloatingIp := make(map[string]interface{})
+ addFloatingIp["address"] = fip
+ reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
+
+ _, res.Err = perigee.Request("POST", associateURL(client, serverId), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: reqBody,
+ OkCodes: []int{202},
+ })
+ return res
+}
+
+// Disassociate decouples an allocated floating IP from an instance
+func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
+ var res DisassociateResult
+
+ removeFloatingIp := make(map[string]interface{})
+ removeFloatingIp["address"] = fip
+ reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
+
+ _, res.Err = perigee.Request("POST", disassociateURL(client, serverId), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: reqBody,
+ OkCodes: []int{202},
+ })
+ return res
+}
diff --git a/openstack/compute/v2/extensions/floatingip/requests_test.go b/openstack/compute/v2/extensions/floatingip/requests_test.go
new file mode 100644
index 0000000..ed2460e
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/requests_test.go
@@ -0,0 +1,80 @@
+package floatingip
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleListSuccessfully(t)
+
+ count := 0
+ err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractFloatingIPs(page)
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, ExpectedFloatingIPsSlice, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, 1, count)
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleCreateSuccessfully(t)
+
+ actual, err := Create(client.ServiceClient(), CreateOpts{
+ Pool: "nova",
+ }).Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &CreatedFloatingIP, actual)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleGetSuccessfully(t)
+
+ actual, err := Get(client.ServiceClient(), "2").Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &SecondFloatingIP, actual)
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleDeleteSuccessfully(t)
+
+ err := Delete(client.ServiceClient(), "1").ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestAssociate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleAssociateSuccessfully(t)
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+ fip := "10.10.10.2"
+
+ err := Associate(client.ServiceClient(), serverId, fip).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestDisassociate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleDisassociateSuccessfully(t)
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+ fip := "10.10.10.2"
+
+ err := Disassociate(client.ServiceClient(), serverId, fip).ExtractErr()
+ th.AssertNoErr(t, err)
+}
diff --git a/openstack/compute/v2/extensions/floatingip/results.go b/openstack/compute/v2/extensions/floatingip/results.go
new file mode 100644
index 0000000..be77fa1
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/results.go
@@ -0,0 +1,99 @@
+package floatingip
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// A FloatingIP is an IP that can be associated with an instance
+type FloatingIP struct {
+ // ID is a unique ID of the Floating IP
+ ID string `mapstructure:"id"`
+
+ // FixedIP is the IP of the instance related to the Floating IP
+ FixedIP string `mapstructure:"fixed_ip,omitempty"`
+
+ // InstanceID is the ID of the instance that is using the Floating IP
+ InstanceID string `mapstructure:"instance_id"`
+
+ // IP is the actual Floating IP
+ IP string `mapstructure:"ip"`
+
+ // Pool is the pool of floating IPs that this floating IP belongs to
+ Pool string `mapstructure:"pool"`
+}
+
+// FloatingIPsPage stores a single, only page of FloatingIPs
+// results from a List call.
+type FloatingIPsPage struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty determines whether or not a FloatingIPsPage is empty.
+func (page FloatingIPsPage) IsEmpty() (bool, error) {
+ va, err := ExtractFloatingIPs(page)
+ return len(va) == 0, err
+}
+
+// ExtractFloatingIPs interprets a page of results as a slice of
+// FloatingIPs.
+func ExtractFloatingIPs(page pagination.Page) ([]FloatingIP, error) {
+ casted := page.(FloatingIPsPage).Body
+ var response struct {
+ FloatingIPs []FloatingIP `mapstructure:"floating_ips"`
+ }
+
+ err := mapstructure.WeakDecode(casted, &response)
+
+ return response.FloatingIPs, err
+}
+
+type FloatingIPResult struct {
+ gophercloud.Result
+}
+
+// Extract is a method that attempts to interpret any FloatingIP resource
+// response as a FloatingIP struct.
+func (r FloatingIPResult) Extract() (*FloatingIP, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ FloatingIP *FloatingIP `json:"floating_ip" mapstructure:"floating_ip"`
+ }
+
+ err := mapstructure.WeakDecode(r.Body, &res)
+ return res.FloatingIP, err
+}
+
+// CreateResult is the response from a Create operation. Call its Extract method to interpret it
+// as a FloatingIP.
+type CreateResult struct {
+ FloatingIPResult
+}
+
+// GetResult is the response from a Get operation. Call its Extract method to interpret it
+// as a FloatingIP.
+type GetResult struct {
+ FloatingIPResult
+}
+
+// DeleteResult is the response from a Delete operation. Call its Extract method to determine if
+// the call succeeded or failed.
+type DeleteResult struct {
+ gophercloud.ErrResult
+}
+
+// AssociateResult is the response from a Delete operation. Call its Extract method to determine if
+// the call succeeded or failed.
+type AssociateResult struct {
+ gophercloud.ErrResult
+}
+
+// DisassociateResult is the response from a Delete operation. Call its Extract method to determine if
+// the call succeeded or failed.
+type DisassociateResult struct {
+ gophercloud.ErrResult
+}
diff --git a/openstack/compute/v2/extensions/floatingip/urls.go b/openstack/compute/v2/extensions/floatingip/urls.go
new file mode 100644
index 0000000..54198f8
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/urls.go
@@ -0,0 +1,37 @@
+package floatingip
+
+import "github.com/rackspace/gophercloud"
+
+const resourcePath = "os-floating-ips"
+
+func resourceURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(resourcePath)
+}
+
+func listURL(c *gophercloud.ServiceClient) string {
+ return resourceURL(c)
+}
+
+func createURL(c *gophercloud.ServiceClient) string {
+ return resourceURL(c)
+}
+
+func getURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(resourcePath, id)
+}
+
+func deleteURL(c *gophercloud.ServiceClient, id string) string {
+ return getURL(c, id)
+}
+
+func serverURL(c *gophercloud.ServiceClient, serverId string) string {
+ return c.ServiceURL("servers/" + serverId + "/action")
+}
+
+func associateURL(c *gophercloud.ServiceClient, serverId string) string {
+ return serverURL(c, serverId)
+}
+
+func disassociateURL(c *gophercloud.ServiceClient, serverId string) string {
+ return serverURL(c, serverId)
+}
diff --git a/openstack/compute/v2/extensions/floatingip/urls_test.go b/openstack/compute/v2/extensions/floatingip/urls_test.go
new file mode 100644
index 0000000..f73d6fb
--- /dev/null
+++ b/openstack/compute/v2/extensions/floatingip/urls_test.go
@@ -0,0 +1,60 @@
+package floatingip
+
+import (
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestListURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+
+ th.CheckEquals(t, c.Endpoint+"os-floating-ips", listURL(c))
+}
+
+func TestCreateURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+
+ th.CheckEquals(t, c.Endpoint+"os-floating-ips", createURL(c))
+}
+
+func TestGetURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ id := "1"
+
+ th.CheckEquals(t, c.Endpoint+"os-floating-ips/"+id, getURL(c, id))
+}
+
+func TestDeleteURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ id := "1"
+
+ th.CheckEquals(t, c.Endpoint+"os-floating-ips/"+id, deleteURL(c, id))
+}
+
+func TestAssociateURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/action", associateURL(c, serverId))
+}
+
+func TestDisassociateURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/action", disassociateURL(c, serverId))
+}