/
settings.go
197 lines (176 loc) · 6.28 KB
/
settings.go
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
// Copyright 2023 MongoDB Inc
//
// 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 settings
import (
"strings"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
)
const (
datadog = "DATADOG"
slack = "SLACK"
victor = "VICTOR_OPS"
email = "EMAIL"
opsGenie = "OPS_GENIE"
pagerDuty = "PAGER_DUTY"
sms = "SMS"
group = "GROUP"
user = "USER"
org = "ORG"
team = "TEAM"
webhook = "WEBHOOK"
microsoftTeams = "MICROSOFT_TEAMS"
)
// ConfigOpts contains all the information and functions to manage an alert configuration.
type ConfigOpts struct {
event string
matcherFieldName string
matcherOperator string
matcherValue string
metricThresholdMetricName string
metricThresholdOperator string
metricThresholdUnits string
metricThresholdMode string
notifierID string
notificationToken string // notificationsApiToken, notificationsFlowdockApiToken
notificationChannelName string
apiKey string // notificationsDatadogApiKey, notificationsOpsGenieApiKey, notificationsVictorOpsApiKey
notificationEmailAddress string
notificationMobileNumber string
notificationRegion string // notificationsOpsGenieRegion, notificationsDatadogRegion
notificationServiceKey string
notificationTeamID string
notificationType string
notificationUsername string
notificationVictorOpsRoutingKey string
notificationWebhookURL string
notificationWebhookSecret string
notificationRoles []string
notificationDelayMin int
notificationIntervalMin int
notificationSmsEnabled bool
enabled bool
notificationEmailEnabled bool
metricThresholdThreshold float64
}
func (opts *ConfigOpts) NewAlertConfiguration(projectID string) *atlasv2.GroupAlertsConfig {
out := new(atlasv2.GroupAlertsConfig)
out.GroupId = &projectID
eventType := strings.ToUpper(opts.event)
out.EventTypeName = &eventType
out.Enabled = &opts.enabled
if opts.matcherFieldName != "" {
out.Matchers = &[]map[string]interface{}{opts.newMatcher()}
}
if opts.metricThresholdMetricName != "" {
out.MetricThreshold = opts.newMetricThreshold()
}
notification := opts.newNotification()
out.Notifications = &[]atlasv2.AlertsNotificationRootForGroup{*notification}
return out
}
func (opts *ConfigOpts) newNotification() *atlasv2.AlertsNotificationRootForGroup {
out := new(atlasv2.AlertsNotificationRootForGroup)
notificationType := strings.ToUpper(opts.notificationType)
out.TypeName = ¬ificationType
out.DelayMin = &opts.notificationDelayMin
out.IntervalMin = &opts.notificationIntervalMin
if opts.notifierID != "" {
out.NotifierId = &opts.notifierID
}
// write set of functions that contain code from switch cases and return error if required fields are not provided
switch out.GetTypeName() {
case datadog:
out.DatadogApiKey = &opts.apiKey
region := strings.ToUpper(opts.notificationRegion)
out.DatadogRegion = ®ion
case email:
out.EmailAddress = &opts.notificationEmailAddress
case group, org:
out.EmailEnabled = &opts.notificationEmailEnabled
out.SmsEnabled = &opts.notificationSmsEnabled
if len(opts.notificationRoles) > 0 {
out.Roles = &opts.notificationRoles
}
case microsoftTeams:
out.MicrosoftTeamsWebhookUrl = &opts.notificationWebhookURL
case opsGenie:
out.OpsGenieApiKey = &opts.apiKey
region := strings.ToUpper(opts.notificationRegion)
out.OpsGenieRegion = ®ion
case pagerDuty:
region := strings.ToUpper(opts.notificationRegion)
out.Region = ®ion
out.ServiceKey = &opts.notificationServiceKey
case slack:
out.ApiToken = &opts.notificationToken
out.ChannelName = &opts.notificationChannelName
case sms:
out.MobileNumber = &opts.notificationMobileNumber
case team:
out.EmailEnabled = &opts.notificationEmailEnabled
out.SmsEnabled = &opts.notificationSmsEnabled
out.TeamId = &opts.notificationTeamID
case user:
out.EmailEnabled = &opts.notificationEmailEnabled
out.SmsEnabled = &opts.notificationSmsEnabled
out.Username = &opts.notificationUsername
case victor:
out.VictorOpsApiKey = &opts.apiKey
out.VictorOpsRoutingKey = &opts.notificationVictorOpsRoutingKey
case webhook:
out.WebhookUrl = &opts.notificationWebhookURL
out.WebhookSecret = &opts.notificationWebhookSecret
}
return out
}
func (opts *ConfigOpts) newMetricThreshold() *atlasv2.ServerlessMetricThreshold {
operator := strings.ToUpper(opts.metricThresholdOperator)
mode := strings.ToUpper(opts.metricThresholdMode)
units := strings.ToUpper(opts.metricThresholdUnits)
result := &atlasv2.ServerlessMetricThreshold{
MetricName: strings.ToUpper(opts.metricThresholdMetricName),
Operator: &operator,
Threshold: &opts.metricThresholdThreshold,
Units: &units,
Mode: &mode,
}
return result
}
func (opts *ConfigOpts) newMatcher() map[string]interface{} {
result := make(map[string]interface{})
result["FieldName"] = strings.ToUpper(opts.matcherFieldName)
result["Operator"] = strings.ToUpper(opts.matcherOperator)
result["Value"] = strings.ToUpper(opts.matcherValue)
return result
}
func Builder() *cobra.Command {
cmd := &cobra.Command{
Use: "settings",
Aliases: []string{"config"},
Short: "Manages alerts configuration for your project.",
Long: `Use this command to list, create, edit, delete, enable and disable alert configurations.`,
}
cmd.AddCommand(
CreateBuilder(),
ListBuilder(),
describeBuilder(),
DeleteBuilder(),
FieldsBuilder(),
UpdateBuilder(),
EnableBuilder(),
DisableBuilder(),
)
return cmd
}