Skip to content

Commit

Permalink
INTMDB-373: Add support for Webhook and MicrosoftTeamsWebhookURL Noti…
Browse files Browse the repository at this point in the history
…fications (#320)
  • Loading branch information
martinstibbe committed Oct 26, 2022
1 parent c9f1e14 commit ca26a1f
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 24 deletions.
50 changes: 26 additions & 24 deletions mongodbatlas/alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,32 @@ type Threshold struct {

// Notification sends when an alert condition is detected.
type Notification struct {
APIToken string `json:"apiToken,omitempty"` // Slack API token or Bot token. Populated for the SLACK notifications type. If the token later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
ChannelName string `json:"channelName,omitempty"` // Slack channel name. Populated for the SLACK notifications type.
DatadogAPIKey string `json:"datadogApiKey,omitempty"` // Datadog API Key. Found in the Datadog dashboard. Populated for the DATADOG notifications type.
DatadogRegion string `json:"datadogRegion,omitempty"` // Region that indicates which API URL to use
DelayMin *int `json:"delayMin,omitempty"` // Number of minutes to wait after an alert condition is detected before sending out the first notification.
EmailAddress string `json:"emailAddress,omitempty"` // Email address to which alert notifications are sent. Populated for the EMAIL notifications type.
EmailEnabled *bool `json:"emailEnabled,omitempty"` // Flag indicating if email notifications should be sent. Populated for ORG, GROUP, and USER notifications types.
FlowdockAPIToken string `json:"flowdockApiToken,omitempty"` // The Flowdock personal API token. Populated for the FLOWDOCK notifications type. If the token later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
FlowName string `json:"flowName,omitempty"` // Flowdock flow namse in lower-case letters.
IntervalMin int `json:"intervalMin,omitempty"` // Number of minutes to wait between successive notifications for unacknowledged alerts that are not resolved.
MobileNumber string `json:"mobileNumber,omitempty"` // Mobile number to which alert notifications are sent. Populated for the SMS notifications type.
OpsGenieAPIKey string `json:"opsGenieApiKey,omitempty"` // Opsgenie API Key. Populated for the OPS_GENIE notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
OpsGenieRegion string `json:"opsGenieRegion,omitempty"` // Region that indicates which API URL to use.
OrgName string `json:"orgName,omitempty"` // Flowdock organization name in lower-case letters. This is the name that appears after www.flowdock.com/app/ in the URL string. Populated for the FLOWDOCK notifications type.
ServiceKey string `json:"serviceKey,omitempty"` // PagerDuty service key. Populated for the PAGER_DUTY notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
SMSEnabled *bool `json:"smsEnabled,omitempty"` // Flag indicating if text message notifications should be sent. Populated for ORG, GROUP, and USER notifications types.
TeamID string `json:"teamId,omitempty"` // Unique identifier of a team.
TeamName string `json:"teamName,omitempty"` // Label for the team that receives this notification.
TypeName string `json:"typeName,omitempty"` // Type of alert notification.
Username string `json:"username,omitempty"` // Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Populated for the USER notifications type.
VictorOpsAPIKey string `json:"victorOpsApiKey,omitempty"` // VictorOps API key. Populated for the VICTOR_OPS notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
VictorOpsRoutingKey string `json:"victorOpsRoutingKey,omitempty"` // VictorOps routing key. Populated for the VICTOR_OPS notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
Roles []string `json:"roles,omitempty"` // The following roles grant privileges within a project.

APIToken string `json:"apiToken,omitempty"` // Slack API token or Bot token. Populated for the SLACK notifications type. If the token later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
ChannelName string `json:"channelName,omitempty"` // Slack channel name. Populated for the SLACK notifications type.
DatadogAPIKey string `json:"datadogApiKey,omitempty"` // Datadog API Key. Found in the Datadog dashboard. Populated for the DATADOG notifications type.
DatadogRegion string `json:"datadogRegion,omitempty"` // Region that indicates which API URL to use
DelayMin *int `json:"delayMin,omitempty"` // Number of minutes to wait after an alert condition is detected before sending out the first notification.
EmailAddress string `json:"emailAddress,omitempty"` // Email address to which alert notifications are sent. Populated for the EMAIL notifications type.
EmailEnabled *bool `json:"emailEnabled,omitempty"` // Flag indicating if email notifications should be sent. Populated for ORG, GROUP, and USER notifications types.
FlowdockAPIToken string `json:"flowdockApiToken,omitempty"` // The Flowdock personal API token. Populated for the FLOWDOCK notifications type. If the token later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
FlowName string `json:"flowName,omitempty"` // Flowdock flow namse in lower-case letters.
IntervalMin int `json:"intervalMin,omitempty"` // Number of minutes to wait between successive notifications for unacknowledged alerts that are not resolved.
MobileNumber string `json:"mobileNumber,omitempty"` // Mobile number to which alert notifications are sent. Populated for the SMS notifications type.
OpsGenieAPIKey string `json:"opsGenieApiKey,omitempty"` // Opsgenie API Key. Populated for the OPS_GENIE notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
OpsGenieRegion string `json:"opsGenieRegion,omitempty"` // Region that indicates which API URL to use.
OrgName string `json:"orgName,omitempty"` // Flowdock organization name in lower-case letters. This is the name that appears after www.flowdock.com/app/ in the URL string. Populated for the FLOWDOCK notifications type.
ServiceKey string `json:"serviceKey,omitempty"` // PagerDuty service key. Populated for the PAGER_DUTY notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
SMSEnabled *bool `json:"smsEnabled,omitempty"` // Flag indicating if text message notifications should be sent. Populated for ORG, GROUP, and USER notifications types.
TeamID string `json:"teamId,omitempty"` // Unique identifier of a team.
TeamName string `json:"teamName,omitempty"` // Label for the team that receives this notification.
TypeName string `json:"typeName,omitempty"` // Type of alert notification.
Username string `json:"username,omitempty"` // Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Populated for the USER notifications type.
VictorOpsAPIKey string `json:"victorOpsApiKey,omitempty"` // VictorOps API key. Populated for the VICTOR_OPS notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
VictorOpsRoutingKey string `json:"victorOpsRoutingKey,omitempty"` // VictorOps routing key. Populated for the VICTOR_OPS notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.
Roles []string `json:"roles,omitempty"` // The following roles grant privileges within a project.
MicrosoftTeamsWebhookURL string `json:"microsoftTeamsWebhookUrl,omitempty"` // Microsoft Teams Wewbhook URL
WebhookSecret string `json:"webhookSecret,omitempty"` // Webhook Secret
WebhookURL string `json:"webhookUrl,omitempty"` // Webhook URL
}

// AlertConfigurationsResponse is the response from the AlertConfigurationsService.List.
Expand Down
170 changes: 170 additions & 0 deletions mongodbatlas/alert_configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,173 @@ func TestAlertConfiguration_ListMatcherFields(t *testing.T) {
t.Error(diff)
}
}

func TestAlertConfiguration_GetAnAlertConfigTeams(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

groupID := "535683b3794d371327b"
alertConfigID := "57b76ddc96e8215c017ceafb"

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/alertConfigs/%s", groupID, alertConfigID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"id": "533dc40ae4b00835ff81eaee",
"groupId": "535683b3794d371327b",
"eventTypeName": "OUTSIDE_METRIC_THRESHOLD",
"created": "2016-08-23T20:26:50Z",
"updated": "2016-08-23T20:26:50Z",
"enabled": true,
"matchers": [
{
"fieldName": "HOSTNAME_AND_PORT",
"operator": "EQUALS",
"value": "mongo.example.com:27017"
}
],
"notifications": [
{
"typeName": "MICROSOFT_TEAMS",
"intervalMin": 5,
"delayMin": 0,
"microsoftTeamsWebhookUrl": "http://941a-47-225-212-178.ngrok.io",
"roles" : ["GROUP_DATA_ACCESS_ADMIN", "GROUP_DATA_ACCESS_READ_ONLY"]
}
],
"metricThreshold": {
"metricName": "ASSERT_REGULAR",
"operator": "LESS_THAN",
"threshold": 99.0,
"units": "RAW",
"mode": "AVERAGE"
}
}`)
})

alertConfiguration, _, err := client.AlertConfigurations.GetAnAlertConfig(ctx, groupID, alertConfigID)
if err != nil {
t.Fatalf("AlertConfigurations.GetAnAlertConfigTeams returned error: %v", err)
}

expected := &AlertConfiguration{
ID: "533dc40ae4b00835ff81eaee",
GroupID: "535683b3794d371327b",
EventTypeName: "OUTSIDE_METRIC_THRESHOLD",
Created: "2016-08-23T20:26:50Z",
Updated: "2016-08-23T20:26:50Z",
Enabled: pointy.Bool(true),
Matchers: []Matcher{
{
FieldName: "HOSTNAME_AND_PORT",
Operator: "EQUALS",
Value: "mongo.example.com:27017",
},
},
Notifications: []Notification{
{
TypeName: "MICROSOFT_TEAMS",
IntervalMin: 5,
DelayMin: pointy.Int(0),
MicrosoftTeamsWebhookURL: "http://941a-47-225-212-178.ngrok.io",
Roles: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_DATA_ACCESS_READ_ONLY"},
},
},
MetricThreshold: &MetricThreshold{
MetricName: "ASSERT_REGULAR",
Operator: "LESS_THAN",
Threshold: 99.0,
Units: "RAW",
Mode: "AVERAGE",
},
}

if diff := deep.Equal(alertConfiguration, expected); diff != nil {
t.Error(diff)
}
}

func TestAlertConfiguration_GetAnAlertConfigWebhook(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

groupID := "535683b3794d371327b"
alertConfigID := "57b76ddc96e8215c017ceafb"

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/alertConfigs/%s", groupID, alertConfigID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"id": "533dc40ae4b00835ff81eaee",
"groupId": "535683b3794d371327b",
"eventTypeName": "OUTSIDE_METRIC_THRESHOLD",
"created": "2016-08-23T20:26:50Z",
"updated": "2016-08-23T20:26:50Z",
"enabled": true,
"matchers": [
{
"fieldName": "HOSTNAME_AND_PORT",
"operator": "EQUALS",
"value": "mongo.example.com:27017"
}
],
"notifications": [
{
"typeName": "WEBHOOK",
"intervalMin": 5,
"delayMin": 0,
"webhookSecret": "SECRET",
"webhookUrl": "http://941a-47-225-212-178.ngrok.io",
"roles" : ["GROUP_DATA_ACCESS_ADMIN", "GROUP_DATA_ACCESS_READ_ONLY"]
}
],
"metricThreshold": {
"metricName": "ASSERT_REGULAR",
"operator": "LESS_THAN",
"threshold": 99.0,
"units": "RAW",
"mode": "AVERAGE"
}
}`)
})

