dbt-labs / terraform-provider-dbtcloud

dbt Cloud Terraform Provider
https://registry.terraform.io/providers/dbt-labs/dbtcloud
MIT License
80 stars 18 forks source link

Plugin crashed: importing environment into terraform #230

Closed obaddour-tiger closed 5 months ago

obaddour-tiger commented 5 months ago

Describe the bug

I have solely worked with DBT Cloud via the web UI. I am now migrating our resources to a terraform representation. I was able to terraform import our DBT Cloud project. However, when trying to import one of our environments, the terraform-provider-dbtcloud_v0.2.2 plugin crashes.

Error message

When running command $ terraform import dbtcloud_environment.prod_environment <redacted>:

dbtcloud_environment.prod_environment: Refreshing state... [id=<redacted>]
╷
│ Error: Plugin did not respond
│ 
│ The plugin encountered an error, and failed to respond to the
│ plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain
│ more details.
╵

Stack trace from the terraform-provider-dbtcloud_v0.2.2 plugin:

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

goroutine 67 [running]:
github.com/dbt-labs/terraform-provider-dbtcloud/pkg/resources.resourceEnvironmentRead({0xce2440, 0xc000598ab0}, 0xc000294980, {0xbd14a0, 0xc00023a7c0})
        github.com/dbt-labs/terraform-provider-dbtcloud/pkg/resources/environment.go:127 +0x8f4
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).read(0xc00038aa80, {0xce2440, 0xc000598ab0}, 0xd, {0xbd14a0, 0xc00023a7c0})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.12.0/helper/schema/resource.go:359 +0x12e
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc00038aa80, {0xce2440, 0xc000598ab0}, 0xc0000eb2b0, {0xbd14a0, 0xc00023a7c0})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.12.0/helper/schema/resource.go:650 +0x47b
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ReadResource(0xc000382348, {0xce2398, 0xc00023b100}, 0xc00023b240)
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.12.0/helper/schema/grpc_provider.go:613 +0x534
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ReadResource(0xc0001ca6e0, {0xce2440, 0xc0005981e0}, 0xc0000bc360)
        github.com/hashicorp/terraform-plugin-go@v0.8.0/tfprotov5/tf5server/server.go:746 +0x48c
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler({0xbae9c0, 0xc0001ca6e0}, {0xce2440, 0xc0005981e0}, 0xc0000bc2a0, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.8.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:349 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001fc8c0, {0xcef118, 0xc000391860}, 0xc0005ac000, 0xc000388a20, 0x11b1c50, 0x0)
        google.golang.org/grpc@v1.45.0/server.go:1282 +0xccf
google.golang.org/grpc.(*Server).handleStream(0xc0001fc8c0, {0xcef118, 0xc000391860}, 0xc0005ac000, 0x0)
        google.golang.org/grpc@v1.45.0/server.go:1619 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        google.golang.org/grpc@v1.45.0/server.go:921 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.45.0/server.go:919 +0x294

Error: The terraform-provider-dbtcloud_v0.2.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.

Resource configuration

main.tf:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.18.0"
    }
    dbtcloud = {
      source  = "dbt-labs/dbtcloud"
      version = "0.2.2"
    }
  }

  backend "s3" {
    bucket  = "<redacted>"
    key     = "<redacted>"
    region  = "<redacted>"
    profile = "<redacted>"
  }
}

provider "aws" {
  region  = "<redacted>"
  profile = "<redacted>"
  allowed_account_ids = [
    "<redacted>",
  ]
}

# DBT Cloud token used for all authentication
data "aws_secretsmanager_secret_version" "dbt_token" {
  secret_id = "<redacted>"
}

provider "dbtcloud" {
  account_id = var.dbt_account_id
  token      = jsondecode(data.aws_secretsmanager_secret_version.dbt_token.secret_string)["token"]
  host_url   = var.dbt_host_url
}

// Project
resource "dbtcloud_project" "project" {
  name                     = "<redacted>"
  dbt_project_subdirectory = "<redacted>"
}

// Environment: PROD
resource "dbtcloud_environment" "prod_environment" {
  dbt_version = "1.6"
  name        = "<redacted>"
  project_id  = dbtcloud_project.project.id
  type        = "<redacted>"
}

variables.tf exists with dbt_account_id and dbt_host_url appropriately.

Expected behavior

An update to terraform state, which means that terraform plan results in No changes

Config:

Additional context

Not sure if there already exists a DBT Cloud project import script that enumerates all DBT Cloud resources, populates the terraform state file, and generates the terraform code, but that would be swell!

b-per commented 5 months ago

Hi @obaddour-tiger !

When importing the environment, are you using this syntax? terraform import dbtcloud_environment.prod_environment "project_id:environment_id", with the : between the project id and the environment id?

The error suggests that the ID doesn't have a : in it.

I would also recommend upgrading to the latest version of the provider, there have been quite a few fixes and enhanced logging since 0.2.2

obaddour-tiger commented 5 months ago

Hi @b-per

That did the trick, thank you! I only had the project_id. Ahhh I'm just seeing now that this is documented at https://registry.terraform.io/providers/dbt-labs/dbtcloud/latest/docs/resources/environment#import... apologies.

Thanks for the tip, will upgrade versions.