Skip to content

Commit

Permalink
fix(request_settings): allow unsetting of action (#814)
Browse files Browse the repository at this point in the history
* style(requestsetting_test): remove line breaks

* test(requestsetting): validate current behaviour

* fix(request_setting): allow unsetting of action attribute

* fix(request_settings): fix test
  • Loading branch information
Integralist authored Feb 29, 2024
1 parent 1cd5da7 commit bd8c5ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
2 changes: 2 additions & 0 deletions fastly/block_fastly_service_requestsetting.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (h *RequestSettingServiceAttributeHandler) Update(_ context.Context, d *sch
opts.Action = gofastly.ToPointer(gofastly.RequestSettingActionLookup)
case "pass":
opts.Action = gofastly.ToPointer(gofastly.RequestSettingActionPass)
default:
opts.Action = gofastly.ToPointer(gofastly.RequestSettingActionUnset)
}
}
if v, ok := modified["bypass_busy_wait"]; ok {
Expand Down
74 changes: 58 additions & 16 deletions fastly/block_fastly_service_requestsetting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestAccFastlyServiceVCLRequestSetting_basic(t *testing.T) {
}

rq2 := gofastly.RequestSetting{
Action: gofastly.ToPointer(gofastly.RequestSettingActionLookup),
DefaultHost: gofastly.ToPointer("tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"),
MaxStaleAge: gofastly.ToPointer(900),
Name: gofastly.ToPointer("alt_backend"),
Expand All @@ -99,6 +100,31 @@ func TestAccFastlyServiceVCLRequestSetting_basic(t *testing.T) {
HashKeys: gofastly.ToPointer(""),
TimerSupport: gofastly.ToPointer(false),
}
rq3 := gofastly.RequestSetting{
Action: gofastly.ToPointer(gofastly.RequestSettingActionUnset),
DefaultHost: gofastly.ToPointer("tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"),
MaxStaleAge: gofastly.ToPointer(900),
Name: gofastly.ToPointer("alt_backend"),
RequestCondition: gofastly.ToPointer("serve_alt_backend"),
XForwardedFor: gofastly.ToPointer(gofastly.RequestSettingXFFAppend),

// We only set a few attributes in our TF config (see above).
// For all the other attributes (with the exception of `action` and `xff`,
// which are only sent to the API if they have a non-zero string value)
// the default value for their types are still sent to the API
// and so the API responds with those default values. Hence we have to set
// those defaults below...
BypassBusyWait: gofastly.ToPointer(false),
ForceMiss: gofastly.ToPointer(false),
ForceSSL: gofastly.ToPointer(false),
GeoHeaders: gofastly.ToPointer(false),
HashKeys: gofastly.ToPointer(""),
TimerSupport: gofastly.ToPointer(false),
}

createAction := "" // initially we expect no action to be set in HTTP request.
updateAction1 := "lookup" // give it a value and expect it to be set.
updateAction2 := "" // set an empty value and expect the empty string to be sent to the API.

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -108,29 +134,44 @@ func TestAccFastlyServiceVCLRequestSetting_basic(t *testing.T) {
CheckDestroy: testAccCheckServiceVCLDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceVCLRequestSetting(name, domainName1, "90"),
Config: testAccServiceVCLRequestSetting(name, domainName1, createAction, "90"),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLRequestSettingsAttributes(&service, []*gofastly.RequestSetting{&rq1}),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "name", name),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "request_setting.#", "1"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "condition.#", "1"),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "name", name),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "request_setting.#", "1"),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "condition.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs("fastly_service_vcl.foo", "request_setting.*", map[string]string{
"action": "", // IMPORTANT: To validate this attribute we need at least one map key to have a non-empty value (hence the `max_stale_age` check below).
"max_stale_age": "900",
}),
),
},
{
Config: testAccServiceVCLRequestSetting(name, domainName1, "900"),
Config: testAccServiceVCLRequestSetting(name, domainName1, updateAction1, "900"),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLRequestSettingsAttributes(&service, []*gofastly.RequestSetting{&rq2}),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "name", name),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "request_setting.#", "1"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "condition.#", "1"),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "name", name),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "request_setting.#", "1"),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "condition.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs("fastly_service_vcl.foo", "request_setting.*", map[string]string{
"action": "lookup",
}),
),
},
{
Config: testAccServiceVCLRequestSetting(name, domainName1, updateAction2, "900"),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLRequestSettingsAttributes(&service, []*gofastly.RequestSetting{&rq3}),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "name", name),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "request_setting.#", "1"),
resource.TestCheckResourceAttr("fastly_service_vcl.foo", "condition.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs("fastly_service_vcl.foo", "request_setting.*", map[string]string{
"action": "", // IMPORTANT: To validate this attribute we need at least one map key to have a non-empty value (hence the `max_stale_age` check below).
"max_stale_age": "900",
}),
),
},
},
Expand Down Expand Up @@ -179,7 +220,7 @@ func testAccCheckFastlyServiceVCLRequestSettingsAttributes(service *gofastly.Ser
}
}

func testAccServiceVCLRequestSetting(name, domain, maxStaleAge string) string {
func testAccServiceVCLRequestSetting(name, domain, action, maxStaleAge string) string {
return fmt.Sprintf(`
resource "fastly_service_vcl" "foo" {
name = "%s"
Expand Down Expand Up @@ -209,6 +250,7 @@ resource "fastly_service_vcl" "foo" {
}
request_setting {
action = "%s"
default_host = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"
name = "alt_backend"
request_condition = "serve_alt_backend"
Expand All @@ -218,5 +260,5 @@ resource "fastly_service_vcl" "foo" {
default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
force_destroy = true
}`, name, domain, maxStaleAge)
}`, name, domain, action, maxStaleAge)
}

0 comments on commit bd8c5ac

Please sign in to comment.