Skip to content

Commit

Permalink
Add support for worker placement
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Miller committed Oct 27, 2023
1 parent a13a12c commit 2a0d517
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ resource "cloudflare_worker_script" "my_script" {
- `kv_namespace_binding` (Block Set) (see [below for nested schema](#nestedblock--kv_namespace_binding))
- `logpush` (Boolean) Enabling allows Worker events to be sent to a defined Logpush destination.
- `module` (Boolean) Whether to upload Worker as a module.
- `placement` (Block Set) (see [below for nested schema](#nestedblock--placement))
- `plain_text_binding` (Block Set) (see [below for nested schema](#nestedblock--plain_text_binding))
- `queue_binding` (Block Set) (see [below for nested schema](#nestedblock--queue_binding))
- `r2_bucket_binding` (Block Set) (see [below for nested schema](#nestedblock--r2_bucket_binding))
Expand Down Expand Up @@ -106,6 +107,14 @@ Required:
- `namespace_id` (String) ID of the KV namespace you want to use.


<a id="nestedblock--placement"></a>
### Nested Schema for `placement`

Required:

- `mode` (String) The placement mode for the Worker (e.g. 'smart').


<a id="nestedblock--plain_text_binding"></a>
### Nested Schema for `plain_text_binding`

Expand Down
17 changes: 17 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) {
}
}

func getPlacement(d *schema.ResourceData) cloudflare.Placement {
for _, rawData := range d.Get("placement").(*schema.Set).List() {
data := rawData.(map[string]interface{})
return cloudflare.Placement{
Mode: cloudflare.PlacementMode(data["mode"].(string)),
}
}

return cloudflare.Placement{}
}

func getCompatibilityFlags(d *schema.ResourceData) []string {
compatibilityFlags := make([]string, 0)
for _, item := range d.Get("compatibility_flags").(*schema.Set).List() {
Expand Down Expand Up @@ -167,6 +178,8 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc

logpush := d.Get("logpush").(bool)

placement := getPlacement(d)

_, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
Expand All @@ -175,6 +188,7 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
})
if err != nil {
return diag.FromErr(errors.Wrap(err, "error creating worker script"))
Expand Down Expand Up @@ -344,6 +358,8 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc

logpush := d.Get("logpush").(bool)

placement := getPlacement(d)

_, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
Expand All @@ -352,6 +368,7 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
})
if err != nil {
return diag.FromErr(errors.Wrap(err, "error updating worker script"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestAccCloudflareWorkerScript_ModuleUpload(t *testing.T) {
resource.TestCheckResourceAttr(name, "compatibility_flags.#", "2"),
resource.TestCheckResourceAttr(name, "compatibility_flags.0", compatibilityFlags[0]),
resource.TestCheckResourceAttr(name, "logpush", "true"),
resource.TestCheckResourceAttr(name, "placement.mode", "smart"),
),
},
},
Expand Down Expand Up @@ -259,8 +260,11 @@ resource "cloudflare_worker_script" "%[1]s" {
compatibility_date = "%[4]s"
compatibility_flags = ["%[5]s"]
logpush = true
placement {
mode = "smart"
}
depends_on = [cloudflare_logpush_job.%[1]s]
depends_on = [cloudflare_logpush_job.%[1]s]
}`, rnd, moduleContent, accountID, compatibilityDate, strings.Join(compatibilityFlags, `","`), r2AccessKeyID, r2AccessKeySecret)
}

Expand Down
15 changes: 15 additions & 0 deletions internal/sdkv2provider/schema_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ var queueBindingResource = &schema.Resource{
},
}

var placementResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"mode": {
Type: schema.TypeString,
Required: true,
Description: "The placement mode for the Worker (e.g. 'smart').",
},
},
}

func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
consts.AccountIDSchemaKey: {
Expand Down Expand Up @@ -173,6 +183,11 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Description: "Enabling allows Worker events to be sent to a defined Logpush destination.",
},
"placement": {
Type: schema.TypeSet,

Check failure on line 187 in internal/sdkv2provider/schema_cloudflare_workers_script.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
Optional: true,
Elem: placementResource,
},
"plain_text_binding": {
Type: schema.TypeSet,
Optional: true,
Expand Down

0 comments on commit 2a0d517

Please sign in to comment.