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

handle panic on parse workers kv id #1635

Merged
merged 2 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/1635.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_workers_kv: handle invalid id during terraform import
```
24 changes: 18 additions & 6 deletions cloudflare/resource_cloudflare_worker_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func resourceCloudflareWorkerKV() *schema.Resource {

func resourceCloudflareWorkersKVRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
namespaceID, key := parseId(d.Id())
namespaceID, key, err := parseId(d.Id())
if err != nil {
return diag.FromErr(err)
}

value, err := client.ReadWorkersKV(ctx, namespaceID, key)
if err != nil {
Expand Down Expand Up @@ -63,11 +66,14 @@ func resourceCloudflareWorkersKVUpdate(ctx context.Context, d *schema.ResourceDa

func resourceCloudflareWorkersKVDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
namespaceID, key := parseId(d.Id())
namespaceID, key, err := parseId(d.Id())
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] Deleting Cloudflare Workers KV with id: %+v", d.Id())

_, err := client.DeleteWorkersKV(ctx, namespaceID, key)
_, err = client.DeleteWorkersKV(ctx, namespaceID, key)
if err != nil {
return diag.FromErr(errors.Wrap(err, "error deleting workers kv"))
}
Expand All @@ -76,7 +82,10 @@ func resourceCloudflareWorkersKVDelete(ctx context.Context, d *schema.ResourceDa
}

func resourceCloudflareWorkersKVImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
namespaceID, key := parseId(d.Id())
namespaceID, key, err := parseId(d.Id())
if err != nil {
return nil, err
}

d.Set("namespace_id", namespaceID)
d.Set("key", key)
Expand All @@ -86,7 +95,10 @@ func resourceCloudflareWorkersKVImport(ctx context.Context, d *schema.ResourceDa
return []*schema.ResourceData{d}, nil
}

func parseId(id string) (string, string) {
func parseId(id string) (string, string, error) {
parts := strings.SplitN(id, "/", 2)
return parts[0], parts[1]
if len(parts) != 2 {
return "", "", fmt.Errorf("error parsing workers kv id: %s", id)
}
return parts[0], parts[1], nil
}