Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Dec 2, 2024
1 parent ce073bd commit 59ae79b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 59 deletions.
8 changes: 4 additions & 4 deletions client/recipient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func TestRecipientsCustomWebhook(t *testing.T) {
assert.Equal(t, tr.Details.WebhookSecret, r.Details.WebhookSecret)
assert.Equal(t, tr.Details.WebhookPayloads, r.Details.WebhookPayloads)
assert.Equal(t, tr.Details.WebhookPayloads.TemplateVariables, r.Details.WebhookPayloads.TemplateVariables)
require.Len(t, tr.Details.WebhookHeaders, 1)
require.Len(t, r.Details.WebhookHeaders, 1)
assert.Equal(t, tr.Details.WebhookHeaders[0].Key, r.Details.WebhookHeaders[0].Key)
assert.Equal(t, tr.Details.WebhookHeaders[0].Value, r.Details.WebhookHeaders[0].Value)
if assert.Len(t, r.Details.WebhookHeaders, 1) {
assert.Equal(t, tr.Details.WebhookHeaders[0].Key, r.Details.WebhookHeaders[0].Key)
assert.Equal(t, tr.Details.WebhookHeaders[0].Value, r.Details.WebhookHeaders[0].Value)
}
})
}
}
Expand Down
25 changes: 6 additions & 19 deletions internal/provider/webhook_recipient_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package provider
import (
"context"
"errors"
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -36,11 +34,8 @@ var (
_ resource.ResourceWithValidateConfig = &webhookRecipientResource{}

webhookTemplateTypes = []string{"trigger", "exhaustion_time", "budget_rate"}
webhookHeaderDefaults = []string{"Content-Type", "User-Agent", "X-Honeycomb-Webhook-Token"}
webhookTemplateNameRegex = regexp.MustCompile(`^[a-z](?:[a-zA-Z0-9]+$)?$`)

defaultHeaderContentType = "Content-Type"
defaultHeaderUserAgent = "User-Agent"
defaultHeaderXHoneycombWebhookToken = "X-Honeycomb-Webhook-Token"
)

type webhookRecipientResource struct {
Expand Down Expand Up @@ -164,13 +159,16 @@ func (*webhookRecipientResource) Schema(_ context.Context, _ resource.SchemaRequ
Description: "The name or key for the header",
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.NoneOfCaseInsensitive(webhookHeaderDefaults...),
},
},
"value": schema.StringAttribute{
Description: "Value for the header",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
stringvalidator.LengthAtMost(256),
stringvalidator.LengthAtMost(512),
},
},
},
Expand Down Expand Up @@ -271,19 +269,8 @@ func (r *webhookRecipientResource) ValidateConfig(ctx context.Context, req resou
duplicateMap[name] = true
}

// webhook headers cannot used reserved keys and must be valid http headers
// webhook headers must be valid http headers
for i, h := range headers {
header := strings.ToLower(h.Name.ValueString())
if header == strings.ToLower(defaultHeaderContentType) ||
header == strings.ToLower(defaultHeaderUserAgent) ||
header == strings.ToLower(defaultHeaderXHoneycombWebhookToken) {
resp.Diagnostics.AddAttributeError(
path.Root("header").AtListIndex(i).AtName("name"),
"Conflicting configuration arguments",
fmt.Sprintf("cannot match reserved \"name\": %s", h.Name.ValueString()),
)
}

if !httpguts.ValidHeaderFieldName(h.Name.ValueString()) {
resp.Diagnostics.AddAttributeError(
path.Root("header").AtListIndex(i).AtName("name"),
Expand Down
83 changes: 47 additions & 36 deletions internal/provider/webhook_recipient_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,53 @@ resource "honeycombio_webhook_recipient" "test" {
},
})
})

t.Run("custom webhook succeeds when a header has no value", func(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()
body := `<<EOT
{
"name": " {{ .Name }}",
"id": " {{ .ID }}"
}
EOT`

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"
header {
name = "Authorization"
}
template {
type = "trigger"
body = %s
}
}`, 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", "header.#", "1"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "header.0.name", "Authorization"),
resource.TestCheckResourceAttr("honeycombio_webhook_recipient.test", "header.0.value", ""),
resource.TestCheckNoResourceAttr("honeycombio_webhook_recipient.test", "secret"),
),
},
},
})
})
}

func TestAcc_WebhookRecipientResource_validateDuplicateTemplateType(t *testing.T) {
Expand Down Expand Up @@ -667,42 +714,6 @@ resource "honeycombio_webhook_recipient" "test" {
})
}

func TestAcc_WebhookRecipientResource_validateReservedWebhookHeader(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()

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 = "body"
}
header {
name = "Content-Type"
value = "xml"
}
variable {
name = "severity"
default_value = "warning"
}
}`, name, url),
ExpectError: regexp.MustCompile(`cannot match reserved "name": Content-Type`),
},
},
})
}

func TestAcc_WebhookRecipientResource_validateInvalidWebhookHeader(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()
Expand Down

0 comments on commit 59ae79b

Please sign in to comment.