Skip to content

Commit

Permalink
Add networking port extra DHCP opts Update call
Browse files Browse the repository at this point in the history
Add an Update request to the networking v2 extra DHCP options extension.
Add unit and acceptance tests.
  • Loading branch information
ozerovandrei committed Mar 5, 2018
1 parent e2b90d6 commit 3289c0e
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
24 changes: 24 additions & 0 deletions acceptance/openstack/networking/v2/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gophercloud/gophercloud/acceptance/clients"
extensions "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/extradhcpopts"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsecurity"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
)
Expand Down Expand Up @@ -417,4 +418,27 @@ func TestPortsWithDHCPOptsCRUD(t *testing.T) {
defer DeletePort(t, client, port.ID)

tools.PrintResource(t, port)

// Update the port with extra DHCP options.
newPortName := tools.RandomString("TESTACC-", 8)
portUpdateOpts := ports.UpdateOpts{
Name: newPortName,
}
updateOpts := extradhcpopts.UpdateOptsExt{
UpdateOptsBuilder: portUpdateOpts,
ExtraDHCPOpts: []extradhcpopts.ExtraDHCPOpts{
{
OptName: "test_option_2",
OptValue: "test_value_2",
},
},
}

newPort := &PortWithDHCPOpts{}
err = ports.Update(client, port.ID, updateOpts).ExtractInto(newPort)
if err != nil {
t.Fatalf("Could not update port: %v", err)
}

tools.PrintResource(t, newPort)
}
28 changes: 28 additions & 0 deletions openstack/networking/v2/extensions/extradhcpopts/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,33 @@ Example to Create a Port with DHCP opts
if err != nil {
panic(err)
}
Example to Update a Port with DHCP opts
portUpdateOpts := ports.UpdateOpts{
Name: "updated-dhcp-conf-port",
FixedIPs: []ports.IP{
{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
},
}
updateOpts := extradhcpopts.UpdateOptsExt{
UpdateOptsBuilder: portUpdateOpts,
ExtraDHCPOpts: []extradhcpopts.ExtraDHCPOpts{
{
OptName: "optionB",
OptValue: "valueB",
},
},
}
var s struct {
ports.Port
extradhcpopts.ExtraDHCPOptsExt
}
portID := "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2"
err := ports.Update(networkClient, portID, updateOpts).ExtractInto(&s)
if err != nil {
panic(err)
}
*/
package extradhcpopts
35 changes: 35 additions & 0 deletions openstack/networking/v2/extensions/extradhcpopts/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@ func (opts CreateOptsExt) ToPortCreateMap() (map[string]interface{}, error) {

return base, nil
}

// UpdateOptsExt adds port DHCP options to the base ports.UpdateOpts
type UpdateOptsExt struct {
// UpdateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Update operation in this package.
ports.UpdateOptsBuilder

// ExtraDHCPOpts field is a set of DHCP options for a single port.
ExtraDHCPOpts []ExtraDHCPOpts `json:"extra_dhcp_opts,omitempty"`
}

// ToPortUpdateMap casts an UpdateOpts struct to a map.
func (opts UpdateOptsExt) ToPortUpdateMap() (map[string]interface{}, error) {
base, err := opts.UpdateOptsBuilder.ToPortUpdateMap()
if err != nil {
return nil, err
}

port := base["port"].(map[string]interface{})

// Convert opts.ExtraDHCPOpts to a slice of maps.
if opts.ExtraDHCPOpts != nil {
extraDHCPOpts := make([]map[string]interface{}, len(opts.ExtraDHCPOpts))
for i, opt := range opts.ExtraDHCPOpts {
extraDHCPOptMap, err := opt.ToMap()
if err != nil {
return nil, err
}
extraDHCPOpts[i] = extraDHCPOptMap
}
port["extra_dhcp_opts"] = extraDHCPOpts
}

return base, nil
}
51 changes: 51 additions & 0 deletions openstack/networking/v2/ports/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,54 @@ const CreateWithDHCPOptsResponse = `
}
}
`

// UpdateWithDHCPOptsRequest represents a raw port update request with extra DHCP options.
const UpdateWithDHCPOptsRequest = `
{
"port": {
"name": "updated-port-with-dhcp-opts",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.3"
}
],
"extra_dhcp_opts": [
{
"opt_name": "option2",
"opt_value": "value2"
}
]
}
}
`

// UpdateWithDHCPOptsResponse represents a raw port update response with extra DHCP options.
const UpdateWithDHCPOptsResponse = `
{
"port": {
"status": "DOWN",
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
"extra_dhcp_opts": [
{
"opt_name": "option2",
"opt_value": "value2",
"ip_version": 4
}
],
"admin_state_up": true,
"name": "updated-port-with-dhcp-opts",
"device_owner": "",
"mac_address": "fa:16:3e:c9:cb:f0",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.3"
}
],
"id": "65c0ee9f-d634-4522-8954-51021b570b0d",
"device_id": ""
}
}
`
61 changes: 61 additions & 0 deletions openstack/networking/v2/ports/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,64 @@ func TestCreateWithDHCPOpts(t *testing.T) {
th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
th.AssertEquals(t, s.DeviceID, "")
}

func TestUpdateWithDHCPOpts(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
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, UpdateWithDHCPOptsRequest)

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

fmt.Fprintf(w, UpdateWithDHCPOptsResponse)
})

portUpdateOpts := ports.UpdateOpts{
Name: "updated-port-with-dhcp-opts",
FixedIPs: []ports.IP{
{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
},
}

updateOpts := extradhcpopts.UpdateOptsExt{
UpdateOptsBuilder: portUpdateOpts,
ExtraDHCPOpts: []extradhcpopts.ExtraDHCPOpts{
{
OptName: "option2",
OptValue: "value2",
},
},
}

var s struct {
ports.Port
extradhcpopts.ExtraDHCPOptsExt
}

err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&s)
th.AssertNoErr(t, err)

th.AssertEquals(t, s.Status, "DOWN")
th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
th.AssertDeepEquals(t, s.ExtraDHCPOptsExt, extradhcpopts.ExtraDHCPOptsExt{
ExtraDHCPOpts: []extradhcpopts.ExtraDHCPOpts{
{OptName: "option2", OptValue: "value2", IPVersion: 4},
},
})
th.AssertEquals(t, s.AdminStateUp, true)
th.AssertEquals(t, s.Name, "updated-port-with-dhcp-opts")
th.AssertEquals(t, s.DeviceOwner, "")
th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0")
th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
})
th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
th.AssertEquals(t, s.DeviceID, "")
}

0 comments on commit 3289c0e

Please sign in to comment.