Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports new notifier_id attribute in alert configuration notifications #1514

Merged
merged 11 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/atlas-alert-configurations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ chmod +x ./import-alerts.sh
terraform apply
```

**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.
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved

## Contingency Plans
If unhappy with the resource file or imports, here are some things that can be done:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ var alertConfigDSSchemaAttributes = map[string]schema.Attribute{
"team_name": schema.StringAttribute{
Computed: true,
},
"notifier_id": schema.StringAttribute{
Computed: true,
},
"type_name": schema.StringAttribute{
Computed: true,
},
Expand Down Expand Up @@ -428,6 +431,10 @@ func convertNotificationToCtyValues(notification *admin.AlertsNotificationRootFo
values["team_name"] = cty.StringVal(*notification.TeamName)
}

if util.IsStringPresent(notification.NotifierId) {
values["notifier_id"] = cty.StringVal(*notification.NotifierId)
}

if util.IsStringPresent(notification.TypeName) {
values["type_name"] = cty.StringVal(*notification.TypeName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAccConfigDSAlertConfiguration_basic(t *testing.T) {
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "notification.0.notifier_id"),
resource.TestCheckResourceAttr(dataSourceName, "matcher.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "metric_threshold_config.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "threshold_config.#", "0"),
Expand Down Expand Up @@ -102,21 +103,21 @@ func TestAccConfigDSAlertConfiguration_withOutput(t *testing.T) {
}

func TestAccConfigDSAlertConfiguration_withPagerDuty(t *testing.T) {
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
var (
alert = &matlas.AlertConfiguration{}
dataSourceName = "data.mongodbatlas_alert_configuration.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
serviceKey = os.Getenv("PAGER_DUTY_SERVICE_KEY")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
serviceKey = dummy32CharKey
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckBasic(t) },
ProtoV6ProviderFactories: testAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(projectID, serviceKey, true),
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
Expand Down Expand Up @@ -245,16 +246,20 @@ func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outp
`, orgID, projectName, outputLabel)
}

func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(projectID, serviceKey string, enabled bool) string {
func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey string, enabled bool) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
org_id = %[1]q
}
resource "mongodbatlas_alert_configuration" "test" {
project_id = %[1]q
project_id = mongodbatlas_project.test.id
event_type = "NO_PRIMARY"
enabled = "%[3]t"
enabled = "%[4]t"

notification {
type_name = "PAGER_DUTY"
service_key = %[2]q
service_key = %[3]q
delay_min = 0
}
}
Expand All @@ -263,5 +268,5 @@ data "mongodbatlas_alert_configuration" "test" {
project_id = "${mongodbatlas_alert_configuration.test.project_id}"
alert_configuration_id = "${mongodbatlas_alert_configuration.test.id}"
}
`, projectID, serviceKey, enabled)
`, orgID, projectName, serviceKey, enabled)
}
19 changes: 15 additions & 4 deletions mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
"github.com/mwielbut/pointy"
"go.mongodb.org/atlas-sdk/v20230201006/admin"
matlas "go.mongodb.org/atlas/mongodbatlas"
Expand Down Expand Up @@ -102,6 +103,7 @@
OpsGenieAPIKey types.String `tfsdk:"ops_genie_api_key"`
TeamID types.String `tfsdk:"team_id"`
TeamName types.String `tfsdk:"team_name"`
NotifierID types.String `tfsdk:"notifier_id"`
TypeName types.String `tfsdk:"type_name"`
ChannelName types.String `tfsdk:"channel_name"`
VictorOpsAPIKey types.String `tfsdk:"victor_ops_api_key"`
Expand Down Expand Up @@ -321,6 +323,10 @@
stringplanmodifier.UseStateForUnknown(),
},
},
"notifier_id": schema.StringAttribute{
Computed: true,
Optional: true,
},
"type_name": schema.StringAttribute{
Required: true,
Validators: []validator.String{
Expand Down Expand Up @@ -580,6 +586,7 @@
ServiceKey: value.ServiceKey.ValueString(),
SMSEnabled: value.SMSEnabled.ValueBoolPointer(),
TeamID: value.TeamID.ValueString(),
NotifierID: value.NotifierID.ValueString(),

Check failure on line 589 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / tf-validate (1.5.2)

unknown field NotifierID in struct literal of type "go.mongodb.org/atlas/mongodbatlas".Notification

Check failure on line 589 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / build

unknown field NotifierID in struct literal of type "go.mongodb.org/atlas/mongodbatlas".Notification
TypeName: value.TypeName.ValueString(),
Username: value.Username.ValueString(),
VictorOpsAPIKey: value.VictorOpsAPIKey.ValueString(),
Expand Down Expand Up @@ -670,6 +677,7 @@
TeamID: conversion.StringNullIfEmpty(value.TeamID),
TypeName: conversion.StringNullIfEmpty(value.TypeName),
Username: conversion.StringNullIfEmpty(value.Username),
NotifierID: types.StringValue(value.NotifierID),

Check failure on line 680 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / tf-validate (1.5.2)

value.NotifierID undefined (type "go.mongodb.org/atlas/mongodbatlas".Notification has no field or method NotifierID)

Check failure on line 680 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / build

value.NotifierID undefined (type "go.mongodb.org/atlas/mongodbatlas".Notification has no field or method NotifierID)
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved
EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled),
SMSEnabled: types.BoolValue(value.SMSEnabled != nil && *value.SMSEnabled),
}
Expand Down Expand Up @@ -722,6 +730,7 @@
newState.Username = conversion.StringNullIfEmpty(value.Username)
}

