Skip to content

Commit

Permalink
feat: Added Several Datasources Part 2 (Snowflake-Labs#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIsidora authored and daniepett committed Feb 9, 2022
1 parent b2fdd33 commit c0593a6
Show file tree
Hide file tree
Showing 49 changed files with 2,161 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_external_functions" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_external_tables" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_file_formats/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_file_formats" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_masking_policies" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_pipes/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_pipes" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "snowflake_resource_monitors" "current" {
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_sequences/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_sequences" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_stages/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_stages" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "snowflake_storage_integrations" "current" {
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_streams/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_streams" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_tasks/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_tasks" "current" {
database = "MYDB"
schema = "MYSCHEMA"
}
2 changes: 2 additions & 0 deletions examples/data-sources/snowflake_warehouses/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "snowflake_warehouses" "current" {
}
96 changes: 96 additions & 0 deletions pkg/datasources/external_functions.go
Original file line number Diff line number Diff line change
@@ -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)
}
79 changes: 79 additions & 0 deletions pkg/datasources/external_functions_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -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 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
depends_on = [snowflake_external_function.test_func]
}
`, databaseName, schemaName, apiName, externalFunctionName)
}
90 changes: 90 additions & 0 deletions pkg/datasources/external_tables.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit c0593a6

Please sign in to comment.