Skip to content

Commit a308471

Browse files
fix: alert configuration data source nil pointer with third party notifications (#1513)
* 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 * execute tests in parallel as they are not using same project
1 parent 1cdb9ca commit a308471

4 files changed

+98
-65
lines changed

mongodbatlas/fw_data_source_mongodbatlas_alert_configuration_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ func TestAccConfigDSAlertConfiguration_withOutput(t *testing.T) {
102102
}
103103

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

113113
resource.Test(t, resource.TestCase{
114-
PreCheck: func() { testAccPreCheck(t) },
114+
PreCheck: func() { testAccPreCheckBasic(t) },
115115
ProtoV6ProviderFactories: testAccProviderV6Factories,
116116
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
117117
Steps: []resource.TestStep{
118118
{
119-
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(projectID, serviceKey, true),
119+
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey, true),
120120
Check: resource.ComposeTestCheckFunc(
121121
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
122122
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
@@ -245,16 +245,20 @@ func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outp
245245
`, orgID, projectName, outputLabel)
246246
}
247247

248-
func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(projectID, serviceKey string, enabled bool) string {
248+
func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey string, enabled bool) string {
249249
return fmt.Sprintf(`
250+
resource "mongodbatlas_project" "test" {
251+
name = %[2]q
252+
org_id = %[1]q
253+
}
250254
resource "mongodbatlas_alert_configuration" "test" {
251-
project_id = %[1]q
255+
project_id = mongodbatlas_project.test.id
252256
event_type = "NO_PRIMARY"
253-
enabled = "%[3]t"
257+
enabled = "%[4]t"
254258
255259
notification {
256260
type_name = "PAGER_DUTY"
257-
service_key = %[2]q
261+
service_key = %[3]q
258262
delay_min = 0
259263
}
260264
}
@@ -263,5 +267,5 @@ data "mongodbatlas_alert_configuration" "test" {
263267
project_id = "${mongodbatlas_alert_configuration.test.project_id}"
264268
alert_configuration_id = "${mongodbatlas_alert_configuration.test.id}"
265269
}
266-
`, projectID, serviceKey, enabled)
270+
`, orgID, projectName, serviceKey, enabled)
267271
}

mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2121
"github.com/hashicorp/terraform-plugin-framework/types"
2222
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/conversion"
23+
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
2324
"github.com/mwielbut/pointy"
2425
"go.mongodb.org/atlas-sdk/v20231001001/admin"
2526
matlas "go.mongodb.org/atlas/mongodbatlas"
@@ -744,9 +745,9 @@ func newTFNotificationModelListV2(n []admin.AlertsNotificationRootForGroup, curr
744745
Roles: value.Roles,
745746
ChannelName: conversion.StringPtrNullIfEmpty(value.ChannelName),
746747
DatadogRegion: conversion.StringPtrNullIfEmpty(value.DatadogRegion),
747-
DelayMin: types.Int64Value(int64(*value.DelayMin)),
748+
DelayMin: types.Int64PointerValue(util.IntPtrToInt64Ptr(value.DelayMin)),
748749
EmailAddress: conversion.StringPtrNullIfEmpty(value.EmailAddress),
749-
IntervalMin: types.Int64Value(int64(*value.IntervalMin)),
750+
IntervalMin: types.Int64PointerValue(util.IntPtrToInt64Ptr(value.IntervalMin)),
750751
MobileNumber: conversion.StringPtrNullIfEmpty(value.MobileNumber),
751752
OpsGenieRegion: conversion.StringPtrNullIfEmpty(value.OpsGenieRegion),
752753
TeamID: conversion.StringPtrNullIfEmpty(value.TeamId),
@@ -804,8 +805,8 @@ func newTFNotificationModelListV2(n []admin.AlertsNotificationRootForGroup, curr
804805
newState.Username = conversion.StringPtrNullIfEmpty(value.Username)
805806
}
806807

807-
newState.IntervalMin = types.Int64Value(int64(*value.IntervalMin))
808-
newState.DelayMin = types.Int64Value(int64(*value.DelayMin))
808+
newState.IntervalMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.IntervalMin))
809+
newState.DelayMin = types.Int64PointerValue(util.IntPtrToInt64Ptr(value.DelayMin))
809810
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)
810811
newState.SMSEnabled = types.BoolValue(value.SmsEnabled != nil && *value.SmsEnabled)
811812

mongodbatlas/fw_resource_mongodbatlas_alert_configuration_test.go

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,17 @@ func TestAccConfigRSAlertConfiguration_importConfigNotifications(t *testing.T) {
364364
})
365365
}
366366

