Skip to content

Commit

Permalink
feat: add uptime alerts commands (#1431)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mikesmithgh authored Oct 11, 2023
1 parent 58e920c commit 4c532f4
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 2 deletions.
17 changes: 17 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ const (
// ArgUptimeCheckEnabled is whether or not an uptime check is enabled.
ArgUptimeCheckEnabled = "enabled"

// ArgUptimeAlertName is the name of an uptime alert.
ArgUptimeAlertName = "name"
// ArgUptimeAlertType is the type of an uptime alert.
ArgUptimeAlertType = "type"
// ArgUptimeAlertThreshold the threshold at which an uptime alert will trigger.
ArgUptimeAlertThreshold = "threshold"
// ArgUptimeAlertComparison is the uptime alert comparator.
ArgUptimeAlertComparison = "comparison"
// ArgUptimeAlertEmails are the emails to send uptime alerts to.
ArgUptimeAlertEmails = "emails"
// ArgUptimeAlertSlackChannels are the Slack channels to send uptime alerts to.
ArgUptimeAlertSlackChannels = "slack-channels"
// ArgUptimeAlertSlackURLs are the Slack URLs to send uptime alerts to.
ArgUptimeAlertSlackURLs = "slack-urls"
// ArgUptimeAlertPeriod is the time threshold at which an uptime alert will trigger.
ArgUptimeAlertPeriod = "period"

// ArgVolumeSize is the size of a volume.
ArgVolumeSize = "size"
// ArgVolumeDesc is the description of a volume.
Expand Down
78 changes: 78 additions & 0 deletions commands/displayers/uptime_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2023 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package displayers

import (
"io"
"strings"

"github.com/digitalocean/doctl/do"
)

type UptimeAlert struct {
UptimeAlerts []do.UptimeAlert
}

func (uc *UptimeAlert) JSON(out io.Writer) error {
return writeJSON(uc.UptimeAlerts, out)
}

func (uc *UptimeAlert) Cols() []string {
return []string{
"ID", "Name", "Type", "Threshold", "Comparison", "Period", "Emails", "Slack Channels",
}
}

func (ua *UptimeAlert) ColMap() map[string]string {
return map[string]string{
"ID": "ID",
"Name": "Name",
"Type": "Type",
"Threshold": "Threshold",
"Comparison": "Comparison",
"Period": "Period",
"Emails": "Emails",
"Slack Channels": "Slack Channels",
}
}

func (ua *UptimeAlert) KV() []map[string]interface{} {
out := make([]map[string]interface{}, 0, len(ua.UptimeAlerts))
for _, uptimeAlert := range ua.UptimeAlerts {
emails := ""
if uptimeAlert.Notifications.Email != nil {
emails = strings.Join(uptimeAlert.Notifications.Email, ",")
}
slackChannels := make([]string, 0)
if uptimeAlert.Notifications.Slack != nil {
for _, v := range uptimeAlert.Notifications.Slack {
slackChannels = append(slackChannels, v.Channel)
}
}
slacks := strings.Join(slackChannels, ",")

m := map[string]interface{}{
"ID": uptimeAlert.ID,
"Name": uptimeAlert.Name,
"Type": uptimeAlert.Type,
"Threshold": uptimeAlert.Threshold,
"Comparison": uptimeAlert.Comparison,
"Period": uptimeAlert.Period,
"Emails": emails,
"Slack Channels": slacks,
}
out = append(out, m)
}
return out
}
Loading

0 comments on commit 4c532f4

Please sign in to comment.