Skip to content

Commit

Permalink
Gnocchi: add Delete method for Resource types
Browse files Browse the repository at this point in the history
Implement Delete method in the resourcetypes package.
Add unit and acceptance tests with documentation.
  • Loading branch information
ozerovandrei committed Jul 21, 2018
1 parent f45d7d7 commit a31b9a8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
13 changes: 13 additions & 0 deletions acceptance/gnocchi/metric/v1/resourcetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ func CreateResourceType(t *testing.T, client *gophercloud.ServiceClient) (*resou
t.Logf("Successfully created the Gnocchi resource type.")
return resourceType, nil
}

// DeleteResourceType deletes a Gnocchi resource type with the specified name.
// A fatal error will occur if the delete was not successful.
func DeleteResourceType(t *testing.T, client *gophercloud.ServiceClient, resourceTypeName string) {
t.Logf("Attempting to delete the Gnocchi resource type: %s", resourceTypeName)

err := resourcetypes.Delete(client, resourceTypeName).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete the Gnocchi resource type %s: %v", resourceTypeName, err)
}

t.Logf("Deleted the Gnocchi resource type: %s", resourceTypeName)
}
2 changes: 1 addition & 1 deletion acceptance/gnocchi/metric/v1/resourcetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestResourceTypesCRUD(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create a Gnocchi resource type: %v", err)
}
// defer DeleteResourceType(t, client, resourceType.Name)
defer DeleteResourceType(t, client, resourceType.Name)

tools.PrintResource(t, resourceType)

Expand Down
7 changes: 7 additions & 0 deletions gnocchi/metric/v1/resourcetypes/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,12 @@ Example of Updating a resource type
if err != nil {
panic(err)
}
Example of Deleting a resource type
err := resourcetypes.Delete(gnocchiClient, resourceType).ExtractErr()
if err != nil {
panic(err)
}
*/
package resourcetypes
11 changes: 11 additions & 0 deletions gnocchi/metric/v1/resourcetypes/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,14 @@ func Update(client *gophercloud.ServiceClient, resourceTypeName string, opts Upd
})
return
}

// Delete accepts a human-readable name and deletes the Gnocchi resource type associated with it.
func Delete(c *gophercloud.ServiceClient, resourceTypeName string) (r DeleteResult) {
requestOpts := &gophercloud.RequestOpts{
MoreHeaders: map[string]string{
"Accept": "application/json, */*",
},
}
_, r.Err = c.Delete(deleteURL(c, resourceTypeName), requestOpts)
return
}
6 changes: 6 additions & 0 deletions gnocchi/metric/v1/resourcetypes/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ 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 {
gophercloud.ErrResult
}

// ResourceType represents custom Gnocchi resource type.
type ResourceType struct {
// Attributes is a collection of keys and values of different resource types.
Expand Down
14 changes: 14 additions & 0 deletions gnocchi/metric/v1/resourcetypes/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,17 @@ func TestUpdate(t *testing.T) {
},
})
}

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

th.Mux.HandleFunc("/v1/resource_type/compute_instance_network", 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 := resourcetypes.Delete(fake.ServiceClient(), "compute_instance_network")
th.AssertNoErr(t, res.Err)
}
4 changes: 4 additions & 0 deletions gnocchi/metric/v1/resourcetypes/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func createURL(c *gophercloud.ServiceClient) string {
func updateURL(c *gophercloud.ServiceClient, resourceTypeName string) string {
return resourceURL(c, resourceTypeName)
}

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

0 comments on commit a31b9a8

Please sign in to comment.