Skip to content

Commit 55a8d93

Browse files
eseioanarm
authored andcommitted
Add alertGroup Service to implement alert_groups API endpoints
1 parent 477f370 commit 55a8d93

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

alert_group.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package aapi
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
// AlertGroupService handles requests to the on-call alert_groups endpoint.
9+
//
10+
// https://grafana.com/docs/oncall/latest/oncall-api-reference/alertgroups/
11+
type AlertGroupService struct {
12+
client *Client
13+
url string
14+
}
15+
16+
// NewAlertGroupService creates an AlertGroupService with the defined URL.
17+
func NewAlertGroupService(client *Client) *AlertGroupService {
18+
alertGroupService := AlertGroupService{}
19+
alertGroupService.client = client
20+
alertGroupService.url = "alert_groups"
21+
return &alertGroupService
22+
}
23+
24+
// PaginatedAlertGroupsResponse represents a paginated response from the on-call alerts API.
25+
type PaginatedAlertGroupsResponse struct {
26+
PaginatedResponse
27+
AlertGroups []*AlertGroup `json:"results"`
28+
}
29+
30+
// AlertGroup represents an on-call alert group.
31+
type AlertGroup struct {
32+
ID string `json:"id"`
33+
IntegrationID string `json:"integration_id"`
34+
RouteID string `json:"route_id"`
35+
AlertsCount int `json:"alerts_count"`
36+
State string `json:"state"`
37+
CreatedAt string `json:"created_at"`
38+
ResolvedAt string `json:"resolved_at"`
39+
AcknowledgedAt string `json:"acknowledged_at"`
40+
Title string `json:"title"`
41+
Permalinks map[string]string `json:"permalinks"`
42+
}
43+
44+
// ListAlertGroupOptions represent filter options supported by the on-call alert_groups API.
45+
type ListAlertGroupOptions struct {
46+
ListOptions
47+
AlertGroupID string `url:"alert_group_id,omitempty" json:"alert_group_id,omitempty"`
48+
RouteID string `url:"route_id,omitempty" json:"route_id,omitempty"`
49+
IntegrationID string `url:"integration_id,omitempty" json:"integration_id,omitempty" `
50+
State string `url:"state,omitempty" json:"state,omitempty" `
51+
Name string `url:"name,omitempty" json:"name,omitempty"`
52+
}
53+
54+
// ListAlertGroups fetches all on-call alerts for authorized organization.
55+
//
56+
// https://grafana.com/docs/oncall/latest/oncall-api-reference/alertgroups/
57+
func (service *AlertGroupService) ListAlertGroups(opt *ListAlertGroupOptions) (*PaginatedAlertGroupsResponse, *http.Response, error) {
58+
u := fmt.Sprintf("%s/", service.url)
59+
60+
req, err := service.client.NewRequest("GET", u, opt)
61+
if err != nil {
62+
return nil, nil, err
63+
}
64+
65+
var alertGroups *PaginatedAlertGroupsResponse
66+
resp, err := service.client.Do(req, &alertGroups)
67+
if err != nil {
68+
return nil, resp, err
69+
}
70+
71+
return alertGroups, resp, err
72+
}

alert_group_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package aapi
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
var testAlertGroup = &AlertGroup{
11+
ID: "I68T24C13IFW1",
12+
IntegrationID: "CFRPV98RPR1U8",
13+
RouteID: "RIYGUJXCPFHXY",
14+
AlertsCount: 3,
15+
State: "resolved",
16+
CreatedAt: "2020-05-19T12:37:01.430444Z",
17+
ResolvedAt: "2020-05-19T13:37:01.429805Z",
18+
Title: "Memory above 90% threshold",
19+
Permalinks: map[string]string{
20+
"slack": "https://ghostbusters.slack.com/archives/C1H9RESGA/p135854651500008",
21+
"telegram": "https://t.me/c/5354/1234?thread=1234",
22+
},
23+
}
24+
25+
var testAlertGroupBody = `{
26+
"id": "I68T24C13IFW1",
27+
"integration_id": "CFRPV98RPR1U8",
28+
"route_id": "RIYGUJXCPFHXY",
29+
"alerts_count": 3,
30+
"state": "resolved",
31+
"created_at": "2020-05-19T12:37:01.430444Z",
32+
"resolved_at": "2020-05-19T13:37:01.429805Z",
33+
"acknowledged_at": null,
34+
"title": "Memory above 90% threshold",
35+
"permalinks": {
36+
"slack": "https://ghostbusters.slack.com/archives/C1H9RESGA/p135854651500008",
37+
"telegram": "https://t.me/c/5354/1234?thread=1234"
38+
}
39+
}`
40+
41+
func TestListAlertGroup(t *testing.T) {
42+
mux, server, client := setup(t)
43+
defer teardown(server)
44+
45+
mux.HandleFunc("/api/v1/alert_groups/", func(w http.ResponseWriter, r *http.Request) {
46+
testRequestMethod(t, r, "GET")
47+
fmt.Fprint(w, fmt.Sprintf(`{"count": 1, "next": null, "previous": null, "results": [%s]}`, testAlertGroupBody))
48+
})
49+
50+
options := &ListAlertGroupOptions{
51+
AlertGroupID: "I68T24C13IFW1",
52+
}
53+
54+
alerts, _, err := client.AlertGroups.ListAlertGroups(options)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
want := &PaginatedAlertGroupsResponse{
60+
PaginatedResponse: PaginatedResponse{
61+
Count: 1,
62+
Next: nil,
63+
Previous: nil,
64+
},
65+
AlertGroups: []*AlertGroup{
66+
testAlertGroup,
67+
},
68+
}
69+
70+
if !reflect.DeepEqual(want, alerts) {
71+
fmt.Println(alerts.AlertGroups[0])
72+
fmt.Println(want.AlertGroups[0])
73+
t.Errorf(" returned\n %+v, \nwant\n %+v", alerts, want)
74+
}
75+
}

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Client struct {
3838
UserAgent string
3939
// List of Services. Keep in sync with func newClient
4040
Alerts *AlertService
41+
AlertGroups *AlertGroupService
4142
Integrations *IntegrationService
4243
EscalationChains *EscalationChainService
4344
Escalations *EscalationService
@@ -97,6 +98,7 @@ func newClient(url, grafana_url string) (*Client, error) {
9798

9899
// Create services. Keep in sync with Client struct
99100
c.Alerts = NewAlertService(c)
101+
c.AlertGroups = NewAlertGroupService(c)
100102
c.Integrations = NewIntegrationService(c)
101103
c.EscalationChains = NewEscalationChainService(c)
102104
c.Escalations = NewEscalationService(c)

0 commit comments

Comments
 (0)