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

chore: Cleanup helpers part 3 #2730

Merged
merged 33 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
223e0b4
Add warehouse test client
sfc-gh-asawicki Apr 18, 2024
7e16517
Copy warehouse methods
sfc-gh-asawicki Apr 18, 2024
20c37e9
Replace use warehouse helper method
sfc-gh-asawicki Apr 18, 2024
9aa39bd
Use new warehouse helpers
sfc-gh-asawicki Apr 18, 2024
e8ff3ba
Move method from acceptance test
sfc-gh-asawicki Apr 18, 2024
5f5e49e
Prepare user test client
sfc-gh-asawicki Apr 18, 2024
fe45116
Copy user helper methods and add if exists to user drop
sfc-gh-asawicki Apr 18, 2024
fc37c7f
Replace old user helpers
sfc-gh-asawicki Apr 18, 2024
c83e312
Prepare role and database role test clients
sfc-gh-asawicki Apr 18, 2024
2c76bc3
Rename database helper's client
sfc-gh-asawicki Apr 18, 2024
daf505d
Copy role helper methods (WIP)
sfc-gh-asawicki Apr 18, 2024
7066d4d
Clean role helpers
sfc-gh-asawicki Apr 19, 2024
df258dd
Replace use role old helper
sfc-gh-asawicki Apr 19, 2024
2a26dbc
Replace old helper functions
sfc-gh-asawicki Apr 19, 2024
eae6f7a
Replace create role helper in acceptance tests
sfc-gh-asawicki Apr 19, 2024
5acad06
Replace create database with ownership helper function
sfc-gh-asawicki Apr 19, 2024
a706d68
Replace create role helper function in acceptance test
sfc-gh-asawicki Apr 19, 2024
667688d
Replace create database helper function in acceptance test
sfc-gh-asawicki Apr 19, 2024
57abbac
Remove unused resources helpers, extract the deprecated ones
sfc-gh-asawicki Apr 19, 2024
3d4fe66
Replace old helper create database role function
sfc-gh-asawicki Apr 19, 2024
e8d5226
Replace create database role old helper in acceptance test
sfc-gh-asawicki Apr 19, 2024
8bf4450
Run pre-push
sfc-gh-asawicki Apr 19, 2024
d0b4140
Prepare context client
sfc-gh-asawicki Apr 19, 2024
67b7a53
Introduce new current accoutn helper and replace old ones
sfc-gh-asawicki Apr 19, 2024
b406f93
Introduce new current role helper and replace old ones
sfc-gh-asawicki Apr 19, 2024
66b40a9
Introduce new current region helper and replace old ones
sfc-gh-asawicki Apr 19, 2024
98928b4
Introduce new current user helper and replace old ones
sfc-gh-asawicki Apr 19, 2024
f46af6f
Introduce new is role in session helper and replace old ones
sfc-gh-asawicki Apr 19, 2024
6bb2525
Prepare table client
sfc-gh-asawicki Apr 19, 2024
5276db9
Replace old table helper functions
sfc-gh-asawicki Apr 19, 2024
88b7a57
Add create table in default schema helper
sfc-gh-asawicki Apr 19, 2024
0decda6
Simplify schema helper methods
sfc-gh-asawicki Apr 19, 2024
b9cf529
Simplify database role helper methods
sfc-gh-asawicki Apr 19, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/gookit/color v1.5.4
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/terraform-json v0.18.0
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.20.0
Expand Down Expand Up @@ -88,6 +87,7 @@ require (
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down
73 changes: 73 additions & 0 deletions pkg/acceptance/helpers/context_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package helpers

import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ContextClient struct {
context *TestClientContext
}

func NewContextClient(context *TestClientContext) *ContextClient {
return &ContextClient{
context: context,
}
}

func (c *ContextClient) client() sdk.ContextFunctions {
return c.context.client.ContextFunctions
}

func (c *ContextClient) CurrentAccount(t *testing.T) string {
t.Helper()
ctx := context.Background()

currentAccount, err := c.client().CurrentAccount(ctx)
require.NoError(t, err)

return currentAccount
}

func (c *ContextClient) CurrentRole(t *testing.T) string {
t.Helper()
ctx := context.Background()

currentRole, err := c.client().CurrentRole(ctx)
require.NoError(t, err)

return currentRole
}

func (c *ContextClient) CurrentRegion(t *testing.T) string {
t.Helper()
ctx := context.Background()

currentRegion, err := c.client().CurrentRegion(ctx)
require.NoError(t, err)

return currentRegion
}

func (c *ContextClient) CurrentUser(t *testing.T) string {
t.Helper()
ctx := context.Background()

currentUser, err := c.client().CurrentUser(ctx)
require.NoError(t, err)

return currentUser
}

func (c *ContextClient) IsRoleInSession(t *testing.T, id sdk.AccountObjectIdentifier) bool {
t.Helper()
ctx := context.Background()

isInSession, err := c.client().IsRoleInSession(ctx, id)
require.NoError(t, err)

return isInSession
}
44 changes: 22 additions & 22 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,77 @@ func NewDatabaseClient(context *TestClientContext) *DatabaseClient {
}
}

