Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Data source for list databases #861

Merged
merged 5 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/data-sources/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# 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"
}
```

<!-- 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)


45 changes: 45 additions & 0 deletions docs/data-sources/databases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# 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" {}
```

<!-- 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)


3 changes: 3 additions & 0 deletions examples/data-sources/snowflake_database/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "snowflake_database" "this" {
name = "DEMO_DB"
}
1 change: 1 addition & 0 deletions examples/data-sources/snowflake_databases/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "snowflake_databases" "this" {}
94 changes: 94 additions & 0 deletions pkg/datasources/database.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions pkg/datasources/database_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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", "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)
}
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
}
Loading