-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Functions (UDF) Resource & Datasource (#647)
- Loading branch information
1 parent
633f2bb
commit f28c7dc
Showing
13 changed files
with
1,282 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_functions" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
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 functionsSchema = 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 functions from.", | ||
}, | ||
"functions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "The 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, | ||
}, | ||
"argument_types": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"return_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func Functions() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: ReadFunctions, | ||
Schema: functionsSchema, | ||
} | ||
} | ||
|
||
func ReadFunctions(d *schema.ResourceData, meta interface{}) error { | ||
db := meta.(*sql.DB) | ||
databaseName := d.Get("database").(string) | ||
schemaName := d.Get("schema").(string) | ||
|
||
currentFunctions, err := snowflake.ListFunctions(databaseName, schemaName, db) | ||
if err == sql.ErrNoRows { | ||
// If not found, mark resource to be removed from statefile during apply or refresh | ||
log.Printf("[DEBUG] functions in schema (%s) not found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} else if err != nil { | ||
log.Printf("[DEBUG] unable to parse functions in schema (%s)", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
functions := []map[string]interface{}{} | ||
|
||
for _, function := range currentFunctions { | ||
functionMap := map[string]interface{}{} | ||
|
||
functionSignatureMap, err := parseArguments(function.Arguments.String) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
functionMap["name"] = function.Name.String | ||
functionMap["database"] = function.DatabaseName.String | ||
functionMap["schema"] = function.SchemaName.String | ||
functionMap["comment"] = function.Comment.String | ||
functionMap["argument_types"] = functionSignatureMap["argumentTypes"].([]string) | ||
functionMap["return_type"] = functionSignatureMap["returnType"].(string) | ||
|
||
functions = append(functions, functionMap) | ||
} | ||
|
||
d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) | ||
return d.Set("functions", functions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 TestAccFunctions(t *testing.T) { | ||
databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
functionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
functionWithArgumentsName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: providers(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: functions(databaseName, schemaName, functionName, functionWithArgumentsName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.snowflake_functions.t", "database", databaseName), | ||
resource.TestCheckResourceAttr("data.snowflake_functions.t", "schema", schemaName), | ||
resource.TestCheckResourceAttrSet("data.snowflake_functions.t", "functions.#"), | ||
resource.TestCheckResourceAttr("data.snowflake_functions.t", "functions.#", "2"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func functions(databaseName string, schemaName string, functionName string, functionWithArgumentsName string) string { | ||
s := ` | ||
resource "snowflake_database" "test_database" { | ||
name = "%v" | ||
comment = "Terraform acceptance test" | ||
} | ||
resource "snowflake_schema" "test_schema" { | ||
name = "%v" | ||
database = snowflake_database.test_database.name | ||
comment = "Terraform acceptance test" | ||
} | ||
resource "snowflake_function" "test_funct_simple" { | ||
name = "%s" | ||
database = snowflake_database.test_database.name | ||
schema = snowflake_schema.test_schema.name | ||
return_type = "float" | ||
statement = "3.141592654::FLOAT" | ||
} | ||
resource "snowflake_function" "test_funct" { | ||
name = "%s" | ||
database = snowflake_database.test_database.name | ||
schema = snowflake_schema.test_schema.name | ||
arguments { | ||
name = "arg1" | ||
type = "varchar" | ||
} | ||
comment = "Terraform acceptance test" | ||
return_type = "varchar" | ||
language = "javascript" | ||
statement = "var X=3\nreturn X" | ||
} | ||
data snowflake_functions "t" { | ||
database = snowflake_database.test_database.name | ||
schema = snowflake_schema.test_schema.name | ||
depends_on = [snowflake_function.test_funct_simple, snowflake_function.test_funct] | ||
} | ||
` | ||
return fmt.Sprintf(s, databaseName, schemaName, functionName, functionWithArgumentsName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.