Skip to content

Commit

Permalink
Merge branch 'main' into feature/add-external-tables-to-streams
Browse files Browse the repository at this point in the history
  • Loading branch information
alldoami authored May 26, 2022
2 parents fc91cab + 3161827 commit ce964ea
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 1,682 deletions.
18 changes: 8 additions & 10 deletions docs/resources/function_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ resource snowflake_function_grant grant {
schema_name = "schema"
function_name = "function"
arguments = [
{
"name": "a",
"type": "array"
},
{
"name": "b",
"type": "string"
}
]
arguments {
name = "a"
type = "array"
}
arguments {
name = "b"
type = "string"
}
return_type = "string"
privilege = "USAGE"
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "snowflake_procedure" "proc" {
name = "SAMPLEPROC"
database = snowflake_database.db.name
schema = snowflake_schema.schema.name
language = "JAVASCRIPT"
arguments {
name = "arg1"
type = "varchar"
Expand Down Expand Up @@ -64,6 +65,7 @@ EOT
- `arguments` (Block List) List of the arguments for the procedure (see [below for nested schema](#nestedblock--arguments))
- `comment` (String) Specifies a comment for the procedure.
- `execute_as` (String) Sets execute context - see caller's rights and owner's rights
- `language` (String) Specifies the language of the stored procedure code.
- `null_input_behavior` (String) Specifies the behavior of the procedure when called with null inputs.
- `return_behavior` (String) Specifies the behavior of the function when returning results

Expand Down
18 changes: 8 additions & 10 deletions docs/resources/procedure_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ resource snowflake_procedure_grant grant {
schema_name = "schema"
procedure_name = "procedure"
arguments = [
{
"name": "a",
"type": "array"
},
{
"name": "b",
"type": "string"
}
]
arguments {
name = "a"
type = "array"
}
arguments {
name = "b"
type = "string"
}
return_type = "string"
privilege = "select"
Expand Down
21 changes: 21 additions & 0 deletions docs/resources/tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ description: |-



## Example Usage

```terraform
resource "snowflake_tag" "test_tag" {
// Required
name = "tag_name"
database = "test_db"
schema = "test_schema"
// Optionals
comment = "test comment"
allowed_values = ["foo", "bar"]
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand All @@ -23,10 +36,18 @@ description: |-

### Optional

- `allowed_values` (List of String) List of allowed values for the tag.
- `comment` (String) Specifies a comment for the tag.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# format is database name | schema name | tag name
terraform import snowflake_tag.example 'dbName|schemaName|tagName'
```
18 changes: 8 additions & 10 deletions examples/resources/snowflake_function_grant/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ resource snowflake_function_grant grant {
schema_name = "schema"
function_name = "function"

arguments = [
{
"name": "a",
"type": "array"
},
{
"name": "b",
"type": "string"
}
]
arguments {
name = "a"
type = "array"
}
arguments {
name = "b"
type = "string"
}
return_type = "string"

privilege = "USAGE"
Expand Down
1 change: 1 addition & 0 deletions examples/resources/snowflake_procedure/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ resource "snowflake_procedure" "proc" {
name = "SAMPLEPROC"
database = snowflake_database.db.name
schema = snowflake_schema.schema.name
language = "JAVASCRIPT"
arguments {
name = "arg1"
type = "varchar"
Expand Down
18 changes: 8 additions & 10 deletions examples/resources/snowflake_procedure_grant/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ resource snowflake_procedure_grant grant {
schema_name = "schema"
procedure_name = "procedure"

arguments = [
{
"name": "a",
"type": "array"
},
{
"name": "b",
"type": "string"
}
]
arguments {
name = "a"
type = "array"
}
arguments {
name = "b"
type = "string"
}
return_type = "string"

privilege = "select"
Expand Down
2 changes: 2 additions & 0 deletions examples/resources/snowflake_tag/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# format is database name | schema name | tag name
terraform import snowflake_tag.example 'dbName|schemaName|tagName'
10 changes: 10 additions & 0 deletions examples/resources/snowflake_tag/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "snowflake_tag" "test_tag" {
// Required
name = "tag_name"
database = "test_db"
schema = "test_schema"

// Optionals
comment = "test comment"
allowed_values = ["foo", "bar"]
}
1,599 changes: 0 additions & 1,599 deletions go.sum

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions pkg/helpers/list_to_string_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package helpers

import (
"fmt"
"strings"
)

// ToDo: We can merge these two functions together and also add more functions here with similar functionality

// This function converts list of string into snowflake formated string like 'ele1', 'ele2'
func ListToSnowflakeString(list []string) string {
for index, element := range list {
list[index] = fmt.Sprintf(`'%v'`, strings.ReplaceAll(element, "'", "\\'"))
}

str := fmt.Sprintf(strings.Join(list, ", "))
return str
}

// IpListToString formats a list of IPs into a Snowflake-DDL friendly string, e.g. ('192.168.1.0', '192.168.1.100')
func IpListToSnowflakeString(ips []string) string {
for index, element := range ips {
ips[index] = fmt.Sprintf(`'%v'`, element)
}

return fmt.Sprintf("(%v)", strings.Join(ips, ", "))
}
3 changes: 2 additions & 1 deletion pkg/resources/network_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestIpListToString(t *testing.T) {
r := require.New(t)

in := []string{"192.168.0.100/24", "29.254.123.20"}
out := snowflake.IpListToString(in)
out := helpers.IpListToSnowflakeString(in)

r.Equal("('192.168.0.100/24', '29.254.123.20')", out)
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/resources/procedure.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/pkg/errors"
)

var procedureLanguages = []string{"JAVASCRIPT", "JAVA", "SCALA", "SQL"}

var procedureSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -70,6 +72,13 @@ var procedureSchema = map[string]*schema.Schema{
ForceNew: true,
DiffSuppressFunc: DiffSuppressStatement,
},
"language": {
Type: schema.TypeString,
Optional: true,
Default: "SQL",
ValidateFunc: validation.StringInSlice(procedureLanguages, false),
Description: "Specifies the language of the stored procedure code.",
},
"execute_as": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -159,6 +168,11 @@ func CreateProcedure(d *schema.ResourceData, meta interface{}) error {
builder.WithExecuteAs(v.(string))
}

// Set optionals, default is SQL
if v, ok := d.GetOk("language"); ok {
builder.WithLanguage(v.(string))
}

if v, ok := d.GetOk("comment"); ok {
builder.WithComment(v.(string))
}
Expand Down Expand Up @@ -259,7 +273,11 @@ func ReadProcedure(d *schema.ResourceData, meta interface{}) error {
return err
}
case "language":
// To ignore
if snowflake.Contains(languages, desc.Value.String) {
if err = d.Set("language", desc.Value.String); err != nil {
return err
}
}
default:
log.Printf("[WARN] unexpected procedure property %v returned from Snowflake", desc.Property.String)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/resources/procedure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func prepDummyProcedureResource(t *testing.T) *schema.ResourceData {
"schema": "my_schema",
"arguments": []interface{}{argument1, argument2},
"return_type": "varchar",
"language": "SCALA",
"comment": "mock comment",
"return_behavior": "IMMUTABLE",
"statement": procedureBody, //var message = DATA + DATA;return message
Expand All @@ -43,7 +44,7 @@ func TestProcedureCreate(t *testing.T) {
d := prepDummyProcedureResource(t)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`CREATE OR REPLACE PROCEDURE "my_db"."my_schema"."my_proc"\(data VARCHAR, event_dt DATE\) RETURNS VARCHAR LANGUAGE javascript CALLED ON NULL INPUT IMMUTABLE COMMENT = 'mock comment' EXECUTE AS OWNER AS \$\$hi\$\$`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`CREATE OR REPLACE PROCEDURE "my_db"."my_schema"."my_proc"\(data VARCHAR, event_dt DATE\) RETURNS VARCHAR LANGUAGE SCALA CALLED ON NULL INPUT IMMUTABLE COMMENT = 'mock comment' EXECUTE AS OWNER AS \$\$hi\$\$`).WillReturnResult(sqlmock.NewResult(1, 1))
expectProcedureRead(mock)
err := resources.CreateProcedure(d, db)
r.NoError(err)
Expand All @@ -61,7 +62,7 @@ func expectProcedureRead(mock sqlmock.Sqlmock) {
describeRows := sqlmock.NewRows([]string{"property", "value"}).
AddRow("signature", "(data VARCHAR, event_dt DATE)").
AddRow("returns", "VARCHAR(123456789)"). // This is how return type is stored in Snowflake DB
AddRow("language", "JAVASCRIPT").
AddRow("language", "SQL").
AddRow("null handling", "CALLED ON NULL INPUT").
AddRow("volatility", "IMMUTABLE").
AddRow("execute as", "CALLER").
Expand All @@ -85,6 +86,7 @@ func TestProcedureRead(t *testing.T) {
r.Equal("MY_SCHEMA", d.Get("schema").(string))
r.Equal("mock comment", d.Get("comment").(string))
r.Equal("VARCHAR", d.Get("return_type").(string))
r.Equal("SQL", d.Get("language").(string))
r.Equal("IMMUTABLE", d.Get("return_behavior").(string))
r.Equal(procedureBody, d.Get("statement").(string))

Expand Down
2 changes: 2 additions & 0 deletions pkg/resources/table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,13 +1017,15 @@ resource "snowflake_tag" "test_tag" {
name = "%[2]s"
database = snowflake_database.test_database.name
schema = snowflake_schema.test_schema.name
allowed_values = ["alv1", "alv2"]
comment = "Terraform acceptance test"
}
resource "snowflake_tag" "test2_tag" {
name = "%[3]s"
database = snowflake_database.test_database.name
schema = snowflake_schema.test_schema.name
allowed_values = ["alv1", "alv2"]
comment = "Terraform acceptance test"
}
Expand Down
Loading

0 comments on commit ce964ea

Please sign in to comment.