From 8625741109b86024483de9d9220a0c5027181d5d Mon Sep 17 00:00:00 2001
From: Chema <chema.robles@civica-soft.com>
Date: Tue, 15 Feb 2022 14:30:51 +0100
Subject: [PATCH 1/5] FEAT Data source for list databases

---
 docs/data-sources/databases.md                |  52 +++++++++
 .../snowflake_databases/data-source.tf        |   8 ++
 pkg/datasources/databases.go                  | 107 ++++++++++++++++++
 pkg/datasources/databases_acceptance_test.go  |  49 ++++++++
 pkg/provider/provider.go                      |   1 +
 5 files changed, 217 insertions(+)
 create mode 100644 docs/data-sources/databases.md
 create mode 100644 examples/data-sources/snowflake_databases/data-source.tf
 create mode 100644 pkg/datasources/databases.go
 create mode 100644 pkg/datasources/databases_acceptance_test.go

diff --git a/docs/data-sources/databases.md b/docs/data-sources/databases.md
new file mode 100644
index 0000000000..b88b4c668e
--- /dev/null
+++ b/docs/data-sources/databases.md
@@ -0,0 +1,52 @@
+---
+# generated by https://github.com/hashicorp/terraform-plugin-docs
+page_title: "snowflake_databases Data Source - terraform-provider-snowflake"
+subcategory: ""
+description: |-
+  
+---
+
+# snowflake_databases (Data Source)
+
+
+
+## Example Usage
+
+```terraform
+data "snowflake_databases" "this" {}
+
+resource "snowflake_database" "backups" {
+    for_each = { for x in data.snowflake_databases.this.databases: x.name => x }
+
+    name  = "BACKUP_${each.key}"
+    comment = "Backup of ${each.key} - ${each.value.comment}"
+}
+```
+
+<!-- schema generated by tfplugindocs -->
+## Schema
+
+### Optional
+
+- **id** (String) The ID of this resource.
+
+### Read-Only
+
+- **databases** (List of Object) Snowflake databases (see [below for nested schema](#nestedatt--databases))
+
+<a id="nestedatt--databases"></a>
+### Nested Schema for `databases`
+
+Read-Only:
+
+- **comment** (String)
+- **created_on** (String)
+- **is_current** (Boolean)
+- **is_default** (Boolean)
+- **name** (String)
+- **options** (String)
+- **origin** (String)
+- **owner** (String)
+- **retention_time** (Number)
+
+
diff --git a/examples/data-sources/snowflake_databases/data-source.tf b/examples/data-sources/snowflake_databases/data-source.tf
new file mode 100644
index 0000000000..01e3d1f9ac
--- /dev/null
+++ b/examples/data-sources/snowflake_databases/data-source.tf
@@ -0,0 +1,8 @@
+data "snowflake_databases" "this" {}
+
+resource "snowflake_database" "backups" {
+    for_each = { for x in data.snowflake_databases.this.databases: x.name => x }
+
+    name  = "BACKUP_${each.key}"
+    comment = "Backup of ${each.key} - ${each.value.comment}"
+}
diff --git a/pkg/datasources/databases.go b/pkg/datasources/databases.go
new file mode 100644
index 0000000000..0f26240902
--- /dev/null
+++ b/pkg/datasources/databases.go
@@ -0,0 +1,107 @@
+package datasources
+
+import (
+	"database/sql"
+	"log"
+	"strconv"
+
+	"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake"
+	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+	"github.com/jmoiron/sqlx"
+)
+
+var databasesSchema = map[string]*schema.Schema{
+	"databases": {
+		Type:        schema.TypeList,
+		Computed:    true,
+		Description: "Snowflake databases",
+		Elem: &schema.Resource{
+			Schema: map[string]*schema.Schema{
+				"name": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+				"comment": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+				"owner": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+				"is_default": {
+					Type:     schema.TypeBool,
+					Computed: true,
+				},
+				"is_current": {
+					Type:     schema.TypeBool,
+					Computed: true,
+				},
+				"origin": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+				"retention_time": {
+					Type:     schema.TypeInt,
+					Computed: true,
+				},
+				"created_on": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+				"options": {
+					Type:     schema.TypeString,
+					Computed: true,
+				},
+			},
+		},
+	},
+}
+
+// Databases the Snowflake current account resource
+func Databases() *schema.Resource {
+	return &schema.Resource{
+		Read:   ReadDatabases,
+		Schema: databasesSchema,
+	}
+}
+
+// ReadDatabases read the current snowflake account information
+func ReadDatabases(d *schema.ResourceData, meta interface{}) error {
+	db := meta.(*sql.DB)
+	dbx := sqlx.NewDb(db, "snowflake")
+	dbs, err := snowflake.ListDatabases(dbx)
+	if err != nil {
+		log.Printf("[DEBUG] list databases failed to decode")
+		d.SetId("")
+		return nil
+	}
+	log.Printf("[DEBUG] list databases: %v", dbs)
+	d.SetId("databases_read")
+	databases := []map[string]interface{}{}
+	for _, db := range dbs {
+		dbR := map[string]interface{}{}
+		if !db.DBName.Valid {
+			continue
+		}
+		dbR["name"] = db.DBName.String
+		dbR["comment"] = db.Comment.String
+		dbR["owner"] = db.Owner.String
+		dbR["is_default"] = db.IsDefault.String == "Y"
+		dbR["is_current"] = db.IsCurrent.String == "Y"
+		dbR["origin"] = db.Origin.String
+		dbR["created_on"] = db.CreatedOn.String
+		dbR["options"] = db.Options.String
+		dbR["retention_time"] = -1
+		if db.RetentionTime.Valid {
+			v, err := strconv.Atoi(db.RetentionTime.String)
+			if err == nil {
+				dbR["retention_time"] = v
+			}
+		}
+		databases = append(databases, dbR)
+
+	}
+	d.Set("databases", databases)
+	return nil
+}
diff --git a/pkg/datasources/databases_acceptance_test.go b/pkg/datasources/databases_acceptance_test.go
new file mode 100644
index 0000000000..ff0b1be633
--- /dev/null
+++ b/pkg/datasources/databases_acceptance_test.go
@@ -0,0 +1,49 @@
+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 TestDatabases(t *testing.T) {
+	databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
+	comment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
+	resource.ParallelTest(t, resource.TestCase{
+
+		Providers: providers(),
+		Steps: []resource.TestStep{
+			{
+				Config: databases(databaseName, comment),
+				Check: resource.ComposeTestCheckFunc(
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.#"),
+					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.#", "1"),
+					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.0.name", databaseName),
+					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.0.comment", comment),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.created_on"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.owner"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.retention_time"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.options"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.origin"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.is_current"),
+					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.is_default"),
+				),
+			},
+		},
+	})
+}
+
+func databases(databaseName, comment string) string {
+	return fmt.Sprintf(`
+		resource snowflake_database "test_database" {
+			name = "%v"
+			comment = "%v"
+		}
+		data snowflake_databases "t" {
+			depends_on = [snowflake_database.test_database]
+		}
+	`, databaseName, comment)
+}
diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go
index 9887691c69..aa6dea17d2 100644
--- a/pkg/provider/provider.go
+++ b/pkg/provider/provider.go
@@ -235,6 +235,7 @@ func getDataSources() map[string]*schema.Resource {
 		"snowflake_row_access_policies":                datasources.RowAccessPolicies(),
 		"snowflake_functions":                          datasources.Functions(),
 		"snowflake_procedures":                         datasources.Procedures(),
+		"snowflake_databases":                          datasources.Databases(),
 	}
 
 	return dataSources

From abe9acae880ee06b608da1cc891dc3b80a40e2c1 Mon Sep 17 00:00:00 2001
From: Chema <chema.robles@civica-soft.com>
Date: Wed, 16 Feb 2022 08:58:11 +0100
Subject: [PATCH 2/5] FEAT add single database data source

---
 docs/data-sources/database.md                 | 48 ++++++++++
 .../snowflake_database/data-source.tf         |  8 ++
 pkg/datasources/database.go                   | 94 +++++++++++++++++++
 pkg/datasources/database_acceptance_test.go   | 48 ++++++++++
 pkg/provider/provider.go                      |  1 +
 pkg/snowflake/database.go                     | 24 +++++
 6 files changed, 223 insertions(+)
 create mode 100644 docs/data-sources/database.md
 create mode 100644 examples/data-sources/snowflake_database/data-source.tf
 create mode 100644 pkg/datasources/database.go
 create mode 100644 pkg/datasources/database_acceptance_test.go

diff --git a/docs/data-sources/database.md b/docs/data-sources/database.md
new file mode 100644
index 0000000000..238d27b946
--- /dev/null
+++ b/docs/data-sources/database.md
@@ -0,0 +1,48 @@
+---
+# generated by https://github.com/hashicorp/terraform-plugin-docs
+page_title: "snowflake_database Data Source - terraform-provider-snowflake"
+subcategory: ""
+description: |-
+  
+---
+
+# snowflake_database (Data Source)
+
+
+
+## Example Usage
+
+```terraform
+data "snowflake_database" "this" {
+    name = "DEMO_DB"
+}
+
+resource "snowflake_database" "backup" {
+    name  = "BACKUP_${data.snowflake_database.this.name}"
+    comment = "Backup of ${data.snowflake_database.this.name} - ${data.snowflake_database.this.comment}"
+}
+```
+
+<!-- schema generated by tfplugindocs -->
+## Schema
+
+### Required
+
+- **name** (String) The database from which to return its metadata.
+
+### Optional
+
+- **id** (String) The ID of this resource.
+
+### Read-Only
+
+- **comment** (String)
+- **created_on** (String)
+- **is_current** (Boolean)
+- **is_default** (Boolean)
+- **options** (String)
+- **origin** (String)
+- **owner** (String)
+- **retention_time** (Number)
+
+
diff --git a/examples/data-sources/snowflake_database/data-source.tf b/examples/data-sources/snowflake_database/data-source.tf
new file mode 100644
index 0000000000..6329155789
--- /dev/null
+++ b/examples/data-sources/snowflake_database/data-source.tf
@@ -0,0 +1,8 @@
+data "snowflake_database" "this" {
+    name = "DEMO_DB"
+}
+
+resource "snowflake_database" "backup" {
+    name  = "BACKUP_${data.snowflake_database.this.name}"
+    comment = "Backup of ${data.snowflake_database.this.name} - ${data.snowflake_database.this.comment}"
+}
diff --git a/pkg/datasources/database.go b/pkg/datasources/database.go
new file mode 100644
index 0000000000..039ef6f053
--- /dev/null
+++ b/pkg/datasources/database.go
@@ -0,0 +1,94 @@
+package datasources
+
+import (
+	"database/sql"
+	"log"
+	"strconv"
+
+	"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake"
+	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+	"github.com/jmoiron/sqlx"
+)
+
+var databaseSchema = map[string]*schema.Schema{
+	"name": {
+		Type:        schema.TypeString,
+		Required:    true,
+		Description: "The database from which to return its metadata.",
+	},
+	"comment": {
+		Type:     schema.TypeString,
+		Computed: true,
+	},
+	"owner": {
+		Type:     schema.TypeString,
+		Computed: true,
+	},
+	"is_default": {
+		Type:     schema.TypeBool,
+		Computed: true,
+	},
+	"is_current": {
+		Type:     schema.TypeBool,
+		Computed: true,
+	},
+	"origin": {
+		Type:     schema.TypeString,
+		Computed: true,
+	},
+	"retention_time": {
+		Type:     schema.TypeInt,
+		Computed: true,
+	},
+	"created_on": {
+		Type:     schema.TypeString,
+		Computed: true,
+	},
+	"options": {
+		Type:     schema.TypeString,
+		Computed: true,
+	},
+}
+
+// Database the Snowflake Database resource
+func Database() *schema.Resource {
+	return &schema.Resource{
+		Read:   ReadDatabase,
+		Schema: databaseSchema,
+	}
+}
+
+// ReadDatabase read the database meta-data information
+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.Printf("[DEBUG] list database failed to decode")
+		d.SetId("")
+		return nil
+	}
+	if dbData == nil || !dbData.DBName.Valid {
+		log.Printf("[DEBUG] database not found")
+		d.SetId("")
+		return nil
+	}
+	log.Printf("[DEBUG] list database: %v", dbData)
+	d.SetId(dbData.DBName.String)
+	d.Set("comment", dbData.Comment.String)
+	d.Set("owner", dbData.Owner.String)
+	d.Set("is_default", dbData.IsDefault.String == "Y")
+	d.Set("is_current", dbData.IsCurrent.String == "Y")
+	d.Set("origin", dbData.Origin.String)
+	d.Set("created_on", dbData.CreatedOn.String)
+	d.Set("options", dbData.Options.String)
+	d.Set("retention_time", -1)
+	if dbData.RetentionTime.Valid {
+		v, err := strconv.Atoi(dbData.RetentionTime.String)
+		if err == nil {
+			d.Set("retention_time", v)
+		}
+	}
+	return nil
+}
diff --git a/pkg/datasources/database_acceptance_test.go b/pkg/datasources/database_acceptance_test.go
new file mode 100644
index 0000000000..75598665b4
--- /dev/null
+++ b/pkg/datasources/database_acceptance_test.go
@@ -0,0 +1,48 @@
+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 TestDatabase(t *testing.T) {
+	databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
+	comment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
+	resource.ParallelTest(t, resource.TestCase{
+
+		Providers: providers(),
+		Steps: []resource.TestStep{
+			{
+				Config: database(databaseName, comment),
+				Check: resource.ComposeTestCheckFunc(
+					resource.TestCheckResourceAttr("data.snowflake_database.t", "name", databaseName),
+					resource.TestCheckResourceAttr("data.snowflake_database.t", "comment", comment),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "created_on"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "owner"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "retention_time"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "options"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "origin"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "is_current"),
+					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "is_default"),
+				),
+			},
+		},
+	})
+}
+
+func database(databaseName, comment string) string {
+	return fmt.Sprintf(`
+		resource snowflake_database "test_database" {
+			name = "%v"
+			comment = "%v"
+		}
+		data snowflake_database "t" {
+			depends_on = [snowflake_database.test_database]
+			name = "%v"
+		}
+	`, databaseName, comment, databaseName)
+}
diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go
index aa6dea17d2..7308724486 100644
--- a/pkg/provider/provider.go
+++ b/pkg/provider/provider.go
@@ -236,6 +236,7 @@ func getDataSources() map[string]*schema.Resource {
 		"snowflake_functions":                          datasources.Functions(),
 		"snowflake_procedures":                         datasources.Procedures(),
 		"snowflake_databases":                          datasources.Databases(),
+		"snowflake_database":                           datasources.Database(),
 	}
 
 	return dataSources
