From 1281319821a13dd39af4782b7a7516db0d0d88a2 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 25 Oct 2021 10:03:09 +1100 Subject: [PATCH] resource/cloudflare_ruleset: allow override + rule `enabled` to be omitted Updates the `rules.*.action_parameters.0.enabled` to be omitted should the value not be set. Closes #1273 --- cloudflare/resource_cloudflare_ruleset.go | 22 +++++++++---------- .../resource_cloudflare_ruleset_test.go | 1 + 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cloudflare/resource_cloudflare_ruleset.go b/cloudflare/resource_cloudflare_ruleset.go index 25e65c4b0be..d550d3992b5 100644 --- a/cloudflare/resource_cloudflare_ruleset.go +++ b/cloudflare/resource_cloudflare_ruleset.go @@ -352,7 +352,7 @@ func resourceCloudflareRulesetCreate(d *schema.ResourceData, meta interface{}) e Phase: d.Get("phase").(string), } - rules, err := buildRulesetRulesFromResource(d.Get("phase").(string), d.Get("rules")) + rules, err := buildRulesetRulesFromResource(d) if err != nil { return errors.Wrap(err, fmt.Sprintf("error building ruleset from resource")) } @@ -446,7 +446,7 @@ func resourceCloudflareRulesetUpdate(d *schema.ResourceData, meta interface{}) e accountID := d.Get("account_id").(string) zoneID := d.Get("zone_id").(string) - rules, err := buildRulesetRulesFromResource(d.Get("phase").(string), d.Get("rules")) + rules, err := buildRulesetRulesFromResource(d) if err != nil { return errors.Wrap(err, fmt.Sprintf("error building ruleset from resource")) } @@ -632,15 +632,15 @@ func buildStateFromRulesetRules(rules []cloudflare.RulesetRule) interface{} { } // receives the resource config and builds a ruleset rule array -func buildRulesetRulesFromResource(phase string, r interface{}) ([]cloudflare.RulesetRule, error) { +func buildRulesetRulesFromResource(d *schema.ResourceData) ([]cloudflare.RulesetRule, error) { var rulesetRules []cloudflare.RulesetRule - rules, ok := r.([]interface{}) + rules, ok := d.Get("rules").([]interface{}) if !ok { return nil, errors.New("unable to create interface array type assertion") } - for _, v := range rules { + for rulesCounter, v := range rules { var rule cloudflare.RulesetRule resourceRule, ok := v.(map[string]interface{}) @@ -688,9 +688,9 @@ func buildRulesetRulesFromResource(phase string, r interface{}) ([]cloudflare.Ru var categories []cloudflare.RulesetRuleActionParametersCategories var rules []cloudflare.RulesetRuleActionParametersRules - for _, overrideParamValue := range pValue.([]interface{}) { - if phase != string(cloudflare.RulesetPhaseDDoSL7) { - overrideConfiguration.Enabled = &[]bool{overrideParamValue.(map[string]interface{})["enabled"].(bool)}[0] + for overrideCounter, overrideParamValue := range pValue.([]interface{}) { + if value, ok := d.GetOkExists(fmt.Sprintf("rules.%d.action_parameters.0.overrides.%d.enabled", rulesCounter, overrideCounter)); ok { + overrideConfiguration.Enabled = &[]bool{value.(bool)}[0] } if val, ok := overrideParamValue.(map[string]interface{})["action"]; ok { @@ -711,12 +711,12 @@ func buildRulesetRulesFromResource(phase string, r interface{}) ([]cloudflare.Ru // Rule ID based overrides if val, ok := overrideParamValue.(map[string]interface{})["rules"]; ok { - for _, rule := range val.([]interface{}) { + for ruleOverrideCounter, rule := range val.([]interface{}) { rData := rule.(map[string]interface{}) var enabled *bool - if phase != string(cloudflare.RulesetPhaseDDoSL7) { - enabled = &[]bool{rData["enabled"].(bool)}[0] + if value, ok := d.GetOk(fmt.Sprintf("rules.%d.action_parameters.0.overrides.%d.rules.%d.enabled", rulesCounter, overrideCounter, ruleOverrideCounter)); ok { + enabled = &[]bool{value.(bool)}[0] } rules = append(rules, cloudflare.RulesetRuleActionParametersRules{ diff --git a/cloudflare/resource_cloudflare_ruleset_test.go b/cloudflare/resource_cloudflare_ruleset_test.go index 2cff74337ee..ad42380e97d 100644 --- a/cloudflare/resource_cloudflare_ruleset_test.go +++ b/cloudflare/resource_cloudflare_ruleset_test.go @@ -909,6 +909,7 @@ func TestAccCloudflareRuleset_ActionParametersOverridesAction(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.#", "1"), resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.id", "efb7b8c949ac4650a09736fc376e9aee"), resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.version", "latest"), + resource.TestCheckNoResourceAttr(resourceName, "rules.0.action_parameters.0.enabled"), resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.overrides.#", "1"), resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.overrides.0.action", "log"), resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.overrides.0.enabled", "true"),