Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Feb 15, 2024
1 parent dfa52b2 commit e5e3e09
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
23 changes: 12 additions & 11 deletions pkg/resources/grant_database_role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ func TestAcc_GrantDatabaseRole_accountRole(t *testing.T) {
})
}

/*
todo: once snowflake_grant_privileges_to_share is implemented. Cannot test this without having 'GRANT USAGE ON DATABASE <NAME> TO SHARE <share_name>',
// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2410 is fixed
func TestAcc_GrantDatabaseRole_share(t *testing.T) {
databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
databaseRoleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
databaseRoleId := sdk.NewDatabaseObjectIdentifier(databaseName, databaseRoleName).FullyQualifiedName()
shareName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_grant_database_role.g"
m := func() map[string]config.Variable {
return map[string]config.Variable{
"database": config.StringVariable(acc.TestDatabaseName),
shareId := sdk.NewAccountObjectIdentifier(shareName).FullyQualifiedName()
resourceName := "snowflake_grant_database_role.test"
configVariables := func() config.Variables {
return config.Variables{
"database": config.StringVariable(databaseName),
"database_role_name": config.StringVariable(databaseRoleName),
"share_name": config.StringVariable(shareName),
}
Expand All @@ -120,25 +122,24 @@ func TestAcc_GrantDatabaseRole_share(t *testing.T) {
Steps: []resource.TestStep{
{
ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/share"),
ConfigVariables: m(),
ConfigVariables: configVariables(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "database_role_name", databaseRoleName),
resource.TestCheckResourceAttr(resourceName, "database_role_name", databaseRoleId),
resource.TestCheckResourceAttr(resourceName, "share_name", shareName),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf(`%v|%v|%v`, databaseRoleName, "SHARE", shareName)),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf(`%v|%v|%v`, databaseRoleId, "SHARE", shareId)),
),
},
// test import
{
ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/share"),
ConfigVariables: m(),
ConfigVariables: configVariables(),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
*/

func testAccCheckGrantDatabaseRoleDestroy(s *terraform.State) error {
db := acc.TestAccProvider.Meta().(*sql.DB)
Expand Down
22 changes: 15 additions & 7 deletions pkg/resources/testdata/TestAcc_GrantDatabaseRole/share/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ variable "database" {
type = string
}

resource "snowflake_database_role" "database_role" {
database = var.database
resource "snowflake_database" "test" {
name = var.database
}

resource "snowflake_database_role" "test" {
database = snowflake_database.test.name
name = var.database_role_name
}

resource "snowflake_share" "share" {
resource "snowflake_share" "test" {
name = var.share_name
}

// todo: add grant_privileges_to_share resource
resource "snowflake_grant_privileges_to_share" "test" {
privileges = ["USAGE"]
on_database = snowflake_database.test.name
to_share = snowflake_share.test.name
}

resource "snowflake_grant_database_role" "g" {
database_role_name = snowflake_database_role.database_role.name
share_name = snowflake_share.name
resource "snowflake_grant_database_role" "test" {
database_role_name = "\"${snowflake_database.test.name}\".\"${snowflake_database_role.test.name}\""
share_name = snowflake_share.test.name
}
9 changes: 7 additions & 2 deletions pkg/sdk/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,13 @@ func (row grantRow) convert() *Grant {
var granteeName AccountObjectIdentifier
if grantedTo == ObjectTypeShare {
parts := strings.Split(row.GranteeName, ".")
name := strings.Join(parts[1:], ".")
granteeName = NewAccountObjectIdentifier(name)
switch {
case len(parts) == 1:
granteeName = NewAccountObjectIdentifier(parts[0])
case len(parts) > 1:
name := strings.Join(parts[1:], ".")
granteeName = NewAccountObjectIdentifier(name)
}
} else {
granteeName = NewAccountObjectIdentifier(row.GranteeName)
}
Expand Down

0 comments on commit e5e3e09

Please sign in to comment.