Skip to content

Commit

Permalink
Add responseHeader to valid condition fields (#168)
Browse files Browse the repository at this point in the history
* Add responseHeader to the valid condition fields

According to Fastly support and the SigSci dashboard, response headers
are a valid attribute to set conditions against. The TF provider does
not currently agree and the spurious warnings make it hard to debug
other SigSci issues. This adds responseHeader to the list to prevent
these warnings.

* Pull condition fields in schema from KnownConditionFields var

This is to avoid having to keep the actual validation and the
documentation in sync in six places.
  • Loading branch information
gabrielg authored May 12, 2023
1 parent 461e67a commit a885493
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
17 changes: 9 additions & 8 deletions provider/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,19 @@ var siteImporter = schema.ResourceImporter{
},
}

func validateConditionField(val interface{}, key string) ([]string, []error) {
knownFields := []string{
"scheme", "method", "path", "useragent", "domain", "ip", "responseCode", "agentname",
"paramname", "paramvalue", "country", "name", "valueString", "valueIp", "signalType",
"signal", "requestHeader", "queryParameter", "postParameter", "requestCookie", "value",
}
var KnownConditionFields = []string{
"scheme", "method", "path", "useragent", "domain", "ip", "responseCode", "agentname",
"paramname", "paramvalue", "country", "name", "valueString", "valueIp", "signalType",
"signal", "requestHeader", "queryParameter", "postParameter", "requestCookie", "value",
"responseHeader",
}

if existsInString(val.(string), knownFields...) {
func validateConditionField(val interface{}, key string) ([]string, []error) {
if existsInString(val.(string), KnownConditionFields...) {
return nil, nil
}

return []string{fmt.Sprintf("received %q for conditions.field. This is not necessarily an error, but we only know about the following values. If this is a new value, please open a PR to get it added.\n(%s)", val.(string), strings.Join(knownFields, ", "))}, nil
return []string{fmt.Sprintf("received %q for conditions.field. This is not necessarily an error, but we only know about the following values. If this is a new value, please open a PR to get it added.\n(%s)", val.(string), strings.Join(KnownConditionFields, ", "))}, nil
}

func validateActionResponseCode(val interface{}, key string) ([]string, []error) {
Expand Down
1 change: 1 addition & 0 deletions provider/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func TestValidateConditionField(t *testing.T) {
{in: "scheme"},
{in: "queryParameter"},
{in: "value"},
{in: "responseHeader"},

{in: "unknownconditionfield", wantErr: true},
}
Expand Down
7 changes: 4 additions & 3 deletions provider/resource_corp_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
Expand Down Expand Up @@ -83,7 +84,7 @@ func resourceCorpRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down Expand Up @@ -117,7 +118,7 @@ func resourceCorpRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down Expand Up @@ -150,7 +151,7 @@ func resourceCorpRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down
7 changes: 4 additions & 3 deletions provider/resource_site_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"fmt"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
Expand Down Expand Up @@ -100,7 +101,7 @@ func resourceSiteRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down Expand Up @@ -133,7 +134,7 @@ func resourceSiteRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down Expand Up @@ -166,7 +167,7 @@ func resourceSiteRule() *schema.Resource {
},
"field": {
Type: schema.TypeString,
Description: "type: single - (scheme, method, path, useragent, domain, ip, responseCode, agentname, paramname, paramvalue, country, name, valueString, valueIp, signalType, signal, requestHeader, queryParameter, postParameter)",
Description: fmt.Sprintf("type: single - (%s)", strings.Join(KnownConditionFields, ", ")),
Optional: true,
ValidateFunc: validateConditionField,
},
Expand Down

0 comments on commit a885493

Please sign in to comment.