Skip to content

Commit

Permalink
Check actions for existence and change status
Browse files Browse the repository at this point in the history
Updates the `transformToCloudflarePageRuleAction` functionality to
perform checks using `HasChange` to determine whether or not the value
should be sent or `nil`.
  • Loading branch information
jacobbednarz committed May 9, 2019
1 parent 6d24359 commit 230976a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cloudflare/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ func resourceCloudflarePageRuleCreate(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] Actions found in config: %#v", actions)
for _, action := range actions {
for id, value := range action.(map[string]interface{}) {
newPageRuleAction, err := transformToCloudflarePageRuleAction(id, value)

newPageRuleAction, err := transformToCloudflarePageRuleAction(id, value, d)
if err != nil {
return err
} else if newPageRuleAction.Value == nil || newPageRuleAction.Value == "" {
Expand Down Expand Up @@ -469,7 +470,7 @@ func resourceCloudflarePageRuleUpdate(d *schema.ResourceData, meta interface{})

for _, action := range actions {
for id, value := range action.(map[string]interface{}) {
newPageRuleAction, err := transformToCloudflarePageRuleAction(id, value)
newPageRuleAction, err := transformToCloudflarePageRuleAction(id, value, d)
if err != nil {
return err
} else if newPageRuleAction.Value == nil {
Expand Down Expand Up @@ -588,10 +589,12 @@ func transformFromCloudflarePageRuleAction(pageRuleAction *cloudflare.PageRuleAc
return
}

func transformToCloudflarePageRuleAction(id string, value interface{}) (pageRuleAction cloudflare.PageRuleAction, err error) {
func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema.ResourceData) (pageRuleAction cloudflare.PageRuleAction, err error) {

pageRuleAction.ID = id

changed := d.HasChange(fmt.Sprintf("actions.0.%s", id))

if strValue, ok := value.(string); ok {
if strValue == "" && !changed {
pageRuleAction.Value = nil
Expand All @@ -613,11 +616,12 @@ func transformToCloudflarePageRuleAction(id string, value interface{}) (pageRule
}
}
} else if intValue, ok := value.(int); ok {
if (id == "edge_cache_ttl" && intValue == 0) || !changed {
// This happens when not set by the user
pageRuleAction.Value = nil
} else {
if id == "browser_cache_ttl" && changed {
pageRuleAction.Value = intValue
} else if id == "edge_cache_ttl" && intValue > 0 && changed {
pageRuleAction.Value = intValue
} else {
pageRuleAction.Value = nil
}
} else if id == "forwarding_url" {
forwardActionSchema := value.([]interface{})
Expand Down

0 comments on commit 230976a

Please sign in to comment.