Skip to content

Commit 7d38199

Browse files
feat: supports new notifier_id attribute in alert configuration notifications (#1514)
* fix: alert configuration data source nil pointer with third party notifications * modify all third party notifications to use fake credentials so acceptance tests are run in CI * extract dummy keys to common variables * remove usage of project id env variable in third party alert configuration tests * feat: supports new notifier_id attribute in alert configuration notifications * temprary change to use atlas go sdk version with changes * docs: align documentation of alert config singular data source with plural data source * fix merge conflict resolution * update atlas sdk to 0.34.0 * addressing docs PR comments
1 parent 1100e25 commit 7d38199

10 files changed

+220
-18
lines changed

examples/atlas-alert-configurations/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ chmod +x ./import-alerts.sh
1414
terraform apply
1515
```
1616

17+
**NOTE**: Third-party notifications will not contain their respective credentials as these are sensitive attributes. If you wish to perform updates on these notifications without providing the original credentials, the corresponding `notifier_id` attribute must be provided instead.
18+
1719
## Contingency Plans
1820
If unhappy with the resource file or imports, here are some things that can be done:
1921

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/mwielbut/pointy v1.1.0
2222
github.com/spf13/cast v1.5.1
2323
github.com/zclconf/go-cty v1.14.1
24-
go.mongodb.org/atlas v0.33.0
24+
go.mongodb.org/atlas v0.34.0
2525
go.mongodb.org/atlas-sdk/v20231001001 v20231001001.0.0
2626
go.mongodb.org/realm v0.1.0
2727
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,8 @@ github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRK
721721
github.com/zclconf/go-cty-yaml v1.0.2 h1:dNyg4QLTrv2IfJpm7Wtxi55ed5gLGOlPrZ6kMd51hY0=
722722
github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
723723
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
724-
go.mongodb.org/atlas v0.33.0 h1:qJhkEuJufh7sVDVHorTF/D7G7naQ1EJAzqf1aV29JWs=
725-
go.mongodb.org/atlas v0.33.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
724+
go.mongodb.org/atlas v0.34.0 h1:C6pDYjKWbjSZCsNoZpgNO6I5e/jH7OVwoQ0OXcoAFCg=
725+
go.mongodb.org/atlas v0.34.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
726726
go.mongodb.org/atlas-sdk/v20231001001 v20231001001.0.0 h1:7hl8ap9WzQDcaAO3/FWYdUYee7+taWWpXA4T4bPv6IE=
727727
go.mongodb.org/atlas-sdk/v20231001001 v20231001001.0.0/go.mod h1:2DismEF/fnloT92wjCkA1hpbBGrjQ5fPNAbC48mkRD4=
728728
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=

mongodbatlas/fw_data_source_mongodbatlas_alert_configuration.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ var alertConfigDSSchemaAttributes = map[string]schema.Attribute{
201201
"team_name": schema.StringAttribute{
202202
Computed: true,
203203
},
204+
"notifier_id": schema.StringAttribute{
205+
Computed: true,
206+
},
204207
"type_name": schema.StringAttribute{
205208
Computed: true,
206209
},
@@ -428,6 +431,10 @@ func convertNotificationToCtyValues(notification *admin.AlertsNotificationRootFo
428431
values["team_name"] = cty.StringVal(*notification.TeamName)
429432
}
430433

434+
if util.IsStringPresent(notification.NotifierId) {
435+
values["notifier_id"] = cty.StringVal(*notification.NotifierId)
436+
}
437+
431438
if util.IsStringPresent(notification.TypeName) {
432439
values["type_name"] = cty.StringVal(*notification.TypeName)
433440
}

mongodbatlas/fw_data_source_mongodbatlas_alert_configuration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestAccConfigDSAlertConfiguration_basic(t *testing.T) {
3030
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
3131
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
3232
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
33+
resource.TestCheckResourceAttrSet(dataSourceName, "notification.0.notifier_id"),
3334
resource.TestCheckResourceAttr(dataSourceName, "matcher.#", "1"),
3435
resource.TestCheckResourceAttr(dataSourceName, "metric_threshold_config.#", "1"),
3536
resource.TestCheckResourceAttr(dataSourceName, "threshold_config.#", "0"),

mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type tfNotificationModel struct {
103103
OpsGenieAPIKey types.String `tfsdk:"ops_genie_api_key"`
104104
TeamID types.String `tfsdk:"team_id"`
105105
TeamName types.String `tfsdk:"team_name"`
106+
NotifierID types.String `tfsdk:"notifier_id"`
106107
TypeName types.String `tfsdk:"type_name"`
107108
ChannelName types.String `tfsdk:"channel_name"`
108109
VictorOpsAPIKey types.String `tfsdk:"victor_ops_api_key"`
@@ -322,6 +323,10 @@ func (r *AlertConfigurationRS) Schema(ctx context.Context, req resource.SchemaRe
322323
stringplanmodifier.UseStateForUnknown(),
323324
},
324325
},
326+
"notifier_id": schema.StringAttribute{
327+
Computed: true,
328+
Optional: true,
329+
},
325330
"type_name": schema.StringAttribute{
326331
Required: true,
327332
Validators: []validator.String{
@@ -581,6 +586,7 @@ func newNotificationList(tfNotificationSlice []tfNotificationModel) ([]matlas.No
581586
ServiceKey: value.ServiceKey.ValueString(),
582587
SMSEnabled: value.SMSEnabled.ValueBoolPointer(),
583588
TeamID: value.TeamID.ValueString(),
589+
NotifierID: value.NotifierID.ValueString(),
584590
TypeName: value.TypeName.ValueString(),
585591
Username: value.Username.ValueString(),
586592
VictorOpsAPIKey: value.VictorOpsAPIKey.ValueString(),
@@ -671,6 +677,7 @@ func newTFNotificationModelList(matlasSlice []matlas.Notification, currStateNoti
671677
TeamID: conversion.StringNullIfEmpty(value.TeamID),
672678
TypeName: conversion.StringNullIfEmpty(value.TypeName),
673679
Username: conversion.StringNullIfEmpty(value.Username),
680+
NotifierID: types.StringValue(value.NotifierID),
674681
EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled),
675682
SMSEnabled: types.BoolValue(value.SMSEnabled != nil && *value.SMSEnabled),
676683
}
@@ -723,6 +730,7 @@ func newTFNotificationModelList(matlasSlice []matlas.Notification, currStateNoti
723730
newState.Username = conversion.StringNullIfEmpty(value.Username)
724731
}
725732

733+
newState.NotifierID = types.StringValue(value.NotifierID)
726734
newState.IntervalMin = types.Int64Value(int64(value.IntervalMin))
727735
newState.DelayMin = types.Int64Value(int64(*value.DelayMin))
728736
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)
@@ -751,6 +759,7 @@ func newTFNotificationModelListV2(n []admin.AlertsNotificationRootForGroup, curr
751759
MobileNumber: conversion.StringPtrNullIfEmpty(value.MobileNumber),
752760
OpsGenieRegion: conversion.StringPtrNullIfEmpty(value.OpsGenieRegion),
753761
TeamID: conversion.StringPtrNullIfEmpty(value.TeamId),
762+
NotifierID: types.StringPointerValue(value.NotifierId),
754763
TypeName: conversion.StringPtrNullIfEmpty(value.TypeName),
755764
Username: conversion.StringPtrNullIfEmpty(value.Username),
756765
EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled),
@@ -805,6 +814,7 @@ func newTFNotificationModelListV2(n []admin.AlertsNotificationRootForGroup, curr
805814
newState.Username = conversion.StringPtrNullIfEmpty(value.Username)
806815
}
807816

817+
newState.NotifierID = types.StringPointerValue(value.NotifierId)
808818
newState.IntervalMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.IntervalMin))
809819
newState.DelayMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.DelayMin))
810820
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)

mongodbatlas/fw_resource_mongodbatlas_alert_configuration_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,42 @@ func TestAccConfigRSAlertConfiguration_importPagerDuty(t *testing.T) {
401401
})
402402
}
403403

404+
func TestAccConfigRSAlertConfiguration_UpdatePagerDutyWithNotifierId(t *testing.T) {
405+
var (
406+
resourceName = "mongodbatlas_alert_configuration.test"
407+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
408+
projectName = acctest.RandomWithPrefix("test-acc")
409+
serviceKey = dummy32CharKey
410+
notifierID = "651dd9336afac13e1c112222"
411+
alert = &matlas.AlertConfiguration{}
412+
)
413+
414+
resource.ParallelTest(t, resource.TestCase{
415+
PreCheck: func() { testAccPreCheckBasic(t) },
416+
ProtoV6ProviderFactories: testAccProviderV6Factories,
417+
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
418+
Steps: []resource.TestStep{
419+
{
420+
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyNotifierIDConfig(orgID, projectName, notifierID, 10, &serviceKey),
421+
Check: resource.ComposeTestCheckFunc(
422+
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
423+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
424+
resource.TestCheckResourceAttr(resourceName, "notification.0.delay_min", "10"),
425+
resource.TestCheckResourceAttr(resourceName, "notification.0.service_key", serviceKey),
426+
),
427+
},
428+
{
429+
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyNotifierIDConfig(orgID, projectName, notifierID, 15, nil),
430+
Check: resource.ComposeTestCheckFunc(
431+
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
432+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
433+
resource.TestCheckResourceAttr(resourceName, "notification.0.delay_min", "15"),
434+
),
435+
},
436+
},
437+
})
438+
}
439+
404440
func TestAccConfigRSAlertConfiguration_DataDog(t *testing.T) {
405441
var (
406442
resourceName = "mongodbatlas_alert_configuration.test"
@@ -850,6 +886,31 @@ resource "mongodbatlas_alert_configuration" "test" {
850886
`, orgID, projectName, serviceKey, enabled)
851887
}
852888

