Skip to content

Commit

Permalink
no variables without template validation
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Nov 26, 2024
1 parent 89ef1da commit e90dae2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
19 changes: 15 additions & 4 deletions internal/provider/webhook_recipient_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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\"",
)
Expand Down
79 changes: 78 additions & 1 deletion internal/provider/webhook_recipient_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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 := `<<EOT
{
"name": " {{ .Name }}",
"id": " {{ .ID }}",
"description": " {{ .Description }}",
}
EOT`

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
Expand Down Expand Up @@ -532,6 +539,76 @@ resource "honeycombio_webhook_recipient" "test" {
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureRecipientDestroyed(t),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "%s"
url = "%s"
variable {
name = "severity"
default_value = "critical"
}
}`, name, url),
ExpectError: regexp.MustCompile(`cannot configure a "variable" without also configuring a "template"`),
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureRecipientDestroyed(t),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "%s"
url = "%s"
template {
type = "trigger"
body = %s
}
variable {
name = "severity"
default_value = "critical"
}
}`, name, url, body),
Check: resource.ComposeAggregateTestCheckFunc(
testAccEnsureRecipientExists(t, "honeycombio_webhook_recipient.test"),
resource.TestCheckResourceAttrSet("honeycombio_webhook_recipient.test", "id"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "name", name),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "url", url),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "template.#", "1"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "template.0.type", "trigger"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.#", "1"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.0.name", "severity"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "variable.0.default_value", "critical"),
),
},
{
Config: fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "%s"
url = "%s"
variable {
name = "severity"
default_value = "critical"
}
}`, name, url),
ExpectError: regexp.MustCompile(`cannot configure a "variable" without also configuring a "template"`),
},
},
})
})
}

Expand Down

0 comments on commit e90dae2

Please sign in to comment.