Skip to content

Commit

Permalink
Add suppoort for Bypass By Default mode for the Edge TTL block in Rul…
Browse files Browse the repository at this point in the history
…esets
  • Loading branch information
iveelsm committed Sep 27, 2023
1 parent 2968c3b commit 1792646
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/2764.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_ruleset: Add support for bypass by default in Edge TTL modes
```
2 changes: 1 addition & 1 deletion docs/resources/ruleset.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ Optional:

Required:

- `mode` (String) Mode of the edge TTL.
- `mode` (String) Mode of the edge TTL. Available values: `override_origin`, `respect_origin`, `bypass_by_default`

Optional:

Expand Down
93 changes: 93 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,50 @@ func TestAccCloudflareRuleset_CacheSettingsInvalidEdgeTTLWithOverrideOrigin(t *t
})
}

func TestAccCloudflareRuleset_CacheSettingsEdgeTTLWithBypassByDefault(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
resourceName := "cloudflare_ruleset." + rnd

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareRulesetCacheSettingsBypassByDefaultEdge(rnd, zoneID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "description", "set cache settings for the request"),
resource.TestCheckResourceAttr(resourceName, "kind", "zone"),
resource.TestCheckResourceAttr(resourceName, "phase", "http_request_cache_settings"),

resource.TestCheckResourceAttr(resourceName, "rules.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action", "set_cache_settings"),
resource.TestCheckResourceAttr(resourceName, "rules.0.description", "example"),

resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.edge_ttl.0.mode", "bypass_by_default"),
),
},
},
})
}

func TestAccCloudflareRuleset_CacheSettingsInvalidEdgeTTLWithBypassByDefault(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareRulesetCacheSettingsBypassByDefaultEdgeInvalid(rnd, zoneID),
ExpectError: regexp.MustCompile("cannot set default ttl when using mode 'bypass_by_default'"),
},
},
})
}

func TestAccCloudflareRuleset_CacheSettingsBrowserTTLWithBypass(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
Expand Down Expand Up @@ -4668,6 +4712,55 @@ func testAccCloudflareRulesetCacheSettingsHandleHeaderExcludeOriginFalse(rnd, zo
`, rnd, zoneID)
}

func testAccCloudflareRulesetCacheSettingsBypassByDefaultEdge(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
zone_id = "%[2]s"
name = "%[1]s"
description = "set cache settings for the request"
kind = "zone"
phase = "http_request_cache_settings"
rules {
action = "set_cache_settings"
description = "example"
enabled = true
expression = "(http.host eq \"example.com\" and starts_with(http.request.uri.path, \"/example\"))"
action_parameters {
cache = true
edge_ttl {
mode = "bypass_by_default"
}
}
}
}
`, rnd, zoneID)
}

func testAccCloudflareRulesetCacheSettingsBypassByDefaultEdgeInvalid(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
zone_id = "%[2]s"
name = "%[1]s"
description = "set cache settings for the request"
kind = "zone"
phase = "http_request_cache_settings"
rules {
action = "set_cache_settings"
description = "example"
enabled = true
expression = "(http.host eq \"example.com\" and starts_with(http.request.uri.path, \"/example\"))"
action_parameters {
cache = true
edge_ttl {
mode = "bypass_by_default"
default = 100
}
}
}
}
`, rnd, zoneID)
}

func testAccCloudflareRulesetCacheSettingsBypassBrowser(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
Expand Down
2 changes: 1 addition & 1 deletion internal/framework/service/rulesets/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (r *RulesetResource) Schema(ctx context.Context, req resource.SchemaRequest
Attributes: map[string]schema.Attribute{
"mode": schema.StringAttribute{
Required: true,
Validators: []validator.String{stringvalidator.OneOf("override_origin", "respect_origin")},
Validators: []validator.String{stringvalidator.OneOf("override_origin", "respect_origin", "bypass_by_default")},
MarkdownDescription: "Mode of the edge TTL.",
},
"default": schema.Int64Attribute{
Expand Down
8 changes: 8 additions & 0 deletions internal/framework/service/rulesets/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ func (v EdgeTTLValidator) ValidateObject(ctx context.Context, req validator.Obje
fmt.Sprintf("using mode '%s' requires setting a default for ttl", parameter.Mode.ValueString()),
)
}
} else if parameter.Mode.ValueString() == "bypass_by_default" {
if !parameter.Default.IsNull() {
resp.Diagnostics.AddAttributeError(
req.Path,
errInvalidConfiguration,
fmt.Sprintf("cannot set default ttl when using mode '%s'", parameter.Mode.ValueString()),
)
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions internal/framework/service/rulesets/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@ func TestEdgeTTLValidation(t *testing.T) {
}
})
})

t.Run("bypass by default mode is specified", func(t *testing.T) {
t.Parallel()

t.Run("errors when ttl is passed", func(t *testing.T) {
t.Parallel()

resp := &validator.ObjectResponse{}
req := constructEdgeTTLObjectRequest("bypass_by_default", big.NewFloat(1))
edgeValidator.ValidateObject(ctx, req, resp)

expected := &validator.ObjectResponse{
Diagnostics: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(path.Root("edge_ttl"), "invalid configuration", "cannot set default ttl when using mode 'bypass'"),
},
}
if diff := cmp.Diff(resp, expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})

t.Run("passes without ttl", func(t *testing.T) {
t.Parallel()

resp := &validator.ObjectResponse{}
req := constructEdgeTTLObjectRequest("bypass_by_default", nil)
edgeValidator.ValidateObject(ctx, req, resp)

expected := &validator.ObjectResponse{
Diagnostics: nil,
}
if diff := cmp.Diff(resp, expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
})
})
}

Expand Down

0 comments on commit 1792646

Please sign in to comment.