Skip to content

Commit

Permalink
Adjust view resource
Browse files Browse the repository at this point in the history
References: #2519
  • Loading branch information
sfc-gh-asawicki committed Feb 23, 2024
1 parent 04dde9c commit c22bc7c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/resources/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SQL
### Optional

- `comment` (String) Specifies a comment for the view.
- `copy_grants` (Boolean) Retains the access permissions from the original view when a new view is created using the OR REPLACE clause.
- `copy_grants` (Boolean) Retains the access permissions from the original view when a new view is created using the OR REPLACE clause. OR REPLACE must be set when COPY GRANTS is set.
- `is_secure` (Boolean) Specifies that the view is secure.
- `or_replace` (Boolean) Overwrites the View if it exists.
- `tag` (Block List, Deprecated) Definitions of a tag to associate with the resource. (see [below for nested schema](#nestedblock--tag))
Expand Down
3 changes: 2 additions & 1 deletion pkg/resources/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ var viewSchema = map[string]*schema.Schema{
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.",
Description: "Retains the access permissions from the original view when a new view is created using the OR REPLACE clause. OR REPLACE must be set when COPY GRANTS is set.",
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
return oldValue != "" && oldValue != newValue
},
RequiredWith: []string{"or_replace"},
},
"is_secure": {
Type: schema.TypeBool,
Expand Down
70 changes: 70 additions & 0 deletions pkg/resources/view_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -378,6 +379,38 @@ func TestAcc_ViewStatementUpdate(t *testing.T) {
})
}

func TestAcc_View_copyGrants(t *testing.T) {
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
query := "SELECT ROLE_NAME, ROLE_OWNER FROM INFORMATION_SCHEMA.APPLICABLE_ROLES"

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckViewDestroy,
Steps: []resource.TestStep{
{
Config: viewConfigWithCopyGrants(acc.TestDatabaseName, acc.TestSchemaName, accName, query, true),
ExpectError: regexp.MustCompile("all of `copy_grants,or_replace` must be specified"),
},
{
Config: viewConfigWithCopyGrantsAndOrReplace(acc.TestDatabaseName, acc.TestSchemaName, accName, query, true, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_view.test", "name", accName),
),
},
{
Config: viewConfigWithOrReplace(acc.TestDatabaseName, acc.TestSchemaName, accName, query, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_view.test", "name", accName),
),
},
},
})
}

func viewConfigWithGrants(databaseName string, schemaName string, selectStatement string) string {
return fmt.Sprintf(`
resource "snowflake_table" "table" {
Expand Down Expand Up @@ -430,6 +463,43 @@ data "snowflake_grants" "grants" {
databaseName, schemaName)
}

func viewConfigWithCopyGrants(databaseName string, schemaName string, name string, selectStatement string, copyGrants bool) string {
return fmt.Sprintf(`
resource "snowflake_view" "test" {
name = "%[3]s"
database = "%[1]s"
schema = "%[2]s"
statement = "%[4]s"
copy_grants = %[5]t
}
`, databaseName, schemaName, name, selectStatement, copyGrants)
}

func viewConfigWithCopyGrantsAndOrReplace(databaseName string, schemaName string, name string, selectStatement string, copyGrants bool, orReplace bool) string {
return fmt.Sprintf(`
resource "snowflake_view" "test" {
name = "%[3]s"
database = "%[1]s"
schema = "%[2]s"
statement = "%[4]s"
copy_grants = %[5]t
or_replace = %[6]t
}
`, databaseName, schemaName, name, selectStatement, copyGrants, orReplace)
}

func viewConfigWithOrReplace(databaseName string, schemaName string, name string, selectStatement string, orReplace bool) string {
return fmt.Sprintf(`
resource "snowflake_view" "test" {
name = "%[3]s"
database = "%[1]s"
schema = "%[2]s"
statement = "%[4]s"
or_replace = %[5]t
}
`, databaseName, schemaName, name, selectStatement, orReplace)
}

func testAccCheckViewDestroy(s *terraform.State) error {
db := acc.TestAccProvider.Meta().(*sql.DB)
client := sdk.NewClientFromDB(db)
Expand Down

0 comments on commit c22bc7c

Please sign in to comment.