Skip to content

Commit

Permalink
LBaaS v2 l7 policy support [Part 4]: delete l7policy
Browse files Browse the repository at this point in the history
  • Loading branch information
lingxiankong committed Mar 19, 2018
1 parent 83c00fc commit 95b12d8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions acceptance/openstack/networking/v2/extensions/lbaas_v2/lbaas_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ func CreateL7Policy(t *testing.T, client *gophercloud.ServiceClient, listener *l
return policy, nil
}

// DeleteL7Policy will delete a specified l7 policy. A fatal error will occur if
// the l7 policy could not be deleted. This works best when used as a deferred
// function.
func DeleteL7Policy(t *testing.T, client *gophercloud.ServiceClient, lbID, policyID string) {
t.Logf("Attempting to delete l7 policy %s", policyID)

if err := l7policies.Delete(client, policyID).ExtractErr(); err != nil {
t.Fatalf("Unable to delete l7 policy: %v", err)
}

if err := WaitForLoadBalancerState(client, lbID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
t.Fatalf("Timed out waiting for loadbalancer to become active")
}

t.Logf("Successfully deleted l7 policy %s", policyID)
}

// DeleteListener will delete a specified listener. A fatal error will occur if
// the listener could not be deleted. This works best when used as a deferred
// function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestLoadbalancersCRUD(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create l7 policy: %v", err)
}
defer DeleteL7Policy(t, client, policy.ID)

newPolicy, err := l7policies.Get(client, policy.ID).Extract()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions openstack/networking/v2/extensions/lbaas_v2/l7policies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@ Example to Get a L7Policy
if err != nil {
panic(err)
}
Example to Delete a L7Policy
l7policyID := "d67d56a6-4a86-4688-a282-f46444705c64"
err := l7policies.Delete(networkClient, l7policyID).ExtractErr()
if err != nil {
panic(err)
}
*/
package l7policies
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
return
}

// Delete will permanently delete a particular l7policy based on its unique ID.
func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, id), nil)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,9 @@ func ExtractL7Policies(r pagination.Page) ([]L7Policy, error) {
type GetResult 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 {
gophercloud.ErrResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,13 @@ func HandleL7PolicyGetSuccessfully(t *testing.T) {
fmt.Fprintf(w, SingleL7PolicyBody)
})
}

// HandleL7PolicyDeletionSuccessfully sets up the test server to respond to a l7policy deletion request.
func HandleL7PolicyDeletionSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)

w.WriteHeader(http.StatusNoContent)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,12 @@ func TestGetL7Policy(t *testing.T) {

th.CheckDeepEquals(t, L7PolicyToURL, *actual)
}

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

res := l7policies.Delete(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd")
th.AssertNoErr(t, res.Err)
}

0 comments on commit 95b12d8

Please sign in to comment.