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: HELP: Error: The terraform-provider-mongodbatlas_v1.11.0 plugin crashed #1419

Merged
merged 2 commits into from
Aug 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions mongodbatlas/resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -515,8 +514,11 @@ func expandAlertConfigurationMatchers(d *schema.ResourceData) []matlas.Matcher {

if m, ok := d.GetOk("matcher"); ok {
for _, value := range m.([]interface{}) {
v := value.(map[string]interface{})
if value == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we do something similar to

			if ok, v := value.(map[string]interface{}); !ok {
				break
			}

break
}

v := value.(map[string]interface{})
matchers = append(matchers, matlas.Matcher{
FieldName: v["field_name"].(string),
Operator: v["operator"].(string),
Expand Down Expand Up @@ -610,6 +612,10 @@ func expandAlertConfigurationThresholdConfig(d *schema.ResourceData) *matlas.Thr
vL := value.([]interface{})

if len(vL) > 0 {
if vL[0] == nil {
return nil
}

v := vL[0].(map[string]interface{})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as here. I have not fully tried myself, but was wondering if something like ok, v := value.(map[string]interface{}); !ok would catch the impossibility to cast due to nil.

I also don't fully understand this one: in which cases would there be an array of length > 0 whose first value is nil? And why do we assume that all other values in the array will also be nil?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried to do it but the ok was still true but I am not 100% sure now 🤦 . I will add an update in a few minutes to see if this would work. Thanks for flagging this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, We can use the ok,v pattern here. I opened #1420 to address this comment. Thanks


return &matlas.Threshold{
Expand Down
69 changes: 69 additions & 0 deletions mongodbatlas/resource_mongodbatlas_alert_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ func TestAccConfigRSAlertConfiguration_EmptyMetricThresholdConfig(t *testing.T)
})
}

func TestAccConfigRSAlertConfiguration_EmptyMatcherMetricThresholdConfig(t *testing.T) {
var (
resourceName = "mongodbatlas_alert_configuration.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
alert = &matlas.AlertConfiguration{}
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckBasic(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasAlertConfigurationConfigEmptyMatcherMetricThresholdConfig(orgID, projectName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "notification.#", "1"),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func TestAccConfigRSAlertConfiguration_Notifications(t *testing.T) {
var (
resourceName = "mongodbatlas_alert_configuration.test"
Expand Down Expand Up @@ -808,3 +833,47 @@ resource "mongodbatlas_alert_configuration" "test" {
}
`, orgID, projectName, enabled)
}

func testAccMongoDBAtlasAlertConfigurationConfigEmptyMatcherMetricThresholdConfig(orgID, projectName 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 = mongodbatlas_project.test.id
event_type = "CLUSTER_MONGOS_IS_MISSING"
enabled = "%[3]t"

notification {
type_name = "GROUP"
interval_min = 60
delay_min = 0
sms_enabled = true
email_enabled = false
roles = ["GROUP_OWNER"]
}

matcher {
field_name = ""
operator = null
value = ""
}

metric_threshold_config {
metric_name = ""
operator = null
threshold = null
units = null
mode = ""
}

threshold_config {
operator = null
threshold = null
units = null
}
}
`, orgID, projectName, enabled)
}