diff --git a/pkg/snowflake/database.go b/pkg/snowflake/database.go
index b234097979..3ef4f82437 100644
--- a/pkg/snowflake/database.go
+++ b/pkg/snowflake/database.go
@@ -110,3 +110,27 @@ func ListDatabases(sdb *sqlx.DB) ([]database, error) {
 	}
 	return dbs, errors.Wrapf(err, "unable to scan row for %s", stmt)
 }
+
+func ListDatabase(sdb *sqlx.DB, databaseName string) (*database, error) {
+	stmt := fmt.Sprintf("SHOW DATABASES LIKE '%s'", databaseName)
+	rows, err := sdb.Queryx(stmt)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+
+	dbs := []database{}
+	err = sqlx.StructScan(rows, &dbs)
+	if err == sql.ErrNoRows || len(dbs) == 0 {
+		log.Printf("[DEBUG] no databases found")
+		return nil, nil
+	}
+	db := &database{}
+	for _, d := range dbs {
+		if d.DBName.String == databaseName {
+			db = &d
+			break
+		}
+	}
+	return db, errors.Wrapf(err, "unable to scan row for %s", stmt)
+}

From 6f34c158c5dc0b7b1ed24429dceb11302bf95128 Mon Sep 17 00:00:00 2001
From: Chema <chema.robles@civica-soft.com>
Date: Thu, 17 Feb 2022 09:39:27 +0100
Subject: [PATCH 3/5] FIX: remove unnecessary resource

