ollieparsley / terraform-provider-paypal

PayPal terraform provider for managing merchant
MIT License
1 stars 1 forks source link

Panic when updating paypal webhook resource #2

Open jpedrofigueiredo opened 9 months ago

jpedrofigueiredo commented 9 months ago

There's currently no binary for darwin_arm64 so, I compiled it and copied the binary file to ~/.terraform.d/plugins/registry.terraform.io/ollieparsley/paypal/0.0.2/darwin_arm64/terraform-provider-paypal_v0.0.2.

I'm using go version go1.21.6 darwin/arm64

After this, the provider became visible to terraform and I was able to run

terraform init
terraform workspace select stage
terraform import paypal_notification_webhook.app_webhook_service 52630153X37814011
terraform plan

However, when trying to apply the changes, I got this error:

terraform apply
...
(initial lines omited for readability)
...

Do you want to perform these actions in workspace "stage"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

paypal_notification_webhook.app_webhook_service: Modifying... [id=52630153X37814011]
╷
│ Error: Request cancelled
│
│   with paypal_notification_webhook.app_webhook_service,
│   on webhooks.tf line 25, in resource "paypal_notification_webhook" "app_webhook_service":
│   25: resource "paypal_notification_webhook" "app_webhook_service" {
│
│ The plugin.(*GRPCProvider).ApplyResourceChange request was cancelled.
╵

Stack trace from the terraform-provider-paypal_v0.0.2 plugin:

panic: interface conversion: interface {} is []interface {}, not []string

goroutine 9 [running]:
github.com/ollieparsley/terraform-provider-paypal/paypal.WebhookResource.Update({}, 0x140001750a0, {0x101a19000?, 0x140003eb300})
    /Users/username/Projects/code/terraform-provider-paypal/paypal/resource_paypal_notification_webhook.go:108 +0x688
github.com/hashicorp/terraform/helper/schema.(*Resource).Apply(0x140004c6400, 0x140000bb1d0, 0x140000ca960, {0x101a19000, 0x140003eb300})
    /Users/username/go/pkg/mod/github.com/hashicorp/terraform@v0.12.6/helper/schema/resource.go:293 +0x4cc
github.com/hashicorp/terraform/helper/schema.(*Provider).Apply(0x140004c6900, 0x140005f57c0, 0x1016d4e86?, 0xf?)
    /Users/username/go/pkg/mod/github.com/hashicorp/terraform@v0.12.6/helper/schema/provider.go:285 +0x6c
github.com/hashicorp/terraform/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0x1400011e8e8, {0x14000506840?, 0x140000baf50?}, 0x14000506840)
    /Users/username/go/pkg/mod/github.com/hashicorp/terraform@v0.12.6/helper/plugin/grpc_provider.go:885 +0x678
github.com/hashicorp/terraform/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1019f2680?, 0x1400011e8e8}, {0x101a2aae8, 0x140000d7170}, 0x140000baf50, 0x0)
    /Users/username/go/pkg/mod/github.com/hashicorp/terraform@v0.12.6/internal/tfplugin5/tfplugin5.pb.go:3217 +0x164
google.golang.org/grpc.(*Server).processUnaryRPC(0x1400025e000, {0x101a2eff8, 0x14000370000}, 0x140004d4d00, 0x14000503740, 0x101f81220, 0x0)
    /Users/username/go/pkg/mod/google.golang.org/grpc@v1.21.1/server.go:998 +0xb48
google.golang.org/grpc.(*Server).handleStream(0x1400025e000, {0x101a2eff8, 0x14000370000}, 0x140004d4d00, 0x0)
    /Users/username/go/pkg/mod/google.golang.org/grpc@v1.21.1/server.go:1278 +0x814
google.golang.org/grpc.(*Server).serveStreams.func1.1()
    /Users/username/go/pkg/mod/google.golang.org/grpc@v1.21.1/server.go:717 +0x84
created by google.golang.org/grpc.(*Server).serveStreams.func1 in goroutine 42
    /Users/username/go/pkg/mod/google.golang.org/grpc@v1.21.1/server.go:715 +0xd4

Error: The terraform-provider-paypal_v0.0.2 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.
jpedrofigueiredo commented 9 months ago

I put together the patch below to workaround this issue, for now.

Subject: [PATCH] Avoid panic
---
Index: paypal/resource_paypal_notification_webhook.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/paypal/resource_paypal_notification_webhook.go b/paypal/resource_paypal_notification_webhook.go
--- a/paypal/resource_paypal_notification_webhook.go    (revision cbdb20677879863a07b196194f5cf14b86746183)
+++ b/paypal/resource_paypal_notification_webhook.go    (date 1707750228332)
@@ -105,7 +105,7 @@
        {
            Operation: "replace",
            Path:      "/event_types",
-           Value:     r.eventTypeNamesToEventTypes(d.Get("event_types").([]string)),
+           Value:     r.eventTypeNamesToEventTypes(toArrString(d.Get("event_types"))),
        },
    })
    if err != nil {
@@ -134,6 +134,16 @@
    return nil
 }

+func toArrString(arr interface{}) []string {
+   inArr := arr.([]interface{})
+
+   var out []string
+   for _, v := range inArr {
+       out = append(out, v.(string))
+   }
+   return out
+}
+
 // eventTypeNamesToEventTypes Convert the event_types object into an array of event type names
 func (r WebhookResource) eventTypeNamesToEventTypes(eventTypeNames []string) []paypalSdk.WebhookEventType {
    eventTypes := []paypalSdk.WebhookEventType{}