-
Notifications
You must be signed in to change notification settings - Fork 210
fix: Removes interval_min validation blocking notification type updates in alert_configuration
#3882
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: Removes interval_min validation blocking notification type updates in alert_configuration
#3882
Changes from 2 commits
96f1570
625c4a3
9bbec77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ```release-note:bug | ||
| resource/mongodbatlas_alert_configuration: Fixes issue preventing updates from notification types supporting `interval_min` (e.g., MICROSOFT_TEAMS) to types that don't support it (PAGER_DUTY, OPS_GENIE, VICTOR_OPS) | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,6 +290,9 @@ func (r *alertConfigurationRS) Schema(ctx context.Context, req resource.SchemaRe | |
| "interval_min": schema.Int64Attribute{ | ||
| Optional: true, | ||
| Computed: true, | ||
| Validators: []validator.Int64{ | ||
| ValidIntervalMin(), | ||
| }, | ||
|
Comment on lines
+293
to
+295
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: If intervalMin is send in a request using incorrect type does the API return any error or simply ignores? Just thinking if we can avoid this validation from our side.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! According to the validator's documentation comment (lines 17-20 in I think it would be misleading if we don't raise an error if the user sets it for a type that doesn't support it |
||
| }, | ||
| "mobile_number": schema.StringAttribute{ | ||
| Optional: true, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package alertconfiguration | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| ) | ||
|
|
||
| // IntervalMinValidator validates that interval_min is not set for notification types | ||
| // that don't support it (PAGER_DUTY, OPS_GENIE, VICTOR_OPS). | ||
| // | ||
| // API-level validation cannot catch this error because when the notification type | ||
| // doesn't support interval_min, the nested_type in the API payload omits the field, | ||
| // so it's never sent to the API. Client-side validation ensures users get immediate | ||
| // feedback during Terraform's plan phase. | ||
| type IntervalMinValidator struct{} | ||
|
|
||
| func (v IntervalMinValidator) Description(_ context.Context) string { | ||
| return "'interval_min' must not be set if type_name is 'PAGER_DUTY', 'OPS_GENIE' or 'VICTOR_OPS'" | ||
| } | ||
|
|
||
| func (v IntervalMinValidator) MarkdownDescription(ctx context.Context) string { | ||
| return v.Description(ctx) | ||
| } | ||
|
|
||
| func (v IntervalMinValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) { | ||
| // If the value is unknown/null/0, there is nothing to validate. | ||
| if req.ConfigValue.ValueInt64() <= 0 { | ||
EspenAlbert marked this conversation as resolved.
Show resolved
Hide resolved
EspenAlbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
|
|
||
| // Parse the path to find which notification index we're validating | ||
| // Path format: notification[0].interval_min | ||
| pathStr := req.Path.String() | ||
|
||
| notificationIndex := -1 | ||
|
|
||
| // Extract the index from the path (e.g., "notification[0]" -> 0) | ||
| if idxStart := strings.Index(pathStr, "["); idxStart != -1 { | ||
| if idxEnd := strings.Index(pathStr[idxStart:], "]"); idxEnd != -1 { | ||
| idxStr := pathStr[idxStart+1 : idxStart+idxEnd] | ||
| if idx, err := strconv.Atoi(idxStr); err == nil { | ||
| notificationIndex = idx | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If we couldn't parse the index, skip validation | ||
| if notificationIndex < 0 { | ||
| return | ||
| } | ||
|
|
||
| // Get the entire notification list from config | ||
| var notifications []TfNotificationModel | ||
| diags := req.Config.GetAttribute(ctx, path.Root("notification"), ¬ifications) | ||
| if diags.HasError() { | ||
| // If we can't read notifications, skip validation (might be unknown during plan) | ||
| return | ||
| } | ||
|
|
||
| // Check if we have the notification at the parsed index | ||
| if notificationIndex >= len(notifications) { | ||
| return | ||
| } | ||
|
|
||
| notification := notifications[notificationIndex] | ||
| typeNameValue := notification.TypeName.ValueString() | ||
| // Check if the type_name is one of the unsupported types | ||
| if strings.EqualFold(typeNameValue, pagerDuty) || | ||
| strings.EqualFold(typeNameValue, opsGenie) || | ||
| strings.EqualFold(typeNameValue, victorOps) { | ||
| resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
| req.Path, | ||
| v.Description(ctx), | ||
| fmt.Sprintf("%d", req.ConfigValue.ValueInt64()), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| func ValidIntervalMin() validator.Int64 { | ||
| return IntervalMinValidator{} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: When value is unset in the config, doesnt the
IntervalMinvalue appear as unknown in the plan? Would expect validation is not ecountered, probably missing something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking the same, especially since we are not using UseStateForUnknown.
However, no reason to have it here as we are doing the validation in the schema.