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: add allowed values #1006

Merged
merged 23 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
413162b
Add allowed_values field to the snowflake_tag resource
sfc-gh-kumaurya May 17, 2022
5dd57f0
Add allowed_values field to the snowflake_tag resource
sfc-gh-kumaurya May 17, 2022
4a6f354
Fixing doc and adding example for snowflake_tag resource
sfc-gh-kumaurya May 17, 2022
45aec4e
Fixing doc and adding example for snowflake_tag resource
sfc-gh-kumaurya May 17, 2022
8bd4e0e
testing allowed_values
sfc-gh-kumaurya May 17, 2022
ba38357
testing allowed_values
sfc-gh-kumaurya May 17, 2022
f6c9c10
testing allowed_values
sfc-gh-kumaurya May 17, 2022
60722d5
testing allowed_values
sfc-gh-kumaurya May 17, 2022
f1ea611
testing allowed_values
sfc-gh-kumaurya May 17, 2022
8f6cf9c
testing allowed_values
sfc-gh-kumaurya May 17, 2022
3bf5861
adding example usage for snowflake_tag resource
sfc-gh-kumaurya May 17, 2022
12d7e57
minor fixes
sfc-gh-kumaurya May 19, 2022
19665c1
Moving ListToSnowflakeString functions to helpers directory
sfc-gh-kumaurya May 19, 2022
9d33bfe
Moving ListToSnowflakeString functions to helpers directory
sfc-gh-kumaurya May 19, 2022
909288f
pull changes from snowflakelabs/terraform-provider-snowflake
sfc-gh-kumaurya May 24, 2022
c6d0d23
Merge branch 'Snowflake-Labs-main' into add_allowed_values
sfc-gh-kumaurya May 24, 2022
9302e1a
Updating with latest changes
sfc-gh-kumaurya May 25, 2022
8d892b0
Merge branch 'Snowflake-Labs-main' into add_allowed_values
sfc-gh-kumaurya May 25, 2022
239a98f
Updating import
sfc-gh-kumaurya May 25, 2022
fd8770c
Updating import for tag
sfc-gh-kumaurya May 25, 2022
92c16cc
Updating import
sfc-gh-kumaurya May 25, 2022
bc1b0d9
Updating acceptance tests of table annd tag for allowed_values
sfc-gh-kumaurya May 26, 2022
1ffdd57
Merge branch 'main' into add_allowed_values
sfc-gh-swinkler May 26, 2022
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
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'
```
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
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
59 changes: 59 additions & 0 deletions pkg/resources/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ var tagSchema = map[string]*schema.Schema{
Optional: true,
Description: "Specifies a comment for the tag.",
},
"allowed_values": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "List of allowed values for the tag.",
},
}

var tagReferenceSchema = &schema.Schema{
Expand Down Expand Up @@ -186,6 +192,10 @@ func CreateTag(d *schema.ResourceData, meta interface{}) error {
builder.WithComment(v.(string))
}

if v, ok := d.GetOk("allowed_values"); ok {
builder.WithAllowedValues(expandStringList(v.([]interface{})))
}

q := builder.Create()

err := snowflake.Exec(db, q)
Expand Down Expand Up @@ -253,6 +263,17 @@ func ReadTag(d *schema.ResourceData, meta interface{}) error {
return err
}

av := strings.ReplaceAll(t.AllowedValues.String, "\"", "")
Copy link

@sfc-gh-afedorov sfc-gh-afedorov May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this still work if someone wants a tag value like foo[bar]? instead of re-writing a partial parser, can we re-use an existing helper function?

av = strings.TrimPrefix(av, "[")
av = strings.TrimSuffix(av, "]")

split := strings.Split(av, ",")

err = d.Set("allowed_values", split)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -284,9 +305,47 @@ func UpdateTag(d *schema.ResourceData, meta interface{}) error {
}
}

// If there is change in allowed_values field
if d.HasChange("allowed_values") {
if _, ok := d.GetOk("allowed_values"); ok {
_, v := d.GetChange("allowed_values")

ns := expandAllowedValues(v)

q := builder.RemoveAllowedValues()
err := snowflake.Exec(db, q)
if err != nil {
return errors.Wrapf(err, "error removing ALLOWED_VALUES for tag %v", tag)
}

addQuery := builder.AddAllowedValues(ns)
err = snowflake.Exec(db, addQuery)
if err != nil {
return errors.Wrapf(err, "error adding ALLOWED_VALUES for tag %v", tag)
}
} else {
sfc-gh-kumaurya marked this conversation as resolved.
Show resolved Hide resolved
q := builder.RemoveAllowedValues()
err := snowflake.Exec(db, q)
if err != nil {
return errors.Wrapf(err, "error removing ALLOWED_VALUES for tag %v", tag)
}
}
}

return ReadTag(d, meta)
}

// Returns the slice of strings for inputed allowed values
func expandAllowedValues(avChangeSet interface{}) []string {
avList := avChangeSet.([]interface{})
newAvs := make([]string, len(avList))
for idx, value := range avList {
newAvs[idx] = fmt.Sprintf("%v", value)
}

return newAvs
}

// DeleteTag implements schema.DeleteFunc
func DeleteTag(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
Expand Down
2 changes: 2 additions & 0 deletions pkg/resources/tag_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAcc_Tag(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_tag.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_tag.test", "database", accName),
resource.TestCheckResourceAttr("snowflake_tag.test", "schema", accName),
resource.TestCheckResourceAttr("snowflake_tag.test", "allowed_values.#", "2"),
resource.TestCheckResourceAttr("snowflake_tag.test", "comment", "Terraform acceptance test"),
),
},
Expand All @@ -46,6 +47,7 @@ resource "snowflake_tag" "test" {
name = "%[1]v"
database = snowflake_database.test.name
schema = snowflake_schema.test.name
allowed_values = ["alv1", "alv2"]
comment = "Terraform acceptance test"
}

Expand Down
30 changes: 16 additions & 14 deletions pkg/resources/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ func TestTagCreate(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
"comment": "great comment",
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
"comment": "great comment",
"allowed_values": []interface{}{"marketing", "finance"},
}
d := schema.TestResourceDataRaw(t, resources.Tag().Schema, in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(
`^CREATE TAG "test_db"."test_schema"."good_name" COMMENT = 'great comment'$`,
`^CREATE TAG "test_db"."test_schema"."good_name" ALLOWED_VALUES 'marketing', 'finance' COMMENT = 'great comment'$`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test case like CREATE OR REPLACE TAG test."foo[bar]" ALLOWED_VALUES '1', '2["3"]';

).WillReturnResult(sqlmock.NewResult(1, 1))

expectReadTag(mock)
Expand All @@ -46,19 +47,20 @@ func TestTagUpdate(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
"comment": "great comment",
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
"comment": "great comment",
"allowed_values": []interface{}{"marketing", "finance"},
}

d := tag(t, "test_db|test_schema|good_name", in)
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(
`^ALTER TAG "test_db"."test_schema"."good_name" SET COMMENT = 'great comment'$`,
).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^ALTER TAG "test_db"."test_schema"."good_name" SET COMMENT = 'great comment'$`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^ALTER TAG "test_db"."test_schema"."good_name" UNSET ALLOWED_VALUES$`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^ALTER TAG "test_db"."test_schema"."good_name" ADD ALLOWED_VALUES 'marketing', 'finance'$`).WillReturnResult(sqlmock.NewResult(1, 1))

