Skip to content

Commit 5cd110d

Browse files
Merge pull request #5906 from zakcutner/ruleset/rules-actions
test(cloudflare_ruleset): add coverage for `action_parameters` attributes
2 parents a7b748f + 2ada740 commit 5cd110d

File tree

9 files changed

+531
-48
lines changed

9 files changed

+531
-48
lines changed

internal/services/ruleset/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (m RulesetModel) MarshalJSONForUpdate(state RulesetModel) (data []byte, err
3434
type RulesetRulesModel struct {
3535
ID types.String `tfsdk:"id" json:"id,computed"`
3636
Action types.String `tfsdk:"action" json:"action,required"`
37-
ActionParameters customfield.NestedObject[RulesetRulesActionParametersModel] `tfsdk:"action_parameters" json:"action_parameters,optional"`
37+
ActionParameters customfield.NestedObject[RulesetRulesActionParametersModel] `tfsdk:"action_parameters" json:"action_parameters,computed_optional,decode_null_to_zero"`
3838
Description types.String `tfsdk:"description" json:"description,optional"`
3939
Enabled types.Bool `tfsdk:"enabled" json:"enabled,computed_optional"`
4040
ExposedCredentialCheck customfield.NestedObject[RulesetRulesExposedCredentialCheckModel] `tfsdk:"exposed_credential_check" json:"exposed_credential_check,optional"`

internal/services/ruleset/resource_test.go

Lines changed: 416 additions & 41 deletions
Large diffs are not rendered by default.

internal/services/ruleset/schema.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1313
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
1414
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
15-
"github.com/hashicorp/terraform-plugin-framework/attr"
1615
"github.com/hashicorp/terraform-plugin-framework/path"
1716
"github.com/hashicorp/terraform-plugin-framework/resource"
1817
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1918
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
2019
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
20+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault"
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2222
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
2323
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -111,11 +111,8 @@ func ResourceSchema(ctx context.Context) schema.Schema {
111111
Description: "The list of rules in the ruleset.",
112112
Computed: true,
113113
Optional: true,
114-
Default: listdefault.StaticValue(types.ListValueMust(
115-
customfield.NewNestedObjectType[RulesetRulesModel](ctx),
116-
[]attr.Value{},
117-
)),
118-
CustomType: customfield.NewNestedObjectListType[RulesetRulesModel](ctx),
114+
Default: listdefault.StaticValue(customfield.NewObjectListMust(ctx, []RulesetRulesModel{}).ListValue),
115+
CustomType: customfield.NewNestedObjectListType[RulesetRulesModel](ctx),
119116
NestedObject: schema.NestedAttributeObject{
120117
Attributes: map[string]schema.Attribute{
121118
"id": schema.StringAttribute{
@@ -150,7 +147,9 @@ func ResourceSchema(ctx context.Context) schema.Schema {
150147
},
151148
"action_parameters": schema.SingleNestedAttribute{
152149
Description: "The parameters configuring the rule's action.",
150+
Computed: true,
153151
Optional: true,
152+
Default: objectdefault.StaticValue(customfield.NewObjectMust(ctx, &RulesetRulesActionParametersModel{}).ObjectValue),
154153
CustomType: customfield.NewNestedObjectType[RulesetRulesActionParametersModel](ctx),
155154
Attributes: map[string]schema.Attribute{
156155
"response": schema.SingleNestedAttribute{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_request_firewall_custom"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "block"
12+
}
13+
]
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_request_firewall_custom"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "block"
12+
action_parameters = {
13+
response = {
14+
status_code = 403
15+
content = "Access denied"
16+
content_type = "text/plain"
17+
}
18+
}
19+
}
20+
]
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_response_compression"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "compress_response"
12+
action_parameters = {
13+
algorithms = [
14+
{
15+
name = "auto"
16+
}
17+
]
18+
}
19+
}
20+
]
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_response_compression"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "compress_response"
12+
action_parameters = {
13+
algorithms = [
14+
{
15+
name = "brotli"
16+
},
17+
{
18+
name = "gzip"
19+
}
20+
]
21+
}
22+
}
23+
]
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_request_firewall_custom"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "block"
12+
}
13+
]
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "zone_id" {}
2+
3+
resource "cloudflare_ruleset" "my_ruleset" {
4+
zone_id = var.zone_id
5+
name = "My ruleset"
6+
phase = "http_request_firewall_custom"
7+
kind = "zone"
8+
rules = [
9+
{
10+
expression = "ip.src eq 1.1.1.1"
11+
action = "block"
12+
action_parameters = {}
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)