Skip to content

Commit

Permalink
feat: enable copy_grants flag in view resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ngaberel committed Mar 28, 2023
1 parent fe4e4db commit 21dce40
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
14 changes: 14 additions & 0 deletions pkg/resources/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ var viewSchema = map[string]*schema.Schema{
Default: false,
Description: "Overwrites the View if it exists.",
},
"copyGrants": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Retains the access permissions from the original view when a new view is created using the OR REPLACE clause.",
ForceNew: true,
},
"is_secure": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -163,6 +170,10 @@ func CreateView(d *schema.ResourceData, meta interface{}) error {
builder.WithSecure()
}

if v, ok := d.GetOk("copyGrants"); ok && v.(bool) {
builder.WithCopyGrants()
}

if v, ok := d.GetOk("comment"); ok {
builder.WithComment(v.(string))
}
Expand Down Expand Up @@ -224,6 +235,9 @@ func ReadView(d *schema.ResourceData, meta interface{}) error {
if err = d.Set("is_secure", v.IsSecure); err != nil {
return err
}
if err = d.Set("copyGrants", v.CopyGrants()); err != nil {
return err
}
if err = d.Set("comment", v.Comment.String); err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/resources/view_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAcc_View(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_view.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_view.test", "database", accName),
resource.TestCheckResourceAttr("snowflake_view.test", "comment", "Terraform test resource"),
resource.TestCheckResourceAttr("snowflake_view.test", "copyGrants", "true"),
checkBool("snowflake_view.test", "is_secure", true), // this is from user_acceptance_test.go
),
},
Expand All @@ -42,6 +43,7 @@ func TestAcc_View2(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_view.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_view.test", "database", accName),
resource.TestCheckResourceAttr("snowflake_view.test", "comment", "Terraform test resource"),
resource.TestCheckResourceAttr("snowflake_view.test", "copyGrants", "true"),
checkBool("snowflake_view.test", "is_secure", true), // this is from user_acceptance_test.go
),
},
Expand All @@ -56,13 +58,14 @@ resource "snowflake_database" "test" {
}
resource "snowflake_view" "test" {
name = "%v"
comment = "Terraform test resource"
database = snowflake_database.test.name
schema = "PUBLIC"
is_secure = true
name = "%v"
comment = "Terraform test resource"
database = snowflake_database.test.name
schema = "PUBLIC"
is_secure = true
or_replace = false
statement = "%s"
copyGrants = true
statement = "%s"
}
`, n, n, q)
}
50 changes: 50 additions & 0 deletions pkg/resources/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ func TestViewCreateOrReplace(t *testing.T) {
})
}

func TestViewCreateWithCopyGrants(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
"copyGrants": true,
"statement": "SELECT * FROM test_db.PUBLIC.GREAT_TABLE WHERE account_id = 'bobs-account-id'",
}
d := schema.TestResourceDataRaw(t, resources.View().Schema, in)
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(
`^CREATE VIEW "test_db"."test_schema"."good_name" COPY GRANTS AS SELECT \* FROM test_db.PUBLIC.GREAT_TABLE WHERE account_id = 'bobs-account-id'$`,
).WillReturnResult(sqlmock.NewResult(1, 1))

expectReadView(mock)
err := resources.CreateView(d, db)
r.NoError(err)
})
}

func TestViewCreateAmpersand(t *testing.T) {
r := require.New(t)

Expand Down Expand Up @@ -152,3 +176,29 @@ func TestViewRead(t *testing.T) {
r.Nil(err)
})
}

func TestViewReadWithCopyGrants(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "good_name",
"database": "test_db",
"schema": "test_schema",
}

d := view(t, "test_db|test_schema|good_name", in)

testhelpers.WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
// Test when resource is not found, checking if state will be empty
r.NotEmpty(d.State())
q := snowflake.NewViewBuilder("good_name").WithDB("test_db").WithSchema("test_schema").Show()
rows := sqlmock.NewRows([]string{
"created_on", "name", "reserved", "database_name", "schema_name", "owner", "comment", "text", "is_secure", "is_materialized",
},
).AddRow("2019-05-19 16:55:36.530 -0700", "good_name", "", "test_db", "test_schema", "admin", "great comment", "CREATE OR REPLACE VIEW test_db.test_schema.good_name COPY GRANTS AS SELECT * FROM test_db.GREAT_SCHEMA.GREAT_TABLE WHERE account_id = 'bobs-account-id'", true, false)
mock.ExpectQuery(q).WillReturnRows(rows)
err := resources.ReadView(d, db)
r.Equal("true", d.State().Attributes["copyGrants"])
r.Nil(err)
})
}

0 comments on commit 21dce40

Please sign in to comment.