Skip to content

Commit

Permalink
fix: avoid diff in state after import for undefined optional attribut…
Browse files Browse the repository at this point in the history
…e (alert_configuration.notification.interval_min)
  • Loading branch information
AgustinBettati committed Aug 24, 2023
1 parent 57cede4 commit 3752da6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mongodbatlas/fw_resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ func (r *AlertConfigurationRS) Schema(ctx context.Context, req resource.SchemaRe
},
"interval_min": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"mobile_number": schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -709,9 +713,6 @@ func newTFNotificationModelList(matlasSlice []matlas.Notification, currStateNoti
if !currState.EmailAddress.IsNull() {
newState.EmailAddress = conversion.StringNullIfEmpty(value.EmailAddress)
}
if !currState.IntervalMin.IsNull() {
newState.IntervalMin = types.Int64Value(int64(value.IntervalMin))
}
if !currState.MobileNumber.IsNull() {
newState.MobileNumber = conversion.StringNullIfEmpty(value.MobileNumber)
}
Expand All @@ -728,6 +729,7 @@ func newTFNotificationModelList(matlasSlice []matlas.Notification, currStateNoti
newState.Username = conversion.StringNullIfEmpty(value.Username)
}

newState.IntervalMin = types.Int64Value(int64(value.IntervalMin))
newState.DelayMin = types.Int64Value(int64(*value.DelayMin))
newState.EmailEnabled = types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled)
newState.SMSEnabled = types.BoolValue(value.SMSEnabled != nil && *value.SMSEnabled)
Expand Down
33 changes: 33 additions & 0 deletions mongodbatlas/fw_resource_mongodbatlas_alert_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,39 @@ func TestAccConfigRSAlertConfiguration_importConfigNotifications(t *testing.T) {
})
}

// used for testing notification that does not define interval_min attribute
func TestAccConfigRSAlertConfiguration_importPagerDuty(t *testing.T) {
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
var (
resourceName = "mongodbatlas_alert_configuration.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
serviceKey = os.Getenv("PAGER_DUTY_SERVICE_KEY")
alert = &matlas.AlertConfiguration{}
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckBasic(t) },
ProtoV6ProviderFactories: testAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasAlertConfigurationPagerDutyConfig(projectID, serviceKey, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(resourceName, alert),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasAlertConfigurationImportStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"notification.0.service_key"}, // service key is not returned by api in import operation
},
},
})
}

func TestAccConfigRSAlertConfiguration_DataDog(t *testing.T) {
SkipTestExtCred(t) // Will skip because requires external credentials aka api key
SkipTest(t) // Will force skip if enabled
Expand Down

0 comments on commit 3752da6

Please sign in to comment.