expectReadTag(mock)
err := resources.UpdateTag(d, db)
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestTagRead(t *testing.T) {

func expectReadTag(mock sqlmock.Sqlmock) {
rows := sqlmock.NewRows([]string{
"created_on", "name", "database_name", "schema_name", "owner", "comment"},
).AddRow("2019-05-19 16:55:36.530 -0700", "good_name", "test_db", "test_schema", "admin", "great comment")
"created_on", "name", "database_name", "schema_name", "owner", "comment", "allowed_values"},
).AddRow("2019-05-19 16:55:36.530 -0700", "good_name", "test_db", "test_schema", "admin", "great comment", "'al1','al2'")
mock.ExpectQuery(`^SHOW TAGS LIKE 'good_name' IN SCHEMA "test_db"."test_schema"$`).WillReturnRows(rows)
}
17 changes: 4 additions & 13 deletions pkg/snowflake/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package snowflake
import (
"database/sql"
"fmt"
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/jmoiron/sqlx"
)

Expand All @@ -24,13 +24,13 @@ func (npb *NetworkPolicyBuilder) WithComment(c string) *NetworkPolicyBuilder {

// WithAllowedIpList adds an allowedIpList to the NetworkPolicyBuilder
func (npb *NetworkPolicyBuilder) WithAllowedIpList(allowedIps []string) *NetworkPolicyBuilder {
npb.allowedIpList = IpListToString(allowedIps)
npb.allowedIpList = helpers.IpListToSnowflakeString(allowedIps)
return npb
}

// WithBlockedIpList adds a blockedIpList to the NetworkPolicyBuilder
func (npb *NetworkPolicyBuilder) WithBlockedIpList(blockedIps []string) *NetworkPolicyBuilder {
npb.blockedIpList = IpListToString(blockedIps)
npb.blockedIpList = helpers.IpListToSnowflakeString(blockedIps)
return npb
}

Expand Down Expand Up @@ -78,7 +78,7 @@ func (npb *NetworkPolicyBuilder) RemoveComment() string {

// ChangeIpList returns the SQL query that will update the ip list (of the specified listType) on the network policy.
func (npb *NetworkPolicyBuilder) ChangeIpList(listType string, ips []string) string {
return fmt.Sprintf(`ALTER NETWORK POLICY "%v" SET %v_IP_LIST = %v`, npb.name, listType, IpListToString(ips))
return fmt.Sprintf(`ALTER NETWORK POLICY "%v" SET %v_IP_LIST = %v`, npb.name, listType, helpers.IpListToSnowflakeString(ips))
}

// Drop returns the SQL query that will drop a network policy.
Expand Down Expand Up @@ -122,15 +122,6 @@ func (npb *NetworkPolicyBuilder) ShowOnAccount() string {
return `SHOW PARAMETERS LIKE 'network_policy' IN ACCOUNT`
}

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

return fmt.Sprintf("(%v)", strings.Join(ips, ", "))
}

type NetworkPolicyStruct struct {
CreatedOn sql.NullString `db:"created_on"`
Name sql.NullString `db:"name"`
Expand Down
Loading