Skip to content

Commit

Permalink
Correctly set multi_script on Enterprise worker imports (#124)
Browse files Browse the repository at this point in the history
This updates the import for `cloudflare_worker_route` resource. Prior to
this it relied on `d.Get("script_name")` which was never accessible so
it would always return an empty string (even when using
`getRouteFromResource`).

With this change, it now queries the Cloudflare API fetching all worker
routes and when the route ID matches one in the API response, it will
set the `isEnterpriseWorker` flag to `true` which will correctly set the
`multi_script` option. This includes the prerequisitie that the
organisation ID must also be set to enable it which maintains
compatibility with the existing behaviour.

Fixes #123
  • Loading branch information
jacobbednarz authored and patryk committed Oct 15, 2018
1 parent ea45774 commit 739e4b8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cloudflare/resource_cloudflare_worker_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func resourceCloudflareWorkerRouteDelete(d *schema.ResourceData, meta interface{

func resourceCloudflareWorkerRouteImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*cloudflare.API)
isEnterpriseWorker := false

// split the id so we can lookup
idAttr := strings.SplitN(d.Id(), "/", 2)
Expand All @@ -186,15 +187,23 @@ func resourceCloudflareWorkerRouteImport(d *schema.ResourceData, meta interface{
} else {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"zoneName/routeId\"", d.Id())
}
zoneId, err := client.ZoneIDByName(zoneName)

zoneID, err := client.ZoneIDByName(zoneName)
routes, err := client.ListWorkerRoutes(zoneID)

for _, r := range routes.Routes {
if r.ID == routeId && client.OrganizationID != "" {
isEnterpriseWorker = true
}
}

if err != nil {
return nil, fmt.Errorf("error finding zoneName %q: %s", zoneName, err)
}

d.Set("zone", zoneName)
d.Set("zone_id", zoneId)
d.Set("multi_script", d.Get("script_name").(string) != "")
d.Set("zone_id", zoneID)
d.Set("multi_script", isEnterpriseWorker)
d.SetId(routeId)

return []*schema.ResourceData{d}, nil
Expand Down

0 comments on commit 739e4b8

Please sign in to comment.