scottwinkler / terraform-provider-shell

Terraform provider for executing shell commands and saving output to state file
Mozilla Public License 2.0
279 stars 60 forks source link

Cannot `terraform import` resources #113

Open richard-jfc opened 1 year ago

richard-jfc commented 1 year ago

I'm attempting to terraform import a pre-existing resource with this provider, however it throws an exception:

Stack trace from the terraform-provider-shell_v1.7.10 plugin:

panic: runtime error: index out of range [0] with length 0

goroutine 43 [running]:
github.com/scottwinkler/terraform-provider-shell/shell.read(0xc0007fe8c0, {0xcb4580, 0xc00000eb60}, {0xc0000e3a00, 0xc0000e3a10, 0x1})
        github.com/scottwinkler/terraform-provider-shell/shell/resource_shell_script.go:247 +0x72d
github.com/scottwinkler/terraform-provider-shell/shell.resourceShellScriptRead(0xc00071b050, {0xcb4580, 0xc00000eb60})
        github.com/scottwinkler/terraform-provider-shell/shell/resource_shell_script.go:106 +0x54
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc000141a40, 0xc00013d130, {0xcb4580, 0xc00000eb60})
        github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/resource.go:470 +0x1aa
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc00000e8e0, {0xc000796360, 0x4b4966}, 0xc000796360)
        github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/helper/plugin/grpc_provider.go:535 +0x34b
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0xde9100, 0xc00000e8e0}, {0xfe8af0, 0xc0005ffb30}, 0xc000796300, 0x0)
        github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/tfplugin5/tfplugin5.pb.go:3269 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0003c7880, {0xff7df8, 0xc00019e480}, 0xc0005fc100, 0xc0007df2c0, 0x15d7ad0, 0x0)
        google.golang.org/grpc@v1.32.0/server.go:1194 +0xc8f
google.golang.org/grpc.(*Server).handleStream(0xc0003c7880, {0xff7df8, 0xc00019e480}, 0xc0005fc100, 0x0)
        google.golang.org/grpc@v1.32.0/server.go:1517 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        google.golang.org/grpc@v1.32.0/server.go:859 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.32.0/server.go:857 +0x294

Error: The terraform-provider-shell_v1.7.10 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.

It seems that read is called without any lifecycle_commands when run via terraform import.

Maybe the solution would be to accept a script path in the terraform import command, something like this:

terraform import resource_name ./scripts/import_resource_name.sh

I'm currently experimenting with the workaround of not running terraform import but instead having the create script check if the resources already exist.

matpen commented 1 year ago

I can reproduce this on 1.7.10 with terraform import shell_script.my_resource '{"some":"data"}'.

Moreover, I cannot find any documentation on terraform import for the shell provider, is it maybe not implemented yet?

namnd commented 1 year ago

Confirm this issue on 1.7.1 and 1.7.10