Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking v2: Create Floating IP with Subnet #753

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
package layer3

import (
"os"
"testing"

"github.com/gophercloud/gophercloud/acceptance/clients"
networking "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)

func TestLayer3FloatingIPsList(t *testing.T) {
Expand Down Expand Up @@ -98,3 +100,48 @@ func TestLayer3FloatingIPsCreateDelete(t *testing.T) {
t.Fatalf("Unable to disassociate floating IP: %v", err)
}
}

func TestLayer3FloatingIPsCreateDeleteBySubnetID(t *testing.T) {
username := os.Getenv("OS_USERNAME")
if username != "admin" {
t.Skip("must be admin to run this test")
}

client, err := clients.NewNetworkV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}

choices, err := clients.AcceptanceTestChoicesFromEnv()
if err != nil {
t.Fatalf("Unable to get choices: %v", err)
}

listOpts := subnets.ListOpts{
NetworkID: choices.ExternalNetworkID,
}

subnetPages, err := subnets.List(client, listOpts).AllPages()
if err != nil {
t.Fatalf("Unable to list subnets: %v", err)
}

allSubnets, err := subnets.ExtractSubnets(subnetPages)
if err != nil {
t.Fatalf("Unable to extract subnets: %v", err)
}

createOpts := floatingips.CreateOpts{
FloatingNetworkID: choices.ExternalNetworkID,
SubnetID: allSubnets[0].ID,
}

fip, err := floatingips.Create(client, createOpts).Extract()
if err != nil {
t.Fatalf("Unable to create floating IP: %v")
}

tools.PrintResource(t, fip)

DeleteFloatingIP(t, client, fip.ID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type CreateOpts struct {
FloatingIP string `json:"floating_ip_address,omitempty"`
PortID string `json:"port_id,omitempty"`
FixedIP string `json:"fixed_ip_address,omitempty"`
SubnetID string `json:"subnet_id,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,58 @@ func TestCreateEmptyPort(t *testing.T) {
th.AssertEquals(t, "10.0.0.3", ip.FixedIP)
}

func TestCreateWithSubnetID(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", fake.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",
"subnet_id": "37adf01c-24db-467a-b845-7ab1e8216c01"
}
}
`)

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)

fmt.Fprintf(w, `
{
"floatingip": {
"router_id": null,
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"fixed_ip_address": null,
"floating_ip_address": "172.24.4.3",
"port_id": null,
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})

options := floatingips.CreateOpts{
FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57",
SubnetID: "37adf01c-24db-467a-b845-7ab1e8216c01",
}

ip, err := floatingips.Create(fake.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, "172.24.4.3", ip.FloatingIP)
th.AssertEquals(t, "", ip.PortID)
th.AssertEquals(t, "", ip.FixedIP)
}

func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
Expand Down