---
 examples/data-sources/snowflake_database/data-source.tf  | 5 -----
 examples/data-sources/snowflake_databases/data-source.tf | 7 -------
 2 files changed, 12 deletions(-)

diff --git a/examples/data-sources/snowflake_database/data-source.tf b/examples/data-sources/snowflake_database/data-source.tf
index 6329155789..ff46367580 100644
--- a/examples/data-sources/snowflake_database/data-source.tf
+++ b/examples/data-sources/snowflake_database/data-source.tf
@@ -1,8 +1,3 @@
 data "snowflake_database" "this" {
     name = "DEMO_DB"
 }
-
-resource "snowflake_database" "backup" {
-    name  = "BACKUP_${data.snowflake_database.this.name}"
-    comment = "Backup of ${data.snowflake_database.this.name} - ${data.snowflake_database.this.comment}"
-}
diff --git a/examples/data-sources/snowflake_databases/data-source.tf b/examples/data-sources/snowflake_databases/data-source.tf
index 01e3d1f9ac..289c9d3563 100644
--- a/examples/data-sources/snowflake_databases/data-source.tf
+++ b/examples/data-sources/snowflake_databases/data-source.tf
@@ -1,8 +1 @@
 data "snowflake_databases" "this" {}
-
-resource "snowflake_database" "backups" {
-    for_each = { for x in data.snowflake_databases.this.databases: x.name => x }
-
-    name  = "BACKUP_${each.key}"
-    comment = "Backup of ${each.key} - ${each.value.comment}"
-}

