From 1c5a7735bf8d0aa016bce0f8eaa3c5cd0e06f323 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 08:42:08 -0700 Subject: [PATCH 1/6] wildcards in database name --- .goreleaser.yml | 5 ----- pkg/datasources/database.go | 1 - pkg/resources/database.go | 20 ++++++-------------- pkg/resources/database_acceptance_test.go | 22 ++++++++++++++++++++++ pkg/snowflake/database.go | 2 ++ 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 5bcdac698f..a08cc17933 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -2,7 +2,6 @@ builds: - env: - CGO_ENABLED=0 goos: - - openbsd - solaris - windows - linux @@ -17,10 +16,6 @@ builds: ignore: - goos: darwin goarch: '386' - - goos: openbsd - goarch: arm - - goos: openbsd - goarch: arm64 binary: '{{ .ProjectName }}_v{{ .Version }}' archives: diff --git a/pkg/datasources/database.go b/pkg/datasources/database.go index 8ce0f05002..9a2915fd49 100644 --- a/pkg/datasources/database.go +++ b/pkg/datasources/database.go @@ -62,7 +62,6 @@ func Database() *schema.Resource { func ReadDatabase(d *schema.ResourceData, meta interface{}) error { db := meta.(*sql.DB) dbx := sqlx.NewDb(db, "snowflake") - log.Printf("[DEBUG] database: %v", d.Get("name")) dbData, err := snowflake.ListDatabase(dbx, d.Get("name").(string)) if err != nil { log.Println("[DEBUG] list database failed to decode") diff --git a/pkg/resources/database.go b/pkg/resources/database.go index ac27cd8f83..cec7cd9a4f 100644 --- a/pkg/resources/database.go +++ b/pkg/resources/database.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/jmoiron/sqlx" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake" ) @@ -219,22 +220,13 @@ func createDatabaseFromReplica(d *schema.ResourceData, meta interface{}) error { func ReadDatabase(d *schema.ResourceData, meta interface{}) error { db := meta.(*sql.DB) - name := d.Id() - - stmt := snowflake.NewDatabaseBuilder(name).Show() - row := snowflake.QueryRow(db, stmt) - - database, err := snowflake.ScanDatabase(row) + dbx := sqlx.NewDb(db, "snowflake") + database, err := snowflake.ListDatabase(dbx, d.Get("name").(string)) if err != nil { - if errors.Is(err, sql.ErrNoRows) { - // If not found, mark resource to be removed from state file during apply or refresh - log.Printf("[DEBUG] database (%s) not found", d.Id()) - d.SetId("") - return nil - } - return fmt.Errorf("unable to scan row for SHOW DATABASES") + log.Println("[DEBUG] list database failed to decode") + d.SetId("") + return nil } - if err := d.Set("name", database.DBName.String); err != nil { return err } diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index ea5351b876..89819fea50 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -10,6 +10,27 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) +func TestAcc_DatabaseWithUnderscore(t *testing.T) { + if _, ok := os.LookupEnv("SKIP_DATABASE_TESTS"); ok { + t.Skip("Skipping TestAccDatabase") + } + + prefix := "_" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + Config: dbConfig(prefix), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.db", "name", prefix), + resource.TestCheckResourceAttr("snowflake_database.db", "comment", "test comment"), + resource.TestCheckResourceAttrSet("snowflake_database.db", "data_retention_time_in_days"), + ), + }, + }, + }) +} func TestAcc_Database(t *testing.T) { if _, ok := os.LookupEnv("SKIP_DATABASE_TESTS"); ok { t.Skip("Skipping TestAccDatabase") @@ -78,3 +99,4 @@ resource "snowflake_database" "db" { ` return fmt.Sprintf(s, prefix) } + diff --git a/pkg/snowflake/database.go b/pkg/snowflake/database.go index a68c68d547..f78b9c03ae 100644 --- a/pkg/snowflake/database.go +++ b/pkg/snowflake/database.go @@ -307,7 +307,9 @@ func ListDatabase(sdb *sqlx.DB, databaseName string) (*Database, error) { db := &Database{} for _, d := range dbs { d := d + log.Printf("[DEBUG] database: %v", d.DBName.String) if d.DBName.String == databaseName { + log.Printf("[DEBUG] match database: %v with string %s", d.DBName.String,databaseName) db = &d break } From 3f88485bed0776e1e8df8aaf3719f2ad6b00b986 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 08:44:37 -0700 Subject: [PATCH 2/6] wildcards in database name --- pkg/snowflake/database.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/snowflake/database.go b/pkg/snowflake/database.go index f78b9c03ae..a68c68d547 100644 --- a/pkg/snowflake/database.go +++ b/pkg/snowflake/database.go @@ -307,9 +307,7 @@ func ListDatabase(sdb *sqlx.DB, databaseName string) (*Database, error) { db := &Database{} for _, d := range dbs { d := d - log.Printf("[DEBUG] database: %v", d.DBName.String) if d.DBName.String == databaseName { - log.Printf("[DEBUG] match database: %v with string %s", d.DBName.String,databaseName) db = &d break } From 67a12b36069653b9acedf65c0ce5d8a0a81fa9f8 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 08:44:44 -0700 Subject: [PATCH 3/6] wildcards in database name --- pkg/resources/database_acceptance_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 89819fea50..65b3555a07 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -99,4 +99,3 @@ resource "snowflake_database" "db" { ` return fmt.Sprintf(s, prefix) } - From da15a34c9440215342931eabe1d0a82e8753fda5 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 09:46:20 -0700 Subject: [PATCH 4/6] remove support for solaris --- .goreleaser.prerelease.yml | 1 - .goreleaser.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.goreleaser.prerelease.yml b/.goreleaser.prerelease.yml index 59b9d9a348..39e968894b 100644 --- a/.goreleaser.prerelease.yml +++ b/.goreleaser.prerelease.yml @@ -2,7 +2,6 @@ builds: - env: - CGO_ENABLED=0 goos: - - solaris - windows - linux - darwin diff --git a/.goreleaser.yml b/.goreleaser.yml index a08cc17933..d03a7f0aec 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -2,7 +2,6 @@ builds: - env: - CGO_ENABLED=0 goos: - - solaris - windows - linux - darwin From e959b3ad869ca33e0a589f55ebb282304497b406 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 09:47:57 -0700 Subject: [PATCH 5/6] remove support for solaris --- pkg/resources/database_acceptance_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 65b3555a07..6098ac25ca 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -31,6 +31,7 @@ func TestAcc_DatabaseWithUnderscore(t *testing.T) { }, }) } + func TestAcc_Database(t *testing.T) { if _, ok := os.LookupEnv("SKIP_DATABASE_TESTS"); ok { t.Skip("Skipping TestAccDatabase") From f0f265e4153dff6a593240d90fdd56a15fff4596 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 28 Mar 2023 10:05:15 -0700 Subject: [PATCH 6/6] fix importer func --- pkg/resources/database.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/resources/database.go b/pkg/resources/database.go index cec7cd9a4f..a8b6bb829a 100644 --- a/pkg/resources/database.go +++ b/pkg/resources/database.go @@ -1,6 +1,7 @@ package resources import ( + "context" "database/sql" "errors" "fmt" @@ -94,7 +95,12 @@ func Database() *schema.Resource { Schema: databaseSchema, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + if err := d.Set("name", d.Id()); err != nil { + return nil, err + } + return []*schema.ResourceData{d}, nil + }, }, } }