Adding create floatingip operation
diff --git a/openstack/networking/v2/extensions/layer3/floatingips/doc.go b/openstack/networking/v2/extensions/layer3/floatingips/doc.go
new file mode 100644
index 0000000..6648963
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/floatingips/doc.go
@@ -0,0 +1 @@
+package floatingips
diff --git a/openstack/networking/v2/extensions/layer3/floatingips/requests.go b/openstack/networking/v2/extensions/layer3/floatingips/requests.go
new file mode 100644
index 0000000..5b60710
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/floatingips/requests.go
@@ -0,0 +1,65 @@
+package floatingips
+
+import (
+ "fmt"
+
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+)
+
+type CreateOpts struct {
+ FloatingNetworkID string
+ PortID string
+ FixedIP string
+ TenantID string
+}
+
+var (
+ errFloatingNetworkIDRequired = fmt.Errorf("A NetworkID is required")
+ errPortIDRequired = fmt.Errorf("A PortID is required")
+)
+
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ var res CreateResult
+
+ // Validate
+ if opts.FloatingNetworkID == "" {
+ res.Err = errFloatingNetworkIDRequired
+ return res
+ }
+ if opts.PortID == "" {
+ res.Err = errPortIDRequired
+ return res
+ }
+
+ // Define structures
+ type floatingIP struct {
+ FloatingNetworkID string `json:"floating_network_id"`
+ PortID string `json:"port_id"`
+ FixedIP string `json:"fixed_ip_address,omitempty"`
+ TenantID string `json:"tenant_id,omitempty"`
+ }
+ type request struct {
+ FloatingIP floatingIP `json:"floatingip"`
+ }
+
+ // Populate request body
+ reqBody := request{FloatingIP: floatingIP{
+ FloatingNetworkID: opts.FloatingNetworkID,
+ PortID: opts.PortID,
+ FixedIP: opts.FixedIP,
+ TenantID: opts.TenantID,
+ }}
+
+ // Send request to API
+ _, err := perigee.Request("POST", rootURL(c), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &res.Resp,
+ OkCodes: []int{201},
+ })
+
+ res.Err = err
+
+ return res
+}
diff --git a/openstack/networking/v2/extensions/layer3/floatingips/requests_test.go b/openstack/networking/v2/extensions/layer3/floatingips/requests_test.go
new file mode 100644
index 0000000..c80e90c
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/floatingips/requests_test.go
@@ -0,0 +1,71 @@
+package floatingips
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+ th "github.com/rackspace/gophercloud/testhelper"
+)
+
+const tokenID = "123"
+
+func serviceClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{
+ Provider: &gophercloud.ProviderClient{TokenID: tokenID},
+ Endpoint: th.Endpoint(),
+ }
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "floatingip": {
+ "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
+ "port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab"
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+
+ fmt.Fprintf(w, `
+{
+ "floatingip": {
+ "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f",
+ "tenant_id": "4969c491a3c74ee4af974e6d800c62de",
+ "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
+ "fixed_ip_address": "10.0.0.3",
+ "floating_ip_address": "",
+ "port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab",
+ "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
+ }
+}
+ `)
+ })
+
+ options := CreateOpts{
+ FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57",
+ PortID: "ce705c24-c1ef-408a-bda3-7bbd946164ab",
+ }
+
+ ip, err := Create(serviceClient(), options).Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID)
+ th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID)
+ th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID)
+ th.AssertEquals(t, "", ip.FloatingIP)
+ th.AssertEquals(t, "ce705c24-c1ef-408a-bda3-7bbd946164ab", ip.PortID)
+ th.AssertEquals(t, "10.0.0.3", ip.FixedIP)
+}
diff --git a/openstack/networking/v2/extensions/layer3/floatingips/results.go b/openstack/networking/v2/extensions/layer3/floatingips/results.go
new file mode 100644
index 0000000..8c61f13
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/floatingips/results.go
@@ -0,0 +1,42 @@
+package floatingips
+
+import (
+ "fmt"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+)
+
+type FloatingIP struct {
+ ID string `json:"id" mapstructure:"id"`
+ FloatingNetworkID string `json:"floating_network_id" mapstructure:"floating_network_id"`
+ FloatingIP string `json:"floating_ip_address" mapstructure:"floating_ip_address"`
+ PortID string `json:"port_id" mapstructure:"port_id"`
+ FixedIP string `json:"fixed_ip_address" mapstructure:"fixed_ip_address"`
+ TenantID string `json:"tenant_id" mapstructure:"tenant_id"`
+}
+
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+func (r commonResult) Extract() (*FloatingIP, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ FloatingIP *FloatingIP `json:"floatingip"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron floating IP: %v", err)
+ }
+
+ return res.FloatingIP, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
diff --git a/openstack/networking/v2/extensions/layer3/floatingips/urls.go b/openstack/networking/v2/extensions/layer3/floatingips/urls.go
new file mode 100644
index 0000000..dbe3f9f
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/floatingips/urls.go
@@ -0,0 +1,16 @@
+package floatingips
+
+import "github.com/rackspace/gophercloud"
+
+const (
+ version = "v2.0"
+ resourcePath = "floatingips"
+)
+
+func rootURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(version, resourcePath)
+}
+
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, resourcePath, id)
+}
diff --git a/openstack/networking/v2/extensions/layer3/routers/urls.go b/openstack/networking/v2/extensions/layer3/routers/urls.go
new file mode 100644
index 0000000..512c8a4
--- /dev/null
+++ b/openstack/networking/v2/extensions/layer3/routers/urls.go
@@ -0,0 +1,24 @@
+package routers
+
+import "github.com/rackspace/gophercloud"
+
+const (
+ version = "v2.0"
+ resourcePath = "routers"
+)
+
+func rootURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(version, resourcePath)
+}
+
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, resourcePath, id)
+}
+
+func addInterfaceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, resourcePath, id, "add_router_interface")
+}
+
+func removeInterfaceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, resourcePath, id, "remove_router_interface")
+}