From 6fd63bb7a89d76a89037bf7b3284859068ac1b42 Mon Sep 17 00:00:00 2001 From: Christopher Isidora Date: Tue, 27 Jul 2021 16:06:43 +0200 Subject: [PATCH 1/6] Added Several Datasources Part 2 --- .../data-source.tf | 4 + .../snowflake_external_tables/data-source.tf | 4 + .../snowflake_file_formats/data-source.tf | 4 + .../snowflake_masking_policies/data-source.tf | 4 + .../snowflake_pipes/data-source.tf | 4 + .../data-source.tf | 2 + .../snowflake_sequences/data-source.tf | 4 + .../snowflake_stages/data-source.tf | 4 + .../data-source.tf | 2 + .../snowflake_streams/data-source.tf | 4 + .../snowflake_tasks/data-source.tf | 4 + .../snowflake_warehouses/data-source.tf | 2 + pkg/datasources/external_functions.go | 96 ++++++++++++++++++ .../external_functions_acceptance_test.go | 78 +++++++++++++++ pkg/datasources/external_tables.go | 90 +++++++++++++++++ .../external_tables_acceptance_test.go | 78 +++++++++++++++ pkg/datasources/file_formats.go | 96 ++++++++++++++++++ .../file_formats_acceptance_test.go | 79 +++++++++++++++ pkg/datasources/masking_policies.go | 96 ++++++++++++++++++ .../masking_policies_acceptance_test.go | 60 ++++++++++++ pkg/datasources/pipes.go | 96 ++++++++++++++++++ pkg/datasources/pipes_acceptance_test.go | 86 ++++++++++++++++ pkg/datasources/resource_monitors.go | 85 ++++++++++++++++ .../resource_monitors_acceptance_test.go | 39 ++++++++ pkg/datasources/sequences.go | 90 +++++++++++++++++ pkg/datasources/sequences_acceptance_test.go | 56 +++++++++++ pkg/datasources/stages.go | 96 ++++++++++++++++++ pkg/datasources/stages_acceptance_test.go | 56 +++++++++++ pkg/datasources/storage_integrations.go | 85 ++++++++++++++++ .../storage_integrations_acceptance_test.go | 42 ++++++++ pkg/datasources/streams.go | 96 ++++++++++++++++++ pkg/datasources/streams_acceptance_test.go | 74 ++++++++++++++ pkg/datasources/tasks.go | 96 ++++++++++++++++++ pkg/datasources/tasks_acceptance_test.go | 67 +++++++++++++ pkg/datasources/warehouses.go | 97 +++++++++++++++++++ pkg/datasources/warehouses_acceptance_test.go | 41 ++++++++ pkg/provider/provider.go | 12 +++ pkg/snowflake/external_function.go | 19 ++++ pkg/snowflake/external_table.go | 19 ++++ pkg/snowflake/file_format.go | 19 ++++ pkg/snowflake/masking_policy.go | 19 ++++ pkg/snowflake/pipe.go | 19 ++++ pkg/snowflake/resource_monitor.go | 19 ++++ pkg/snowflake/sequence.go | 8 +- pkg/snowflake/stage.go | 19 ++++ pkg/snowflake/storage_integration.go | 20 ++++ pkg/snowflake/stream.go | 19 ++++ pkg/snowflake/task.go | 20 ++++ pkg/snowflake/warehouse.go | 20 ++++ 49 files changed, 2145 insertions(+), 4 deletions(-) create mode 100644 examples/data-sources/snowflake_external_functions/data-source.tf create mode 100644 examples/data-sources/snowflake_external_tables/data-source.tf create mode 100644 examples/data-sources/snowflake_file_formats/data-source.tf create mode 100644 examples/data-sources/snowflake_masking_policies/data-source.tf create mode 100644 examples/data-sources/snowflake_pipes/data-source.tf create mode 100644 examples/data-sources/snowflake_resource_monitors/data-source.tf create mode 100644 examples/data-sources/snowflake_sequences/data-source.tf create mode 100644 examples/data-sources/snowflake_stages/data-source.tf create mode 100644 examples/data-sources/snowflake_storage_integrations/data-source.tf create mode 100644 examples/data-sources/snowflake_streams/data-source.tf create mode 100644 examples/data-sources/snowflake_tasks/data-source.tf create mode 100644 examples/data-sources/snowflake_warehouses/data-source.tf create mode 100644 pkg/datasources/external_functions.go create mode 100644 pkg/datasources/external_functions_acceptance_test.go create mode 100644 pkg/datasources/external_tables.go create mode 100644 pkg/datasources/external_tables_acceptance_test.go create mode 100644 pkg/datasources/file_formats.go create mode 100644 pkg/datasources/file_formats_acceptance_test.go create mode 100644 pkg/datasources/masking_policies.go create mode 100644 pkg/datasources/masking_policies_acceptance_test.go create mode 100644 pkg/datasources/pipes.go create mode 100644 pkg/datasources/pipes_acceptance_test.go create mode 100644 pkg/datasources/resource_monitors.go create mode 100644 pkg/datasources/resource_monitors_acceptance_test.go create mode 100644 pkg/datasources/sequences.go create mode 100644 pkg/datasources/sequences_acceptance_test.go create mode 100644 pkg/datasources/stages.go create mode 100644 pkg/datasources/stages_acceptance_test.go create mode 100644 pkg/datasources/storage_integrations.go create mode 100644 pkg/datasources/storage_integrations_acceptance_test.go create mode 100644 pkg/datasources/streams.go create mode 100644 pkg/datasources/streams_acceptance_test.go create mode 100644 pkg/datasources/tasks.go create mode 100644 pkg/datasources/tasks_acceptance_test.go create mode 100644 pkg/datasources/warehouses.go create mode 100644 pkg/datasources/warehouses_acceptance_test.go diff --git a/examples/data-sources/snowflake_external_functions/data-source.tf b/examples/data-sources/snowflake_external_functions/data-source.tf new file mode 100644 index 0000000000..fc3a9519df --- /dev/null +++ b/examples/data-sources/snowflake_external_functions/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_external_functions" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_external_tables/data-source.tf b/examples/data-sources/snowflake_external_tables/data-source.tf new file mode 100644 index 0000000000..d5c67095c7 --- /dev/null +++ b/examples/data-sources/snowflake_external_tables/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_external_tables" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_file_formats/data-source.tf b/examples/data-sources/snowflake_file_formats/data-source.tf new file mode 100644 index 0000000000..2d21bbe36e --- /dev/null +++ b/examples/data-sources/snowflake_file_formats/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_file_formats" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_masking_policies/data-source.tf b/examples/data-sources/snowflake_masking_policies/data-source.tf new file mode 100644 index 0000000000..eca3077b3b --- /dev/null +++ b/examples/data-sources/snowflake_masking_policies/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_masking_policies" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_pipes/data-source.tf b/examples/data-sources/snowflake_pipes/data-source.tf new file mode 100644 index 0000000000..fb63852e81 --- /dev/null +++ b/examples/data-sources/snowflake_pipes/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_pipes" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_resource_monitors/data-source.tf b/examples/data-sources/snowflake_resource_monitors/data-source.tf new file mode 100644 index 0000000000..aec8f1fcf6 --- /dev/null +++ b/examples/data-sources/snowflake_resource_monitors/data-source.tf @@ -0,0 +1,2 @@ +data "snowflake_resource_monitors" "current" { +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_sequences/data-source.tf b/examples/data-sources/snowflake_sequences/data-source.tf new file mode 100644 index 0000000000..a822f7a88a --- /dev/null +++ b/examples/data-sources/snowflake_sequences/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_sequences" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_stages/data-source.tf b/examples/data-sources/snowflake_stages/data-source.tf new file mode 100644 index 0000000000..1a9829d876 --- /dev/null +++ b/examples/data-sources/snowflake_stages/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_stages" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_storage_integrations/data-source.tf b/examples/data-sources/snowflake_storage_integrations/data-source.tf new file mode 100644 index 0000000000..29b61e5c02 --- /dev/null +++ b/examples/data-sources/snowflake_storage_integrations/data-source.tf @@ -0,0 +1,2 @@ +data "snowflake_storage_integrations" "current" { +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_streams/data-source.tf b/examples/data-sources/snowflake_streams/data-source.tf new file mode 100644 index 0000000000..f2015e0e62 --- /dev/null +++ b/examples/data-sources/snowflake_streams/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_streams" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_tasks/data-source.tf b/examples/data-sources/snowflake_tasks/data-source.tf new file mode 100644 index 0000000000..8aa5d48e23 --- /dev/null +++ b/examples/data-sources/snowflake_tasks/data-source.tf @@ -0,0 +1,4 @@ +data "snowflake_tasks" "current" { + database = "MYDB" + schema = "MYSCHEMA" +} \ No newline at end of file diff --git a/examples/data-sources/snowflake_warehouses/data-source.tf b/examples/data-sources/snowflake_warehouses/data-source.tf new file mode 100644 index 0000000000..3046f77d4c --- /dev/null +++ b/examples/data-sources/snowflake_warehouses/data-source.tf @@ -0,0 +1,2 @@ +data "snowflake_warehouses" "current" { +} \ No newline at end of file diff --git a/pkg/datasources/external_functions.go b/pkg/datasources/external_functions.go new file mode 100644 index 0000000000..62de91b3fa --- /dev/null +++ b/pkg/datasources/external_functions.go @@ -0,0 +1,96 @@ +package datasources + +import ( + "database/sql" + "fmt" + "log" + + "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var externalFunctionsSchema = map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + Description: "The database from which to return the schemas from.", + }, + "schema": { + Type: schema.TypeString, + Required: true, + Description: "The schema from which to return the external functions from.", + }, + "external_functions": { + Type: schema.TypeList, + Computed: true, + Description: "The external functions in the schema", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "database": { + Type: schema.TypeString, + Computed: true, + }, + "schema": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "language": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, +} + +func ExternalFunctions() *schema.Resource { + return &schema.Resource{ + Read: ReadExternalFunctions, + Schema: externalFunctionsSchema, + } +} + +func ReadExternalFunctions(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + databaseName := d.Get("database").(string) + schemaName := d.Get("schema").(string) + + currentExternalFunctions, err := snowflake.ListExternalFunctions(databaseName, schemaName, db) + if err == sql.ErrNoRows { + // If not found, mark resource to be removed from statefile during apply or refresh + log.Printf("[DEBUG] external functions in schema (%s) not found", d.Id()) + d.SetId("") + return nil + } else if err != nil { + log.Printf("[DEBUG] unable to parse external functions in schema (%s)", d.Id()) + d.SetId("") + return nil + } + + externalFunctions := []map[string]interface{}{} + + for _, externalFunction := range currentExternalFunctions { + externalFunctionMap := map[string]interface{}{} + + externalFunctionMap["name"] = externalFunction.ExternalFunctionName.String + externalFunctionMap["database"] = externalFunction.DatabaseName.String + externalFunctionMap["schema"] = externalFunction.SchemaName.String + externalFunctionMap["comment"] = externalFunction.Comment.String + externalFunctionMap["language"] = externalFunction.Language.String + + externalFunctions = append(externalFunctions, externalFunctionMap) + } + + d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) + return d.Set("external_functions", externalFunctions) +} diff --git a/pkg/datasources/external_functions_acceptance_test.go b/pkg/datasources/external_functions_acceptance_test.go new file mode 100644 index 0000000000..66e5cef36f --- /dev/null +++ b/pkg/datasources/external_functions_acceptance_test.go @@ -0,0 +1,78 @@ +package datasources_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccExternalFunctions(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + apiName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + externalFunctionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: externalFunctions(databaseName, schemaName, apiName, externalFunctionName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "database", databaseName), + resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "schema", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_external_functions.t", "external_functions.#"), + resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "external_functions.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "external_functions.0.name", externalFunctionName), + ), + }, + }, + }) +} + +func externalFunctions(databaseName string, schemaName string, apiName string, externalFunctionName string) string { + return fmt.Sprintf(` + + resource snowflake_database "test_database" { + name = "%v" + } + + resource snowflake_schema "test_schema"{ + name = "%v" + database = snowflake_database.test_database.name + } + + resource "snowflake_api_integration" "test_api_int" { + name = "%v" + api_provider = "aws_api_gateway" + api_aws_role_arn = "arn:aws:iam::000000000001:/role/test" + api_allowed_prefixes = ["https://123456.execute-api.us-west-2.amazonaws.com/prod/"] + enabled = true + } + + resource "snowflake_external_function" "test_func" { + name = "%v" + database = snowflake_database.test_database.name + schema = snowflake_schema.test_schema.name + arg { + name = "arg1" + type = "varchar" + } + arg { + name = "arg2" + type = "varchar" + } + comment = "Terraform acceptance test" + return_type = "varchar" + return_behavior = "IMMUTABLE" + api_integration = snowflake_api_integration.test_api_int.name + url_of_proxy_and_resource = "https://123456.execute-api.us-west-2.amazonaws.com/prod/test_func" + } + + data snowflake_external_functions "t" { + database = snowflake_external_function.test_func.database + schema = snowflake_external_function.test_func.schema + } + `, databaseName, schemaName, apiName, externalFunctionName) +} diff --git a/pkg/datasources/external_tables.go b/pkg/datasources/external_tables.go new file mode 100644 index 0000000000..f63408425f --- /dev/null +++ b/pkg/datasources/external_tables.go @@ -0,0 +1,90 @@ +package datasources + +import ( + "database/sql" + "fmt" + "log" + + "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var externalTablesSchema = map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + Description: "The database from which to return the schemas from.", + }, + "schema": { + Type: schema.TypeString, + Required: true, + Description: "The schema from which to return the external tables from.", + }, + "external_tables": { + Type: schema.TypeList, + Computed: true, + Description: "The external tables in the schema", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "database": { + Type: schema.TypeString, + Computed: true, + }, + "schema": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, +} + +func ExternalTables() *schema.Resource { + return &schema.Resource{ + Read: ReadExternalTables, + Schema: externalTablesSchema, + } +} + +func ReadExternalTables(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + databaseName := d.Get("database").(string) + schemaName := d.Get("schema").(string) + + currentExternalTables, err := snowflake.ListExternalTables(databaseName, schemaName, db) + if err == sql.ErrNoRows { + // If not found, mark resource to be removed from statefile during apply or refresh + log.Printf("[DEBUG] external tables in schema (%s) not found", d.Id()) + d.SetId("") + return nil + } else if err != nil { + log.Printf("[DEBUG] unable to parse external tables in schema (%s)", d.Id()) + d.SetId("") + return nil + } + + externalTables := []map[string]interface{}{} + + for _, externalTable := range currentExternalTables { + externalTableMap := map[string]interface{}{} + + externalTableMap["name"] = externalTable.ExternalTableName.String + externalTableMap["database"] = externalTable.DatabaseName.String + externalTableMap["schema"] = externalTable.SchemaName.String + externalTableMap["comment"] = externalTable.Comment.String + + externalTables = append(externalTables, externalTableMap) + } + + d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) + return d.Set("external_tables", externalTables) +} diff --git a/pkg/datasources/external_tables_acceptance_test.go b/pkg/datasources/external_tables_acceptance_test.go new file mode 100644 index 0000000000..af7b5dbbb3 --- /dev/null +++ b/pkg/datasources/external_tables_acceptance_test.go @@ -0,0 +1,78 @@ +package datasources_test + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccExternalTables(t *testing.T) { + if _, ok := os.LookupEnv("SKIP_EXTERNAL_TABLE_TESTS"); ok { + t.Skip("Skipping TestAccExternalTable") + } + + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + stageName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + externalTableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: externalTables(databaseName, schemaName, stageName, externalTableName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_external_tables.t", "database", databaseName), + resource.TestCheckResourceAttr("data.snowflake_external_tables.t", "schema", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_external_tables.t", "external_tables.#"), + resource.TestCheckResourceAttr("data.snowflake_external_tables.t", "external_tables.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_external_tables.t", "external_tables.0.name", externalTableName), + ), + }, + }, + }) +} + +func externalTables(databaseName string, schemaName string, stageName string, externalTableName string) string { + return fmt.Sprintf(` + + resource snowflake_database "test" { + name = "%v" + } + + resource snowflake_schema "test"{ + name = "%v" + database = snowflake_database.test.name + } + + resource "snowflake_stage" "test" { + name = "%v" + url = "s3://snowflake-workshop-lab/weather-nyc" + database = snowflake_database.test.name + schema = snowflake_schema.test.name + comment = "Terraform acceptance test" + } + + resource "snowflake_external_table" "test_table" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = "%v" + comment = "Terraform acceptance test" + column { + name = "column1" + type = "STRING" + as = "TO_VARCHAR(TO_TIMESTAMP_NTZ(value:unix_timestamp_property::NUMBER, 3), 'yyyy-mm-dd-hh')" + } + file_format = "TYPE = CSV" + location = "@${snowflake_database.test.name}.${snowflake_schema.test.name}.${snowflake_stage.test.name}" + } + + data snowflake_external_tables "t" { + database = snowflake_external_table.test_table.database + schema = snowflake_external_table.test_table.schema + } + `, databaseName, schemaName, stageName, externalTableName) +} diff --git a/pkg/datasources/file_formats.go b/pkg/datasources/file_formats.go new file mode 100644 index 0000000000..71934d5d7b --- /dev/null +++ b/pkg/datasources/file_formats.go @@ -0,0 +1,96 @@ +package datasources + +import ( + "database/sql" + "fmt" + "log" + + "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var fileFormatsSchema = map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + Description: "The database from which to return the schemas from.", + }, + "schema": { + Type: schema.TypeString, + Required: true, + Description: "The schema from which to return the file formats from.", + }, + "file_formats": { + Type: schema.TypeList, + Computed: true, + Description: "The file formats in the schema", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "database": { + Type: schema.TypeString, + Computed: true, + }, + "schema": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "format_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, +} + +func FileFormats() *schema.Resource { + return &schema.Resource{ + Read: ReadFileFormats, + Schema: fileFormatsSchema, + } +} + +func ReadFileFormats(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + databaseName := d.Get("database").(string) + schemaName := d.Get("schema").(string) + + currentFileFormats, err := snowflake.ListFileFormats(databaseName, schemaName, db) + if err == sql.ErrNoRows { + // If not found, mark resource to be removed from statefile during apply or refresh + log.Printf("[DEBUG] file formats in schema (%s) not found", d.Id()) + d.SetId("") + return nil + } else if err != nil { + log.Printf("[DEBUG] unable to parse file formats in schema (%s)", d.Id()) + d.SetId("") + return nil + } + + fileFormats := []map[string]interface{}{} + + for _, fileFormat := range currentFileFormats { + fileFormatMap := map[string]interface{}{} + + fileFormatMap["name"] = fileFormat.FileFormatName.String + fileFormatMap["database"] = fileFormat.DatabaseName.String + fileFormatMap["schema"] = fileFormat.SchemaName.String + fileFormatMap["comment"] = fileFormat.Comment.String + fileFormatMap["format_type"] = fileFormat.FormatType.String + + fileFormats = append(fileFormats, fileFormatMap) + } + + d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) + return d.Set("file_formats", fileFormats) +} diff --git a/pkg/datasources/file_formats_acceptance_test.go b/pkg/datasources/file_formats_acceptance_test.go new file mode 100644 index 0000000000..d1bd4599eb --- /dev/null +++ b/pkg/datasources/file_formats_acceptance_test.go @@ -0,0 +1,79 @@ +package datasources_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccFileFormats(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + fileFormatName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: fileFormats(databaseName, schemaName, fileFormatName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_file_formats.t", "database", databaseName), + resource.TestCheckResourceAttr("data.snowflake_file_formats.t", "schema", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_file_formats.t", "file_formats.#"), + resource.TestCheckResourceAttr("data.snowflake_file_formats.t", "file_formats.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_file_formats.t", "file_formats.0.name", fileFormatName), + ), + }, + }, + }) +} + +func fileFormats(databaseName string, schemaName string, fileFormatName string) string { + return fmt.Sprintf(` + + resource snowflake_database "d" { + name = "%v" + } + + resource snowflake_schema "s"{ + name = "%v" + database = snowflake_database.d.name + } + + resource snowflake_file_format "t"{ + name = "%v" + database = snowflake_schema.s.database + schema = snowflake_schema.s.name + format_type = "CSV" + compression = "GZIP" + record_delimiter = "\r" + field_delimiter = ";" + file_extension = ".ssv" + skip_header = 1 + skip_blank_lines = true + date_format = "YYY-MM-DD" + time_format = "HH24:MI" + timestamp_format = "YYYY-MM-DD HH24:MI:SS.FFTZH:TZM" + binary_format = "UTF8" + escape = "\\" + escape_unenclosed_field = "!" + trim_space = true + field_optionally_enclosed_by = "'" + null_if = ["NULL"] + error_on_column_count_mismatch = true + replace_invalid_characters = true + validate_utf8 = false + empty_field_as_null = false + skip_byte_order_mark = false + encoding = "UTF-16" + comment = "Terraform acceptance test" + } + + data snowflake_file_formats "t" { + database = snowflake_file_format.t.database + schema = snowflake_file_format.t.schema + } + `, databaseName, schemaName, fileFormatName) +} diff --git a/pkg/datasources/masking_policies.go b/pkg/datasources/masking_policies.go new file mode 100644 index 0000000000..c48c4ee8bd --- /dev/null +++ b/pkg/datasources/masking_policies.go @@ -0,0 +1,96 @@ +package datasources + +import ( + "database/sql" + "fmt" + "log" + + "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var maskingPoliciesSchema = map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + Description: "The database from which to return the schemas from.", + }, + "schema": { + Type: schema.TypeString, + Required: true, + Description: "The schema from which to return the maskingPolicies from.", + }, + "masking_policies": { + Type: schema.TypeList, + Computed: true, + Description: "The maskingPolicies in the schema", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "database": { + Type: schema.TypeString, + Computed: true, + }, + "schema": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "kind": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, +} + +func MaskingPolicies() *schema.Resource { + return &schema.Resource{ + Read: ReadMaskingPolicies, + Schema: maskingPoliciesSchema, + } +} + +func ReadMaskingPolicies(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + databaseName := d.Get("database").(string) + schemaName := d.Get("schema").(string) + + currentMaskingPolicies, err := snowflake.ListMaskingPolicies(databaseName, schemaName, db) + if err == sql.ErrNoRows { + // If not found, mark resource to be removed from statefile during apply or refresh + log.Printf("[DEBUG] masking policies in schema (%s) not found", d.Id()) + d.SetId("") + return nil + } else if err != nil { + log.Printf("[DEBUG] unable to parse masking policies in schema (%s)", d.Id()) + d.SetId("") + return nil + } + + maskingPolicies := []map[string]interface{}{} + + for _, maskingPolicy := range currentMaskingPolicies { + maskingPolicyMap := map[string]interface{}{} + + maskingPolicyMap["name"] = maskingPolicy.Name.String + maskingPolicyMap["database"] = maskingPolicy.DatabaseName.String + maskingPolicyMap["schema"] = maskingPolicy.SchemaName.String + maskingPolicyMap["comment"] = maskingPolicy.Comment.String + maskingPolicyMap["kind"] = maskingPolicy.Kind.String + + maskingPolicies = append(maskingPolicies, maskingPolicyMap) + } + + d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) + return d.Set("masking_policies", maskingPolicies) +} diff --git a/pkg/datasources/masking_policies_acceptance_test.go b/pkg/datasources/masking_policies_acceptance_test.go new file mode 100644 index 0000000000..d2cc9602ab --- /dev/null +++ b/pkg/datasources/masking_policies_acceptance_test.go @@ -0,0 +1,60 @@ +package datasources_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccMaskingPolicies(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + maskingPolicyName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: masking_policies(databaseName, schemaName, maskingPolicyName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "database", databaseName), + resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "schema", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_masking_policies.t", "masking_policies.#"), + resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "masking_policies.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "masking_policies.0.name", maskingPolicyName), + ), + }, + }, + }) +} + +func masking_policies(databaseName string, schemaName string, maskingPolicyName string) string { + return fmt.Sprintf(` + + resource snowflake_database "test" { + name = "%v" + } + + resource snowflake_schema "test"{ + name = "%v" + database = snowflake_database.test.name + } + + resource "snowflake_masking_policy" "test" { + name = "%v" + database = snowflake_database.test.name + schema = snowflake_schema.test.name + value_data_type = "VARCHAR" + masking_expression = "case when current_role() in ('ANALYST') then val else sha2(val, 512) end" + return_data_type = "VARCHAR(16777216)" + comment = "Terraform acceptance test" + } + + data snowflake_masking_policies "t" { + database = snowflake_masking_policy.test.database + schema = snowflake_masking_policy.test.schema + } + `, databaseName, schemaName, maskingPolicyName) +} diff --git a/pkg/datasources/pipes.go b/pkg/datasources/pipes.go new file mode 100644 index 0000000000..539ee4f6d0 --- /dev/null +++ b/pkg/datasources/pipes.go @@ -0,0 +1,96 @@ +package datasources + +import ( + "database/sql" + "fmt" + "log" + + "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var pipesSchema = map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + Description: "The database from which to return the schemas from.", + }, + "schema": { + Type: schema.TypeString, + Required: true, + Description: "The schema from which to return the pipes from.", + }, + "pipes": { + Type: schema.TypeList, + Computed: true, + Description: "The pipes in the schema", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "database": { + Type: schema.TypeString, + Computed: true, + }, + "schema": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "integration": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, +} + +func Pipes() *schema.Resource { + return &schema.Resource{ + Read: ReadPipes, + Schema: pipesSchema, + } +} + +func ReadPipes(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + databaseName := d.Get("database").(string) + schemaName := d.Get("schema").(string) + + currentPipes, err := snowflake.ListPipes(databaseName, schemaName, db) + if err == sql.ErrNoRows { + // If not found, mark resource to be removed from statefile during apply or refresh + log.Printf("[DEBUG] pipes in schema (%s) not found", d.Id()) + d.SetId("") + return nil + } else if err != nil { + log.Printf("[DEBUG] unable to parse pipes in schema (%s)", d.Id()) + d.SetId("") + return nil + } + + pipes := []map[string]interface{}{} + + for _, pipe := range currentPipes { + pipeMap := map[string]interface{}{} + + pipeMap["name"] = pipe.Name + pipeMap["database"] = pipe.DatabaseName + pipeMap["schema"] = pipe.SchemaName + pipeMap["comment"] = pipe.Comment + pipeMap["integration"] = pipe.Integration.String + + pipes = append(pipes, pipeMap) + } + + d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) + return d.Set("pipes", pipes) +} diff --git a/pkg/datasources/pipes_acceptance_test.go b/pkg/datasources/pipes_acceptance_test.go new file mode 100644 index 0000000000..a9842a95a2 --- /dev/null +++ b/pkg/datasources/pipes_acceptance_test.go @@ -0,0 +1,86 @@ +package datasources_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccPipes(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + pipeName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: pipes(databaseName, schemaName, pipeName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_pipes.t", "database", databaseName), + resource.TestCheckResourceAttr("data.snowflake_pipes.t", "schema", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_pipes.t", "pipes.#"), + resource.TestCheckResourceAttr("data.snowflake_pipes.t", "pipes.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_pipes.t", "pipes.0.name", pipeName), + ), + }, + }, + }) +} + +func pipes(databaseName string, schemaName string, pipeName string) string { + s := ` +resource "snowflake_database" "test" { + name = "%v" + comment = "Terraform acceptance test" +} + +resource "snowflake_schema" "test" { + name = "%v" + database = snowflake_database.test.name + comment = "Terraform acceptance test" +} + +resource "snowflake_table" "test" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = snowflake_schema.test.name + column { + name = "id" + type = "NUMBER(5,0)" + } + column { + name = "data" + type = "VARCHAR(16)" + } +} + +resource "snowflake_stage" "test" { + name = snowflake_schema.test.name + database = snowflake_database.test.name + schema = snowflake_schema.test.name + comment = "Terraform acceptance test" +} + +data snowflake_pipes "t" { + database = snowflake_pipe.test.database + schema = snowflake_pipe.test.schema +} + +resource "snowflake_pipe" "test" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = "%v" + comment = "Terraform acceptance test" + copy_statement = < Date: Wed, 28 Jul 2021 08:56:51 +0200 Subject: [PATCH 2/6] Fixed Acceptance Tests --- pkg/datasources/resource_monitors.go | 4 +++- pkg/datasources/storage_integrations.go | 4 +++- pkg/datasources/streams_acceptance_test.go | 9 +++++---- pkg/datasources/warehouses.go | 4 +++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/datasources/resource_monitors.go b/pkg/datasources/resource_monitors.go index 3ed2dd1c72..4d2a1febb0 100644 --- a/pkg/datasources/resource_monitors.go +++ b/pkg/datasources/resource_monitors.go @@ -2,6 +2,7 @@ package datasources import ( "database/sql" + "fmt" "log" "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" @@ -55,6 +56,8 @@ func ReadResourceMonitors(d *schema.ResourceData, meta interface{}) error { return nil } + d.SetId(fmt.Sprintf("%s.%s", account.Account, account.Region)) + currentResourceMonitors, err := snowflake.ListResourceMonitors(db) if err == sql.ErrNoRows { // If not found, mark resource to be removed from statefile during apply or refresh @@ -80,6 +83,5 @@ func ReadResourceMonitors(d *schema.ResourceData, meta interface{}) error { resourceMonitors = append(resourceMonitors, resourceMonitorMap) } - d.SetId(account.Account) return d.Set("resource_monitors", resourceMonitors) } diff --git a/pkg/datasources/storage_integrations.go b/pkg/datasources/storage_integrations.go index ddb465d80a..1abf0af20a 100644 --- a/pkg/datasources/storage_integrations.go +++ b/pkg/datasources/storage_integrations.go @@ -2,6 +2,7 @@ package datasources import ( "database/sql" + "fmt" "log" "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" @@ -55,6 +56,8 @@ func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error { return nil } + d.SetId(fmt.Sprintf("%s.%s", account.Account, account.Region)) + currentStorageIntegrations, err := snowflake.ListStorageIntegrations(db) if err == sql.ErrNoRows { // If not found, mark resource to be removed from statefile during apply or refresh @@ -80,6 +83,5 @@ func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error { storageIntegrations = append(storageIntegrations, storageIntegrationMap) } - d.SetId(account.Account) return d.Set("storage_integrations", storageIntegrations) } diff --git a/pkg/datasources/streams_acceptance_test.go b/pkg/datasources/streams_acceptance_test.go index 0d618ba63f..1ee227005b 100644 --- a/pkg/datasources/streams_acceptance_test.go +++ b/pkg/datasources/streams_acceptance_test.go @@ -44,10 +44,11 @@ func streams(databaseName string, schemaName string, tableName string, streamNam } resource snowflake_table "test_stream_on_table" { - database = snowflake_database.test_database.name - schema = snowflake_schema.test_schema.name - name = "%v" - comment = "Terraform acceptance test" + database = snowflake_database.test_database.name + schema = snowflake_schema.test_schema.name + change_tracking = true + name = "%v" + comment = "Terraform acceptance test" column { name = "column1" type = "VARIANT" diff --git a/pkg/datasources/warehouses.go b/pkg/datasources/warehouses.go index 1f6430010a..e7c2a612b2 100644 --- a/pkg/datasources/warehouses.go +++ b/pkg/datasources/warehouses.go @@ -2,6 +2,7 @@ package datasources import ( "database/sql" + "fmt" "log" "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" @@ -65,6 +66,8 @@ func ReadWarehouses(d *schema.ResourceData, meta interface{}) error { return nil } + d.SetId(fmt.Sprintf("%s.%s", account.Account, account.Region)) + currentWarehouses, err := snowflake.ListWarehouses(db) if err == sql.ErrNoRows { // If not found, mark resource to be removed from statefile during apply or refresh @@ -92,6 +95,5 @@ func ReadWarehouses(d *schema.ResourceData, meta interface{}) error { warehouses = append(warehouses, warehouseMap) } - d.SetId(account.Account) return d.Set("warehouses", warehouses) } From 94a88843230f9792304980e63f62987195d5931e Mon Sep 17 00:00:00 2001 From: Christopher Isidora Date: Wed, 28 Jul 2021 21:48:26 +0200 Subject: [PATCH 3/6] Added PreventPostDestroyRefresh to Tests --- pkg/datasources/resource_monitors_acceptance_test.go | 3 ++- pkg/datasources/storage_integrations_acceptance_test.go | 3 ++- pkg/datasources/warehouses_acceptance_test.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/datasources/resource_monitors_acceptance_test.go b/pkg/datasources/resource_monitors_acceptance_test.go index 85c293b07d..a61980ddc0 100644 --- a/pkg/datasources/resource_monitors_acceptance_test.go +++ b/pkg/datasources/resource_monitors_acceptance_test.go @@ -15,7 +15,8 @@ func TestAccResourceMonitors(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - Config: resourceMonitors(resourceMonitorName), + PreventPostDestroyRefresh: true, + Config: resourceMonitors(resourceMonitorName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.#"), resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.0.name"), diff --git a/pkg/datasources/storage_integrations_acceptance_test.go b/pkg/datasources/storage_integrations_acceptance_test.go index 882790e28f..d8508c913c 100644 --- a/pkg/datasources/storage_integrations_acceptance_test.go +++ b/pkg/datasources/storage_integrations_acceptance_test.go @@ -15,7 +15,8 @@ func TestAccStorageIntegrations(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - Config: storageIntegrations(storageIntegrationName), + PreventPostDestroyRefresh: true, + Config: storageIntegrations(storageIntegrationName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.#"), resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.0.name"), diff --git a/pkg/datasources/warehouses_acceptance_test.go b/pkg/datasources/warehouses_acceptance_test.go index 8ee1cfe3f8..51bfcfbc38 100644 --- a/pkg/datasources/warehouses_acceptance_test.go +++ b/pkg/datasources/warehouses_acceptance_test.go @@ -15,7 +15,8 @@ func TestAccWarehouses(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - Config: warehouses(warehouseName), + PreventPostDestroyRefresh: true, + Config: warehouses(warehouseName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.#"), resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.0.name"), From a2ebbbdac9b37f3f01caa51cfa1fe30bf40831f6 Mon Sep 17 00:00:00 2001 From: Christopher Isidora Date: Thu, 29 Jul 2021 10:40:24 +0200 Subject: [PATCH 4/6] Added workaround --- pkg/datasources/resource_monitors_acceptance_test.go | 5 +++-- pkg/datasources/storage_integrations_acceptance_test.go | 5 +++-- pkg/datasources/warehouses_acceptance_test.go | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/datasources/resource_monitors_acceptance_test.go b/pkg/datasources/resource_monitors_acceptance_test.go index a61980ddc0..751efbdc6d 100644 --- a/pkg/datasources/resource_monitors_acceptance_test.go +++ b/pkg/datasources/resource_monitors_acceptance_test.go @@ -15,12 +15,13 @@ func TestAccResourceMonitors(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - PreventPostDestroyRefresh: true, - Config: resourceMonitors(resourceMonitorName), + Config: resourceMonitors(resourceMonitorName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.#"), resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.0.name"), ), + //Workaround for https://github.com/hashicorp/terraform/issues/17034 + ExpectNonEmptyPlan: true, }, }, }) diff --git a/pkg/datasources/storage_integrations_acceptance_test.go b/pkg/datasources/storage_integrations_acceptance_test.go index d8508c913c..a299dc51b2 100644 --- a/pkg/datasources/storage_integrations_acceptance_test.go +++ b/pkg/datasources/storage_integrations_acceptance_test.go @@ -15,12 +15,13 @@ func TestAccStorageIntegrations(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - PreventPostDestroyRefresh: true, - Config: storageIntegrations(storageIntegrationName), + Config: storageIntegrations(storageIntegrationName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.#"), resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.0.name"), ), + //Workaround for https://github.com/hashicorp/terraform/issues/17034 + ExpectNonEmptyPlan: true, }, }, }) diff --git a/pkg/datasources/warehouses_acceptance_test.go b/pkg/datasources/warehouses_acceptance_test.go index 51bfcfbc38..90c9eec86f 100644 --- a/pkg/datasources/warehouses_acceptance_test.go +++ b/pkg/datasources/warehouses_acceptance_test.go @@ -15,12 +15,13 @@ func TestAccWarehouses(t *testing.T) { Providers: providers(), Steps: []resource.TestStep{ { - PreventPostDestroyRefresh: true, - Config: warehouses(warehouseName), + Config: warehouses(warehouseName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.#"), resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.0.name"), ), + //Workaround for https://github.com/hashicorp/terraform/issues/17034 + ExpectNonEmptyPlan: true, }, }, }) From c93a6a26988576addbbf794ed4bf2e072f9c68a5 Mon Sep 17 00:00:00 2001 From: Christopher Isidora Date: Thu, 29 Jul 2021 20:15:01 +0200 Subject: [PATCH 5/6] Removed workaround --- pkg/datasources/resource_monitors_acceptance_test.go | 2 -- pkg/datasources/storage_integrations_acceptance_test.go | 2 -- pkg/datasources/warehouses_acceptance_test.go | 2 -- 3 files changed, 6 deletions(-) diff --git a/pkg/datasources/resource_monitors_acceptance_test.go b/pkg/datasources/resource_monitors_acceptance_test.go index 751efbdc6d..85c293b07d 100644 --- a/pkg/datasources/resource_monitors_acceptance_test.go +++ b/pkg/datasources/resource_monitors_acceptance_test.go @@ -20,8 +20,6 @@ func TestAccResourceMonitors(t *testing.T) { resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.#"), resource.TestCheckResourceAttrSet("data.snowflake_resource_monitors.s", "resource_monitors.0.name"), ), - //Workaround for https://github.com/hashicorp/terraform/issues/17034 - ExpectNonEmptyPlan: true, }, }, }) diff --git a/pkg/datasources/storage_integrations_acceptance_test.go b/pkg/datasources/storage_integrations_acceptance_test.go index a299dc51b2..882790e28f 100644 --- a/pkg/datasources/storage_integrations_acceptance_test.go +++ b/pkg/datasources/storage_integrations_acceptance_test.go @@ -20,8 +20,6 @@ func TestAccStorageIntegrations(t *testing.T) { resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.#"), resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.0.name"), ), - //Workaround for https://github.com/hashicorp/terraform/issues/17034 - ExpectNonEmptyPlan: true, }, }, }) diff --git a/pkg/datasources/warehouses_acceptance_test.go b/pkg/datasources/warehouses_acceptance_test.go index 90c9eec86f..8ee1cfe3f8 100644 --- a/pkg/datasources/warehouses_acceptance_test.go +++ b/pkg/datasources/warehouses_acceptance_test.go @@ -20,8 +20,6 @@ func TestAccWarehouses(t *testing.T) { resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.#"), resource.TestCheckResourceAttrSet("data.snowflake_warehouses.s", "warehouses.0.name"), ), - //Workaround for https://github.com/hashicorp/terraform/issues/17034 - ExpectNonEmptyPlan: true, }, }, }) From 8f65a3a56d6debd3c702a9aed8f79fefbea04b3b Mon Sep 17 00:00:00 2001 From: Christopher Isidora Date: Fri, 30 Jul 2021 13:18:19 +0200 Subject: [PATCH 6/6] Added Depends On --- pkg/datasources/external_functions_acceptance_test.go | 1 + pkg/datasources/external_tables_acceptance_test.go | 1 + pkg/datasources/file_formats_acceptance_test.go | 1 + pkg/datasources/masking_policies_acceptance_test.go | 1 + pkg/datasources/materialized_views_acceptance_test.go | 1 + pkg/datasources/pipes_acceptance_test.go | 1 + pkg/datasources/schemas_acceptance_test.go | 1 + pkg/datasources/sequences_acceptance_test.go | 1 + pkg/datasources/stages_acceptance_test.go | 1 + pkg/datasources/streams_acceptance_test.go | 1 + .../system_generate_scim_access_token_acceptance_test.go | 1 + pkg/datasources/tables_acceptance_test.go | 1 + pkg/datasources/tasks_acceptance_test.go | 1 + pkg/datasources/views_acceptance_test.go | 1 + 14 files changed, 14 insertions(+) diff --git a/pkg/datasources/external_functions_acceptance_test.go b/pkg/datasources/external_functions_acceptance_test.go index 66e5cef36f..8b53ba1b20 100644 --- a/pkg/datasources/external_functions_acceptance_test.go +++ b/pkg/datasources/external_functions_acceptance_test.go @@ -73,6 +73,7 @@ func externalFunctions(databaseName string, schemaName string, apiName string, e data snowflake_external_functions "t" { database = snowflake_external_function.test_func.database schema = snowflake_external_function.test_func.schema + depends_on = [snowflake_external_function.test_func] } `, databaseName, schemaName, apiName, externalFunctionName) } diff --git a/pkg/datasources/external_tables_acceptance_test.go b/pkg/datasources/external_tables_acceptance_test.go index af7b5dbbb3..e74a37829a 100644 --- a/pkg/datasources/external_tables_acceptance_test.go +++ b/pkg/datasources/external_tables_acceptance_test.go @@ -73,6 +73,7 @@ func externalTables(databaseName string, schemaName string, stageName string, ex data snowflake_external_tables "t" { database = snowflake_external_table.test_table.database schema = snowflake_external_table.test_table.schema + depends_on = [snowflake_external_table.test_table] } `, databaseName, schemaName, stageName, externalTableName) } diff --git a/pkg/datasources/file_formats_acceptance_test.go b/pkg/datasources/file_formats_acceptance_test.go index d1bd4599eb..43ef8daeda 100644 --- a/pkg/datasources/file_formats_acceptance_test.go +++ b/pkg/datasources/file_formats_acceptance_test.go @@ -74,6 +74,7 @@ func fileFormats(databaseName string, schemaName string, fileFormatName string) data snowflake_file_formats "t" { database = snowflake_file_format.t.database schema = snowflake_file_format.t.schema + depends_on = [snowflake_file_format.t] } `, databaseName, schemaName, fileFormatName) } diff --git a/pkg/datasources/masking_policies_acceptance_test.go b/pkg/datasources/masking_policies_acceptance_test.go index d2cc9602ab..abe1dae82b 100644 --- a/pkg/datasources/masking_policies_acceptance_test.go +++ b/pkg/datasources/masking_policies_acceptance_test.go @@ -55,6 +55,7 @@ func masking_policies(databaseName string, schemaName string, maskingPolicyName data snowflake_masking_policies "t" { database = snowflake_masking_policy.test.database schema = snowflake_masking_policy.test.schema + depends_on = [snowflake_masking_policy.test] } `, databaseName, schemaName, maskingPolicyName) } diff --git a/pkg/datasources/materialized_views_acceptance_test.go b/pkg/datasources/materialized_views_acceptance_test.go index 1942291209..7b9f1c1286 100644 --- a/pkg/datasources/materialized_views_acceptance_test.go +++ b/pkg/datasources/materialized_views_acceptance_test.go @@ -72,6 +72,7 @@ func materializedViews(warehouseName string, databaseName string, schemaName str data snowflake_materialized_views "v" { database = snowflake_materialized_view.v.database schema = snowflake_materialized_view.v.schema + depends_on = [snowflake_materialized_view.v] } `, warehouseName, databaseName, schemaName, tableName, viewName) } diff --git a/pkg/datasources/pipes_acceptance_test.go b/pkg/datasources/pipes_acceptance_test.go index a9842a95a2..309e37b533 100644 --- a/pkg/datasources/pipes_acceptance_test.go +++ b/pkg/datasources/pipes_acceptance_test.go @@ -67,6 +67,7 @@ resource "snowflake_stage" "test" { data snowflake_pipes "t" { database = snowflake_pipe.test.database schema = snowflake_pipe.test.schema + depends_on = [snowflake_pipe.test] } resource "snowflake_pipe" "test" { diff --git a/pkg/datasources/schemas_acceptance_test.go b/pkg/datasources/schemas_acceptance_test.go index 3967ce3d28..44e4605801 100644 --- a/pkg/datasources/schemas_acceptance_test.go +++ b/pkg/datasources/schemas_acceptance_test.go @@ -41,6 +41,7 @@ func schemas(databaseName string, schemaName string) string { data snowflake_schemas "s" { database = snowflake_schema.s.database + depends_on = [snowflake_schema.s] } `, databaseName, schemaName) } diff --git a/pkg/datasources/sequences_acceptance_test.go b/pkg/datasources/sequences_acceptance_test.go index 9305cdddf3..429cb785a5 100644 --- a/pkg/datasources/sequences_acceptance_test.go +++ b/pkg/datasources/sequences_acceptance_test.go @@ -51,6 +51,7 @@ func sequences(databaseName string, schemaName string, sequenceName string) stri data snowflake_sequences "t" { database = snowflake_sequence.t.database schema = snowflake_sequence.t.schema + depends_on = [snowflake_sequence.t] } `, databaseName, schemaName, sequenceName) } diff --git a/pkg/datasources/stages_acceptance_test.go b/pkg/datasources/stages_acceptance_test.go index dfd7c78e8f..907411cd41 100644 --- a/pkg/datasources/stages_acceptance_test.go +++ b/pkg/datasources/stages_acceptance_test.go @@ -51,6 +51,7 @@ func stages(databaseName string, schemaName string, stageName string) string { data snowflake_stages "t" { database = snowflake_stage.t.database schema = snowflake_stage.t.schema + depends_on = [snowflake_stage.t] } `, databaseName, schemaName, stageName) } diff --git a/pkg/datasources/streams_acceptance_test.go b/pkg/datasources/streams_acceptance_test.go index 1ee227005b..fce8f14760 100644 --- a/pkg/datasources/streams_acceptance_test.go +++ b/pkg/datasources/streams_acceptance_test.go @@ -70,6 +70,7 @@ func streams(databaseName string, schemaName string, tableName string, streamNam data snowflake_streams "t" { database = snowflake_stream.test_stream.database schema = snowflake_stream.test_stream.schema + depends_on = [snowflake_stream.test_stream] } `, databaseName, schemaName, tableName, streamName) } diff --git a/pkg/datasources/system_generate_scim_access_token_acceptance_test.go b/pkg/datasources/system_generate_scim_access_token_acceptance_test.go index b11bce81f2..f127ceb4ca 100644 --- a/pkg/datasources/system_generate_scim_access_token_acceptance_test.go +++ b/pkg/datasources/system_generate_scim_access_token_acceptance_test.go @@ -58,6 +58,7 @@ func generateAccessTokenConfig(name string) string { data snowflake_system_generate_scim_access_token p { integration_name = snowflake_scim_integration.azured.name + depends_on = [snowflake_scim_integration.azured] } `, name) } diff --git a/pkg/datasources/tables_acceptance_test.go b/pkg/datasources/tables_acceptance_test.go index fd163a800c..f4a8c41f39 100644 --- a/pkg/datasources/tables_acceptance_test.go +++ b/pkg/datasources/tables_acceptance_test.go @@ -55,6 +55,7 @@ func tables(databaseName string, schemaName string, tableName string) string { data snowflake_tables "t" { database = snowflake_table.t.database schema = snowflake_table.t.schema + depends_on = [snowflake_table.t] } `, databaseName, schemaName, tableName) } diff --git a/pkg/datasources/tasks_acceptance_test.go b/pkg/datasources/tasks_acceptance_test.go index d4653996a5..d14c0bd491 100644 --- a/pkg/datasources/tasks_acceptance_test.go +++ b/pkg/datasources/tasks_acceptance_test.go @@ -62,6 +62,7 @@ func tasks(databaseName string, schemaName string, taskName string) string { data snowflake_tasks "t" { database = snowflake_task.test.database schema = snowflake_task.test.schema + depends_on = [snowflake_task.test] } `, databaseName, schemaName, taskName) } diff --git a/pkg/datasources/views_acceptance_test.go b/pkg/datasources/views_acceptance_test.go index 29c3a74768..5a91dd9239 100644 --- a/pkg/datasources/views_acceptance_test.go +++ b/pkg/datasources/views_acceptance_test.go @@ -52,6 +52,7 @@ func views(databaseName string, schemaName string, viewName string) string { data snowflake_views "v" { database = snowflake_view.v.database schema = snowflake_view.v.schema + depends_on = [snowflake_view.v] } `, databaseName, schemaName, viewName) }