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

Allow zone_id to set zone and vice versa #162

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ func resourceCloudflareFilterCreate(d *schema.ResourceData, meta interface{}) er
func resourceCloudflareFilterRead(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
}
}

if zoneName == "" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a safe guard to ensure that if we do get to this point, we really do have a zoneName set

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need to check for zoneID being empty here, will be always populated by Create or Import.
We should also mark zone field as Computed: true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that causes a bit of confusion here because zone_id is already marked as computed in the schema.

https://github.com/terraform-providers/terraform-provider-cloudflare/blob/5c9bbc63bcd4cbe0d108aec8e575e3e1e1b1129f/cloudflare/resource_cloudflare_filter.go#L29-L34

Should it only be zone that is computed? If that's the case, we won't need majority of this PR as we'll assume we always have the zone_id and can work everything around that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computed here means that the code may set it itself, so it should not be taken into consideration on diff unless explicitly set (yes, Computed fields can be set in HCL).

Because now in Create we set zone_id if we provide zone, but we will be setting zone when we import with zone_id only, we need both to be Computed.

I made a mistake coding in the zone field, I was young and stupid at that time ;)
We really need to clean zone vs zone_id mess in v2.0 😰

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computed here means that the code may set it itself, so it should not be taken into consideration on diff unless explicitly set (yes, Computed fields can be set in HCL).

TIL, other uses of this that I have seen have all been for things like UUIDs or generating values at runtime.

We really need to clean zone vs zone_id mess in v2.0 😰

Do you want to start a v2.0 issue where we can keep track of these things so that we have a record of it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, good idea. Issue and/or milestone (will need to read GitHub guides on this).

return fmt.Errorf("failed to find zone name")
}
}

log.Printf("[DEBUG] Getting a Filter record for zone %q, id %s", zoneID, d.Id())
filter, err := client.Filter(zoneID, d.Id())
Expand All @@ -129,6 +149,8 @@ func resourceCloudflareFilterRead(d *schema.ResourceData, meta interface{}) erro

log.Printf("[DEBUG] Cloudflare Filter read configuration: %#v", filter)

d.Set("zone", zoneName)
d.Set("zone_id", zoneID)
d.Set("paused", filter.Paused)
d.Set("description", filter.Description)
d.Set("expression", filter.Expression)
Expand Down
2 changes: 2 additions & 0 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestFilterSimple(t *testing.T) {
resource.TestCheckResourceAttr(name, "description", "this is notes"),
resource.TestCheckResourceAttr(name, "expression", filterUnquoted),
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "zone", zone),
resource.TestCheckResourceAttrSet(name, "zone_id"),
),
},
},
Expand Down