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

Update port forwarding #1703

Merged
merged 5 commits into from
Sep 17, 2019
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 @@ -59,6 +59,18 @@ func TestLayer3PortForwardingsCreateDelete(t *testing.T) {
newPf, err := portforwarding.Get(client, fip.ID, pf.ID).Extract()
th.AssertNoErr(t, err)

updateOpts := portforwarding.UpdateOpts{
Protocol: "udp",
InternalPort: 30,
ExternalPort: 678,
}

_, err = portforwarding.Update(client, fip.ID, newPf.ID, updateOpts).Extract()
th.AssertNoErr(t, err)

newPf, err = portforwarding.Get(client, fip.ID, pf.ID).Extract()
th.AssertNoErr(t, err)

allPages, err := portforwarding.List(client, portforwarding.ListOpts{}, fip.ID).AllPages()
th.AssertNoErr(t, err)

Expand Down
17 changes: 16 additions & 1 deletion openstack/networking/v2/extensions/layer3/portforwarding/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,26 @@ Example to Create a Port Forwarding for a floating IP
panic(err)
}

Example to Update a Port Forwarding

updateOpts := portforwarding.UpdateOpts{
Protocol: "udp",
InternalPort: 30,
ExternalPort: 678,
}
fipID := "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
pfID := "725ade3c-9760-4880-8080-8fc2dbab9acc"

pf, err := portforwarding.Update(client, fipID, pfID, updateOpts).Extract()
if err != nil {
panic(err)
}

Example to Delete a Port forwarding

fipID := "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
pfID := "725ade3c-9760-4880-8080-8fc2dbab9acc"
err := floatingips.Delete(networkClient, fipID, pfID).ExtractErr()
err := portforwarding.Delete(networkClient, fipID, pfID).ExtractErr()
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,45 @@ func Create(c *gophercloud.ServiceClient, floatingIpId string, opts CreateOptsBu
return
}

// UpdateOpts contains the values used when updating a port forwarding resource.
type UpdateOpts struct {
InternalPortID string `json:"internal_port_id,omitempty"`
InternalIPAddress string `json:"internal_ip_address,omitempty"`
InternalPort int `json:"internal_port,omitempty"`
ExternalPort int `json:"external_port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}

// ToPortForwardingUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder
// interface
func (opts UpdateOpts) ToPortForwardingUpdateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "port_forwarding")
if err != nil {
return nil, err
}

return b, nil
}

// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToPortForwardingUpdateMap() (map[string]interface{}, error)
}

// Update allows port forwarding resources to be updated.
func Update(c *gophercloud.ServiceClient, fipID string, pfID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToPortForwardingUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(singlePortForwardingUrl(c, fipID, pfID), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}

// Delete will permanently delete a particular port forwarding for a given floating ID.
func Delete(c *gophercloud.ServiceClient, floatingIpId string, pfId string) (r DeleteResult) {
_, r.Err = c.Delete(singlePortForwardingUrl(c, floatingIpId, pfId), nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type GetResult struct {
commonResult
}

// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a PortForwarding.
type UpdateResult struct {
commonResult
}

// DeleteResult represents the result of a delete operation. Call its
// ExtractErr method to determine if the request succeeded or failed.
type DeleteResult struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,64 @@ func TestDelete(t *testing.T) {
res := portforwarding.Delete(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", "725ade3c-9760-4880-8080-8fc2dbab9acc")
th.AssertNoErr(t, res.Err)
}

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

th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7/port_forwardings/725ade3c-9760-4880-8080-8fc2dbab9acc", 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, `
{
"port_forwarding": {
"protocol": "udp",
"internal_port": 37,
"internal_port_id": "99889dc2-19a7-4edb-b9d0-d2ace8d1e144",
"external_port": 1960
}
}
`)

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

fmt.Fprintf(w, `
{
"port_forwarding": {
"protocol": "udp",
"internal_ip_address": "10.0.0.14",
"internal_port": 37,
"internal_port_id": "99889dc2-19a7-4edb-b9d0-d2ace8d1e144",
"external_port": 1960,
"id": "725ade3c-9760-4880-8080-8fc2dbab9acc"
}
}
`)
})

updatedProtocol := "udp"
updatedInternalPort := 37
updatedInternalPortID := "99889dc2-19a7-4edb-b9d0-d2ace8d1e144"
updatedExternalPort := 1960
options := portforwarding.UpdateOpts{
Protocol: updatedProtocol,
InternalPort: updatedInternalPort,
InternalPortID: updatedInternalPortID,
ExternalPort: updatedExternalPort,
}

actual, err := portforwarding.Update(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", "725ade3c-9760-4880-8080-8fc2dbab9acc", options).Extract()
th.AssertNoErr(t, err)
expected := portforwarding.PortForwarding{
Protocol: "udp",
InternalIPAddress: "10.0.0.14",
InternalPort: 37,
ID: "725ade3c-9760-4880-8080-8fc2dbab9acc",
InternalPortID: "99889dc2-19a7-4edb-b9d0-d2ace8d1e144",
ExternalPort: 1960,
}
th.AssertDeepEquals(t, expected, *actual)
}