From e90dae2c9c97f2fac06b5ca22a8ee89d68209551 Mon Sep 17 00:00:00 2001 From: brookesargent Date: Tue, 26 Nov 2024 08:21:40 -0500 Subject: [PATCH] no variables without template validation --- .../provider/webhook_recipient_resource.go | 19 ++++- .../webhook_recipient_resource_test.go | 79 ++++++++++++++++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/internal/provider/webhook_recipient_resource.go b/internal/provider/webhook_recipient_resource.go index 499380cc..5f05b63d 100644 --- a/internal/provider/webhook_recipient_resource.go +++ b/internal/provider/webhook_recipient_resource.go @@ -170,8 +170,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) + // template variables cannot be configured without a template and variable names cannot be duplicated + validateAttributesWhenVariablesIncluded(ctx, data, resp) } func (r *webhookRecipientResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { @@ -501,16 +501,27 @@ func validateAttributesWhenTemplatesIncluded(ctx context.Context, data models.We } } -func validateTemplateVarsNotDuplicated(ctx context.Context, data models.WebhookRecipientModel, resp *resource.ValidateConfigResponse) { +func validateAttributesWhenVariablesIncluded(ctx context.Context, data models.WebhookRecipientModel, resp *resource.ValidateConfigResponse) { + var templates []models.WebhookTemplateModel + data.Templates.ElementsAs(ctx, &templates, false) + var variables []models.TemplateVariableModel data.Variables.ElementsAs(ctx, &variables, false) + if len(variables) >= 1 && len(templates) == 0 { + resp.Diagnostics.AddAttributeError( + path.Root("variable").AtListIndex(0), + "Conflicting configuration arguments", + "cannot configure a \"variable\" without also configuring a \"template\"", + ) + } + 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("name"), + path.Root("variable").AtListIndex(i).AtName("name"), "Conflicting configuration arguments", "cannot have more than one \"variable\" with the same \"name\"", ) diff --git a/internal/provider/webhook_recipient_resource_test.go b/internal/provider/webhook_recipient_resource_test.go index fa385118..4416e318 100644 --- a/internal/provider/webhook_recipient_resource_test.go +++ b/internal/provider/webhook_recipient_resource_test.go @@ -424,7 +424,7 @@ resource "honeycombio_webhook_recipient" "test" { resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.#", "2"), resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.0.name", "variable1"), resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.0.default_value", ""), - resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.1.name", "variable2"), + resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.1.name", ""), resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.1.default_value", "critical"), resource.TestCheckNoResourceAttr("honeycombio_webhook_recipient.test", "secret"), ), @@ -475,6 +475,13 @@ resource "honeycombio_webhook_recipient" "test" { t.Run("custom webhook validations error when they should", func(t *testing.T) { name := test.RandomStringWithPrefix("test.", 20) url := test.RandomURL() + body := `<