Skip to content

Commit 4c532f4

Browse files
authored
feat: add uptime alerts commands (#1431)
* feat: add uptime alerts commands * chore: add uptime alert tests * chore: make mocks and run gofmt * chore: run go mod vendor * chore: fix uptime alert command descriptions
1 parent 58e920c commit 4c532f4

File tree

8 files changed

+723
-2
lines changed

8 files changed

+723
-2
lines changed

args.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ const (
251251
// ArgUptimeCheckEnabled is whether or not an uptime check is enabled.
252252
ArgUptimeCheckEnabled = "enabled"
253253

254+
// ArgUptimeAlertName is the name of an uptime alert.
255+
ArgUptimeAlertName = "name"
256+
// ArgUptimeAlertType is the type of an uptime alert.
257+
ArgUptimeAlertType = "type"
258+
// ArgUptimeAlertThreshold the threshold at which an uptime alert will trigger.
259+
ArgUptimeAlertThreshold = "threshold"
260+
// ArgUptimeAlertComparison is the uptime alert comparator.
261+
ArgUptimeAlertComparison = "comparison"
262+
// ArgUptimeAlertEmails are the emails to send uptime alerts to.
263+
ArgUptimeAlertEmails = "emails"
264+
// ArgUptimeAlertSlackChannels are the Slack channels to send uptime alerts to.
265+
ArgUptimeAlertSlackChannels = "slack-channels"
266+
// ArgUptimeAlertSlackURLs are the Slack URLs to send uptime alerts to.
267+
ArgUptimeAlertSlackURLs = "slack-urls"
268+
// ArgUptimeAlertPeriod is the time threshold at which an uptime alert will trigger.
269+
ArgUptimeAlertPeriod = "period"
270+
254271
// ArgVolumeSize is the size of a volume.
255272
ArgVolumeSize = "size"
256273
// ArgVolumeDesc is the description of a volume.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023 The Doctl Authors All rights reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package displayers
15+
16+
import (
17+
"io"
18+
"strings"
19+
20+
"github.com/digitalocean/doctl/do"
21+
)
22+
23+
type UptimeAlert struct {
24+
UptimeAlerts []do.UptimeAlert
25+
}
26+
27+
func (uc *UptimeAlert) JSON(out io.Writer) error {
28+
return writeJSON(uc.UptimeAlerts, out)
29+
}
30+
31+
func (uc *UptimeAlert) Cols() []string {
32+
return []string{
33+
"ID", "Name", "Type", "Threshold", "Comparison", "Period", "Emails", "Slack Channels",
34+
}
35+
}
36+
37+
func (ua *UptimeAlert) ColMap() map[string]string {
38+
return map[string]string{
39+
"ID": "ID",
40+
"Name": "Name",
41+
"Type": "Type",
42+
"Threshold": "Threshold",
43+
"Comparison": "Comparison",
44+
"Period": "Period",
45+
"Emails": "Emails",
46+
"Slack Channels": "Slack Channels",
47+
}
48+
}
49+
50+
func (ua *UptimeAlert) KV() []map[string]interface{} {
51+
out := make([]map[string]interface{}, 0, len(ua.UptimeAlerts))
52+
for _, uptimeAlert := range ua.UptimeAlerts {
53+
emails := ""
54+
if uptimeAlert.Notifications.Email != nil {
55+
emails = strings.Join(uptimeAlert.Notifications.Email, ",")
56+
}
57+
slackChannels := make([]string, 0)
58+
if uptimeAlert.Notifications.Slack != nil {
59+
for _, v := range uptimeAlert.Notifications.Slack {
60+
slackChannels = append(slackChannels, v.Channel)
61+
}
62+
}
63+
slacks := strings.Join(slackChannels, ",")
64+
65+
m := map[string]interface{}{
66+
"ID": uptimeAlert.ID,
67+
"Name": uptimeAlert.Name,
68+
"Type": uptimeAlert.Type,
69+
"Threshold": uptimeAlert.Threshold,
70+
"Comparison": uptimeAlert.Comparison,
71+
"Period": uptimeAlert.Period,
72+
"Emails": emails,
73+
"Slack Channels": slacks,
74+
}
75+
out = append(out, m)
76+
}
77+
return out
78+
}

0 commit comments

Comments
 (0)