-
Notifications
You must be signed in to change notification settings - Fork 426
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
chore: Continue random ids rework #2819
Changes from 21 commits
60e5aa6
e11cd46
9e4f942
ee0704b
244a03d
1f99de8
b5eed4b
a7390c2
37758b5
ef8038c
45461fb
6249726
3b03063
46e46a0
6ecbb32
f037fb5
f4612e4
daef1cd
2b7c8fa
811cd29
5f2c544
c07fea1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MaterializedViewClient struct { | ||
context *TestClientContext | ||
ids *IdsGenerator | ||
} | ||
|
||
func NewMaterializedViewClient(context *TestClientContext, idsGenerator *IdsGenerator) *MaterializedViewClient { | ||
return &MaterializedViewClient{ | ||
context: context, | ||
ids: idsGenerator, | ||
} | ||
} | ||
|
||
func (c *MaterializedViewClient) client() sdk.MaterializedViews { | ||
return c.context.client.MaterializedViews | ||
} | ||
|
||
func (c *MaterializedViewClient) CreateMaterializedView(t *testing.T, query string, orReplace bool) (*sdk.MaterializedView, func()) { | ||
t.Helper() | ||
return c.CreateMaterializedViewWithName(t, c.ids.RandomSchemaObjectIdentifier(), query, orReplace) | ||
} | ||
|
||
func (c *MaterializedViewClient) CreateMaterializedViewWithName(t *testing.T, id sdk.SchemaObjectIdentifier, query string, orReplace bool) (*sdk.MaterializedView, func()) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.client().Create(ctx, sdk.NewCreateMaterializedViewRequest(id, query).WithOrReplace(sdk.Bool(orReplace))) | ||
require.NoError(t, err) | ||
|
||
view, err := c.client().ShowByID(ctx, id) | ||
require.NoError(t, err) | ||
|
||
return view, c.DropMaterializedViewFunc(t, id) | ||
} | ||
|
||
func (c *MaterializedViewClient) DropMaterializedViewFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
return func() { | ||
err := c.client().Drop(ctx, sdk.NewDropMaterializedViewRequest(id).WithIfExists(sdk.Bool(true))) | ||
require.NoError(t, err) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,18 +24,18 @@ func (c *RoleClient) client() sdk.Roles { | |
return c.context.client.Roles | ||
} | ||
|
||
func (c *RoleClient) UseRole(t *testing.T, roleName string) func() { | ||
func (c *RoleClient) UseRole(t *testing.T, roleId sdk.AccountObjectIdentifier) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
currentRole, err := c.context.client.ContextFunctions.CurrentRole(ctx) | ||
require.NoError(t, err) | ||
|
||
err = c.context.client.Sessions.UseRole(ctx, sdk.NewAccountObjectIdentifier(roleName)) | ||
err = c.context.client.Sessions.UseRole(ctx, roleId) | ||
require.NoError(t, err) | ||
|
||
return func() { | ||
err = c.context.client.Sessions.UseRole(ctx, sdk.NewAccountObjectIdentifier(currentRole)) | ||
err = c.context.client.Sessions.UseRole(ctx, currentRole) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
@@ -53,9 +53,14 @@ func (c *RoleClient) CreateRoleWithName(t *testing.T, name string) (*sdk.Role, f | |
|
||
func (c *RoleClient) CreateRoleGrantedToCurrentUser(t *testing.T) (*sdk.Role, func()) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
role, roleCleanup := c.CreateRole(t) | ||
c.GrantRoleToCurrentUser(t, role.ID()) | ||
|
||
currentUser, err := c.context.client.ContextFunctions.CurrentUser(ctx) | ||
require.NoError(t, err) | ||
|
||
c.GrantRoleToUser(t, role.ID(), currentUser) | ||
return role, roleCleanup | ||
} | ||
|
||
|
@@ -80,15 +85,12 @@ func (c *RoleClient) DropRoleFunc(t *testing.T, id sdk.AccountObjectIdentifier) | |
} | ||
} | ||
|
||
func (c *RoleClient) GrantRoleToCurrentUser(t *testing.T, id sdk.AccountObjectIdentifier) { | ||
func (c *RoleClient) GrantRoleToUser(t *testing.T, id sdk.AccountObjectIdentifier, userId sdk.AccountObjectIdentifier) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
currentUser, err := c.context.client.ContextFunctions.CurrentUser(ctx) | ||
require.NoError(t, err) | ||
|
||
err = c.client().Grant(ctx, sdk.NewGrantRoleRequest(id, sdk.GrantRole{ | ||
User: sdk.Pointer(sdk.NewAccountObjectIdentifier(currentUser)), | ||
err := c.client().Grant(ctx, sdk.NewGrantRoleRequest(id, sdk.GrantRole{ | ||
User: sdk.Pointer(userId), | ||
})) | ||
require.NoError(t, err) | ||
} | ||
|
@@ -114,6 +116,31 @@ func (c *RoleClient) GrantOwnershipOnAccountObject(t *testing.T, roleId sdk.Acco | |
require.NoError(t, err) | ||
} | ||
|
||
// TODO: move later to grants client | ||
func (c *RoleClient) RevokeCurrentGrantsFromSchemaObject(t *testing.T, roleId sdk.AccountObjectIdentifier, objectId sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the signature to |
||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.context.client.Grants.GrantOwnership( | ||
ctx, | ||
sdk.OwnershipGrantOn{ | ||
Object: &sdk.Object{ | ||
ObjectType: objectType, | ||
Name: objectId, | ||
}, | ||
}, | ||
sdk.OwnershipGrantTo{ | ||
AccountRoleName: sdk.Pointer(roleId), | ||
}, | ||
&sdk.GrantOwnershipOptions{ | ||
CurrentGrants: &sdk.OwnershipCurrentGrants{ | ||
OutboundPrivileges: sdk.Revoke, | ||
}, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
} | ||
|
||
// TODO: move later to grants client | ||
func (c *RoleClient) GrantPrivilegeOnDatabaseToShare(t *testing.T, databaseId sdk.AccountObjectIdentifier, shareId sdk.AccountObjectIdentifier) { | ||
t.Helper() | ||
|
@@ -122,3 +149,18 @@ func (c *RoleClient) GrantPrivilegeOnDatabaseToShare(t *testing.T, databaseId sd | |
err := c.context.client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}, &sdk.ShareGrantOn{Database: databaseId}, shareId) | ||
require.NoError(t, err) | ||
} | ||
|
||
// TODO: move later to grants client | ||
func (c *RoleClient) ShowGrantsTo(t *testing.T, roleId sdk.AccountObjectIdentifier) []sdk.Grant { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
grants, err := c.context.client.Grants.Show(ctx, &sdk.ShowGrantOptions{ | ||
To: &sdk.ShowGrantsTo{ | ||
Role: roleId, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
return grants | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it a constructor name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, fixed