-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathalerts.go
More file actions
304 lines (273 loc) · 10.1 KB
/
Copy pathalerts.go
File metadata and controls
304 lines (273 loc) · 10.1 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
package cmd
import (
"context"
"github.com/urfave/cli/v3"
"github.com/Scalingo/cli/alerts"
"github.com/Scalingo/cli/cmd/autocomplete"
"github.com/Scalingo/cli/detect"
"github.com/Scalingo/cli/utils"
"github.com/Scalingo/go-scalingo/v11"
)
var (
alertsListCommand = cli.Command{
Name: "alerts",
Category: "Alerts",
Flags: []cli.Flag{&appFlag},
Usage: "List the alerts of an application",
ArgsUsage: " ",
Description: CommandDescription{
Description: "List all the alerts of an application",
Examples: []string{"scalingo --app my-app alerts"},
SeeAlso: []string{"alerts-add", "alerts-update", "alerts-remove"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 0 {
_ = cli.ShowCommandHelp(ctx, c, "alerts")
return nil
}
err := alerts.List(ctx, detect.CurrentApp(ctx, c))
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts")
},
}
alertsAddCommand = cli.Command{
Name: "alerts-add",
Category: "Alerts",
Usage: "Add an alert to an application metric",
Flags: []cli.Flag{&appFlag,
&cli.StringFlag{Name: "container-type", Aliases: []string{"c"}, Usage: "Specify the container type affected by the alert", Required: true},
&cli.StringFlag{Name: "metric", Aliases: []string{"m"}, Usage: "Specify the metric you want the alert to apply on", Required: true},
&cli.Float64Flag{Name: "limit", Aliases: []string{"l"}, Usage: "Target value for the metric the alert will maintain", Required: true},
&cli.DurationFlag{Name: "duration-before-trigger", Usage: "Alert is activated if the value is above the limit for the given duration", Required: false},
&cli.DurationFlag{Name: "remind-every", Aliases: []string{"r"}, Usage: "Send the alert at regular interval when activated", Required: false},
&cli.BoolFlag{Name: "below", Aliases: []string{"b"}, Usage: "Send the alert when metric value is *below* the limit", Required: false},
&cli.StringSliceFlag{Name: "notifiers", Aliases: []string{"n"}, Usage: "notifiers' id notified when an alert is activated. Can be specified multiple times.", Required: false},
},
Description: CommandDescription{
Description: "Add an alert to an application metric.\n\nThe 'duration-before-trigger', 'remind-every', 'below' and 'notifiers' flags are optional",
Examples: []string{
"scalingo --app my-app alerts-add --container-type web --metric cpu --limit 0.75",
"scalingo --app my-app alerts-add --container-type web --metric rpm_per_container --limit 100 --remind-every 5m30s --below",
"scalingo --app my-app alerts-add --container-type web --metric cpu --limit 0.75 --notifiers 5aaab14dcbf5e7000120fd01 --notifiers 5aaab3cacbf5e7000120fd19",
},
SeeAlso: []string{"alerts-update", "alerts-remove"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if !isValidAlertAddOpts(c) {
err := cli.ShowCommandHelp(ctx, c, "alerts-add")
if err != nil {
errorQuit(ctx, err)
}
return nil
}
currentApp := detect.CurrentApp(ctx, c)
utils.CheckForConsent(ctx, currentApp, utils.ConsentTypeContainers)
remindEvery := c.Duration("r")
durationBeforeTrigger := c.Duration("duration-before-trigger")
err := alerts.Add(ctx, currentApp, scalingo.AlertAddParams{
ContainerType: c.String("c"),
Metric: c.String("m"),
Limit: c.Float64("l"),
SendWhenBelow: c.Bool("b"),
DurationBeforeTrigger: &durationBeforeTrigger,
RemindEvery: &remindEvery,
Notifiers: c.StringSlice("n"),
})
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts-add")
},
}
alertsUpdateCommand = cli.Command{
Name: "alerts-update",
Category: "Alerts",
Usage: "Update an alert",
ArgsUsage: "alert-id",
Flags: []cli.Flag{&appFlag,
&cli.StringFlag{Name: "container-type", Aliases: []string{"c"}, Usage: "Specify the container type affected by the alert"},
&cli.StringFlag{Name: "metric", Aliases: []string{"m"}, Usage: "Specify the metric you want the alert to apply on"},
&cli.Float64Flag{Name: "limit", Aliases: []string{"l"}, Usage: "Target value for the metric the alert will maintain"},
&cli.DurationFlag{Name: "duration-before-trigger", Usage: "Alert is activated if the value is above the limit for the given duration"},
&cli.DurationFlag{Name: "remind-every", Aliases: []string{"r"}, Usage: "Send the alert at regular interval when activated"},
&cli.BoolFlag{Name: "below", Aliases: []string{"b"}, Usage: "Send the alert when metric value is *below* the limit"},
&cli.BoolFlag{Name: "disabled", Aliases: []string{"d"}, Usage: "Disable the alert (nothing is sent)"},
&cli.StringSliceFlag{Name: "notifiers", Aliases: []string{"n"}, Usage: "notifiers' id notified when an alert is activated. Can be specified multiple times."},
},
Description: CommandDescription{
Description: "Update an existing alert.\n\nAll flags are optional.",
Examples: []string{
"scalingo --app my-app alerts-update --metric rpm-per-container --limit 150 alert-id",
"scalingo --app my-app alerts-update --disabled alert-id",
"scalingo --app my-app alerts-update --notifiers 5aaab14dcbf5e7000120fd01 --notifiers 5aaab3cacbf5e7000120fd19 alert-id",
},
SeeAlso: []string{"alerts-disable", "alerts-enable"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 1 {
err := cli.ShowCommandHelp(ctx, c, "alerts-update")
if err != nil {
errorQuit(ctx, err)
}
return nil
}
alertID := c.Args().First()
currentApp := detect.CurrentApp(ctx, c)
utils.CheckForConsent(ctx, currentApp, utils.ConsentTypeContainers)
params := scalingo.AlertUpdateParams{}
if c.IsSet("c") {
ct := c.String("c")
params.ContainerType = &ct
}
if c.IsSet("m") {
m := c.String("m")
params.Metric = &m
}
if c.IsSet("l") {
l := c.Float64("l")
params.Limit = &l
}
if c.IsSet("duration-before-trigger") {
durationBeforeTrigger := c.Duration("duration-before-trigger")
params.DurationBeforeTrigger = &durationBeforeTrigger
}
if c.IsSet("r") {
remindEvery := c.Duration("r")
params.RemindEvery = &remindEvery
}
if c.IsSet("b") {
b := c.Bool("b")
params.SendWhenBelow = &b
}
if c.IsSet("d") {
d := c.Bool("d")
params.Disabled = &d
}
if c.IsSet("n") {
n := c.StringSlice("n")
params.Notifiers = &n
}
err := alerts.Update(ctx, currentApp, alertID, params)
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts-add")
},
}
alertsEnableCommand = cli.Command{
Name: "alerts-enable",
Category: "Alerts",
Usage: "Enable an alert",
ArgsUsage: "alert-id",
Flags: []cli.Flag{&appFlag},
Description: CommandDescription{
Description: "Enable an alert",
Examples: []string{"scalingo --app my-app alerts-enable alert-id"},
SeeAlso: []string{"alerts-update", "alerts-remove"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 1 {
err := cli.ShowCommandHelp(ctx, c, "alerts-enable")
if err != nil {
errorQuit(ctx, err)
}
return nil
}
currentApp := detect.CurrentApp(ctx, c)
utils.CheckForConsent(ctx, currentApp, utils.ConsentTypeContainers)
err := alerts.Update(ctx, currentApp, c.Args().First(), scalingo.AlertUpdateParams{
Disabled: utils.BoolPtr(false),
})
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts-enable")
},
}
alertsDisableCommand = cli.Command{
Name: "alerts-disable",
Category: "Alerts",
Usage: "Disable an alert",
ArgsUsage: "alert-id",
Flags: []cli.Flag{&appFlag},
Description: CommandDescription{
Description: "Disable an alert",
Examples: []string{"scalingo --app my-app alerts-disable alert-id"},
SeeAlso: []string{"alerts-update", "alerts-remove"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 1 {
err := cli.ShowCommandHelp(ctx, c, "alerts-disable")
if err != nil {
errorQuit(ctx, err)
}
return nil
}
currentApp := detect.CurrentApp(ctx, c)
utils.CheckForConsent(ctx, currentApp, utils.ConsentTypeContainers)
err := alerts.Update(ctx, currentApp, c.Args().First(), scalingo.AlertUpdateParams{
Disabled: utils.BoolPtr(true),
})
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts-disable")
},
}
alertsRemoveCommand = cli.Command{
Name: "alerts-remove",
Category: "Alerts",
Usage: "Remove an alert from an application",
ArgsUsage: "alert-id",
Flags: []cli.Flag{&appFlag},
Description: CommandDescription{
Description: "Remove an alert",
Examples: []string{"scalingo --app my-app alerts-remove alert-id"},
SeeAlso: []string{"alerts-add", "alerts-update"},
}.Render(),
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 1 {
_ = cli.ShowCommandHelp(ctx, c, "alerts-remove")
return nil
}
currentApp := detect.CurrentApp(ctx, c)
utils.CheckForConsent(ctx, currentApp, utils.ConsentTypeContainers)
err := alerts.Remove(ctx, currentApp, c.Args().First())
if err != nil {
errorQuit(ctx, err)
}
return nil
},
ShellComplete: func(_ context.Context, c *cli.Command) {
_ = autocomplete.CmdFlagsAutoComplete(c, "alerts-remove")
},
}
)
func isValidAlertAddOpts(c *cli.Command) bool {
if c.Args().Len() > 0 {
return false
}
for _, opt := range []string{"container-type", "metric", "limit"} {
if !c.IsSet(opt) {
return false
}
}
return true
}