alertConfiguration, _, err := client.AlertConfigurations.GetAnAlertConfig(ctx, groupID, alertConfigID)
if err != nil {
t.Fatalf("AlertConfigurations.GetAnAlertConfig returned error: %v", err)
}

expected := &AlertConfiguration{
ID: "533dc40ae4b00835ff81eaee",
GroupID: "535683b3794d371327b",
EventTypeName: "OUTSIDE_METRIC_THRESHOLD",
Created: "2016-08-23T20:26:50Z",
Updated: "2016-08-23T20:26:50Z",
Enabled: pointy.Bool(true),
Matchers: []Matcher{
{
FieldName: "HOSTNAME_AND_PORT",
Operator: "EQUALS",
Value: "mongo.example.com:27017",
},
},
Notifications: []Notification{
{
TypeName: "WEBHOOK",
IntervalMin: 5,
DelayMin: pointy.Int(0),
WebhookSecret: "SECRET",
WebhookURL: "http://941a-47-225-212-178.ngrok.io",
Roles: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_DATA_ACCESS_READ_ONLY"},
},
},
MetricThreshold: &MetricThreshold{
MetricName: "ASSERT_REGULAR",
Operator: "LESS_THAN",
Threshold: 99.0,
Units: "RAW",
Mode: "AVERAGE",
},
}

if diff := deep.Equal(alertConfiguration, expected); diff != nil {
t.Error(diff)
}
}

0 comments on commit ca26a1f

Please sign in to comment.