Skip to content

Commit

Permalink
FEAT Data source for list databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Chema committed Feb 15, 2022
1 parent 63f2b85 commit 8625741
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/data-sources/databases.md
Original file line number Diff line number Diff line change
@@ -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)


8 changes: 8 additions & 0 deletions examples/data-sources/snowflake_databases/data-source.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
107 changes: 107 additions & 0 deletions pkg/datasources/databases.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions pkg/datasources/databases_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8625741

Please sign in to comment.