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

chore: Updates alert_configuration resource with new SDK #1851

Merged
merged 3 commits into from
Jan 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func TestAccConfigDSAlertConfiguration_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName),
Config: configBasicDS(orgID, projectName),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
checkExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "notification.0.notifier_id"),
Expand All @@ -51,12 +51,12 @@ func TestAccConfigDSAlertConfiguration_withThreshold(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectName, true, 1),
Config: configWithThreshold(orgID, projectName, true, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
checkExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
Expand All @@ -81,12 +81,12 @@ func TestAccConfigDSAlertConfiguration_withOutput(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outputLabel),
Config: configWithOutputs(orgID, projectName, outputLabel),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
checkExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(dataSourceName, "notification.#", "1"),
Expand Down Expand Up @@ -114,20 +114,20 @@ func TestAccConfigDSAlertConfiguration_withPagerDuty(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey, true),
Config: configWithPagerDutyDS(orgID, projectName, serviceKey, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
checkExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
),
},
},
})
}

func testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName string) string {
func configBasicDS(orgID, projectName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
Expand Down Expand Up @@ -168,7 +168,7 @@ func testAccDSMongoDBAtlasAlertConfiguration(orgID, projectName string) string {
`, orgID, projectName)
}

func testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectName string, enabled bool, threshold float64) string {
func configWithThreshold(orgID, projectName string, enabled bool, threshold float64) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
Expand Down Expand Up @@ -208,7 +208,7 @@ func testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(orgID, projectNa
`, orgID, projectName, enabled, threshold)
}

func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outputLabel string) string {
func configWithOutputs(orgID, projectName, outputLabel string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
Expand Down Expand Up @@ -246,7 +246,7 @@ func testAccDSMongoDBAtlasAlertConfigurationWithOutputs(orgID, projectName, outp
`, orgID, projectName, outputLabel)
}

func testAccDSMongoDBAtlasAlertConfigurationConfigWithPagerDuty(orgID, projectName, serviceKey string, enabled bool) string {
func configWithPagerDutyDS(orgID, projectName, serviceKey string, enabled bool) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccConfigDSAlertConfigurations_basic(t *testing.T) {
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: configBasic(orgID, projectName),
Config: configBasicPluralDS(orgID, projectName),
Check: resource.ComposeTestCheckFunc(
checkCount(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestAccConfigDSAlertConfigurations_totalCount(t *testing.T) {
})
}

func configBasic(orgID, projectName string) string {
func configBasicPluralDS(orgID, projectName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[2]q
Expand Down
41 changes: 19 additions & 22 deletions internal/service/alertconfiguration/model_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"go.mongodb.org/atlas-sdk/v20231115003/admin"
)

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

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

for i := range tfNotificationSlice {
n := &tfNotificationSlice[i]
notification := admin.AlertsNotificationRootForGroup{
for i := range list {
n := &list[i]
notifications[i] = admin.AlertsNotificationRootForGroup{
ApiToken: n.APIToken.ValueStringPointer(),
ChannelName: n.ChannelName.ValueStringPointer(),
DatadogApiKey: n.DatadogAPIKey.ValueStringPointer(),
Expand All @@ -43,17 +43,16 @@ func NewNotificationList(tfNotificationSlice []TfNotificationModel) ([]admin.Ale
Username: n.Username.ValueStringPointer(),
VictorOpsApiKey: n.VictorOpsAPIKey.ValueStringPointer(),
VictorOpsRoutingKey: n.VictorOpsRoutingKey.ValueStringPointer(),
Roles: conversion.NonEmptyToPtr(n.Roles),
Roles: &n.Roles,
MicrosoftTeamsWebhookUrl: n.MicrosoftTeamsWebhookURL.ValueStringPointer(),
WebhookSecret: n.WebhookSecret.ValueStringPointer(),
WebhookUrl: n.WebhookURL.ValueStringPointer(),
}
if !n.NotifierID.IsUnknown() {
notification.NotifierId = n.NotifierID.ValueStringPointer()
notifications[i].NotifierId = n.NotifierID.ValueStringPointer()
}
notifications = append(notifications, notification)
}
return notifications, nil
return &notifications, nil
}

func NewThreshold(tfThresholdConfigSlice []TfThresholdConfigModel) *admin.GreaterThanRawThreshold {
Expand Down Expand Up @@ -83,18 +82,16 @@ func NewMetricThreshold(tfMetricThresholdConfigSlice []TfMetricThresholdConfigMo
}
}

func NewMatcherList(tfMatcherSlice []TfMatcherModel) []map[string]interface{} {
matchers := make([]map[string]interface{}, 0)

for i := range tfMatcherSlice {
matcher := map[string]interface{}{
"fieldName": tfMatcherSlice[i].FieldName.ValueString(),
"operator": tfMatcherSlice[i].Operator.ValueString(),
"value": tfMatcherSlice[i].Value.ValueString(),
func NewMatcherList(list []TfMatcherModel) *[]map[string]any {
matchers := make([]map[string]any, len(list))
for i, matcher := range list {
matchers[i] = map[string]any{
"fieldName": matcher.FieldName.ValueString(),
"operator": matcher.Operator.ValueString(),
"value": matcher.Value.ValueString(),
}
matchers = append(matchers, matcher)
}
return matchers
return &matchers
}

func NewTFAlertConfigurationModel(apiRespConfig *admin.GroupAlertsConfig, currState *TfAlertConfigurationRSModel) TfAlertConfigurationRSModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestNotificationSDKToTFModel(t *testing.T) {
SmsEnabled: admin.PtrBool(disabled),
EmailEnabled: admin.PtrBool(enabled),
ChannelName: admin.PtrString("#channel"),
Roles: conversion.NonEmptyToPtr(roles),
Roles: &roles,
ApiToken: admin.PtrString("newApiToken"),
},
},
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestNotificationTFModelToSDK(t *testing.T) {
DelayMin: admin.PtrInt(delayMin),
SmsEnabled: admin.PtrBool(disabled),
EmailEnabled: admin.PtrBool(enabled),
Roles: conversion.NonEmptyToPtr(roles),
Roles: &roles,
},
},
},
Expand All @@ -299,7 +299,7 @@ func TestNotificationTFModelToSDK(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiReqResult, _ := alertconfiguration.NewNotificationList(tc.tfModel)
if !reflect.DeepEqual(apiReqResult, *tc.expectedSDKReq) {
if !reflect.DeepEqual(*apiReqResult, *tc.expectedSDKReq) {
t.Errorf("created sdk model did not match expected output")
}
})
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestMatcherTFModelToSDK(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiReqResult := alertconfiguration.NewMatcherList(tc.tfModel)
apiReqResult := *alertconfiguration.NewMatcherList(tc.tfModel)
if !reflect.DeepEqual(apiReqResult, tc.expectedSDKReq) {
t.Errorf("created sdk model did not match expected output.\n Expected: %s \n Result: %s", tc.expectedSDKReq, apiReqResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (r *alertConfigurationRS) Create(ctx context.Context, req resource.CreateRe
apiReq := &admin.GroupAlertsConfig{
EventTypeName: alertConfigPlan.EventType.ValueStringPointer(),
Enabled: alertConfigPlan.Enabled.ValueBoolPointer(),
Matchers: conversion.NonEmptyToPtr(NewMatcherList(alertConfigPlan.Matcher)),
Matchers: NewMatcherList(alertConfigPlan.Matcher),
MetricThreshold: NewMetricThreshold(alertConfigPlan.MetricThresholdConfig),
Threshold: NewThreshold(alertConfigPlan.ThresholdConfig),
}
Expand All @@ -390,7 +390,7 @@ func (r *alertConfigurationRS) Create(ctx context.Context, req resource.CreateRe
resp.Diagnostics.AddError(errorCreateAlertConf, err.Error())
return
}
apiReq.Notifications = conversion.NonEmptyToPtr(notifications)
apiReq.Notifications = notifications

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

if !reflect.DeepEqual(alertConfigPlan.Matcher, alertConfigState.Matcher) {
apiReq.Matchers = conversion.NonEmptyToPtr(NewMatcherList(alertConfigPlan.Matcher))
apiReq.Matchers = NewMatcherList(alertConfigPlan.Matcher)
}

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

var updatedAlertConfigResp *admin.GroupAlertsConfig

Expand Down
Loading