Skip to content

Commit 25b070d

Browse files
authored
chore: Updates alert_configuration resource with new SDK (#1851)
* remove NonEmptyToPtr * fix tests * rename test helper functions
1 parent 1171c7b commit 25b070d

7 files changed

+161
-164
lines changed

internal/service/alertconfiguration/data_source_alert_configuration_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func TestAccConfigDSAlertConfiguration_basic(t *testing.T) {
2222
resource.ParallelTest(t, resource.TestCase{
2323
PreCheck: func() { acc.PreCheckBasic(t) },
2424
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
25-
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
25+
CheckDestroy: checkDestroy,
2626
Steps: []resource.TestStep{
2727
{
28-
Config: testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName),
28+
Config: configBasicDS(orgID, projectName),
2929
Check: resource.ComposeTestCheckFunc(
30-
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
30+
checkExists(dataSourceName, alert),
3131
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
3232
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
3333
resource.TestCheckResourceAttrSet(dataSourceName, "notification.0.notifier_id"),
@@ -51,12 +51,12 @@ func TestAccConfigDSAlertConfiguration_withThreshold(t *testing.T) {
5151
resource.ParallelTest(t, resource.TestCase{
5252
PreCheck: func() { acc.PreCheckBasic(t) },
5353
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
54-
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
54+
CheckDestroy: checkDestroy,
5555
Steps: []resource.TestStep{
5656
{
57-
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectName, true, 1),
57+
Config: configWithThreshold(orgID, projectName, true, 1),
5858
Check: resource.ComposeTestCheckFunc(
59-
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
59+
checkExists(dataSourceName, alert),
6060
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
6161
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
6262
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
@@ -81,12 +81,12 @@ func TestAccConfigDSAlertConfiguration_withOutput(t *testing.T) {
8181
resource.ParallelTest(t, resource.TestCase{
8282
PreCheck: func() { acc.PreCheckBasic(t) },
8383
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
84-
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
84+
CheckDestroy: checkDestroy,
8585
Steps: []resource.TestStep{
8686
{
87-
Config: testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outputLabel),
87+
Config: configWithOutputs(orgID, projectName, outputLabel),
8888
Check: resource.ComposeTestCheckFunc(
89-
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
89+
checkExists(dataSourceName, alert),
9090
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
9191
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
9292
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
@@ -114,20 +114,20 @@ func TestAccConfigDSAlertConfiguration_withPagerDuty(t *testing.T) {
114114
resource.Test(t, resource.TestCase{
115115
PreCheck: func() { acc.PreCheckBasic(t) },
116116
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
117-
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
117+
CheckDestroy: checkDestroy,
118118
Steps: []resource.TestStep{
119119
{
120-
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey, true),
120+
Config: configWithPagerDutyDS(orgID, projectName, serviceKey, true),
121121
Check: resource.ComposeTestCheckFunc(
122-
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
122+
checkExists(dataSourceName, alert),
123123
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
124124
),
125125
},
126126
},
127127
})
128128
}
129129

130-
func testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName string) string {
130+
func configBasicDS(orgID, projectName string) string {
131131
return fmt.Sprintf(`
132132
resource "mongodbatlas_project" "test" {
133133
name = %[2]q
@@ -168,7 +168,7 @@ func testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName string) string {
168168
`, orgID, projectName)
169169
}
170170

171-
func testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectName string, enabled bool, threshold float64) string {
171+
func configWithThreshold(orgID, projectName string, enabled bool, threshold float64) string {
172172
return fmt.Sprintf(`
173173
resource "mongodbatlas_project" "test" {
174174
name = %[2]q
@@ -208,7 +208,7 @@ func testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectNa
208208
`, orgID, projectName, enabled, threshold)
209209
}
210210

211-
func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outputLabel string) string {
211+
func configWithOutputs(orgID, projectName, outputLabel string) string {
212212
return fmt.Sprintf(`
213213
resource "mongodbatlas_project" "test" {
214214
name = %[2]q
@@ -246,7 +246,7 @@ func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outp
246246
`, orgID, projectName, outputLabel)
247247
}
248248

249-
func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey string, enabled bool) string {
249+
func configWithPagerDutyDS(orgID, projectName, serviceKey string, enabled bool) string {
250250
return fmt.Sprintf(`
251251
resource "mongodbatlas_project" "test" {
252252
name = %[2]q

internal/service/alertconfiguration/data_source_alert_configurations_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAccConfigDSAlertConfigurations_basic(t *testing.T) {
3030
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
3131
Steps: []resource.TestStep{
3232
{
33-
Config: configBasic(orgID, projectName),
33+
Config: configBasicPluralDS(orgID, projectName),
3434
Check: resource.ComposeTestCheckFunc(
3535
checkCount(dataSourceName),
3636
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
@@ -106,7 +106,7 @@ func TestAccConfigDSAlertConfigurations_totalCount(t *testing.T) {
106106
})
107107
}
108108

109-
func configBasic(orgID, projectName string) string {
109+
func configBasicPluralDS(orgID, projectName string) string {
110110
return fmt.Sprintf(`
111111
resource "mongodbatlas_project" "test" {
112112
name = %[2]q

internal/service/alertconfiguration/model_alert_configuration.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
"go.mongodb.org/atlas-sdk/v20231115003/admin"
1111
)
1212

13-
func NewNotificationList(tfNotificationSlice []TfNotificationModel) ([]admin.AlertsNotificationRootForGroup, error) {
14-
notifications := make([]admin.AlertsNotificationRootForGroup, 0)
13+
func NewNotificationList(list []TfNotificationModel) (*[]admin.AlertsNotificationRootForGroup, error) {
14+
notifications := make([]admin.AlertsNotificationRootForGroup, len(list))
1515

16-
for i := range tfNotificationSlice {
17-
if !tfNotificationSlice[i].IntervalMin.IsNull() && tfNotificationSlice[i].IntervalMin.ValueInt64() > 0 {
18-
typeName := tfNotificationSlice[i].TypeName.ValueString()
16+
for i := range list {
17+
if !list[i].IntervalMin.IsNull() && list[i].IntervalMin.ValueInt64() > 0 {
18+
typeName := list[i].TypeName.ValueString()
1919
if strings.EqualFold(typeName, pagerDuty) || strings.EqualFold(typeName, opsGenie) || strings.EqualFold(typeName, victorOps) {
2020
return nil, fmt.Errorf(`'interval_min' must not be set if type_name is 'PAGER_DUTY', 'OPS_GENIE' or 'VICTOR_OPS'`)
2121
}
2222
}
2323
}
2424

25-
for i := range tfNotificationSlice {
26-
n := &tfNotificationSlice[i]
27-
notification := admin.AlertsNotificationRootForGroup{
25+
for i := range list {
26+
n := &list[i]
27+
notifications[i] = admin.AlertsNotificationRootForGroup{
2828
ApiToken: n.APIToken.ValueStringPointer(),
2929
ChannelName: n.ChannelName.ValueStringPointer(),
3030
DatadogApiKey: n.DatadogAPIKey.ValueStringPointer(),
@@ -43,17 +43,16 @@ func NewNotificationList(tfNotificationSlice []TfNotificationModel) ([]admin.Ale
4343
Username: n.Username.ValueStringPointer(),
4444
VictorOpsApiKey: n.VictorOpsAPIKey.ValueStringPointer(),
4545
VictorOpsRoutingKey: n.VictorOpsRoutingKey.ValueStringPointer(),
46-
Roles: conversion.NonEmptyToPtr(n.Roles),
46+
Roles: &n.Roles,
4747
MicrosoftTeamsWebhookUrl: n.MicrosoftTeamsWebhookURL.ValueStringPointer(),
4848
WebhookSecret: n.WebhookSecret.ValueStringPointer(),
4949
WebhookUrl: n.WebhookURL.ValueStringPointer(),
5050
}
5151
if !n.NotifierID.IsUnknown() {
52-
notification.NotifierId = n.NotifierID.ValueStringPointer()
52+
notifications[i].NotifierId = n.NotifierID.ValueStringPointer()
5353
}
54-
notifications = append(notifications, notification)
5554
}
56-
return notifications, nil
55+
return &notifications, nil
5756
}
5857

5958
func NewThreshold(tfThresholdConfigSlice []TfThresholdConfigModel) *admin.GreaterThanRawThreshold {
@@ -83,18 +82,16 @@ func NewMetricThreshold(tfMetricThresholdConfigSlice []TfMetricThresholdConfigMo
8382
}
8483
}
8584

86-
func NewMatcherList(tfMatcherSlice []TfMatcherModel) []map[string]interface{} {
87-
matchers := make([]map[string]interface{}, 0)
88-
89-
for i := range tfMatcherSlice {
90-
matcher := map[string]interface{}{
91-
"fieldName": tfMatcherSlice[i].FieldName.ValueString(),
92-
"operator": tfMatcherSlice[i].Operator.ValueString(),
93-
"value": tfMatcherSlice[i].Value.ValueString(),
85+
func NewMatcherList(list []TfMatcherModel) *[]map[string]any {
86+
matchers := make([]map[string]any, len(list))
87+
for i, matcher := range list {
88+
matchers[i] = map[string]any{
89+
"fieldName": matcher.FieldName.ValueString(),
90+
"operator": matcher.Operator.ValueString(),
91+
"value": matcher.Value.ValueString(),
9492
}
95-
matchers = append(matchers, matcher)
9693
}
97-
return matchers
94+
return &matchers
9895
}
9996

10097
func NewTFAlertConfigurationModel(apiRespConfig *admin.GroupAlertsConfig, currState *TfAlertConfigurationRSModel) TfAlertConfigurationRSModel {

internal/service/alertconfiguration/model_alert_configuration_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestNotificationSDKToTFModel(t *testing.T) {
4545
SmsEnabled: admin.PtrBool(disabled),
4646
EmailEnabled: admin.PtrBool(enabled),
4747
ChannelName: admin.PtrString("#channel"),
48-
Roles: conversion.NonEmptyToPtr(roles),
48+
Roles: &roles,
4949
ApiToken: admin.PtrString("newApiToken"),
5050
},
5151
},
@@ -290,7 +290,7 @@ func TestNotificationTFModelToSDK(t *testing.T) {
290290
DelayMin: admin.PtrInt(delayMin),
291291
SmsEnabled: admin.PtrBool(disabled),
292292
EmailEnabled: admin.PtrBool(enabled),
293-
Roles: conversion.NonEmptyToPtr(roles),
293+
Roles: &roles,
294294
},
295295
},
296296
},
@@ -299,7 +299,7 @@ func TestNotificationTFModelToSDK(t *testing.T) {
299299
for _, tc := range testCases {
300300
t.Run(tc.name, func(t *testing.T) {
301301
apiReqResult, _ := alertconfiguration.NewNotificationList(tc.tfModel)
302-
if !reflect.DeepEqual(apiReqResult, *tc.expectedSDKReq) {
302+
if !reflect.DeepEqual(*apiReqResult, *tc.expectedSDKReq) {
303303
t.Errorf("created sdk model did not match expected output")
304304
}
305305
})
@@ -416,7 +416,7 @@ func TestMatcherTFModelToSDK(t *testing.T) {
416416

417417
for _, tc := range testCases {
418418
t.Run(tc.name, func(t *testing.T) {
419-
apiReqResult := alertconfiguration.NewMatcherList(tc.tfModel)
419+
apiReqResult := *alertconfiguration.NewMatcherList(tc.tfModel)
420420
if !reflect.DeepEqual(apiReqResult, tc.expectedSDKReq) {
421421
t.Errorf("created sdk model did not match expected output.\n Expected: %s \n Result: %s", tc.expectedSDKReq, apiReqResult)
422422
}

internal/service/alertconfiguration/resource_alert_configuration.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (r *alertConfigurationRS) Create(ctx context.Context, req resource.CreateRe
380380
apiReq := &admin.GroupAlertsConfig{
381381
EventTypeName: alertConfigPlan.EventType.ValueStringPointer(),
382382
Enabled: alertConfigPlan.Enabled.ValueBoolPointer(),
383-
Matchers: conversion.NonEmptyToPtr(NewMatcherList(alertConfigPlan.Matcher)),
383+
Matchers: NewMatcherList(alertConfigPlan.Matcher),
384384
MetricThreshold: NewMetricThreshold(alertConfigPlan.MetricThresholdConfig),
385385
Threshold: NewThreshold(alertConfigPlan.ThresholdConfig),
386386
}
@@ -390,7 +390,7 @@ func (r *alertConfigurationRS) Create(ctx context.Context, req resource.CreateRe
390390
resp.Diagnostics.AddError(errorCreateAlertConf, err.Error())
391391
return
392392
}
393-
apiReq.Notifications = conversion.NonEmptyToPtr(notifications)
393+
apiReq.Notifications = notifications
394394

395395
apiResp, _, err := connV2.AlertConfigurationsApi.CreateAlertConfiguration(ctx, projectID, apiReq).Execute()
396396
if err != nil {
@@ -482,7 +482,7 @@ func (r *alertConfigurationRS) Update(ctx context.Context, req resource.UpdateRe
482482
}
483483

484484
if !reflect.DeepEqual(alertConfigPlan.Matcher, alertConfigState.Matcher) {
485-
apiReq.Matchers = conversion.NonEmptyToPtr(NewMatcherList(alertConfigPlan.Matcher))
485+
apiReq.Matchers = NewMatcherList(alertConfigPlan.Matcher)
486486
}
487487

488488
// Always refresh structure to handle service keys being obfuscated coming back from read API call
@@ -491,7 +491,7 @@ func (r *alertConfigurationRS) Update(ctx context.Context, req resource.UpdateRe
491491
resp.Diagnostics.AddError(errorUpdateAlertConf, err.Error())
492492
return
493493
}
494-
apiReq.Notifications = conversion.NonEmptyToPtr(notifications)
494+
apiReq.Notifications = notifications
495495

496496
var updatedAlertConfigResp *admin.GroupAlertsConfig
497497

0 commit comments

Comments
 (0)