terraform-provider-snowflake icon indicating copy to clipboard operation
terraform-provider-snowflake copied to clipboard

panic: runtime error when attempting to import view definitions

Open jamiekt opened this issue 1 year ago • 1 comments

Provider Version

0.70.1

Terraform Version

1.5.7

Describe the bug

I'm attempting to use terraform's new import block to import a view into my terraform configuration. When I run terraform plan on my configuration I receive an error:

Terraform planned the following actions, but then encountered a problem:
  
    # snowflake_database.db will be imported
      resource "snowflake_database" "db" {
          comment                     = "Account usage and other datasets which lets us monitor Snowflake"
          data_retention_time_in_days = 0
          id                          = "MONITORING_DB"
          is_transient                = false
          name                        = "MONITORING_DB"
      }
  
    # snowflake_schema.account_usage will be imported
      resource "snowflake_schema" "account_usage" {
          data_retention_days = 0
          database            = "MONITORING_DB"
          id                  = "MONITORING_DB|ACCOUNT_USAGE"
          is_managed          = false
          is_transient        = false
          name                = "ACCOUNT_USAGE"
      }
  
  Plan: 2 to import, 0 to add, 0 to change, 0 to destroy.
  ╷
  │ 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.
  ╵
  ╷
  │ 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-snowflake_v0.70.1 plugin:
  
  panic: runtime error: index out of range [0] with length 0
  
  goroutine 103 [running]:
  github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake.(*ViewSelectStatementExtractor).consumeToken(0xc000b49200, {0x1ae7177, 0x6})
  	github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake/parser.go:97 +0x148
  github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake.(*ViewSelectStatementExtractor).Extract(0xc000b49200)
  	github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake/parser.go:30 +0x9b
  github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.ReadView(0xc000b13f00, {0x1ac67c0?, 0xc00015eb60})
  	github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/view.go:261 +0x605
  github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).read(0x1de2900?, {0x1de2900?, 0xc000f89e00?}, 0xd?, {0x1ac67c0?, 0xc00015eb60?})
  	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:712 +0x178
  github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc0004a47e0, {0x1de2900, 0xc000f89e00}, 0xc0009ad520, {0x1ac67c0, 0xc00015eb60})
  	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:1015 +0x585
  github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ReadResource(0xc0009018f0, {0x1de2900?, 0xc000f89ce0?}, 0xc000ece240)
  	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:613 +0x4a5
  github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ReadResource(0xc0008da780, {0x1de2900?, 0xc000f89530?}, 0xc000d4f140)
  	github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:748 +0x4b1
  github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler({0x1a79800?, 0xc0008da780}, {0x1de2900, 0xc000f89530}, 0xc0004a09a0, 0x0)
  	github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:349 +0x[170](https://github.com/hellofresh/snowflake-automation/actions/runs/6164744517/job/16731135451#step:6:177)
  google.golang.org/grpc.(*Server).processUnaryRPC(0xc0007e4960, {0x1df22c0, 0xc000582820}, 0xc0005c2120, 0xc00078e0c0, 0x2a3c3b0, 0x0)
  	google.golang.org/[email protected]/server.go:1336 +0xd33
  google.golang.org/grpc.(*Server).handleStream(0xc0007e4960, {0x1df22c0, 0xc000582820}, 0xc0005c2120, 0x0)
  	google.golang.org/[email protected]/server.go:1704 +0xa36
  google.golang.org/grpc.(*Server).serveStreams.func1.2()
  	google.golang.org/[email protected]/server.go:965 +0x98
  created by google.golang.org/grpc.(*Server).serveStreams.func1
  	google.golang.org/[email protected]/server.go:963 +0x28a
  
  Error: The terraform-provider-snowflake_v0.70.1 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.
  
  Error: Terraform exited with code 1.
  Error: Process completed with exit code 1.

Note that if I remove the import block that attempts to import the view then terraform plan succeeds.

Expected behavior I would have expected the plan output to report that the view will be imported successfully.

Code samples and commands

terraform {
  required_version = ">= 0.13"
  required_providers {
    snowflake = {
      source  = "snowflake-labs/snowflake"
      version = "0.70.1"
    }
    vault = {
      source  = "hashicorp/vault"
      version = ">=3.0.0"
    }
  }
  backend "s3" {
    bucket = "redacted"
    key    = "snowflake-automation/monitoring-db.tfstate"
    region = "eu-west-1"
  }

}


data "vault_generic_secret" "vault_secrets_automation" {
  path = format("path/to/secret")
}

provider "snowflake" {
  username    = "[email protected]"
  alias       = "sys"
  account     = "oo69432"
  region      = "eu-west-1"
  private_key = data.vault_generic_secret.vault_secrets_automation.data["srv-automation-sf"]
  role        = "SYSADMIN"
}

import {
  id = "MONITORING_DB"
  to = snowflake_database.db
}

resource "snowflake_database" "db" {
  provider                    = snowflake.sys
  name                        = "MONITORING_DB"
  comment                     = "Account usage and other datasets which lets us monitor Snowflake"
  data_retention_time_in_days = 0
}

import {
  provider = snowflake.sys
  id       = "MONITORING_DB|ACCOUNT_USAGE"
  to       = snowflake_schema.account_usage
}
import {
  provider = snowflake.sys
  id       = "MONITORING_DB|ORGANIZATION_USAGE"
  to       = snowflake_schema.organization_usage
}

resource "snowflake_schema" "account_usage" {
  provider            = snowflake.sys
  name                = "ACCOUNT_USAGE"
  database            = snowflake_database.db.name
  data_retention_days = 0
}
resource "snowflake_schema" "organization_usage" {
  provider            = snowflake.sys
  name                = "ORGANIZATION_USAGE"
  database            = snowflake_database.db.name
  data_retention_days = 0
}

locals {
  views = {
    "ACCOUNT_USAGE.ACCESS_HISTORY" = {
      schema = "ACCOUNT_USAGE"
      name   = "ACCESS_HISTORY"
    }
  }
}

resource "snowflake_view" "view" {
  provider  = snowflake.sys
  for_each  = local.views
  database  = snowflake_database.db.name
  schema    = each.value.schema
  name      = each.value.name
  statement = file("./view_definitions/${each.key}.sql")
}

# import blocks do not support for_each at the time of writing. Keep an eye on
# https://github.com/hashicorp/terraform/issues/33624
import {
  provider = snowflake.sys
  id       = "MONITORING_DB|ACCOUNT_USAGE|ACCESS_HISTORY"
  to       = snowflake_view.view["ACCOUNT_USAGE.ACCESS_HISTORY"]
}

Note that the plan successfully reports an import of the database and two schemas. Its simply the import block for the view that fails.

jamiekt avatar Sep 12 '23 21:09 jamiekt

Hey 👋 Could you share what's hiding in the file passed to the statement field? From the logs, I can see the statement is causing it to crash. I'm not sure what the file function returns if the file path is invalid, but it looks like the statement may be an empty string. If you would provide us the logs with the TF_LOG=DEBUG flag enabled we would see from the logs what statement is being parsed before the crash (it should say [DEBUG] extracting view query: <statement>).

sfc-gh-jcieslak avatar Apr 29 '24 11:04 sfc-gh-jcieslak

Closing due to long inactivity. If the issue persists, please create a new issue.

sfc-gh-jcieslak avatar May 21 '24 09:05 sfc-gh-jcieslak