Skip to content

Commit

Permalink
Network v2: RBAC-Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
PriyankaJ77 committed Feb 20, 2018
1 parent 1858d1f commit 3e400a2
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ func CreateRBACPolicy(t *testing.T, client *gophercloud.ServiceClient, tenantID,
t.Logf("Successfully created rbac_policy")
return rbacPolicy, nil
}

// DeleteRBACPolicy will delete a rbac-policy with a specified ID. A fatal error will
// occur if the delete was not successful. This works best when used as a
// deferred function.
func DeleteRBACPolicy(t *testing.T, client *gophercloud.ServiceClient, rbacPolicyID string) {
t.Logf("Trying to delete rbac_policy: %s", rbacPolicyID)

err := rbacpolicies.Delete(client, rbacPolicyID).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete rbac_policy %s: %v", rbacPolicyID, err)
}

t.Logf("Deleted rbac_policy: %s", rbacPolicyID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestRBACPolicyCreate(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create rbac-policy: %v", err)
}
defer DeleteRBACPolicy(t, client, rbacPolicy.ID)

tools.PrintResource(t, rbacPolicy)
}
8 changes: 8 additions & 0 deletions openstack/networking/v2/extensions/rbacpolicies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,13 @@ Example to Create a RBAC Policy
panic(err)
}
Example to Delete a RBAC Policy
rbacPolicyID := "94fe107f-da78-4d92-a9d7-5611b06dad8d"
err := rbacpolicies.Delete(rbacClient, rbacPolicyID).ExtractErr()
if err != nil {
panic(err)
}
*/
package rbacpolicies
6 changes: 6 additions & 0 deletions openstack/networking/v2/extensions/rbacpolicies/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResul
_, r.Err = c.Post(createURL(c), b, &r.Body, nil)
return
}

// Delete accepts a unique ID and deletes the rbac-policy associated with it.
func Delete(c *gophercloud.ServiceClient, rbacPolicyID string) (r DeleteResult) {
_, r.Err = c.Delete(deleteURL(c, rbacPolicyID), nil)
return
}
6 changes: 6 additions & 0 deletions openstack/networking/v2/extensions/rbacpolicies/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type CreateResult 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
}

// RBACPolicy represents a RBAC policy.
type RBACPolicy struct {
// UUID of the RBAC policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ func TestCreate(t *testing.T) {

th.AssertDeepEquals(t, &rbacPolicy1, rbacResult)
}

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

th.Mux.HandleFunc("/v2.0/rbac-policies/71d55b18-d2f8-4c76-a5e6-e0a3dd114361", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})

res := rbacpolicies.Delete(fake.ServiceClient(), "71d55b18-d2f8-4c76-a5e6-e0a3dd114361").ExtractErr()
th.AssertNoErr(t, res)
}
8 changes: 8 additions & 0 deletions openstack/networking/v2/extensions/rbacpolicies/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ package rbacpolicies

import "github.com/gophercloud/gophercloud"

func resourceURL(c *gophercloud.ServiceClient, id string) string {
return c.ServiceURL("rbac-policies", id)
}

func rootURL(c *gophercloud.ServiceClient) string {
return c.ServiceURL("rbac-policies")
}

func createURL(c *gophercloud.ServiceClient) string {
return rootURL(c)
}

func deleteURL(c *gophercloud.ServiceClient, id string) string {
return resourceURL(c, id)
}

0 comments on commit 3e400a2

Please sign in to comment.