-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdowntimes.go
More file actions
177 lines (153 loc) · 5.65 KB
/
Copy pathdowntimes.go
File metadata and controls
177 lines (153 loc) · 5.65 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
package mackerel
import (
"context"
"encoding/json"
"fmt"
"time"
)
// Downtime information
type Downtime struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Memo string `json:"memo,omitempty"`
Start int64 `json:"start"`
Duration int64 `json:"duration"`
Recurrence *DowntimeRecurrence `json:"recurrence,omitempty"`
ServiceScopes []string `json:"serviceScopes,omitempty"`
ServiceExcludeScopes []string `json:"serviceExcludeScopes,omitempty"`
RoleScopes []string `json:"roleScopes,omitempty"`
RoleExcludeScopes []string `json:"roleExcludeScopes,omitempty"`
MonitorScopes []string `json:"monitorScopes,omitempty"`
MonitorExcludeScopes []string `json:"monitorExcludeScopes,omitempty"`
}
// DowntimeRecurrence ...
type DowntimeRecurrence struct {
Type DowntimeRecurrenceType `json:"type"`
Interval int64 `json:"interval"`
Weekdays []DowntimeWeekday `json:"weekdays,omitempty"`
Until int64 `json:"until,omitempty"`
}
// DowntimeRecurrenceType ...
type DowntimeRecurrenceType int
// DowntimeRecurrenceType ...
const (
DowntimeRecurrenceTypeHourly DowntimeRecurrenceType = iota + 1
DowntimeRecurrenceTypeDaily
DowntimeRecurrenceTypeWeekly
DowntimeRecurrenceTypeMonthly
DowntimeRecurrenceTypeYearly
)
var stringToRecurrenceType = map[string]DowntimeRecurrenceType{
"hourly": DowntimeRecurrenceTypeHourly,
"daily": DowntimeRecurrenceTypeDaily,
"weekly": DowntimeRecurrenceTypeWeekly,
"monthly": DowntimeRecurrenceTypeMonthly,
"yearly": DowntimeRecurrenceTypeYearly,
}
var recurrenceTypeToString = map[DowntimeRecurrenceType]string{
DowntimeRecurrenceTypeHourly: "hourly",
DowntimeRecurrenceTypeDaily: "daily",
DowntimeRecurrenceTypeWeekly: "weekly",
DowntimeRecurrenceTypeMonthly: "monthly",
DowntimeRecurrenceTypeYearly: "yearly",
}
// UnmarshalJSON implements json.Unmarshaler
func (t *DowntimeRecurrenceType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if x, ok := stringToRecurrenceType[s]; ok {
*t = x
return nil
}
return fmt.Errorf("unknown downtime recurrence type: %q", s)
}
// MarshalJSON implements json.Marshaler
func (t DowntimeRecurrenceType) MarshalJSON() ([]byte, error) {
return json.Marshal(recurrenceTypeToString[t])
}
// String implements Stringer
func (t DowntimeRecurrenceType) String() string {
return recurrenceTypeToString[t]
}
// DowntimeWeekday ...
type DowntimeWeekday time.Weekday
var stringToWeekday = map[string]DowntimeWeekday{
"Sunday": DowntimeWeekday(time.Sunday),
"Monday": DowntimeWeekday(time.Monday),
"Tuesday": DowntimeWeekday(time.Tuesday),
"Wednesday": DowntimeWeekday(time.Wednesday),
"Thursday": DowntimeWeekday(time.Thursday),
"Friday": DowntimeWeekday(time.Friday),
"Saturday": DowntimeWeekday(time.Saturday),
}
var weekdayToString = map[DowntimeWeekday]string{
DowntimeWeekday(time.Sunday): "Sunday",
DowntimeWeekday(time.Monday): "Monday",
DowntimeWeekday(time.Tuesday): "Tuesday",
DowntimeWeekday(time.Wednesday): "Wednesday",
DowntimeWeekday(time.Thursday): "Thursday",
DowntimeWeekday(time.Friday): "Friday",
DowntimeWeekday(time.Saturday): "Saturday",
}
// UnmarshalJSON implements json.Unmarshaler
func (w *DowntimeWeekday) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if x, ok := stringToWeekday[s]; ok {
*w = x
return nil
}
return fmt.Errorf("unknown downtime weekday: %q", s)
}
// MarshalJSON implements json.Marshaler
func (w DowntimeWeekday) MarshalJSON() ([]byte, error) {
return json.Marshal(weekdayToString[w])
}
// String implements Stringer
func (w DowntimeWeekday) String() string {
return weekdayToString[w]
}
// FindDowntimes finds downtimes.
func (c *Client) FindDowntimes() ([]*Downtime, error) {
return c.FindDowntimesContext(context.Background())
}
// FindDowntimesContext finds downtimes.
func (c *Client) FindDowntimesContext(ctx context.Context) ([]*Downtime, error) {
data, err := requestGetContext[struct {
Downtimes []*Downtime `json:"downtimes"`
}](ctx, c, "/api/v0/downtimes")
if err != nil {
return nil, err
}
return data.Downtimes, nil
}
// CreateDowntime creates a downtime.
func (c *Client) CreateDowntime(param *Downtime) (*Downtime, error) {
return c.CreateDowntimeContext(context.Background(), param)
}
// CreateDowntimeContext creates a downtime.
func (c *Client) CreateDowntimeContext(ctx context.Context, param *Downtime) (*Downtime, error) {
return requestPostContext[Downtime](ctx, c, "/api/v0/downtimes", param)
}
// UpdateDowntime updates a downtime.
func (c *Client) UpdateDowntime(downtimeID string, param *Downtime) (*Downtime, error) {
return c.UpdateDowntimeContext(context.Background(), downtimeID, param)
}
// UpdateDowntimeContext updates a downtime.
func (c *Client) UpdateDowntimeContext(ctx context.Context, downtimeID string, param *Downtime) (*Downtime, error) {
path := fmt.Sprintf("/api/v0/downtimes/%s", downtimeID)
return requestPutContext[Downtime](ctx, c, path, param)
}
// DeleteDowntime deletes a downtime.
func (c *Client) DeleteDowntime(downtimeID string) (*Downtime, error) {
return c.DeleteDowntimeContext(context.Background(), downtimeID)
}
// DeleteDowntimeContext deletes a downtime.
func (c *Client) DeleteDowntimeContext(ctx context.Context, downtimeID string) (*Downtime, error) {
path := fmt.Sprintf("/api/v0/downtimes/%s", downtimeID)
return requestDeleteContext[Downtime](ctx, c, path)
}