-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add duplicate variable validation for notification recipient
- Loading branch information
1 parent
18b7397
commit d4ece22
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
internal/helper/validation/valid_notification_variables.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
|
||
"github.com/honeycombio/terraform-provider-honeycombio/internal/models" | ||
) | ||
|
||
var _ validator.Set = notificationVariablesValidator{} | ||
|
||
type notificationVariablesValidator struct { | ||
schemes []string | ||
} | ||
|
||
func (v notificationVariablesValidator) Description(_ context.Context) string { | ||
return "value must be a valid set of webhook variables" | ||
} | ||
|
||
func (v notificationVariablesValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v notificationVariablesValidator) ValidateSet(ctx context.Context, request validator.SetRequest, response *validator.SetResponse) { | ||
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
// variable names cannot be duplicated | ||
var variables []models.NotificationVariableModel | ||
response.Diagnostics.Append(request.ConfigValue.ElementsAs(ctx, &variables, false)...) | ||
|
||
duplicateMap := make(map[string]bool) | ||
for i, v := range variables { | ||
name := v.Name.ValueString() | ||
if duplicateMap[name] { | ||
response.Diagnostics.AddAttributeError( | ||
path.Root("variable").AtListIndex(i).AtName("name"), | ||
"Conflicting configuration arguments", | ||
"cannot have more than one \"variable\" with the same \"name\"", | ||
) | ||
} | ||
duplicateMap[name] = true | ||
} | ||
} | ||
|
||
// ValidQuerySpec determines if the provided JSON is a valid Honeycomb Query Specification | ||
func ValidNotificationVariables() validator.Set { | ||
return notificationVariablesValidator{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters