Skip to content

Commit

Permalink
Allow uploading Workers to dispatch namespaces + tags
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Feb 29, 2024
1 parent 548a6bb commit 68765f6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
67 changes: 46 additions & 21 deletions internal/sdkv2provider/resource_cloudflare_workers_script.go
Expand Up @@ -54,8 +54,12 @@ func getScriptData(d *schema.ResourceData, client *cloudflare.API) (ScriptData,

type ScriptBindings map[string]cloudflare.WorkerBinding

func getWorkerScriptBindings(ctx context.Context, accountId, scriptName string, client *cloudflare.API) (ScriptBindings, error) {
resp, err := client.ListWorkerBindings(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.ListWorkerBindingsParams{ScriptName: scriptName})
func getWorkerScriptBindings(ctx context.Context, accountId, scriptName string, dispatchNamespace *string, client *cloudflare.API) (ScriptBindings, error) {
resp, err := client.ListWorkerBindings(
ctx,
cloudflare.AccountIdentifier(accountId),
cloudflare.ListWorkerBindingsParams{ScriptName: scriptName, DispatchNamespace: dispatchNamespace},

Check failure on line 61 in internal/sdkv2provider/resource_cloudflare_workers_script.go

View workflow job for this annotation

GitHub Actions / test

unknown field DispatchNamespace in struct literal of type cloudflare.ListWorkerBindingsParams
)
if err != nil {
return nil, fmt.Errorf("cannot list script bindings: %w", err)
}
Expand Down Expand Up @@ -158,6 +162,14 @@ func getCompatibilityFlags(d *schema.ResourceData) []string {
return compatibilityFlags
}

func getTags(d *schema.ResourceData) []string {
tags := make([]string, 0)
for _, item := range d.Get("tags").(*schema.Set).List() {
tags = append(tags, item.(string))
}
return tags
}

func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get(consts.AccountIDSchemaKey).(string)
Expand All @@ -167,8 +179,10 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

dispatchNamespace := d.Get("dispatch_namespace").(string)

// make sure that the worker does not already exist
r, _ := client.GetWorker(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName)
r, _ := client.GetWorkerWithDispatchNamespace(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName, dispatchNamespace)

Check failure on line 185 in internal/sdkv2provider/resource_cloudflare_workers_script.go

View workflow job for this annotation

GitHub Actions / test

client.GetWorkerWithDispatchNamespace undefined (type *cloudflare.API has no field or method GetWorkerWithDispatchNamespace)
if r.WorkerScript.Script != "" {
return diag.FromErr(fmt.Errorf("script already exists"))
}
Expand All @@ -188,15 +202,19 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc

placement := getPlacement(d)

tags := getTags(d)

_, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
DispatchNamespaceName: &dispatchNamespace,
Tags: tags,
})
if err != nil {
return diag.FromErr(errors.Wrap(err, "error creating worker script"))
Expand All @@ -216,7 +234,9 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

r, err := client.GetWorker(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName)
dispatchNamespace := d.Get("dispatch_namespace").(string)

r, err := client.GetWorkerWithDispatchNamespace(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName, dispatchNamespace)

Check failure on line 239 in internal/sdkv2provider/resource_cloudflare_workers_script.go

View workflow job for this annotation

GitHub Actions / test

client.GetWorkerWithDispatchNamespace undefined (type *cloudflare.API has no field or method GetWorkerWithDispatchNamespace)
if err != nil {
// If the resource is deleted, we should set the ID to "" and not
// return an error according to the terraform spec
Expand All @@ -234,7 +254,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD

parseWorkerBindings(d, existingBindings)

bindings, err := getWorkerScriptBindings(ctx, accountID, d.Get("name").(string), client)
bindings, err := getWorkerScriptBindings(ctx, accountID, d.Get("name").(string), &dispatchNamespace, client)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -380,15 +400,20 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc

placement := getPlacement(d)

dispatchNamespace := d.Get("dispatch_namespace").(string)
tags := getTags(d)

_, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
DispatchNamespaceName: &dispatchNamespace,
Tags: tags,
})
if err != nil {
return diag.FromErr(errors.Wrap(err, "error updating worker script"))
Expand Down
Expand Up @@ -308,7 +308,7 @@ func testAccCheckCloudflareWorkerScriptExists(n string, script *cloudflare.Worke
}

name := strings.Replace(n, "cloudflare_worker_script.", "", -1)
foundBindings, err := getWorkerScriptBindings(context.Background(), accountID, name, client)
foundBindings, err := getWorkerScriptBindings(context.Background(), accountID, name, nil, client)
if err != nil {
return fmt.Errorf("cannot list script bindings: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/sdkv2provider/schema_cloudflare_workers_script.go
Expand Up @@ -207,6 +207,18 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: placementResource,
},
"dispatch_namespace": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the Workers for Platforms dispatch namespace.",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
// TODO: dispatch_namespace binding
"plain_text_binding": {
Type: schema.TypeSet,
Optional: true,
Expand Down

0 comments on commit 68765f6

Please sign in to comment.