Skip to content

Commit

Permalink
Allow zone_id to set zone and vice versa (#174)
Browse files Browse the repository at this point in the history
During an import of the `cloudflare_firewall_rule` resource, the `zone_id` is
defined within the composite ID. This value is then populated and synced
within `resourceCloudflareFirewallRuleRead` to match the expected schema
resource. However, we don't currently set zone (the zone name)
anywhere. This becomes problematic on subsequent terraform operations as
the Read attempts to sync the schema with the firewall rule and ends up
attempting to recreate the resource due to detecting a change.

This is a similar issue to #161 and is addressed in a similar manner.

Fixes #165
  • Loading branch information
jacobbednarz authored and patryk committed Dec 5, 2018
1 parent d6475a8 commit 0afb203
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cloudflare/resource_cloudflare_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func resourceCloudflareFirewallRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"zone_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -124,6 +125,20 @@ func resourceCloudflareFirewallRuleCreate(d *schema.ResourceData, meta interface
func resourceCloudflareFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
zoneName := d.Get("zone").(string)
if zoneID == "" {
zoneID, _ = client.ZoneIDByName(zoneName)
} else {
zones, err := client.ListZones()
if err != nil {
return fmt.Errorf("failed to lookup all zones: %s", err)
}
for _, zone := range zones {
if zone.ID == zoneID {
zoneName = zone.Name
}
}
}

firewallRule, err := client.FirewallRule(zoneID, d.Id())

Expand All @@ -141,6 +156,8 @@ func resourceCloudflareFirewallRuleRead(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] Cloudflare Firewall Rule read configuration: %#v", firewallRule)

d.Set("zone", zoneName)
d.Set("zone_id", zoneID)
d.Set("paused", firewallRule.Paused)
d.Set("description", firewallRule.Description)
d.Set("action", firewallRule.Action)
Expand Down
2 changes: 2 additions & 0 deletions cloudflare/resource_cloudflare_firewall_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestFirewallRuleSimple(t *testing.T) {
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "action", "allow"),
resource.TestCheckResourceAttr(name, "priority", "1"),
resource.TestCheckResourceAttr(name, "zone", zone),
resource.TestCheckResourceAttrSet(name, "zone_id"),
),
},
},
Expand Down

0 comments on commit 0afb203

Please sign in to comment.