Skip to content

Commit

Permalink
support alerts api
Browse files Browse the repository at this point in the history
  • Loading branch information
stanaka committed Nov 19, 2015
1 parent 21535b0 commit 03dc5f3
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
86 changes: 86 additions & 0 deletions alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package mackerel

import (
"encoding/json"
"fmt"
"net/http"
)

/*
{
"alerts": [
{
"id": "2wpLU5fBXbG",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1445399342,
"hostId": "2vJ965ygiXf"
},
{
"id": "2ust8jNxFH3",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1441939801,
"hostId": "2tFrtykgMib"
}
]
}
*/

// Alert information
type Alert struct {
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
MonitorID string `json:"monitorId,omitempty"`
Type string `json:"type,omitempty"`
HostID string `json:"hostId,omitempty"`
Value uint64 `json:"value,omitempty"`
Message string `json:"message,omitempty"`
Reason string `json:"reason,omitempty"`
OpenedAt uint64 `json:"openedAt,omitempty"`
ClosedAt uint64 `json:"closedAt,omitempty"`
}

// FindAlerts find monitors
func (c *Client) FindAlerts() ([]*Alert, error) {
req, err := http.NewRequest("GET", c.urlFor("/api/v0/alerts").String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data struct {
Alerts []*(Alert) `json:"alerts"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return data.Alerts, err
}

// CloseAlert update monitor
func (c *Client) CloseAlert(alertID string, reason string) (*Alert, error) {
var reqBody struct {
Reason string `json:"reason"`
}
reqBody.Reason = reason
resp, err := c.PostJSON(fmt.Sprintf("/api/v0/alerts/%s/close", alertID), &reqBody)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data Alert
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return &data, nil
}
65 changes: 65 additions & 0 deletions alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mackerel

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

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

respJSON, _ := json.Marshal(map[string][]map[string]interface{}{
"alerts": []map[string]interface{}{
map[string]interface{}{
"id": "2wpLU5fBXbG",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1445399342,
"hostId": "2vJ965ygiXf",
},
map[string]interface{}{
"id": "2ust8jNxFH3",
"status": "CRITICAL",
"monitorId": "2cYjfibBkaj",
"type": "connectivity",
"openedAt": 1441939801,
"hostId": "2tFrtykgMib",
},
},
})

res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

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

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

if alerts[0].Type != "connectivity" {
t.Error("request sends json including type but: ", alerts[0])
}

if alerts[1].Type != "connectivity" {
t.Error("request sends json including type but: ", alerts[1])
}

if alerts[1].Status != "CRITICAL" {
t.Error("request sends json including status but: ", alerts[1])
}

if alerts[1].OpenedAt != 1441939801 {
t.Error("request sends json including openedAt but: ", alerts[1])
}
}

0 comments on commit 03dc5f3

Please sign in to comment.