Snowflake-Labs / terraform-provider-snowflake

Terraform provider for managing Snowflake accounts
https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest
MIT License
528 stars 411 forks source link

Can not create external stage with storage integration #1795

Open toni-moreno opened 1 year ago

toni-moreno commented 1 year ago

Provider Version

Terraform Version

Terraform v1.4.6 on linux_amd64

Problem description

When trying to create new external stages with storage_integration this error happens

╷
│ Error: error creating stage MYSTAGE
│ 
│   with snowflake_stage.stage["MYDB|PUBLIC|MYSTAGE"],
│   on db-objects.tf line 174, in resource "snowflake_stage" "stage":
│  174: resource "snowflake_stage" "stage" {
│ 
╵

Expected behavior

Create a new storage integration

Code samples and commands

resource "snowflake_stage" "stage" {
  for_each = local.stage_map

  database            = MYDB
  schema              = PUBLIC
  name                = MYSTAGE
  url                 = "azure://xxxxxx.blob.core.windows.net/snowflakecsvx"
  storage_integration = "azure_integration"

}

Additional context

After review some logs setting TF_LOG env var, I've noticed that the provider is building erroneously the query . It creates the following query

CREATE STAGE "MYDB"."PUBLIC"."MYSTAGE" URL = 'azure://xxxxxx.blob.core.windows.net/snowflakecsv' STORAGE_INTEGRATION = "azure_integration"

but the correct query should built without double quotes in "azure_integration"

CREATE STAGE "MYDB"."PUBLIC"."MYSTAGE" URL = 'azure://xxxxxx.blob.core.windows.net/snowflakecsv' STORAGE_INTEGRATION = azure_integration
danu165 commented 1 year ago

If you change storage_integration = "azure_integration" to storage_integration = "AZURE_INTEGRATION" then that should fix your issue

toni-moreno commented 1 year ago

@danu165 you are right .

But the provider should work with both cases upper and lowercase integration names. IMHO The bug still exist.

These are the 4 possible options: lower/upper with double quotes and without

With double quotes

CREATE STAGE "MYDB"."PUBLIC"."MYSTAKE" URL = 'azure://xxxxx' STORAGE_INTEGRATION = "azure_integration"; --> FAIL  
CREATE STAGE "MYDB"."PUBLIC"."MYSTAKE" URL = 'azure://xxxxx' STORAGE_INTEGRATION = "AZURE_INTEGRATION"; OK  

Without double quotes

CREATE STAGE "MYDB"."PUBLIC"."MYSTAKE" URL = 'azure://xxxxx' STORAGE_INTEGRATION = azure_integration;   OK
CREATE STAGE "MYDB"."PUBLIC"."MYSTAKE" URL = 'azure://xxxxx' STORAGE_INTEGRATION = AZURE_INTEGRATION; OK  

The way it works with both upper and lower case names is without double quotes.

danu165 commented 1 year ago

Wouldn't that make it impossible for users that have lowercase storage integrations to specify their integrations? IE. if someone created a storage integration using CREATE STORAGE INTEGRATION "azure_integration" then it needs to be referred as "azure_integration". Not using quotes would fail.

toni-moreno commented 1 year ago

Hi @danu165 this is a rare case but , you are right, this could happens, and removing the double quotes won't fix it. Maybe could be good if anybody could add better information in the provider documentation, remembering and/or recommending use uppercase names for integrations in this resource.