Skip to content

Conversation

@irregulator
Copy link
Contributor

Make sure that datasourceConnectorRead() returns the appropriate error when no connectors are found.

@irregulator
Copy link
Contributor Author

Currently, the provider panics when no Kibana connectors are found:

➜  test_elasticstack_provider cat main.tf
data "elasticstack_kibana_action_connector" "my-connector" {
  name              = "doesnotexist"
  connector_type_id = ".pagerduty"
}
➜  test_elasticstack_provider terraform plan                                              
data.elasticstack_kibana_action_connector.my-connector: Reading...

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Request cancelled
│ 
│ The plugin6.(*GRPCProvider).ReadDataSource request was cancelled.
╵

Stack trace from the terraform-provider-elasticstack_v0.11.6 plugin:

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

goroutine 81 [running]:
github.com/elastic/terraform-provider-elasticstack/internal/kibana.datasourceConnectorRead({0x1752640, 0xc0003ae460}, 0xc000518e00, {0x14d2e20?, 0xc00098e000?})
        github.com/elastic/terraform-provider-elasticstack/internal/kibana/connector_data_source.go:86 +0x4c5
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).read(0xc0003610a0, {0x1752598, 0xc0006d95f0}, 0xc000518e00, {0x14d2e20, 0xc00098e000})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/resource.go:823 +0x119
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).ReadDataApply(0xc0003610a0, {0x1752598, 0xc0006d95f0}, 0xc000518d00, {0x14d2e20, 0xc00098e000})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/resource.go:1043 +0x13a
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ReadDataSource(0xc0000119c8, {0x1752598?, 0xc0006d94a0?}, 0xc0006d9560)
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/schema/grpc_provider.go:1434 +0x6b1
github.com/hashicorp/terraform-plugin-mux/tf5to6server.v5tov6Server.ReadDataSource({{0x175c9e8?, 0xc0000119c8?}}, {0x1752598?, 0xc0006d94a0?}, 0xc0006d9170?)
        github.com/hashicorp/terraform-plugin-mux@v0.16.0/tf5to6server/tf5to6server.go:142 +0x282
github.com/hashicorp/terraform-plugin-mux/tf6muxserver.(*muxServer).ReadDataSource(0xc0003aed20, {0x1752598?, 0xc0006d91d0?}, 0xc0006d9170)
        github.com/hashicorp/terraform-plugin-mux@v0.16.0/tf6muxserver/mux_server_ReadDataSource.go:36 +0x193
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ReadDataSource(0xc0003a8960, {0x1752598?, 0xc0006d83c0?}, 0xc00032cf00)
        github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/tf6server/server.go:688 +0x27d
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ReadDataSource_Handler({0x14fbba0, 0xc0003a8960}, {0x1752598, 0xc0006d83c0}, 0xc000518800, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:572 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001cd200, {0x1752598, 0xc0006d8330}, {0x1758f40, 0xc0001f5080}, 0xc0005bfb00, 0xc000472120, 0x225e7a0, 0x0)
        google.golang.org/grpc@v1.65.0/server.go:1379 +0xdf8
google.golang.org/grpc.(*Server).handleStream(0xc0001cd200, {0x1758f40, 0xc0001f5080}, 0xc0005bfb00)
        google.golang.org/grpc@v1.65.0/server.go:1790 +0xe8b
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/grpc@v1.65.0/server.go:1029 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 53
        google.golang.org/grpc@v1.65.0/server.go:1040 +0x125

Error: The terraform-provider-elasticstack_v0.11.6 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.

I locally compiled the provider out of current branch and tested it like so:

➜  test_elasticstack_provider cat .terraform.rc 
provider_installation {
  dev_overrides {
    "registry.terraform.io/elastic/elasticstack" = "/home/alexandros/go/bin"
  }
  direct {}
}

➜  test_elasticstack_provider export TF_CLI_CONFIG_FILE=.terraform.rc 
➜  test_elasticstack_provider terraform plan
╷
│ Warning: Provider development overrides are in effect
│ 
│ The following provider development overrides are set in the CLI configuration:- elastic/elasticstack in /home/alexandros/go/bin
│ 
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
data.elasticstack_kibana_action_connector.my-connector: Reading...

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: error while creating elasticstack_kibana_action_connector datasource: connector with name [default/doesnotexist] and type [.pagerduty] not found
│ 
│   with data.elasticstack_kibana_action_connector.my-connector,
│   on main.tf line 1, in data "elasticstack_kibana_action_connector" "my-connector":1: data "elasticstack_kibana_action_connector" "my-connector" {
│ 
╵

Now it errors out normally.

@irregulator irregulator requested a review from wandergeek August 26, 2024 21:35
Alexandros Afentoulis and others added 2 commits August 27, 2024 09:17
Make sure that datasourceConnectorRead() returns the appropriate
error when no connectors are found.
@tobio tobio force-pushed the alexaf/fix_kibana_connector_datasource branch from 5784234 to cd5d615 Compare August 26, 2024 23:18
Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 😍

@tobio tobio enabled auto-merge (squash) August 26, 2024 23:19
@tobio tobio merged commit e907847 into main Aug 26, 2024
@tobio tobio deleted the alexaf/fix_kibana_connector_datasource branch August 26, 2024 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants