opentofu / terraform-provider-go

Mozilla Public License 2.0
24 stars 4 forks source link

Improve Error Messages #7

Open ImIOImI opened 1 month ago

ImIOImI commented 1 month ago

I'm experimenting a bit with creating functions to deal with maps and I'm getting some very incomprehensible error messages (especially for a go newb like me).

I'm less interested in getting this particular code working as I am interested in learning how to deal with maps (especially ones with an arbitrary depth) and transform them via go scripts.

This is the code I wrote:

package lib

import (
    "reflect"
)

// GetKeys recursively retrieves all keys from a map of arbitrary depth.
func Keys(data interface{}) []string {
    var keys []string

    if reflect.TypeOf(data).Kind() == reflect.Map {
        mapValue := reflect.ValueOf(data)

        for _, key := range mapValue.MapKeys() {
            keys = append(keys, key.String())

            nestedKeys := Keys(mapValue.MapIndex(key).Interface())
            keys = append(keys, nestedKeys...)
        }
    }

    return keys
}

What I think I did wrong is that I need to define the input like a struct, because what's getting submitted is the wrong type... but this is the error message:

│ Error: Error in function call
│
│   on deleteme.tf line 29, in output "filtered_map":
│   29:   value = provider::go::keys({
│   30:     item1 = {
│   31:       name = "find me"
│   32:       type = "fruit"
│   33:     }
│   34:     item2 = {
│   35:       name = "not me"
│   36:       type = "vegetable"
│   37:     }
│   38:     item3 = {
│   39:       name = "find me"
│   40:       type = "fruit"
│   41:     }
│   42:   })
│     ├────────────────
│     │ while calling provider::go::keys()
│
│ Call to function "provider::go::keys" failed: rpc error: code = Unavailable desc = error reading from server: read unix
│ @->/tmp/plugin1868917891: read: connection reset by peer.
╵

Stack trace from the terraform-provider-go_v0.0.3 plugin:

panic: implement interface{}

goroutine 98 [running]:
main.TfToGoValue({0x132ac08, 0xf9c120}, {{0x1322000?, 0xc0008893b0?}, {0xfb2a20?, 0xc000888780?}})
        github.com/opentofu/terraform-provider-go/main.go:449 +0xd18
main.ProtoToGo({0x1321ef8?, 0xc000644090?}, {0x132ac08, 0xf9c120}, 0x1a9b100?)
        github.com/opentofu/terraform-provider-go/main.go:397 +0xe5
main.GoFunctionToTFFunction.func1({0xc0006b4090, 0x1, 0xc0004ae000?})
        github.com/opentofu/terraform-provider-go/main.go:264 +0x13b
main.(*FunctionProvider).CallFunction(0xc00017db20, {0xc000532fa0?, 0xfce5c0?}, 0xc00047fc80)
        github.com/opentofu/terraform-provider-go/main.go:101 +0x108
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).CallFunction(0xc00024a960, {0x131c4c0?, 0xc000786270?}, 0xc00043a000)
        github.com/hashicorp/terraform-plugin-go@v0.22.1/tfprotov6/tf6server/server.go:987 +0x623
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_CallFunction_Handler({0x10d7920, 0xc00024a960}, {0x131c4c0, 0xc000786270}, 0xc000434100, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.22.1/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:608 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001b9200, {0x131c4c0, 0xc0007861b0}, {0x1322b20, 0xc00047c000}, 0xc0001efc20, 0xc00039af30, 0x1a1d950, 0x0)
        google.golang.org/grpc@v1.62.1/server.go:1386 +0xdf8
google.golang.org/grpc.(*Server).handleStream(0xc0001b9200, {0x1322b20, 0xc00047c000}, 0xc0001efc20)
        google.golang.org/grpc@v1.62.1/server.go:1797 +0xe87
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/grpc@v1.62.1/server.go:1027 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 39
        google.golang.org/grpc@v1.62.1/server.go:1038 +0x125

Error: The terraform-provider-go_v0.0.3 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.

Again, I'm fairly new to using go, but nothing in the above error message screams out that I screwed up the input type.

ghost commented 1 month ago

Hey @ImIOImI thanks for this issue. The go provider is very experimental and this is likely the reason why you are getting crashes. Panics should never happen in a regular provider, so this is definitely a bug.

cam72cam commented 1 month ago

Looks like this scenario was never implemented in the initial version: https://github.com/opentofu/terraform-provider-go/blob/main/main.go#L449