Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Mar 29, 2024
1 parent 2fa50e7 commit 595988c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/resources/database_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func checkAccountAndDatabaseDataRetentionTime(id sdk.AccountObjectIdentifier, ex
}
}

func createTemporaryDatabaseOutsideTerraform(t *testing.T, name string) func() {
func createDatabaseOutsideTerraform(t *testing.T, name string) func() {
t.Helper()
client, err := sdk.NewDefaultClient()
if err != nil {
Expand Down
26 changes: 6 additions & 20 deletions pkg/resources/grant_privileges_to_account_role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveGrantedObjectOutsideTerraform(t
Steps: []resource.TestStep{
{
PreConfig: func() {
databaseCleanup = createTemporaryDatabaseOutsideTerraform(t, databaseName)
databaseCleanup = createDatabaseOutsideTerraform(t, databaseName)
createAccountRoleOutsideTerraform(t, name)
},
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"),
Expand Down Expand Up @@ -1259,8 +1259,8 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveAccountRoleOutsideTerraform(t *t
Steps: []resource.TestStep{
{
PreConfig: func() {
t.Cleanup(createTemporaryDatabaseOutsideTerraform(t, databaseName))
roleCleanup = createTemporaryAccountRoleOutsideTerraform(t, name)
t.Cleanup(createDatabaseOutsideTerraform(t, databaseName))
roleCleanup = createAccountRoleOutsideTerraform(t, name)
},
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"),
ConfigVariables: configVariables,
Expand Down Expand Up @@ -1338,7 +1338,7 @@ func dropSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, sha
)
}

func createAccountRoleOutsideTerraform(t *testing.T, name string) {
func createAccountRoleOutsideTerraform(t *testing.T, name string) func() {
t.Helper()
client, err := sdk.NewDefaultClient()
if err != nil {
Expand All @@ -1349,24 +1349,10 @@ func createAccountRoleOutsideTerraform(t *testing.T, name string) {
if err := client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId).WithOrReplace(true)); err != nil {
t.Fatal(fmt.Errorf("error account role (%s): %w", roleId.FullyQualifiedName(), err))
}
}

func createTemporaryAccountRoleOutsideTerraform(t *testing.T, name string) func() {
t.Helper()
client, err := sdk.NewDefaultClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
roleId := sdk.NewAccountObjectIdentifier(name)

if err := client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId)); err != nil {
t.Fatal(err)
}

return func() {
if err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId)); err != nil {
t.Fatal(err)
if err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId).WithIfExists(true)); err != nil {
t.Fatal(fmt.Errorf("error account role (%s): %w", roleId.FullyQualifiedName(), err))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnFuture_Streamlits_In
CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked,
Steps: []resource.TestStep{
{
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, name) },
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) },
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnFuture_InDatabase"),
ConfigVariables: configVariables,
ExpectError: regexp.MustCompile("Unsupported feature 'STREAMLIT'"),
Expand Down Expand Up @@ -590,7 +590,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAll_Streamlits_InDat
CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name),
Steps: []resource.TestStep{
{
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, name) },
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) },
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnAll_InDatabase"),
ConfigVariables: configVariables,
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -905,7 +905,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_MLPrivileges(t *testing.T) {
CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name),
Steps: []resource.TestStep{
{
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, name) },
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) },
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema"),
ConfigVariables: configVariables,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -929,7 +929,6 @@ func TestAcc_GrantPrivilegesToDatabaseRole_MLPrivileges(t *testing.T) {
})
}

func createDatabaseRoleOutsideTerraform(t *testing.T, name string) {
// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2621 doesn't apply to this resource
func TestAcc_GrantPrivilegesToDatabaseRole_RemoveGrantedObjectOutsideTerraform(t *testing.T) {
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
Expand All @@ -954,7 +953,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveGrantedObjectOutsideTerraform(t
Steps: []resource.TestStep{
{
PreConfig: func() {
databaseCleanup = createTemporaryDatabaseOutsideTerraform(t, databaseName)
databaseCleanup = createDatabaseOutsideTerraform(t, databaseName)
createDatabaseRoleOutsideTerraform(t, databaseName, name)
},
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"),
Expand Down Expand Up @@ -995,7 +994,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveDatabaseRoleOutsideTerraform(t
Steps: []resource.TestStep{
{
PreConfig: func() {
t.Cleanup(createTemporaryDatabaseOutsideTerraform(t, databaseName))
t.Cleanup(createDatabaseOutsideTerraform(t, databaseName))
databaseRoleCleanup = createDatabaseRoleOutsideTerraform(t, databaseName, name)
},
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"),
Expand Down
6 changes: 6 additions & 0 deletions pkg/resources/grant_privileges_to_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,12 @@ func setRolePrivilegeOptions(privileges []string, allPrivileges bool, onAccount
}

func readRoleGrantPrivileges(ctx context.Context, client *sdk.Client, grantedOn sdk.ObjectType, id GrantPrivilegesToRoleID, opts *sdk.ShowGrantOptions, d *schema.ResourceData) error {
if _, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(sdk.NewAccountObjectIdentifier(id.RoleName))); err != nil && err.Error() == "object does not exist" {
d.SetId("")
log.Printf("[DEBUG] Failed to retrieve account role. Marking the resource as removed.")
return nil
}

logging.DebugLogger.Printf("[DEBUG] About to show grants")
grants, err := client.Grants.Show(ctx, opts)
logging.DebugLogger.Printf("[DEBUG] After showing grants: err = %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/grant_privileges_to_share_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func TestAcc_GrantPrivilegesToShare_RemoveShareOutsideTerraform(t *testing.T) {
Steps: []resource.TestStep{
{
PreConfig: func() {
shareCleanup = createTemporaryShareOutsideTerraform(t, shareName)
shareCleanup = createShareOutsideTerraform(t, shareName)
},
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToShare/OnCustomShare"),
ConfigVariables: configVariables,
Expand Down Expand Up @@ -590,7 +590,7 @@ func testAccCheckSharePrivilegesRevoked() func(*terraform.State) error {
}
}

func createTemporaryShareOutsideTerraform(t *testing.T, name string) func() {
func createShareOutsideTerraform(t *testing.T, name string) func() {
t.Helper()
client, err := sdk.NewDefaultClient()
if err != nil {
Expand Down

0 comments on commit 595988c

Please sign in to comment.