func (d *DatabaseClient) client() sdk.Databases {
return d.context.client.Databases
func (c *DatabaseClient) client() sdk.Databases {
return c.context.client.Databases
}

func (d *DatabaseClient) CreateDatabase(t *testing.T) (*sdk.Database, func()) {
func (c *DatabaseClient) CreateDatabase(t *testing.T) (*sdk.Database, func()) {
t.Helper()
return d.CreateDatabaseWithOptions(t, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{})
return c.CreateDatabaseWithOptions(t, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{})
}

func (d *DatabaseClient) CreateDatabaseWithName(t *testing.T, name string) (*sdk.Database, func()) {
func (c *DatabaseClient) CreateDatabaseWithName(t *testing.T, name string) (*sdk.Database, func()) {
t.Helper()
return d.CreateDatabaseWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateDatabaseOptions{})
return c.CreateDatabaseWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateDatabaseOptions{})
}

func (d *DatabaseClient) CreateDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) {
func (c *DatabaseClient) CreateDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) {
t.Helper()
ctx := context.Background()
err := d.client().Create(ctx, id, opts)
err := c.client().Create(ctx, id, opts)
require.NoError(t, err)
database, err := d.client().ShowByID(ctx, id)
database, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
return database, d.DropDatabaseFunc(t, id)
return database, c.DropDatabaseFunc(t, id)
}

func (d *DatabaseClient) DropDatabaseFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
func (c *DatabaseClient) DropDatabaseFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := d.client().Drop(ctx, id, &sdk.DropDatabaseOptions{IfExists: sdk.Bool(true)})
err := c.client().Drop(ctx, id, &sdk.DropDatabaseOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
err = d.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(d.context.database, d.context.schema))
err = c.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema))
require.NoError(t, err)
}
}

func (d *DatabaseClient) CreateSecondaryDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, externalId sdk.ExternalObjectIdentifier, opts *sdk.CreateSecondaryDatabaseOptions) (*sdk.Database, func()) {
func (c *DatabaseClient) CreateSecondaryDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, externalId sdk.ExternalObjectIdentifier, opts *sdk.CreateSecondaryDatabaseOptions) (*sdk.Database, func()) {
t.Helper()
ctx := context.Background()

// TODO [926148]: make this wait better with tests stabilization
// waiting because sometimes creating secondary db right after primary creation resulted in error
time.Sleep(1 * time.Second)

err := d.client().CreateSecondary(ctx, id, externalId, opts)
err := c.client().CreateSecondary(ctx, id, externalId, opts)
require.NoError(t, err)

// TODO [926148]: make this wait better with tests stabilization
// waiting because sometimes secondary database is not shown as SHOW REPLICATION DATABASES results right after creation
time.Sleep(1 * time.Second)

database, err := d.client().ShowByID(ctx, id)
database, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
return database, func() {
err := d.client().Drop(ctx, id, nil)
err := c.client().Drop(ctx, id, nil)
require.NoError(t, err)

// TODO [926148]: make this wait better with tests stabilization
// waiting because sometimes dropping primary db right after dropping the secondary resulted in error
time.Sleep(1 * time.Second)
err = d.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(d.context.database, d.context.schema))
err = c.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema))
require.NoError(t, err)
}
}

