Skip to content

Commit

Permalink
monitoring: add CRUD commands for alert policies
Browse files Browse the repository at this point in the history
  • Loading branch information
bsnyder788 committed Jul 26, 2021
1 parent 343c86e commit 0e2b09d
Show file tree
Hide file tree
Showing 452 changed files with 29,307 additions and 4,478 deletions.
35 changes: 35 additions & 0 deletions args.go
Expand Up @@ -354,4 +354,39 @@ const (

// ArgDatabaseFirewallRuleUUID is the UUID for the firewall rules.
ArgDatabaseFirewallRuleUUID = "uuid"

// Monitoring Args

// ArgAlertPolicyDescription is the flag to pass in the alert policy description.
ArgAlertPolicyDescription = "description"

// ArgAlertPolicyType is the alert policy type.
ArgAlertPolicyType = "type"

// ArgAlertPolicyValue is the alert policy value.
ArgAlertPolicyValue = "value"

// ArgAlertPolicyWindow is the alert policy window.
ArgAlertPolicyWindow = "window"

// ArgAlertPolicyTags is the alert policy tags.
ArgAlertPolicyTags = "tags"

// ArgAlertPolicyEntities is the alert policy entities.
ArgAlertPolicyEntities = "entities"

// ArgAlertPolicyEnabled is whether the alert policy is enabled.
ArgAlertPolicyEnabled = "enabled"

// ArgAlertPolicyCompare is the alert policy comparator.
ArgAlertPolicyCompare = "compare"

// ArgAlertPolicyEmails are the emails to send alerts to.
ArgAlertPolicyEmails = "emails"

// ArgAlertPolicySlackChannels are the slack channels to send alerts to.
ArgAlertPolicySlackChannels = "slack-channels"

// ArgAlertPolicySlackURLs are the slack URLs to send alerts to.
ArgAlertPolicySlackURLs = "slack-urls"
)
2 changes: 2 additions & 0 deletions commands/command_config.go
Expand Up @@ -66,6 +66,7 @@ type CmdConfig struct {
VPCs func() do.VPCsService
OneClicks func() do.OneClickService
Apps func() do.AppsService
Monitoring func() do.MonitoringService
}

// NewCmdConfig creates an instance of a CmdConfig.
Expand Down Expand Up @@ -114,6 +115,7 @@ func NewCmdConfig(ns string, dc doctl.Config, out io.Writer, args []string, init
c.VPCs = func() do.VPCsService { return do.NewVPCsService(godoClient) }
c.OneClicks = func() do.OneClickService { return do.NewOneClickService(godoClient) }
c.Apps = func() do.AppsService { return do.NewAppsService(godoClient) }
c.Monitoring = func() do.MonitoringService { return do.NewMonitoringService(godoClient) }

return nil
},
Expand Down
3 changes: 3 additions & 0 deletions commands/commands_test.go
Expand Up @@ -178,6 +178,7 @@ type tcMocks struct {
vpcs *domocks.MockVPCsService
oneClick *domocks.MockOneClickService
listen *domocks.MockListenerService
monitoring *domocks.MockMonitoringService
}

func withTestClient(t *testing.T, tFn testFn) {
Expand Down Expand Up @@ -217,6 +218,7 @@ func withTestClient(t *testing.T, tFn testFn) {
vpcs: domocks.NewMockVPCsService(ctrl),
oneClick: domocks.NewMockOneClickService(ctrl),
listen: domocks.NewMockListenerService(ctrl),
monitoring: domocks.NewMockMonitoringService(ctrl),
}

config := &CmdConfig{
Expand Down Expand Up @@ -263,6 +265,7 @@ func withTestClient(t *testing.T, tFn testFn) {
VPCs: func() do.VPCsService { return tm.vpcs },
OneClicks: func() do.OneClickService { return tm.oneClick },
Apps: func() do.AppsService { return tm.apps },
Monitoring: func() do.MonitoringService { return tm.monitoring },
}

tFn(config, tm)
Expand Down
87 changes: 87 additions & 0 deletions commands/displayers/monitoring.go
@@ -0,0 +1,87 @@
/*
Copyright 2018 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 AlertPolicy struct {
AlertPolicies do.AlertPolicies
}

var _ Displayable = &AlertPolicy{}

func (a *AlertPolicy) JSON(out io.Writer) error {
return writeJSON(a.AlertPolicies, out)
}

func (a *AlertPolicy) Cols() []string {
return []string{"UUID", "Type", "Description", "Compare",
"Value", "Window", "Entities", "Tags", "Emails", "Slack Channels", "Enabled"}
}

func (a *AlertPolicy) ColMap() map[string]string {
return map[string]string{
"UUID": "UUID",
"Type": "Type",
"Description": "Description",
"Compare": "Compare",
"Value": "Value",
"Window": "Window",
"Entities": "Entities",
"Tags": "Tags",
"Emails": "Emails",
"Slack Channels": "Slack Channels",
"Enabled": "Enabled",
}
}

func (a *AlertPolicy) KV() []map[string]interface{} {
out := []map[string]interface{}{}

for _, x := range a.AlertPolicies {
emails := ""
if x.Alerts.Email != nil {
emails = strings.Join(x.Alerts.Email, ",")
}
slackChannels := make([]string, 0)
if x.Alerts.Slack != nil {
for _, v := range x.Alerts.Slack {
slackChannels = append(slackChannels, v.Channel)
}
}
slacks := strings.Join(slackChannels, ",")

o := map[string]interface{}{
"UUID": x.UUID,
"Type": x.Type,
"Description": x.Description,
"Compare": x.Compare,
"Value": x.Value,
"Window": x.Window,
"Entities": x.Entities,
"Tags": x.Tags,
"Emails": emails,
"Slack Channels": slacks,
"Enabled": x.Enabled,
}
out = append(out, o)
}

return out
}
1 change: 1 addition & 0 deletions commands/doit.go
Expand Up @@ -145,6 +145,7 @@ func addCommands() {
DoitCmd.AddCommand(Registry())
DoitCmd.AddCommand(VPCs())
DoitCmd.AddCommand(OneClicks())
DoitCmd.AddCommand(Monitoring())
}

func computeCmd() *Command {
Expand Down

0 comments on commit 0e2b09d

Please sign in to comment.