889+
func testAccMongoDBAtlasAlertConfigurationPagerDutyNotifierIDConfig(orgID, projectName, notifierID string, delayMin int, serviceKey *string) string {
890+
var serviceKeyString string
891+
if serviceKey != nil {
892+
serviceKeyString = fmt.Sprintf(`service_key = %q`, *serviceKey)
893+
}
894+
return fmt.Sprintf(`
895+
resource "mongodbatlas_project" "test" {
896+
name = %[2]q
897+
org_id = %[1]q
898+
}
899+
resource "mongodbatlas_alert_configuration" "test" {
900+
project_id = mongodbatlas_project.test.id
901+
event_type = "NO_PRIMARY"
902+
enabled = "true"
903+
904+
notification {
905+
type_name = "PAGER_DUTY"
906+
notifier_id = %[3]q
907+
%[4]s
908+
delay_min = %[5]d
909+
}
910+
}
911+
`, orgID, projectName, notifierID, serviceKeyString, delayMin)
912+
}
913+
853914
func testAccMongoDBAtlasAlertConfigurationOpsGenieConfig(orgID, projectName, apiKey string, enabled bool) string {
854915
return fmt.Sprintf(`
855916
resource "mongodbatlas_project" "test" {

website/docs/d/alert_configuration.html.markdown

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ In addition to all arguments above, the following attributes are exported:
121121
* `updated` - Timestamp in ISO 8601 date and time format in UTC when this alert configuration was last updated.
122122
* `enabled` - If set to true, the alert configuration is enabled. If enabled is not exported it is set to false.
123123
* `event_type` - The type of event that will trigger an alert.
124+
* `matcher` - Rules to apply when matching an object against this alert configuration. See [matchers](#matchers).
125+
* `metric_threshold_config` - The threshold that causes an alert to be triggered. Required if `event_type_name` : `OUTSIDE_METRIC_THRESHOLD` or `OUTSIDE_SERVERLESS_METRIC_THRESHOLD`. See [metric threshold config](#metric-threshold-config).
126+
* `threshold_config` - Threshold that triggers an alert. Required if `event_type_name` is any value other than `OUTSIDE_METRIC_THRESHOLD` or `OUTSIDE_SERVERLESS_METRIC_THRESHOLD`. See [threshold config](#threshold-config).
127+
* `notifications` - List of notifications to send when an alert condition is detected. See [notifications](#notifications).
124128

125-
-> ***IMPORTANT:*** Event Type has many possible values. All current options at available at https://docs.atlas.mongodb.com/reference/api/alert-configurations-create-config/ Details for both conditional and metric based alerts can be found by selecting the tabs on the [alert config page](https://docs.atlas.mongodb.com/reference/api/alert-configurations-create-config/) and checking the latest eventTypeName options.
129+
-> ***IMPORTANT:*** Event Type has many possible values. Details for both conditional and metric based alerts can be found by selecting the tabs on the [alert config page](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Alert-Configurations/operation/createAlertConfiguration) and checking the latest eventTypeName options.
126130

127131
-> **NOTE:** If `event_type` is set to `OUTSIDE_METRIC_THRESHOLD` or `OUTSIDE_SERVERLESS_METRIC_THRESHOLD`, the `metric_threshold_config` field must also be configured.
128132

@@ -163,12 +167,12 @@ Rules to apply when matching an object against this alert configuration. Only en
163167
- `CONFIG`
164168
- `MONGOS`
165169

166-
### Metric Threshold Config (`metric_threshold_config`)
170+
### Metric Threshold Config
167171
The threshold that causes an alert to be triggered. Required if `event_type_name` : `OUTSIDE_METRIC_THRESHOLD` or `OUTSIDE_SERVERLESS_METRIC_THRESHOLD`.
168172

169173
* `metric_name` - Name of the metric to check. The full list being quite large, please refer to atlas docs [here for general metrics](https://docs.atlas.mongodb.com/reference/alert-host-metrics/#measurement-types) and [here for serverless metrics](https://www.mongodb.com/docs/atlas/reference/api/alert-configurations-create-config/#serverless-measurements)
170174

171-
* `operator` - Operator to apply when checking the current metric value against the threshold value.
175+
* `operator` - The operator to apply when checking the current metric value against the threshold value.
172176
Accepted values are:
173177
- `GREATER_THAN`
174178
- `LESS_THAN`
@@ -178,8 +182,8 @@ The threshold that causes an alert to be triggered. Required if `event_type_name
178182
Refer to the [MongoDB API Alert Configuration documentation](https://www.mongodb.com/docs/atlas/reference/api/alert-configurations-get-config/#request-body-parameters) for a list of accepted values.
179183
* `mode` - This must be set to AVERAGE. Atlas computes the current metric value as an average.
180184

181-
### Threshold Config (`threshold_config`)
182-
* `operator` - Operator to apply when checking the current metric value against the threshold value.
185+
### Threshold Config
186+
* `operator` - The operator to apply when checking the current metric value against the threshold value.
183187
Accepted values are:
184188
- `GREATER_THAN`
185189
- `LESS_THAN`
@@ -199,7 +203,7 @@ Notifications to send when an alert condition is detected.
199203
* `email_address` - Email address to which alert notifications are sent. Required for the EMAIL notifications type.
200204
* `email_enabled` - Flag indicating email notifications should be sent. Atlas returns this value if `type_name` is set to `ORG`, `GROUP`, or `USER`.
201205
* `flowdock_api_token` - The Flowdock personal API token. Required for the `FLOWDOCK` notifications type. If the token later becomes invalid, Atlas sends an email to the project owner and eventually removes the token.
202-
* `flow_name` - Flowdock flow name in lower-case letters. Required for the `FLOWDOCK` notifications type
206+
* `flow_name` - Flowdock flow name in lower-case letters. Required for the `FLOWDOCK` notifications type.
203207
* `interval_min` - Number of minutes to wait between successive notifications for unacknowledged alerts that are not resolved. The minimum value is 5.
204208
* `mobile_number` - Mobile number to which alert notifications are sent. Required for the SMS notifications type.
205209
* `ops_genie_api_key` - Opsgenie API Key. Required 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.
@@ -226,13 +230,13 @@ Notifications to send when an alert condition is detected.
226230
- `WEBHOOK`
227231
- `MICROSOFT_TEAMS`
228232

233+
* `notifier_id` - The notifier id is a system-generated unique identifier assigned to each notification method. This is needed when updating third-party notifications without requiring explicit authentication credentials.
229234
* `username` - Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Required for the `USER` notifications type.
230235
* `victor_ops_api_key` - VictorOps API key. Required 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.
231236
* `victor_ops_routing_key` - VictorOps routing key. Optional 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.
232237
* `webhook_secret` - Authentication secret for the `WEBHOOK` notifications type.
233238
* `webhook_url` - Target URL for the `WEBHOOK` notifications type.
234239
* `microsoft_teams_webhook_url` - Microsoft Teams channel incoming webhook URL. Required for the `MICROSOFT_TEAMS` notifications type.
235-
236240
* `roles` - Atlas role in current Project or Organization. Atlas returns this value if you set `type_name` to `ORG` or `GROUP`.
237241

238242
See detailed information for arguments and attributes: [MongoDB API Alert Configuration](https://docs.atlas.mongodb.com/reference/api/alert-configurations-get-config/)

0 commit comments

Comments
 (0)