From be945108ddb68d69e449d8a6f473a59d228fe76f Mon Sep 17 00:00:00 2001
From: Chema <chema.robles@civica-soft.com>
Date: Thu, 17 Feb 2022 10:15:55 +0100
Subject: [PATCH 4/5] fix: acceptance test

---
 pkg/datasources/database_acceptance_test.go  |  2 -
 pkg/datasources/databases_acceptance_test.go | 73 +++++++++++++++++---
 2 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/pkg/datasources/database_acceptance_test.go b/pkg/datasources/database_acceptance_test.go
index 75598665b4..910e8b0805 100644
--- a/pkg/datasources/database_acceptance_test.go
+++ b/pkg/datasources/database_acceptance_test.go
@@ -24,8 +24,6 @@ func TestDatabase(t *testing.T) {
 					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "created_on"),
 					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "owner"),
 					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "retention_time"),
-					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "options"),
-					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "origin"),
 					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "is_current"),
 					resource.TestCheckResourceAttrSet("data.snowflake_database.t", "is_default"),
 				),
diff --git a/pkg/datasources/databases_acceptance_test.go b/pkg/datasources/databases_acceptance_test.go
index ff0b1be633..86a360cbc2 100644
--- a/pkg/datasources/databases_acceptance_test.go
+++ b/pkg/datasources/databases_acceptance_test.go
@@ -2,11 +2,13 @@ package datasources_test
 
 import (
 	"fmt"
+	"strconv"
 	"strings"
 	"testing"
 
 	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
 	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
+	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
 )
 
 func TestDatabases(t *testing.T) {
@@ -19,17 +21,7 @@ func TestDatabases(t *testing.T) {
 			{
 				Config: databases(databaseName, comment),
 				Check: resource.ComposeTestCheckFunc(
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.#"),
-					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.#", "1"),
-					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.0.name", databaseName),
-					resource.TestCheckResourceAttr("data.snowflake_databases.t", "databases.0.comment", comment),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.created_on"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.owner"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.retention_time"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.options"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.origin"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.is_current"),
-					resource.TestCheckResourceAttrSet("data.snowflake_databases.t", "databases.0.is_default"),
+					testAccCheckDatabases(databaseName, comment),
 				),
 			},
 		},
