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

Fix #171: added validation to avoid nil type error #173

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions mongodbatlas/resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,16 @@ func expandAlertConfigurationMetricThreshold(d *schema.ResourceData) *matlas.Met
}

func flattenAlertConfigurationMetricThreshold(m *matlas.MetricThreshold) map[string]interface{} {
return map[string]interface{}{
"metric_name": m.MetricName,
"operator": m.Operator,
"threshold": cast.ToString(m.Threshold),
"units": m.Units,
"mode": m.Mode,
if m != nil {
return map[string]interface{}{
"metric_name": m.MetricName,
"operator": m.Operator,
"threshold": cast.ToString(m.Threshold),
"units": m.Units,
"mode": m.Mode,
}
}
return map[string]interface{}{}
}

func expandAlertConfigurationNotification(d *schema.ResourceData) []matlas.Notification {
Expand Down
45 changes: 35 additions & 10 deletions mongodbatlas/resource_mongodbatlas_alert_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func TestAccResourceMongoDBAtlasAlertConfiguration_whitMetricUpdated(t *testing.
},
})
}

func TestAccResourceMongoDBAtlasAlertConfiguration_importBasic(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")

Expand All @@ -170,6 +171,30 @@ func TestAccResourceMongoDBAtlasAlertConfiguration_importBasic(t *testing.T) {
})
}

func TestAccResourceMongoDBAtlasAlertConfiguration_importConfigNotifications(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")

resourceName := "mongodbatlas_alert_configuration.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasAlertConfigurationConfigNotifications(projectID, true, true, false),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasAlertConfigurationImportStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project_id"},
},
},
})
}

func testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName string, alert *matlas.AlertConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*matlas.Client)
Expand Down Expand Up @@ -228,21 +253,21 @@ func testAccMongoDBAtlasAlertConfigurationConfig(projectID string, enabled bool)
project_id = "%s"
event_type = "OUTSIDE_METRIC_THRESHOLD"
enabled = "%t"

notification {
type_name = "GROUP"
interval_min = 5
delay_min = 0
sms_enabled = false
email_enabled = true
}

matcher {
field_name = "HOSTNAME_AND_PORT"
operator = "EQUALS"
value = "SECONDARY"
}

metric_threshold = {
metric_name = "ASSERT_REGULAR"
operator = "LESS_THAN"
Expand All @@ -260,7 +285,7 @@ func testAccMongoDBAtlasAlertConfigurationConfigNotifications(projectID string,
project_id = "%[1]s"
event_type = "NO_PRIMARY"
enabled = "%[2]t"

notification {
type_name = "GROUP"
interval_min = 5
Expand All @@ -286,15 +311,15 @@ func testAccMongoDBAtlasAlertConfigurationConfigWithMatchers(projectID string, e
project_id = "%s"
event_type = "NO_PRIMARY"
enabled = "%t"

notification {
type_name = "GROUP"
interval_min = 5
delay_min = 0
sms_enabled = %t
email_enabled = %t
}

matcher {
field_name = "%s"
operator = "%s"
Expand All @@ -305,7 +330,7 @@ func testAccMongoDBAtlasAlertConfigurationConfigWithMatchers(projectID string, e
operator = "%s"
value = "%s"
}
}
}
`, projectID, enabled, smsEnabled, emailEnabled,
m1.FieldName, m1.Operator, m1.Value,
m2.FieldName, m2.Operator, m2.Value)
Expand All @@ -317,21 +342,21 @@ func testAccMongoDBAtlasAlertConfigurationConfigWithMetrictUpdated(projectID str
project_id = "%s"
event_type = "OUTSIDE_METRIC_THRESHOLD"
enabled = "%t"

notification {
type_name = "GROUP"
interval_min = 5
delay_min = 0
sms_enabled = false
email_enabled = true
}

matcher {
field_name = "HOSTNAME_AND_PORT"
operator = "EQUALS"
value = "SECONDARY"
}

metric_threshold = {
metric_name = "ASSERT_REGULAR"
operator = "LESS_THAN"
Expand Down