|
| 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 | +} |
0 commit comments