-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatuspage.go
More file actions
354 lines (314 loc) · 12 KB
/
statuspage.go
File metadata and controls
354 lines (314 loc) · 12 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package flashduty
import (
"context"
"encoding/json"
"errors"
"fmt"
"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) {
result, err := getData[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"`
}](c, ctx, "/status-page/list", "failed to list status pages")
if err != nil {
return nil, err
}
if result == nil || len(result.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.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)
result, err := getData[struct {
Items []StatusChange `json:"items"`
Total int `json:"total"`
}](c, ctx, "/status-page/change/active/list?"+params.Encode(), "failed to list status changes")
if err != nil {
return nil, err
}
changes := []StatusChange{}
total := 0
if result != nil {
changes = result.Items
total = result.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,
}
data, err := postData[any](c, ctx, "/status-page/change/create", requestBody, "failed to create status incident")
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
return *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
}
}
return postEmpty(c, ctx, "/status-page/change/timeline/create", requestBody, "failed to create timeline")
}
// StartStatusPageMigrationInput contains parameters for starting a status page
// structure and history migration from an external provider.
type StartStatusPageMigrationInput struct {
SourceAPIKey string // Required. API key for the source provider (e.g. Atlassian Statuspage)
SourcePageID string // Required. Page identifier in the source provider
URLName string // Optional. URL name to use when creating a new Flashduty public status page
}
// StartStatusPageEmailSubscriberMigrationInput contains parameters for starting
// an email subscriber migration from an external provider into an existing
// Flashduty status page.
type StartStatusPageEmailSubscriberMigrationInput struct {
SourceAPIKey string // Required. API key for the source provider
SourcePageID string // Required. Page identifier in the source provider
TargetPageID int64 // Required. Flashduty status page ID to import into
}
// StartStatusPageMigrationOutput contains the result of starting an async
// status page migration. Both structure and email subscriber migrations return
// a job ID that can be polled with GetStatusPageMigrationStatus.
type StartStatusPageMigrationOutput struct {
JobID string `json:"job_id"`
}
// StatusPageMigrationProgress describes incremental counters reported by a
// migration job. Fields are populated best-effort; zero values indicate
// either the counter does not apply to the current phase or no items of that
// kind have been processed yet.
type StatusPageMigrationProgress struct {
TotalSteps int `json:"total_steps"`
CompletedSteps int `json:"completed_steps"`
ComponentsImported int `json:"components_imported"`
SectionsImported int `json:"sections_imported"`
IncidentsImported int `json:"incidents_imported"`
MaintenancesImported int `json:"maintenances_imported"`
SubscribersImported int `json:"subscribers_imported"`
SubscribersSkipped int `json:"subscribers_skipped"`
TemplatesImported int `json:"templates_imported"`
Warnings []string `json:"warnings,omitempty"`
}
// StatusPageMigrationJob describes the state of a status page migration job.
type StatusPageMigrationJob struct {
JobID string `json:"job_id"`
SourcePageID string `json:"source_page_id"`
TargetPageID int64 `json:"target_page_id"`
Phase string `json:"phase"`
Status string `json:"status"`
Progress StatusPageMigrationProgress `json:"progress"`
Error string `json:"error,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
// StartStatusPageMigration starts an asynchronous migration of status page
// structure and history from an external provider into Flashduty. The returned
// job ID can be polled with GetStatusPageMigrationStatus and cancelled with
// CancelStatusPageMigration.
func (c *Client) StartStatusPageMigration(ctx context.Context, input *StartStatusPageMigrationInput) (*StartStatusPageMigrationOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
body := map[string]any{
"api_key": input.SourceAPIKey,
"source_page_id": input.SourcePageID,
}
if input.URLName != "" {
body["url_name"] = input.URLName
}
return c.startStatusPageMigration(ctx, "/status-page/migrate-structure", body)
}
// StartStatusPageEmailSubscriberMigration starts an asynchronous migration of
// email subscribers from an external provider into an existing Flashduty
// status page. The returned job ID can be polled with
// GetStatusPageMigrationStatus and cancelled with CancelStatusPageMigration.
func (c *Client) StartStatusPageEmailSubscriberMigration(ctx context.Context, input *StartStatusPageEmailSubscriberMigrationInput) (*StartStatusPageMigrationOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
return c.startStatusPageMigration(ctx, "/status-page/migrate-email-subscribers", map[string]any{
"api_key": input.SourceAPIKey,
"source_page_id": input.SourcePageID,
"target_page_id": input.TargetPageID,
})
}
func (c *Client) startStatusPageMigration(ctx context.Context, path string, body map[string]any) (*StartStatusPageMigrationOutput, error) {
result, err := postOptionalData[StartStatusPageMigrationOutput](c, ctx, path, body, "failed to start status page migration")
if err != nil {
return nil, err
}
if result == nil {
return nil, errors.New("status page migration response missing data")
}
return result, nil
}
// GetStatusPageMigrationStatus fetches the current state of a status page
// migration job identified by jobID.
func (c *Client) GetStatusPageMigrationStatus(ctx context.Context, jobID string) (*StatusPageMigrationJob, error) {
if jobID == "" {
return nil, errors.New("jobID is required")
}
params := url.Values{}
params.Set("job_id", jobID)
result, err := getOptionalData[StatusPageMigrationJob](c, ctx, "/status-page/migration/status?"+params.Encode(), "failed to get status page migration status")
if err != nil {
return nil, err
}
if result == nil {
return nil, fmt.Errorf("status page migration status response missing data")
}
return result, nil
}
// CancelStatusPageMigration requests cancellation of an in-flight status page
// migration job identified by jobID.
func (c *Client) CancelStatusPageMigration(ctx context.Context, jobID string) error {
if jobID == "" {
return errors.New("jobID is required")
}
return postEmpty(c, ctx, "/status-page/migration/cancel", map[string]any{
"job_id": jobID,
}, "failed to cancel status page migration")
}