-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatuspage.go
More file actions
275 lines (242 loc) · 7.66 KB
/
statuspage.go
File metadata and controls
275 lines (242 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package flashduty
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// ListStatusPages queries status pages, optionally filtering by page IDs
func (c *Client) ListStatusPages(ctx context.Context, pageIDs []int64) ([]StatusPage, error) {
resp, err := c.makeRequest(ctx, "GET", "/status-page/list", nil)
if err != nil {
return nil, fmt.Errorf("failed to list status pages: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleAPIError(c.logger, resp)
}
var result struct {
Error *DutyError `json:"error,omitempty"`
Data *struct {
Items []struct {
PageID int64 `json:"page_id"`
PageName string `json:"name"`
URLName string `json:"url_name,omitempty"`
Description string `json:"description,omitempty"`
Components []struct {
ComponentID string `json:"component_id"`
Name string `json:"name"`
} `json:"components,omitempty"`
} `json:"items"`
} `json:"data,omitempty"`
}
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
if result.Data == nil || len(result.Data.Items) == 0 {
return []StatusPage{}, nil
}
// Build page ID filter set
pageIDSet := make(map[int64]struct{})
for _, id := range pageIDs {
pageIDSet[id] = struct{}{}
}
pages := make([]StatusPage, 0)
for _, item := range result.Data.Items {
if len(pageIDs) > 0 {
if _, ok := pageIDSet[item.PageID]; !ok {
continue
}
}
page := StatusPage{
PageID: item.PageID,
PageName: item.PageName,
Slug: item.URLName,
Description: item.Description,
}
worstStatus := "operational"
if len(item.Components) > 0 {
page.Components = make([]StatusComponent, 0, len(item.Components))
for _, comp := range item.Components {
page.Components = append(page.Components, StatusComponent{
ComponentID: comp.ComponentID,
ComponentName: comp.Name,
Status: "operational",
})
}
}
page.OverallStatus = worstStatus
pages = append(pages, page)
}
return pages, nil
}
// ListStatusChangesInput contains parameters for listing status page changes
type ListStatusChangesInput struct {
PageID int64 // Required
ChangeType string // Required: "incident" or "maintenance"
}
// ListStatusChangesOutput contains the result of listing status changes
type ListStatusChangesOutput struct {
Changes []StatusChange `json:"changes"`
Total int `json:"total"`
}
// ListStatusChanges lists active incidents or maintenances on a status page
func (c *Client) ListStatusChanges(ctx context.Context, input *ListStatusChangesInput) (*ListStatusChangesOutput, error) {
if input.ChangeType != "incident" && input.ChangeType != "maintenance" {
return nil, fmt.Errorf("type must be 'incident' or 'maintenance'")
}
params := url.Values{}
params.Set("page_id", strconv.FormatInt(input.PageID, 10))
params.Set("type", input.ChangeType)
resp, err := c.makeRequest(ctx, "GET", "/status-page/change/active/list?"+params.Encode(), nil)
if err != nil {
return nil, fmt.Errorf("failed to list status changes: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleAPIError(c.logger, resp)
}
var result struct {
Error *DutyError `json:"error,omitempty"`
Data *struct {
Items []StatusChange `json:"items"`
Total int `json:"total"`
} `json:"data,omitempty"`
}
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
changes := []StatusChange{}
total := 0
if result.Data != nil {
changes = result.Data.Items
total = result.Data.Total
}
return &ListStatusChangesOutput{
Changes: changes,
Total: total,
}, nil
}
// CreateStatusIncidentInput contains parameters for creating a status page incident
type CreateStatusIncidentInput struct {
PageID int64 // Required
Title string // Required. Max 255 characters
Message string // Optional. Initial update message
Status string // Optional. Default: "investigating"
AffectedComponents string // Optional. Format: "id1:degraded,id2:partial_outage"
NotifySubscribers bool // Whether to notify page subscribers
}
// CreateStatusIncident creates an incident on a status page
func (c *Client) CreateStatusIncident(ctx context.Context, input *CreateStatusIncidentInput) (any, error) {
status := input.Status
if status == "" {
status = "investigating"
}
update := map[string]any{
"at_seconds": time.Now().Unix(),
"status": status,
}
if input.Message != "" {
update["description"] = input.Message
}
// Parse component changes if provided
if input.AffectedComponents != "" {
var componentChanges []map[string]string
parts := parseCommaSeparatedStrings(input.AffectedComponents)
for _, part := range parts {
kv := strings.SplitN(part, ":", 2)
if len(kv) == 2 {
componentChanges = append(componentChanges, map[string]string{
"component_id": strings.TrimSpace(kv[0]),
"status": strings.TrimSpace(kv[1]),
})
} else if len(kv) == 1 && kv[0] != "" {
componentChanges = append(componentChanges, map[string]string{
"component_id": strings.TrimSpace(kv[0]),
"status": "partial_outage",
})
}
}
if len(componentChanges) > 0 {
update["component_changes"] = componentChanges
}
}
description := input.Message
if description == "" {
description = input.Title
}
requestBody := map[string]any{
"page_id": input.PageID,
"title": input.Title,
"type": "incident",
"status": status,
"description": description,
"updates": []map[string]any{update},
"notify_subscribers": input.NotifySubscribers,
}
resp, err := c.makeRequest(ctx, "POST", "/status-page/change/create", requestBody)
if err != nil {
return nil, fmt.Errorf("failed to create status incident: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var result FlashdutyResponse
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
return result.Data, nil
}
// CreateChangeTimelineInput contains parameters for adding a timeline entry
type CreateChangeTimelineInput struct {
PageID int64 // Required
ChangeID int64 // Required
Message string // Required
AtSeconds int64 // Optional. Defaults to current time
Status string // Optional
ComponentChanges string // Optional. JSON array of component status changes
}
// CreateChangeTimeline adds a timeline update to a status page incident or maintenance
func (c *Client) CreateChangeTimeline(ctx context.Context, input *CreateChangeTimelineInput) error {
requestBody := map[string]any{
"page_id": input.PageID,
"change_id": input.ChangeID,
"description": input.Message,
}
if input.AtSeconds > 0 {
requestBody["at_seconds"] = input.AtSeconds
}
if input.Status != "" {
requestBody["status"] = input.Status
}
if input.ComponentChanges != "" {
var changes []map[string]string
if err := json.Unmarshal([]byte(input.ComponentChanges), &changes); err == nil {
requestBody["component_changes"] = changes
}
}
resp, err := c.makeRequest(ctx, "POST", "/status-page/change/timeline/create", requestBody)
if err != nil {
return fmt.Errorf("failed to create timeline: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var result FlashdutyResponse
if err := parseResponse(c.logger, resp, &result); err != nil {
return err
}
if result.Error != nil {
return result.Error
}
return nil
}