Skip to content

Commit

Permalink
Add DeleteGraphDef to graph-defs API
Browse files Browse the repository at this point in the history
  • Loading branch information
roodni committed May 28, 2024
1 parent 15657a5 commit 4c058fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions graph_defs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mackerel

import "net/http"

// GraphDefsParam parameters for posting graph definitions
type GraphDefsParam struct {
Name string `json:"name"`
Expand All @@ -20,3 +22,9 @@ func (c *Client) CreateGraphDefs(graphDefs []*GraphDefsParam) error {
_, err := requestPost[any](c, "/api/v0/graph-defs/create", graphDefs)
return err
}

// DeleteGraphDef deletes a graph definition.
func (c *Client) DeleteGraphDef(name string) error {
_, err := requestJSON[any](c, http.MethodDelete, "/api/v0/graph-defs/delete", map[string]string{"name": name})
return err
}
38 changes: 38 additions & 0 deletions graph_defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,41 @@ func TestGraphDefsOmitJSON(t *testing.T) {
t.Errorf("json.Marshal(%#v) = %q; want %q", g, s, want)
}
}

func TestDeleteGraphDef(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-defs/delete" {
t.Error("request URL should be /api/v0/graph-defs/delete but:", req.URL.Path)
}

if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)

var data struct {
Name string `json:"name"`
}

err := json.Unmarshal(body, &data)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}

if data.Name != "mackerel" {
t.Errorf("request sends json including name but: %s", data.Name)
}

respJSON, _ := json.Marshal((map[string]bool{"success": true}))
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.DeleteGraphDef("mackerel")

if err != nil {
t.Error("err should be nil but: ", err)
}
}

0 comments on commit 4c058fe

Please sign in to comment.