newState.NotifierID = types.StringValue(value.NotifierID)

Check failure on line 733 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / tf-validate (1.5.2)

value.NotifierID undefined (type "go.mongodb.org/atlas/mongodbatlas".Notification has no field or method NotifierID)

Check failure on line 733 in mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

View workflow job for this annotation

GitHub Actions / build

value.NotifierID undefined (type "go.mongodb.org/atlas/mongodbatlas".Notification has no field or method NotifierID)
newState.IntervalMin = types.Int64Value(int64(value.IntervalMin))
newState.DelayMin = types.Int64Value(int64(*value.DelayMin))
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)
Expand All @@ -744,12 +753,13 @@
Roles: value.Roles,
ChannelName: conversion.StringPtrNullIfEmpty(value.ChannelName),
DatadogRegion: conversion.StringPtrNullIfEmpty(value.DatadogRegion),
DelayMin: types.Int64Value(int64(*value.DelayMin)),
DelayMin: types.Int64PointerValue(util.IntPtrToInt64Ptr(value.DelayMin)),
EmailAddress: conversion.StringPtrNullIfEmpty(value.EmailAddress),
IntervalMin: types.Int64Value(int64(*value.IntervalMin)),
IntervalMin: types.Int64PointerValue(util.IntPtrToInt64Ptr(value.IntervalMin)),
MobileNumber: conversion.StringPtrNullIfEmpty(value.MobileNumber),
OpsGenieRegion: conversion.StringPtrNullIfEmpty(value.OpsGenieRegion),
TeamID: conversion.StringPtrNullIfEmpty(value.TeamId),
NotifierID: types.StringPointerValue(value.NotifierId),
TypeName: conversion.StringPtrNullIfEmpty(value.TypeName),
Username: conversion.StringPtrNullIfEmpty(value.Username),
EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled),
Expand Down Expand Up @@ -804,8 +814,9 @@
newState.Username = conversion.StringPtrNullIfEmpty(value.Username)
}

newState.IntervalMin = types.Int64Value(int64(*value.IntervalMin))
newState.DelayMin = types.Int64Value(int64(*value.DelayMin))
newState.NotifierID = types.StringPointerValue(value.NotifierId)
newState.IntervalMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.IntervalMin))
newState.DelayMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.DelayMin))
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)
newState.SMSEnabled = types.BoolValue(value.SmsEnabled != nil && *value.SmsEnabled)

Expand Down
Loading
Loading