Skip to content

Commit

Permalink
add template variable validations
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Nov 25, 2024
1 parent af873b0 commit 1f9841f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
36 changes: 35 additions & 1 deletion internal/provider/webhook_recipient_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package provider
import (
"context"
"errors"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand All @@ -29,7 +32,8 @@ var (
_ resource.ResourceWithImportState = &webhookRecipientResource{}
_ resource.ResourceWithValidateConfig = &webhookRecipientResource{}

webhookTemplateTypes = []string{"trigger", "exhaustion_time", "budget_rate"}
webhookTemplateTypes = []string{"trigger", "exhaustion_time", "budget_rate"}
webhookTemplateNameRegex = regexp.MustCompile(`^[a-z](?:[a-zA-Z0-9]+$)?$`)
)

type webhookRecipientResource struct {
Expand Down Expand Up @@ -116,15 +120,25 @@ func (*webhookRecipientResource) Schema(_ context.Context, _ resource.SchemaRequ
},
"variable": schema.SetNestedBlock{
Description: "Variables for webhook templates",
Validators: []validator.Set{
setvalidator.SizeAtMost(10),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Description: "The name of the variable",
Validators: []validator.String{
stringvalidator.LengthAtMost(63),
stringvalidator.RegexMatches(webhookTemplateNameRegex, "must be an alphanumeric string beginning with a lowercase letter"),
},
},
"default_value": schema.StringAttribute{
Description: "An optional default value for the variable",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(255),
},
},
},
},
Expand Down Expand Up @@ -157,6 +171,8 @@ func (r *webhookRecipientResource) ValidateConfig(ctx context.Context, req resou

// only allow one template of each type (trigger, budget_rate, exhaustion_time)
validateAttributesWhenTemplatesIncluded(ctx, data, resp)
// template variable names cannot be duplicated
validateTemplateVarsNotDuplicated(ctx, data, resp)
}

func (r *webhookRecipientResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand Down Expand Up @@ -485,3 +501,21 @@ func validateAttributesWhenTemplatesIncluded(ctx context.Context, data models.We

}
}

func validateTemplateVarsNotDuplicated(ctx context.Context, data models.WebhookRecipientModel, resp *resource.ValidateConfigResponse) {
var variables []models.TemplateVariableModel
data.Variables.ElementsAs(ctx, &variables, false)

duplicateMap := make(map[string]bool)
for i, v := range variables {
name := v.Name.ValueString()
if duplicateMap[name] {
resp.Diagnostics.AddAttributeError(
path.Root("template").AtListIndex(i).AtName("type"),
"Conflicting configuration arguments",
fmt.Sprintf("cannot have more than one \"variable\" with the name \"%s\"", name),
)
}
duplicateMap[name] = true
}
}
4 changes: 2 additions & 2 deletions internal/provider/webhook_recipient_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ resource "honeycombio_webhook_recipient" "test" {
})
})

t.Run("custom webhook when a template is removed", func(t *testing.T) {
t.Run("custom webhook succeeds when a template is removed", func(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()
body := `<<EOT
Expand Down Expand Up @@ -302,7 +302,7 @@ resource "honeycombio_webhook_recipient" "test" {
})
})

t.Run("custom webhook when a variable is removed", func(t *testing.T) {
t.Run("custom webhook succeeds when a variable is removed", func(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()
body := `<<EOT
Expand Down

0 comments on commit 1f9841f

Please sign in to comment.