hocmodo / terraform-provider-leostream

MIT License
0 stars 0 forks source link

Handling of nil values #1

Open Joustie opened 1 month ago

Joustie commented 1 month ago

The current code base needs more testing, because up until now, I have chose only happy paths. This means that if you use existing resources, import them, you are fine. But trouble comes if you start minimal.

For instance: Given this terraform file which defines only a minimal basic pool:

terraform {
  required_providers {
    leostream = {
      source = "registry.terraform.io/hocmodo/leostream"

    }
  }
}

// This block configures the Leostream provider.
provider "leostream" {
  host     = "https://<broker url>"
  username = "someapiuser"
  password = "somepassword"
}

resource "leostream_basic_pool" "pool1" {
    name = "Test a basic pool"
    pool_definition = {
        restrict_by = "A"
    }
}

Above will fail with:

% terraform apply --autoapprove
......
Plan: 1 to add, 0 to change, 0 to destroy.
leostream_basic_pool.pool1: Creating...
│ Error: Plugin did not respond
│ 
│ The plugin encountered an error, and failed to respond to the plugin6.(*GRPCProvider).ApplyResourceChange call. The plugin logs may contain more details.
╵

Stack trace from the terraform-provider-leostream_v0.1.0 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x104613a3c]

goroutine 56 [running]:
terraform-provider-leostream/leostream.(*basicPoolResource).Create(0xd?, {0x104b3eff8, 0x14000526cf0}, {{{{0x104b43328, 0x14000546690}, {0x104a3a820, 0x14000546030}}, {0x104b451a0, 0x140000999f0}}, {{{0x104b43328, ...}, ...}, ...}, ...}, ...)
    terraform-provider-leostream/leostream/pool_resource_basic.go:386 +0x1dc
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0x140001f81e0, {0x104b3eff8, 0x14000526cf0}, 0x140004d9560, 0x140004d9500)
    github.com/hashicorp/terraform-plugin-framework@v1.9.0/internal/fwserver/server_createresource.go:101 +0x41c
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0x140004d96c0?, {0x104b3eff8, 0x14000526cf0}, 0x14000528410, 0x140004d96c0)
    github.com/hashicorp/terraform-plugin-framework@v1.9.0/internal/fwserver/server_applyresourcechange.go:57 +0x380
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ApplyResourceChange(0x140001f81e0, {0x104b3eff8?, 0x14000526bd0?}, 0x14000528370)
    github.com/hashicorp/terraform-plugin-framework@v1.9.0/internal/proto6server/server_applyresourcechange.go:55 +0x314
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ApplyResourceChange(0x140001ce640, {0x104b3eff8?, 0x140005261e0?}, 0x1400031a0e0)
    github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/tf6server/server.go:865 +0x2b0
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ApplyResourceChange_Handler({0x104b11fc0?, 0x140001ce640}, {0x104b3eff8, 0x140005261e0}, 0x14000112000, 0x0)
    github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:518 +0x164
google.golang.org/grpc.(*Server).processUnaryRPC(0x1400018ce00, {0x104b3eff8, 0x14000526150}, {0x104b43e78, 0x14000178000}, 0x1400052a000, 0x140002ef980, 0x104ff69b8, 0x0)
    google.golang.org/grpc@v1.64.0/server.go:1379 +0xba0
google.golang.org/grpc.(*Server).handleStream(0x1400018ce00, {0x104b43e78, 0x14000178000}, 0x1400052a000)
    google.golang.org/grpc@v1.64.0/server.go:1790 +0xc78
google.golang.org/grpc.(*Server).serveStreams.func2.1()
    google.golang.org/grpc@v1.64.0/server.go:1029 +0x8c
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 20
    google.golang.org/grpc@v1.64.0/server.go:1040 +0x150

Error: The terraform-provider-leostream_v0.1.0 plugin crashed!

This is caused by me not thinking of the fact that building up nested resources without actively checking for nil values will cause these hideous crashes.

I will start a new branch to work on this and try to eliminate this throughout the code.

Joustie commented 1 month ago

Branch #2 opened and I fixed the the situation above. I also added a test for it.