Skip to content

Commit 2b469f8

Browse files
Merge pull request #5902 from zakcutner/ruleset/rules
fix(cloudflare_ruleset): handle omitted `rules` in API responses
2 parents 840ee94 + 7f15668 commit 2b469f8

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

internal/services/ruleset/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type RulesetModel struct {
2020
Name types.String `tfsdk:"name" json:"name,required"`
2121
Phase types.String `tfsdk:"phase" json:"phase,required"`
2222
Description types.String `tfsdk:"description" json:"description,computed_optional"`
23-
Rules customfield.NestedObjectList[RulesetRulesModel] `tfsdk:"rules" json:"rules,optional"`
23+
Rules customfield.NestedObjectList[RulesetRulesModel] `tfsdk:"rules" json:"rules,computed_optional,decode_null_to_zero"`
2424
}
2525

2626
func (m RulesetModel) MarshalJSON() (data []byte, err error) {

internal/services/ruleset/resource_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,52 @@ func TestAccCloudflareRuleset_Description(t *testing.T) {
222222
})
223223
}
224224

225+
func TestAccCloudflareRuleset_Rules(t *testing.T) {
226+
resource.Test(t, resource.TestCase{
227+
PreCheck: func() { acctest.TestAccPreCheck(t) },
228+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
229+
Steps: []resource.TestStep{
230+
{
231+
ConfigFile: config.TestNameFile("1.tf"),
232+
ConfigVariables: configVariables,
233+
ConfigPlanChecks: resource.ConfigPlanChecks{
234+
PreApply: []plancheck.PlanCheck{
235+
plancheck.ExpectResourceAction("cloudflare_ruleset.my_ruleset", plancheck.ResourceActionCreate),
236+
plancheck.ExpectKnownValue(
237+
"cloudflare_ruleset.my_ruleset",
238+
tfjsonpath.New("rules"),
239+
knownvalue.ListExact([]knownvalue.Check{}),
240+
),
241+
},
242+
},
243+
ConfigStateChecks: []statecheck.StateCheck{
244+
statecheck.ExpectKnownValue(
245+
"cloudflare_ruleset.my_ruleset",
246+
tfjsonpath.New("rules"),
247+
knownvalue.ListExact([]knownvalue.Check{}),
248+
),
249+
},
250+
},
251+
{
252+
ConfigFile: config.TestNameFile("2.tf"),
253+
ConfigVariables: configVariables,
254+
ConfigPlanChecks: resource.ConfigPlanChecks{
255+
PreApply: []plancheck.PlanCheck{
256+
plancheck.ExpectEmptyPlan(),
257+
},
258+
},
259+
ConfigStateChecks: []statecheck.StateCheck{
260+
statecheck.ExpectKnownValue(
261+
"cloudflare_ruleset.my_ruleset",
262+
tfjsonpath.New("rules"),
263+
knownvalue.ListExact([]knownvalue.Check{}),
264+
),
265+
},
266+
},
267+
},
268+
})
269+
}
270+
225271
func TestAccCloudflareRuleset_RulesLogging(t *testing.T) {
226272
resource.Test(t, resource.TestCase{
227273
PreCheck: func() { acctest.TestAccPreCheck(t) },

internal/services/ruleset/schema.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +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"
1516
"github.com/hashicorp/terraform-plugin-framework/path"
1617
"github.com/hashicorp/terraform-plugin-framework/resource"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
20+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
1921
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2022
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
2123
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -107,8 +109,13 @@ func ResourceSchema(ctx context.Context) schema.Schema {
107109
},
108110
"rules": schema.ListNestedAttribute{
109111
Description: "The list of rules in the ruleset.",
112+
Computed: true,
110113
Optional: true,
111-
CustomType: customfield.NewNestedObjectListType[RulesetRulesModel](ctx),
114+
Default: listdefault.StaticValue(types.ListValueMust(
115+
customfield.NewNestedObjectType[RulesetRulesModel](ctx),
116+
[]attr.Value{},
117+
)),
118+
CustomType: customfield.NewNestedObjectListType[RulesetRulesModel](ctx),
112119
NestedObject: schema.NestedAttributeObject{
113120
Attributes: map[string]schema.Attribute{
114121
"id": schema.StringAttribute{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)