Skip to content

Commit

Permalink
resource/cloudflare_ruleset: allow override + rule enabled to be om…
Browse files Browse the repository at this point in the history
…itted

Updates the `rules.*.action_parameters.0.enabled` to be omitted should
the value not be set.

Closes #1273
  • Loading branch information
jacobbednarz committed Oct 24, 2021
1 parent 7b8fe2e commit 1281319
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
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

0 comments on commit 1281319

Please sign in to comment.