367+
// dummy keys used for credential values in third party notifications
368+
const dummy32CharKey = "11111111111111111111111111111111"
369+
const dummy36CharKey = "11111111-1111-1111-1111-111111111111"
370+
367371
// used for testing notification that does not define interval_min attribute
368372
func TestAccConfigRSAlertConfiguration_importPagerDuty(t *testing.T) {
369-
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
370373
var (
371374
resourceName = "mongodbatlas_alert_configuration.test"
372-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
373-
serviceKey = os.Getenv("PAGER_DUTY_SERVICE_KEY")
375+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
376+
projectName = acctest.RandomWithPrefix("test-acc")
377+
serviceKey = dummy32CharKey
374378
alert = &matlas.AlertConfiguration{}
375379
)
376380

@@ -380,7 +384,7 @@ func TestAccConfigRSAlertConfiguration_importPagerDuty(t *testing.T) {
380384
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
381385
Steps: []resource.TestStep{
382386
{
383-
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(projectID, serviceKey, true),
387+
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(orgID, projectName, serviceKey, true),
384388
Check: resource.ComposeTestCheckFunc(
385389
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
386390
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
@@ -398,23 +402,22 @@ func TestAccConfigRSAlertConfiguration_importPagerDuty(t *testing.T) {
398402
}
399403

400404
func TestAccConfigRSAlertConfiguration_DataDog(t *testing.T) {
401-
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
402-
SkipTest(t) // Will force skip if enabled
403405
var (
404406
resourceName = "mongodbatlas_alert_configuration.test"
405-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
406-
ddAPIKey = os.Getenv("DD_API_KEY")
407+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
408+
projectName = acctest.RandomWithPrefix("test-acc")
409+
ddAPIKey = dummy32CharKey
407410
ddRegion = "US"
408411
alert = &matlas.AlertConfiguration{}
409412
)
410413

411-
resource.Test(t, resource.TestCase{
412-
PreCheck: func() { testAccPreCheck(t) },
414+
resource.ParallelTest(t, resource.TestCase{
415+
PreCheck: func() { testAccPreCheckBasic(t) },
413416
ProtoV6ProviderFactories: testAccProviderV6Factories,
414417
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
415418
Steps: []resource.TestStep{
416419
{
417-
Config: testAccMongoDBAtlasAlertConfigurationConfigWithDataDog(projectID, ddAPIKey, ddRegion, true),
420+
Config: testAccMongoDBAtlasAlertConfigurationConfigWithDataDog(orgID, projectName, ddAPIKey, ddRegion, true),
418421
Check: resource.ComposeTestCheckFunc(
419422
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
420423
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
@@ -425,21 +428,21 @@ func TestAccConfigRSAlertConfiguration_DataDog(t *testing.T) {
425428
}
426429

427430
func TestAccConfigRSAlertConfiguration_PagerDuty(t *testing.T) {
428-
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
429431
var (
430432
resourceName = "mongodbatlas_alert_configuration.test"
431-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
432-
serviceKey = os.Getenv("PAGER_DUTY_SERVICE_KEY")
433+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
434+
projectName = acctest.RandomWithPrefix("test-acc")
435+
serviceKey = dummy32CharKey
433436
alert = &matlas.AlertConfiguration{}
434437
)
435438

436-
resource.Test(t, resource.TestCase{
437-
PreCheck: func() { testAccPreCheck(t) },
439+
resource.ParallelTest(t, resource.TestCase{
440+
PreCheck: func() { testAccPreCheckBasic(t) },
438441
ProtoV6ProviderFactories: testAccProviderV6Factories,
439442
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
440443
Steps: []resource.TestStep{
441444
{
442-
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(projectID, serviceKey, true),
445+
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(orgID, projectName, serviceKey, true),
443446
Check: resource.ComposeTestCheckFunc(
444447
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
445448
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
@@ -450,21 +453,21 @@ func TestAccConfigRSAlertConfiguration_PagerDuty(t *testing.T) {
450453
}
451454

452455
func TestAccConfigRSAlertConfiguration_OpsGenie(t *testing.T) {
453-
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
454456
var (
455457
resourceName = "mongodbatlas_alert_configuration.test"
456-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
457-
apiKey = os.Getenv("OPS_GENIE_API_KEY")
458+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
459+
projectName = acctest.RandomWithPrefix("test-acc")
460+
apiKey = dummy36CharKey
458461
alert = &matlas.AlertConfiguration{}
459462
)
460463

461-
resource.Test(t, resource.TestCase{
462-
PreCheck: func() { testAccPreCheck(t) },
464+
resource.ParallelTest(t, resource.TestCase{
465+
PreCheck: func() { testAccPreCheckBasic(t) },
463466
ProtoV6ProviderFactories: testAccProviderV6Factories,
464467
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
465468
Steps: []resource.TestStep{
466469
{
467-
Config: testAccMongoDBAtlasAlertConfigurationOpsGenieConfig(projectID, apiKey, true),
470+
Config: testAccMongoDBAtlasAlertConfigurationOpsGenieConfig(orgID, projectName, apiKey, true),
468471
Check: resource.ComposeTestCheckFunc(
469472
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
470473
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
@@ -475,21 +478,21 @@ func TestAccConfigRSAlertConfiguration_OpsGenie(t *testing.T) {
475478
}
476479

477480
func TestAccConfigRSAlertConfiguration_VictorOps(t *testing.T) {
478-
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
479481
var (
480482
resourceName = "mongodbatlas_alert_configuration.test"
481-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
482-
apiKey = os.Getenv("VICTOR_OPS_API_KEY")
483+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
484+
projectName = acctest.RandomWithPrefix("test-acc")
485+
apiKey = dummy36CharKey
483486
alert = &matlas.AlertConfiguration{}
484487
)
485488

486-
resource.Test(t, resource.TestCase{
487-
PreCheck: func() { testAccPreCheck(t) },
489+
resource.ParallelTest(t, resource.TestCase{
490+
PreCheck: func() { testAccPreCheckBasic(t) },
488491
ProtoV6ProviderFactories: testAccProviderV6Factories,
489492
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
490493
Steps: []resource.TestStep{
491494
{
492-
Config: testAccMongoDBAtlasAlertConfigurationVictorOpsConfig(projectID, apiKey, true),
495+
Config: testAccMongoDBAtlasAlertConfigurationVictorOpsConfig(orgID, projectName, apiKey, true),
493496
Check: resource.ComposeTestCheckFunc(
494497
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
495498
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
@@ -777,19 +780,23 @@ func testAccMongoDBAtlasAlertConfigurationConfigWithThresholdUpdated(orgID, proj
777780
`, orgID, projectName, enabled, threshold)
778781
}
779782

780-
func testAccMongoDBAtlasAlertConfigurationConfigWithDataDog(projectID, dataDogAPIKey, dataDogRegion string, enabled bool) string {
783+
func testAccMongoDBAtlasAlertConfigurationConfigWithDataDog(orgID, projectName, dataDogAPIKey, dataDogRegion string, enabled bool) string {
781784
return fmt.Sprintf(`
785+
resource "mongodbatlas_project" "test" {
786+
name = %[2]q
787+
org_id = %[1]q
788+
}
782789
resource "mongodbatlas_third_party_integration" "atlas_datadog" {
783-
project_id = "%[1]s"
790+
project_id = mongodbatlas_project.test.id
784791
type = "DATADOG"
785-
api_key = "%[3]s"
786-
region = "%[4]s"
792+
api_key = "%[4]s"
793+
region = "%[5]s"
787794
}
788795
789796
resource "mongodbatlas_alert_configuration" "test" {
790-
project_id = "%[1]s"
797+
project_id = mongodbatlas_project.test.id
791798
event_type = "REPLICATION_OPLOG_WINDOW_RUNNING_OUT"
792-
enabled = %t
799+
enabled = %[3]t
793800
794801
notification {
795802
type_name = "GROUP"
@@ -820,57 +827,69 @@ resource "mongodbatlas_alert_configuration" "test" {
820827
units = "HOURS"
821828
}
822829
}
823-
`, projectID, enabled, dataDogAPIKey, dataDogRegion)
830+
`, orgID, projectName, enabled, dataDogAPIKey, dataDogRegion)
824831
}
825832

826-
func testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(projectID, serviceKey string, enabled bool) string {
833+
func testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(orgID, projectName, serviceKey string, enabled bool) string {
827834
return fmt.Sprintf(`
835+
resource "mongodbatlas_project" "test" {
836+
name = %[2]q
837+
org_id = %[1]q
838+
}
828839
resource "mongodbatlas_alert_configuration" "test" {
829-
project_id = %[1]q
840+
project_id = mongodbatlas_project.test.id
830841
event_type = "NO_PRIMARY"
831-
enabled = "%[3]t"
842+
enabled = "%[4]t"
832843
833844
notification {
834845
type_name = "PAGER_DUTY"
835-
service_key = %[2]q
846+
service_key = %[3]q
836847
delay_min = 0
837848
}
838849
}
839-
`, projectID, serviceKey, enabled)
850+
`, orgID, projectName, serviceKey, enabled)
840851
}
841852

842-
func testAccMongoDBAtlasAlertConfigurationOpsGenieConfig(projectID, apiKey string, enabled bool) string {
853+
func testAccMongoDBAtlasAlertConfigurationOpsGenieConfig(orgID, projectName, apiKey string, enabled bool) string {
843854
return fmt.Sprintf(`
855+
resource "mongodbatlas_project" "test" {
856+
name = %[2]q
857+
org_id = %[1]q
858+
}
844859
resource "mongodbatlas_alert_configuration" "test" {
845-
project_id = %[1]q
860+
project_id = mongodbatlas_project.test.id
846861
event_type = "NO_PRIMARY"
847-
enabled = "%[3]t"
862+
enabled = "%[4]t"
848863
849864
notification {
850865
type_name = "OPS_GENIE"
851-
ops_genie_api_key = %[2]q
866+
ops_genie_api_key = %[3]q
852867
ops_genie_region = "US"
853868
delay_min = 0
854869
}
855870
}
856-
`, projectID, apiKey, enabled)
871+
`, orgID, projectName, apiKey, enabled)
857872
}
858873

859-
func testAccMongoDBAtlasAlertConfigurationVictorOpsConfig(projectID, apiKey string, enabled bool) string {
874+
func testAccMongoDBAtlasAlertConfigurationVictorOpsConfig(orgID, projectName, apiKey string, enabled bool) string {
860875
return fmt.Sprintf(`
876+
resource "mongodbatlas_project" "test" {
877+
name = %[2]q
878+
org_id = %[1]q
879+
}
861880
resource "mongodbatlas_alert_configuration" "test" {
862-
project_id = %[1]q
881+
project_id = mongodbatlas_project.test.id
863882
event_type = "NO_PRIMARY"
864-
enabled = "%[3]t"
883+
enabled = "%[4]t"
865884
866885
notification {
867886
type_name = "VICTOR_OPS"
868-
victor_ops_api_key = %[2]q
887+
victor_ops_api_key = %[3]q
869888
victor_ops_routing_key = "testing"
870889
delay_min = 0
871890
}
872891
}
873-
`, projectID, apiKey, enabled)
892+
`, orgID, projectName, apiKey, enabled)
874893
}
875894

876895
func testAccMongoDBAtlasAlertConfigurationConfigEmptyMetricThresholdConfig(orgID, projectName string, enabled bool) string {

mongodbatlas/util/type_conversion.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ func Int64PtrToIntPtr(i64 *int64) *int {
2828
return &i
2929
}
3030

31+
func IntPtrToInt64Ptr(i *int) *int64 {
32+
if i == nil {
33+
return nil
34+
}
35+
36+
i64 := int64(*i)
37+
return &i64
38+
}
39+
3140
// IsStringPresent returns true if the string is non-empty.
3241
func IsStringPresent(strPtr *string) bool {
3342
return strPtr != nil && len(*strPtr) > 0

0 commit comments

Comments
 (0)