Skip to content

Commit

Permalink
Update parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Hume committed Nov 3, 2023
1 parent 813d293 commit 3c3011b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 49 deletions.
9 changes: 6 additions & 3 deletions integration/source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ resource "materialize_source_webhook" "example_webhook_source" {
name = "example_webhook_source"
comment = "source webhook comment"

cluster_name = materialize_cluster.cluster_source.name
body_format = "json"
include_headers = false
include_headers {
all = true
}

cluster_name = materialize_cluster.cluster_source.name
body_format = "json"
}

resource "materialize_source_grant" "source_grant_select" {
Expand Down
33 changes: 18 additions & 15 deletions pkg/materialize/source_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type HeaderStruct struct {
Bytes bool
}

type IncludeHeadersStruct struct {
All bool
Only []string
Not []string
}

type CheckOptionsStruct struct {
Field FieldStruct
Alias string
Expand All @@ -31,8 +37,7 @@ type SourceWebhookBuilder struct {
size string
bodyFormat string
includeHeader []HeaderStruct
includeHeaders bool
excludeHeaders []string
includeHeaders IncludeHeadersStruct
checkOptions []CheckOptionsStruct
checkExpression string
}
Expand All @@ -59,16 +64,11 @@ func (b *SourceWebhookBuilder) IncludeHeader(h []HeaderStruct) *SourceWebhookBui
return b
}

func (b *SourceWebhookBuilder) IncludeHeaders(h bool) *SourceWebhookBuilder {
func (b *SourceWebhookBuilder) IncludeHeaders(h IncludeHeadersStruct) *SourceWebhookBuilder {
b.includeHeaders = h
return b
}

func (b *SourceWebhookBuilder) ExcludeHeaders(e []string) *SourceWebhookBuilder {
b.excludeHeaders = e
return b
}

func (b *SourceWebhookBuilder) CheckOptions(o []CheckOptionsStruct) *SourceWebhookBuilder {
b.checkOptions = o
return b
Expand Down Expand Up @@ -100,16 +100,19 @@ func (b *SourceWebhookBuilder) Create() error {
}
}

if b.includeHeaders {
if b.includeHeaders.All || len(b.includeHeaders.Only) > 0 || len(b.includeHeaders.Not) > 0 {
q.WriteString(` INCLUDE HEADERS`)
}

if len(b.excludeHeaders) > 0 {
var ih []string
for _, h := range b.excludeHeaders {
ih = append(ih, fmt.Sprintf("NOT %s", QuoteString(h)))
var headers []string
for _, h := range b.includeHeaders.Only {
headers = append(headers, QuoteString(h))
}
for _, h := range b.includeHeaders.Not {
headers = append(headers, fmt.Sprintf("NOT %s", QuoteString(h)))
}
if len(headers) > 0 {
q.WriteString(fmt.Sprintf(` (%s)`, strings.Join(headers, ", ")))
}
q.WriteString(fmt.Sprintf(` (%s)`, strings.Join(ih, ", ")))
}

if len(b.checkOptions) > 0 || b.checkExpression != "" {
Expand Down
9 changes: 4 additions & 5 deletions pkg/materialize/source_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ func TestSourceWebhookCreateIncludeHeaders(t *testing.T) {
`CREATE SOURCE "database"."schema"."webhook_source" IN CLUSTER "cluster" FROM WEBHOOK BODY FORMAT JSON INCLUDE HEADERS \(NOT 'authorization', NOT 'x-api-key'\);`,
).WillReturnResult(sqlmock.NewResult(1, 1))

var excludeHeaders = []string{"authorization", "x-api-key"}

b := NewSourceWebhookBuilder(db, sourceWebhook)
b.ClusterName("cluster")
b.BodyFormat("JSON")
b.IncludeHeaders(true)
b.ExcludeHeaders(excludeHeaders)
b.IncludeHeaders(IncludeHeadersStruct{
Not: []string{"authorization", "x-api-key"},
})

if err := b.Create(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -132,7 +131,7 @@ func TestSourceWebhookCreateSegment(t *testing.T) {
b.ClusterName("cluster")
b.BodyFormat("JSON")
b.IncludeHeader(includeHeader)
b.IncludeHeaders(true)
b.IncludeHeaders(IncludeHeadersStruct{All: true})
b.CheckOptions(checkOptions)
b.CheckExpression("decode(headers->'x-signature', 'hex') = hmac(body, secret, 'sha1')")

Expand Down
12 changes: 9 additions & 3 deletions pkg/provider/acceptance_source_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func TestAccSourceWebhook_basic(t *testing.T) {
resource.TestCheckResourceAttr("materialize_source_webhook.test", "name", sourceName),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "cluster_name", clusterName),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "body_format", "json"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers", "false"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers.0.only.0", "a"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers.0.only.1", "b"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers.0.not.0", "c"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers.0.not.1", "d"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "ownership_role", roleName),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "size", ""),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "check_options.0.field.0.body", "true"),
Expand Down Expand Up @@ -101,7 +104,6 @@ func TestAccSourceWebhook_update(t *testing.T) {
testAccCheckSourceWebhookExists("materialize_source_webhook.test"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "name", newSourceName),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "body_format", "json"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "include_headers", "false"),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "ownership_role", roleName),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "size", ""),
resource.TestCheckResourceAttr("materialize_source_webhook.test", "check_options.0.field.0.body", "true"),
Expand Down Expand Up @@ -136,9 +138,13 @@ resource "materialize_source_webhook" "test" {
name = "%[4]s"
cluster_name = materialize_cluster.example_cluster.name
body_format = "json"
include_headers = false
ownership_role = materialize_role.test.name
include_headers {
only = ["a", "b"]
not = ["c", "d"]
}
check_options {
field {
body = true
Expand Down
68 changes: 51 additions & 17 deletions pkg/resources/resource_source_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,38 @@ var sourceWebhookSchema = map[string]*schema.Schema{
},
"include_headers": {
Description: "Include headers in the webhook.",
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"exclude_headers": {
Description: "Headers to exclude from mapping.",
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ForceNew: true,
RequiredWith: []string{"include_headers"},
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"all": {
Description: "Include all headers.",
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"include_headers.0.only", "include_headers.0.not"},
AtLeastOneOf: []string{"include_headers.0.all", "include_headers.0.only", "include_headers.0.not"},
},
"only": {
Description: "Headers that should be included.",
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ConflictsWith: []string{"include_headers.0.all"},
AtLeastOneOf: []string{"include_headers.0.all", "include_headers.0.only", "include_headers.0.not"},
},
"not": {
Description: "Headers that should be excluded.",
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ConflictsWith: []string{"include_headers.0.all"},
AtLeastOneOf: []string{"include_headers.0.all", "include_headers.0.only", "include_headers.0.not"},
},
},
},
Optional: true,
MinItems: 1,
MaxItems: 1,
ForceNew: true,
},
"check_options": {
Description: "The check options for the webhook.",
Expand Down Expand Up @@ -160,14 +181,8 @@ func sourceWebhookCreate(ctx context.Context, d *schema.ResourceData, meta inter

b.ClusterName(clusterName).
BodyFormat(bodyFormat).
IncludeHeaders(d.Get("include_headers").(bool)).
CheckExpression(d.Get("check_expression").(string))

if v, ok := d.GetOk("exclude_headers"); ok {
h := materialize.GetSliceValueString(v.([]interface{}))
b.ExcludeHeaders(h)
}

if v, ok := d.GetOk("include_header"); ok {
var headers []materialize.HeaderStruct
for _, header := range v.([]interface{}) {
Expand All @@ -181,6 +196,25 @@ func sourceWebhookCreate(ctx context.Context, d *schema.ResourceData, meta inter
b.IncludeHeader(headers)
}

if v, ok := d.GetOk("include_headers"); ok {
var i materialize.IncludeHeadersStruct
u := v.([]interface{})[0].(map[string]interface{})
if v, ok := u["all"]; ok {
i.All = v.(bool)
}

if v, ok := u["only"]; ok {
o := materialize.GetSliceValueString(v.([]interface{}))
i.Only = o
}

if v, ok := u["not"]; ok {
n := materialize.GetSliceValueString(v.([]interface{}))
i.Not = n
}
b.IncludeHeaders(i)
}

if v, ok := d.GetOk("check_options"); ok {
var options []materialize.CheckOptionsStruct
for _, option := range v.([]interface{}) {
Expand Down
16 changes: 10 additions & 6 deletions pkg/resources/resource_source_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import (
)

var inSourceWebhook = map[string]interface{}{
"name": "webhook_source",
"schema_name": "schema",
"database_name": "database",
"cluster_name": "cluster",
"body_format": "JSON",
"include_headers": true,
"name": "webhook_source",
"schema_name": "schema",
"database_name": "database",
"cluster_name": "cluster",
"body_format": "JSON",
"include_headers": []interface{}{
map[string]interface{}{
"all": true,
},
},
"check_options": []interface{}{
map[string]interface{}{
"field": []interface{}{map[string]interface{}{
Expand Down

0 comments on commit 3c3011b

Please sign in to comment.