@@ -47,3 +39,62 @@ func databases(databaseName, comment string) string {
 		}
 	`, databaseName, comment)
 }
+
+func testAccCheckDatabases(databaseName string, comment string) resource.TestCheckFunc {
+	return func(s *terraform.State) error {
+		resourceState := s.Modules[0].Resources["data.snowflake_databases.t"]
+		if resourceState == nil {
+			return fmt.Errorf("resource not found in state")
+		}
+		instanceState := resourceState.Primary
+		if instanceState == nil {
+			return fmt.Errorf("resource has no primary instance")
+		}
+		if instanceState.ID != "databases_read" {
+			return fmt.Errorf("expected ID to be 'databases_read', got %s", instanceState.ID)
+		}
+		nDbs, err := strconv.Atoi(instanceState.Attributes["databases.#"])
+		if err != nil {
+			return fmt.Errorf("expected a number for field 'databases', got %s", instanceState.Attributes["databases.#"])
+		}
+		if nDbs == 0 {
+			return fmt.Errorf("expected databases to be greater or equal to 1, got %s", instanceState.Attributes["databases.#"])
+		}
+		dbIdx := -1
+		for i := 0; i < nDbs; i++ {
+			idxName := fmt.Sprintf("databases.%d.name", i)
+			if instanceState.Attributes[idxName] == databaseName {
+				dbIdx = i
+				break
+			}
+		}
+		if dbIdx == -1 {
+			return fmt.Errorf("database %s not found", databaseName)
+		}
+		idxComment := fmt.Sprintf("databases.%d.comment", dbIdx)
+		if instanceState.Attributes[idxComment] != comment {
+			return fmt.Errorf("expected comment '%s', got '%s'", comment, instanceState.Attributes[idxComment])
+		}
+		idxCreatedOn := fmt.Sprintf("databases.%d.created_on", dbIdx)
+		if instanceState.Attributes[idxCreatedOn] == "" {
+			return fmt.Errorf("expected 'created_on' to be set")
+		}
+		idxOwner := fmt.Sprintf("databases.%d.owner", dbIdx)
+		if instanceState.Attributes[idxOwner] == "" {
+			return fmt.Errorf("expected 'owner' to be set")
+		}
+		idxRetentionTime := fmt.Sprintf("databases.%d.retention_time", dbIdx)
+		if instanceState.Attributes[idxRetentionTime] == "" {
+			return fmt.Errorf("expected 'retention_time' to be set")
+		}
+		idxIsCurrent := fmt.Sprintf("databases.%d.is_current", dbIdx)
+		if instanceState.Attributes[idxIsCurrent] == "" {
+			return fmt.Errorf("expected 'is_current' to be set")
+		}
+		idxIsDefault := fmt.Sprintf("databases.%d.is_default", dbIdx)
+		if instanceState.Attributes[idxIsDefault] == "" {
+			return fmt.Errorf("expected 'is_default' to be set")
+		}
+		return nil
+	}
+}

From 7145a89cb25912e0713d2974b0a15bd91f8ae5c1 Mon Sep 17 00:00:00 2001
From: Chema <chema.robles@civica-soft.com>
Date: Fri, 18 Feb 2022 07:02:41 +0100
Subject: [PATCH 5/5] fix: re-run make docs

---
 docs/data-sources/database.md  | 5 -----
 docs/data-sources/databases.md | 7 -------
 2 files changed, 12 deletions(-)

diff --git a/docs/data-sources/database.md b/docs/data-sources/database.md
index 238d27b946..8d4e198cda 100644
--- a/docs/data-sources/database.md
+++ b/docs/data-sources/database.md
@@ -16,11 +16,6 @@ description: |-
 data "snowflake_database" "this" {
     name = "DEMO_DB"
 }
-
-resource "snowflake_database" "backup" {
-    name  = "BACKUP_${data.snowflake_database.this.name}"
-    comment = "Backup of ${data.snowflake_database.this.name} - ${data.snowflake_database.this.comment}"
-}
 ```
 
 <!-- schema generated by tfplugindocs -->
diff --git a/docs/data-sources/databases.md b/docs/data-sources/databases.md
index b88b4c668e..8ab04cc578 100644
--- a/docs/data-sources/databases.md
+++ b/docs/data-sources/databases.md
@@ -14,13 +14,6 @@ description: |-
 
 ```terraform
 data "snowflake_databases" "this" {}
-
-resource "snowflake_database" "backups" {
-    for_each = { for x in data.snowflake_databases.this.databases: x.name => x }
-
-    name  = "BACKUP_${each.key}"
-    comment = "Backup of ${each.key} - ${each.value.comment}"
-}
 ```
 
 <!-- schema generated by tfplugindocs -->