Skip to content

Commit

Permalink
Merge pull request #765 from ozerovandrei/feature/subnet-create-subne…
Browse files Browse the repository at this point in the history
…tpoolid

Add the SubnetPoolID to the Subnet Create request
  • Loading branch information
jtopjian committed Feb 15, 2018
2 parents bd6e512 + 3efdea1 commit cf2bbaa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
26 changes: 26 additions & 0 deletions acceptance/openstack/networking/v2/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ func CreateSubnetWithNoGateway(t *testing.T, client *gophercloud.ServiceClient,
return subnet, nil
}

// CreateSubnetWithSubnetPool will create a subnet associated with the provided subnetpool on the specified Network ID.
// An error will be returned if the subnet or the subnetpool could not be created.
func CreateSubnetWithSubnetPool(t *testing.T, client *gophercloud.ServiceClient, networkID string, subnetPoolID string) (*subnets.Subnet, error) {
subnetName := tools.RandomString("TESTACC-", 8)
subnetOctet := tools.RandomInt(1, 250)
subnetCIDR := fmt.Sprintf("10.%d.0.0/24", subnetOctet)
createOpts := subnets.CreateOpts{
NetworkID: networkID,
CIDR: subnetCIDR,
IPVersion: 4,
Name: subnetName,
EnableDHCP: gophercloud.Disabled,
SubnetPoolID: subnetPoolID,
}

t.Logf("Attempting to create subnet: %s", subnetName)

subnet, err := subnets.Create(client, createOpts).Extract()
if err != nil {
return subnet, err
}

t.Logf("Successfully created subnet.")
return subnet, nil
}

// DeleteNetwork will delete a network with a specified ID. A fatal error will
// occur if the delete was not successful. This works best when used as a
// deferred function.
Expand Down
35 changes: 35 additions & 0 deletions acceptance/openstack/networking/v2/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gophercloud/gophercloud/acceptance/clients"
subnetpools "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions/subnetpools"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)
Expand Down Expand Up @@ -156,3 +157,37 @@ func TestSubnetsNoGateway(t *testing.T) {
t.Fatalf("Gateway was not updated correctly")
}
}

func TestSubnetsWithSubnetPool(t *testing.T) {
client, err := clients.NewNetworkV2Client()
if err != nil {
t.Fatalf("Unable to create a network client: %v", err)
}

// Create Network
network, err := CreateNetwork(t, client)
if err != nil {
t.Fatalf("Unable to create network: %v", err)
}
defer DeleteNetwork(t, client, network.ID)

// Create SubnetPool
subnetPool, err := subnetpools.CreateSubnetPool(t, client)
if err != nil {
t.Fatalf("Unable to create subnet pool: %v", err)
}
defer subnetpools.DeleteSubnetPool(t, client, subnetPool.ID)

// Create Subnet
subnet, err := CreateSubnetWithSubnetPool(t, client, network.ID, subnetPool.ID)
if err != nil {
t.Fatalf("Unable to create subnet: %v", err)
}
defer DeleteSubnet(t, client, subnet.ID)

tools.PrintResource(t, subnet)

if subnet.GatewayIP == "" {
t.Fatalf("A subnet pool was not associated.")
}
}
3 changes: 3 additions & 0 deletions openstack/networking/v2/subnets/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ type CreateOpts struct {
// The IPv6 router advertisement specifies whether the networking service
// should transmit ICMPv6 packets.
IPv6RAMode string `json:"ipv6_ra_mode,omitempty"`

// SubnetPoolID is the id of the subnet pool that subnet should be associated to.
SubnetPoolID string `json:"subnetpool_id,omitempty"`
}

// ToSubnetCreateMap builds a request body from CreateOpts.
Expand Down
6 changes: 4 additions & 2 deletions openstack/networking/v2/subnets/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ const SubnetCreateRequest = `
"end": "192.168.199.254"
}
],
"host_routes": [{"destination":"","nexthop": "bar"}]
"host_routes": [{"destination":"","nexthop": "bar"}],
"subnetpool_id": "b80340c7-9960-4f67-a99c-02501656284b"
}
}
`
Expand All @@ -222,7 +223,8 @@ const SubnetCreateResult = `
"ip_version": 4,
"gateway_ip": "192.168.199.1",
"cidr": "192.168.199.0/24",
"id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
"id": "3b80198d-4f7b-4f77-9ef5-774d54e17126",
"subnetpool_id": "b80340c7-9960-4f67-a99c-02501656284b"
}
}
`
Expand Down
2 changes: 2 additions & 0 deletions openstack/networking/v2/subnets/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func TestCreate(t *testing.T) {
HostRoutes: []subnets.HostRoute{
{NextHop: "bar"},
},
SubnetPoolID: "b80340c7-9960-4f67-a99c-02501656284b",
}
s, err := subnets.Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
Expand All @@ -141,6 +142,7 @@ func TestCreate(t *testing.T) {
th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
th.AssertEquals(t, s.SubnetPoolID, "b80340c7-9960-4f67-a99c-02501656284b")
}

func TestCreateNoGateway(t *testing.T) {
Expand Down

0 comments on commit cf2bbaa

Please sign in to comment.