Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle "null" properly for the nullable bool text in user
Browse files Browse the repository at this point in the history
References: #2817
sfc-gh-asawicki committed Aug 23, 2024

Verified

This commit was signed with the committer’s verified signature.
fwyzard Andrea Bocci
1 parent b616e15 commit d81e8f6
Showing 2 changed files with 31 additions and 9 deletions.
10 changes: 9 additions & 1 deletion pkg/sdk/testint/users_integration_test.go
Original file line number Diff line number Diff line change
@@ -982,13 +982,21 @@ func TestInt_Users(t *testing.T) {
// This test proves issue https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2817.
// sql: Scan error on column index 10, name "disabled": sql/driver: couldn't convert "null" into type bool
t.Run("issue #2817: handle show properly without OWNERSHIP and MANAGE GRANTS", func(t *testing.T) {
disabledUser, disabledUserCleanup := testClientHelper().User.CreateUserWithOptions(t, testClientHelper().Ids.RandomAccountObjectIdentifier(), &sdk.CreateUserOptions{ObjectProperties: &sdk.UserObjectProperties{Disable: sdk.Bool(true)}})
t.Cleanup(disabledUserCleanup)

fetchedDisabledUser, err := client.Users.ShowByID(ctx, disabledUser.ID())
require.NoError(t, err)
require.True(t, fetchedDisabledUser.Disabled)

role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t)
t.Cleanup(roleCleanup)

revertRole := testClientHelper().Role.UseRole(t, role.ID())
t.Cleanup(revertRole)

_, err := client.Users.ShowByID(ctx, user.ID())
fetchedDisabledUser, err = client.Users.ShowByID(ctx, disabledUser.ID())
require.NoError(t, err)
require.False(t, fetchedDisabledUser.Disabled)
})
}
30 changes: 22 additions & 8 deletions pkg/sdk/users.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"strconv"
"time"
)

@@ -72,14 +74,14 @@ type userDBRow struct {
MinsToUnlock sql.NullString `db:"mins_to_unlock"`
DaysToExpiry sql.NullString `db:"days_to_expiry"`
Comment sql.NullString `db:"comment"`
Disabled bool `db:"disabled"`
MustChangePassword bool `db:"must_change_password"`
SnowflakeLock bool `db:"snowflake_lock"`
Disabled sql.NullString `db:"disabled"`
MustChangePassword sql.NullString `db:"must_change_password"`
SnowflakeLock sql.NullString `db:"snowflake_lock"`
DefaultWarehouse sql.NullString `db:"default_warehouse"`
DefaultNamespace string `db:"default_namespace"`
DefaultRole string `db:"default_role"`
DefaultSecondaryRoles string `db:"default_secondary_roles"`
ExtAuthnDuo bool `db:"ext_authn_duo"`
ExtAuthnDuo sql.NullString `db:"ext_authn_duo"`
ExtAuthnUid string `db:"ext_authn_uid"`
MinsToBypassMfa string `db:"mins_to_bypass_mfa"`
Owner string `db:"owner"`
@@ -95,13 +97,9 @@ func (row userDBRow) convert() *User {
Name: row.Name,
CreatedOn: row.CreatedOn,
LoginName: row.LoginName,
Disabled: row.Disabled,
MustChangePassword: row.MustChangePassword,
SnowflakeLock: row.SnowflakeLock,
DefaultNamespace: row.DefaultNamespace,
DefaultRole: row.DefaultRole,
DefaultSecondaryRoles: row.DefaultSecondaryRoles,
ExtAuthnDuo: row.ExtAuthnDuo,
ExtAuthnUid: row.ExtAuthnUid,
MinsToBypassMfa: row.MinsToBypassMfa,
Owner: row.Owner,
@@ -129,6 +127,10 @@ func (row userDBRow) convert() *User {
if row.Comment.Valid {
user.Comment = row.Comment.String
}
handleNullableBoolString(row.Disabled, &user.Disabled)
handleNullableBoolString(row.MustChangePassword, &user.MustChangePassword)
handleNullableBoolString(row.SnowflakeLock, &user.SnowflakeLock)
handleNullableBoolString(row.ExtAuthnDuo, &user.ExtAuthnDuo)
if row.DefaultWarehouse.Valid {
user.DefaultWarehouse = row.DefaultWarehouse.String
}
@@ -144,6 +146,18 @@ func (row userDBRow) convert() *User {
return user
}

func handleNullableBoolString(nullableBoolString sql.NullString, field *bool) {
if nullableBoolString.Valid && nullableBoolString.String != "" && nullableBoolString.String != "null" {
parsed, err := strconv.ParseBool(nullableBoolString.String)
if err != nil {
// TODO [SNOW-1561641]: address during handling the issue
log.Printf("[DEBUG] Could not parse text boolean value %v", nullableBoolString.String)
} else {
*field = parsed
}
}
}

func (v *User) ID() AccountObjectIdentifier {
return AccountObjectIdentifier{v.Name}
}

0 comments on commit d81e8f6

Please sign in to comment.