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

fix: SHOW GRANTS mapping for share data type #2508

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
}
12 changes: 10 additions & 2 deletions pkg/sdk/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sdk

import (
"context"
"log"
"strings"
"time"
)
Expand Down Expand Up @@ -222,9 +223,16 @@ func (row grantRow) convert() *Grant {
grantTo := ObjectType(strings.ReplaceAll(row.GrantTo, "_", " "))
var granteeName AccountObjectIdentifier
if grantedTo == ObjectTypeShare {
// TODO(SNOW-1058419): Change this logic during identifiers rework
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) == 2:
granteeName = NewAccountObjectIdentifier(parts[1])
default:
log.Printf("unsupported case for share's grantee name: %s", row.GranteeName)
}
} else {
granteeName = NewAccountObjectIdentifier(row.GranteeName)
}
Expand Down
Loading