Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/cloudflare_ruleset: allow override + rule enabled to be omitted #1275

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1275.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_ruleset: allow action parameter override `enabled` to be true/false or uninitialised
```
22 changes: 11 additions & 11 deletions cloudflare/resource_cloudflare_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
1 change: 1 addition & 0 deletions cloudflare/resource_cloudflare_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down