func (d *DatabaseClient) UpdateDataRetentionTime(t *testing.T, id sdk.AccountObjectIdentifier, days int) func() {
func (c *DatabaseClient) UpdateDataRetentionTime(t *testing.T, id sdk.AccountObjectIdentifier, days int) func() {
t.Helper()
ctx := context.Background()

return func() {
err := d.client().Alter(ctx, id, &sdk.AlterDatabaseOptions{
err := c.client().Alter(ctx, id, &sdk.AlterDatabaseOptions{
Set: &sdk.DatabaseSet{
DataRetentionTimeInDays: sdk.Int(days),
},
Expand All @@ -98,9 +98,9 @@ func (d *DatabaseClient) UpdateDataRetentionTime(t *testing.T, id sdk.AccountObj
}
}

func (d *DatabaseClient) Show(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Database, error) {
func (c *DatabaseClient) Show(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Database, error) {
t.Helper()
ctx := context.Background()

return d.client().ShowByID(ctx, id)
return c.client().ShowByID(ctx, id)
}
71 changes: 71 additions & 0 deletions pkg/acceptance/helpers/database_role_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package helpers

import (
"context"
"errors"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type DatabaseRoleClient struct {
context *TestClientContext
}

func NewDatabaseRoleClient(context *TestClientContext) *DatabaseRoleClient {
return &DatabaseRoleClient{
context: context,
}
}

func (c *DatabaseRoleClient) client() sdk.DatabaseRoles {
return c.context.client.DatabaseRoles
}

func (c *DatabaseRoleClient) CreateDatabaseRole(t *testing.T) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabase(t, sdk.NewAccountObjectIdentifier(c.context.database))
}

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabaseWithName(t, databaseId, random.String())
}

func (c *DatabaseRoleClient) CreateDatabaseRoleWithName(t *testing.T, name string) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabaseWithName(t, sdk.NewAccountObjectIdentifier(c.context.database), name)
}

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabaseWithName(t *testing.T, databaseId sdk.AccountObjectIdentifier, name string) (*sdk.DatabaseRole, func()) {
t.Helper()
ctx := context.Background()

id := sdk.NewDatabaseObjectIdentifier(databaseId.Name(), name)

err := c.client().Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)

databaseRole, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)

return databaseRole, c.CleanupDatabaseRoleFunc(t, id)
}

func (c *DatabaseRoleClient) CleanupDatabaseRoleFunc(t *testing.T, id sdk.DatabaseObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
// to prevent error when db was removed before the role
_, err := c.context.client.Databases.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.DatabaseName()))
if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) {
return
}

err = c.client().Drop(ctx, sdk.NewDropDatabaseRoleRequest(id).WithIfExists(true))
require.NoError(t, err)
}
}
112 changes: 112 additions & 0 deletions pkg/acceptance/helpers/role_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package helpers

import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type RoleClient struct {
context *TestClientContext
}

func NewRoleClient(context *TestClientContext) *RoleClient {
return &RoleClient{
context: context,
}
}

func (c *RoleClient) client() sdk.Roles {
return c.context.client.Roles
}

func (c *RoleClient) UseRole(t *testing.T, roleName string) 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))
require.NoError(t, err)

return func() {
err = c.context.client.Sessions.UseRole(ctx, sdk.NewAccountObjectIdentifier(currentRole))
require.NoError(t, err)
}
}

func (c *RoleClient) CreateRole(t *testing.T) (*sdk.Role, func()) {
t.Helper()
return c.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(sdk.RandomAccountObjectIdentifier()))
}

func (c *RoleClient) CreateRoleWithName(t *testing.T, name string) (*sdk.Role, func()) {
t.Helper()
return c.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(sdk.NewAccountObjectIdentifier(name)))
}

func (c *RoleClient) CreateRoleGrantedToCurrentUser(t *testing.T) (*sdk.Role, func()) {
t.Helper()

role, roleCleanup := c.CreateRole(t)
c.GrantRoleToCurrentUser(t, role.ID())
return role, roleCleanup
}

func (c *RoleClient) CreateRoleWithRequest(t *testing.T, req *sdk.CreateRoleRequest) (*sdk.Role, func()) {
t.Helper()
ctx := context.Background()

err := c.client().Create(ctx, req)
require.NoError(t, err)
role, err := c.client().ShowByID(ctx, req.GetName())
require.NoError(t, err)
return role, c.DropRoleFunc(t, req.GetName())
}

func (c *RoleClient) DropRoleFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, sdk.NewDropRoleRequest(id).WithIfExists(true))
require.NoError(t, err)
}
}

func (c *RoleClient) GrantRoleToCurrentUser(t *testing.T, id 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)),
}))
require.NoError(t, err)
}

// TODO: move later to grants client
func (c *RoleClient) GrantOwnershipOnAccountObject(t *testing.T, roleId sdk.AccountObjectIdentifier, objectId sdk.AccountObjectIdentifier, objectType sdk.ObjectType) {
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),
},
new(sdk.GrantOwnershipOptions),
)
require.NoError(t, err)
}
Loading
Loading