diff --git a/go.mod b/go.mod index 00da7a83b9..1bca6424a5 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/pkg/acceptance/helpers/context_client.go b/pkg/acceptance/helpers/context_client.go new file mode 100644 index 0000000000..adb0a11c8e --- /dev/null +++ b/pkg/acceptance/helpers/context_client.go @@ -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 +} diff --git a/pkg/acceptance/helpers/database_client.go b/pkg/acceptance/helpers/database_client.go index 75065c4f9c..6f1dbe9a65 100644 --- a/pkg/acceptance/helpers/database_client.go +++ b/pkg/acceptance/helpers/database_client.go @@ -19,43 +19,43 @@ 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() @@ -63,33 +63,33 @@ func (d *DatabaseClient) CreateSecondaryDatabaseWithOptions(t *testing.T, id sdk // 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), }, @@ -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) } diff --git a/pkg/acceptance/helpers/database_role_client.go b/pkg/acceptance/helpers/database_role_client.go new file mode 100644 index 0000000000..1953507a09 --- /dev/null +++ b/pkg/acceptance/helpers/database_role_client.go @@ -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) + } +} diff --git a/pkg/acceptance/helpers/role_client.go b/pkg/acceptance/helpers/role_client.go new file mode 100644 index 0000000000..bb4fd7c12f --- /dev/null +++ b/pkg/acceptance/helpers/role_client.go @@ -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) +} diff --git a/pkg/acceptance/helpers/schema_client.go b/pkg/acceptance/helpers/schema_client.go index b620c5cea3..dd9df1cea1 100644 --- a/pkg/acceptance/helpers/schema_client.go +++ b/pkg/acceptance/helpers/schema_client.go @@ -23,18 +23,28 @@ func (c *SchemaClient) client() sdk.Schemas { return c.context.client.Schemas } -func (c *SchemaClient) CreateSchema(t *testing.T, database *sdk.Database) (*sdk.Schema, func()) { +func (c *SchemaClient) CreateSchema(t *testing.T) (*sdk.Schema, func()) { t.Helper() - return c.CreateSchemaWithIdentifier(t, database, random.StringRange(8, 28)) + return c.CreateSchemaInDatabase(t, sdk.NewAccountObjectIdentifier(c.context.database)) } -func (c *SchemaClient) CreateSchemaWithIdentifier(t *testing.T, database *sdk.Database, name string) (*sdk.Schema, func()) { +func (c *SchemaClient) CreateSchemaInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.Schema, func()) { + t.Helper() + return c.CreateSchemaInDatabaseWithIdentifier(t, databaseId, random.AlphaN(12)) +} + +func (c *SchemaClient) CreateSchemaWithName(t *testing.T, name string) (*sdk.Schema, func()) { + t.Helper() + return c.CreateSchemaInDatabaseWithIdentifier(t, sdk.NewAccountObjectIdentifier(c.context.database), name) +} + +func (c *SchemaClient) CreateSchemaInDatabaseWithIdentifier(t *testing.T, databaseId sdk.AccountObjectIdentifier, name string) (*sdk.Schema, func()) { t.Helper() ctx := context.Background() - schemaID := sdk.NewDatabaseObjectIdentifier(database.Name, name) + schemaID := sdk.NewDatabaseObjectIdentifier(databaseId.Name(), name) err := c.client().Create(ctx, schemaID, nil) require.NoError(t, err) - schema, err := c.client().ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(database.Name, name)) + schema, err := c.client().ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(databaseId.Name(), name)) require.NoError(t, err) return schema, c.DropSchemaFunc(t, schemaID) } diff --git a/pkg/acceptance/helpers/table_client.go b/pkg/acceptance/helpers/table_client.go new file mode 100644 index 0000000000..b12360e8bf --- /dev/null +++ b/pkg/acceptance/helpers/table_client.go @@ -0,0 +1,78 @@ +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 TableClient struct { + context *TestClientContext +} + +func NewTableClient(context *TestClientContext) *TableClient { + return &TableClient{ + context: context, + } +} + +func (c *TableClient) client() sdk.Tables { + return c.context.client.Tables +} + +func (c *TableClient) CreateTable(t *testing.T) (*sdk.Table, func()) { + t.Helper() + return c.CreateTableInSchema(t, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema)) +} + +func (c *TableClient) CreateTableInSchema(t *testing.T, schemaId sdk.DatabaseObjectIdentifier) (*sdk.Table, func()) { + t.Helper() + + columns := []sdk.TableColumnRequest{ + *sdk.NewTableColumnRequest("id", sdk.DataTypeNumber), + } + name := random.StringRange(8, 28) + return c.CreateTableWithColumns(t, schemaId, name, columns) +} + +func (c *TableClient) CreateTableWithColumns(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, name string, columns []sdk.TableColumnRequest) (*sdk.Table, func()) { + t.Helper() + + id := sdk.NewSchemaObjectIdentifier(schemaId.DatabaseName(), schemaId.Name(), name) + ctx := context.Background() + + dbCreateRequest := sdk.NewCreateTableRequest(id, columns) + err := c.client().Create(ctx, dbCreateRequest) + require.NoError(t, err) + + table, err := c.client().ShowByID(ctx, id) + require.NoError(t, err) + + return table, c.DropTableFunc(t, id) +} + +func (c *TableClient) DropTableFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() { + t.Helper() + ctx := context.Background() + + return func() { + // to prevent error when db was removed before the table + _, err := c.context.client.Databases.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.DatabaseName())) + if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { + return + } + + // to prevent error when schema was removed before the table + _, err = c.context.client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())) + if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { + return + } + + dropErr := c.client().Drop(ctx, sdk.NewDropTableRequest(id).WithIfExists(sdk.Bool(true))) + require.NoError(t, dropErr) + } +} diff --git a/pkg/acceptance/helpers/test_client.go b/pkg/acceptance/helpers/test_client.go index 3aa9003fc3..55bf948f8e 100644 --- a/pkg/acceptance/helpers/test_client.go +++ b/pkg/acceptance/helpers/test_client.go @@ -5,8 +5,14 @@ import "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" type TestClient struct { context *TestClientContext - Database *DatabaseClient - Schema *SchemaClient + Context *ContextClient + Database *DatabaseClient + DatabaseRole *DatabaseRoleClient + Role *RoleClient + Schema *SchemaClient + Table *TableClient + User *UserClient + Warehouse *WarehouseClient } func NewTestClient(c *sdk.Client, database string, schema string, warehouse string) *TestClient { @@ -17,9 +23,15 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri warehouse: warehouse, } return &TestClient{ - context: context, - Database: NewDatabaseClient(context), - Schema: NewSchemaClient(context), + context: context, + Context: NewContextClient(context), + Database: NewDatabaseClient(context), + DatabaseRole: NewDatabaseRoleClient(context), + Role: NewRoleClient(context), + Schema: NewSchemaClient(context), + Table: NewTableClient(context), + User: NewUserClient(context), + Warehouse: NewWarehouseClient(context), } } diff --git a/pkg/acceptance/helpers/user_client.go b/pkg/acceptance/helpers/user_client.go new file mode 100644 index 0000000000..cf46929deb --- /dev/null +++ b/pkg/acceptance/helpers/user_client.go @@ -0,0 +1,53 @@ +package helpers + +import ( + "context" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/stretchr/testify/require" +) + +type UserClient struct { + context *TestClientContext +} + +func NewUserClient(context *TestClientContext) *UserClient { + return &UserClient{ + context: context, + } +} + +func (c *UserClient) client() sdk.Users { + return c.context.client.Users +} + +func (c *UserClient) CreateUser(t *testing.T) (*sdk.User, func()) { + t.Helper() + return c.CreateUserWithOptions(t, sdk.RandomAccountObjectIdentifier(), &sdk.CreateUserOptions{}) +} + +func (c *UserClient) CreateUserWithName(t *testing.T, name string) (*sdk.User, func()) { + t.Helper() + return c.CreateUserWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateUserOptions{}) +} + +func (c *UserClient) CreateUserWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateUserOptions) (*sdk.User, func()) { + t.Helper() + ctx := context.Background() + err := c.client().Create(ctx, id, opts) + require.NoError(t, err) + user, err := c.client().ShowByID(ctx, id) + require.NoError(t, err) + return user, c.DropUserFunc(t, id) +} + +func (c *UserClient) DropUserFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() { + t.Helper() + ctx := context.Background() + + return func() { + err := c.client().Drop(ctx, id, &sdk.DropUserOptions{IfExists: sdk.Bool(true)}) + require.NoError(t, err) + } +} diff --git a/pkg/acceptance/helpers/warehouse_client.go b/pkg/acceptance/helpers/warehouse_client.go new file mode 100644 index 0000000000..9065f3e5b9 --- /dev/null +++ b/pkg/acceptance/helpers/warehouse_client.go @@ -0,0 +1,68 @@ +package helpers + +import ( + "context" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/stretchr/testify/require" +) + +type WarehouseClient struct { + context *TestClientContext +} + +func NewWarehouseClient(context *TestClientContext) *WarehouseClient { + return &WarehouseClient{ + context: context, + } +} + +func (c *WarehouseClient) client() sdk.Warehouses { + return c.context.client.Warehouses +} + +func (c *WarehouseClient) UseWarehouse(t *testing.T, id sdk.AccountObjectIdentifier) func() { + t.Helper() + ctx := context.Background() + err := c.context.client.Sessions.UseWarehouse(ctx, id) + require.NoError(t, err) + return func() { + err = c.context.client.Sessions.UseWarehouse(ctx, sdk.NewAccountObjectIdentifier(c.context.warehouse)) + require.NoError(t, err) + } +} + +func (c *WarehouseClient) CreateWarehouse(t *testing.T) (*sdk.Warehouse, func()) { + t.Helper() + return c.CreateWarehouseWithOptions(t, sdk.RandomAccountObjectIdentifier(), &sdk.CreateWarehouseOptions{}) +} + +func (c *WarehouseClient) CreateWarehouseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateWarehouseOptions) (*sdk.Warehouse, func()) { + t.Helper() + ctx := context.Background() + err := c.client().Create(ctx, id, opts) + require.NoError(t, err) + return &sdk.Warehouse{Name: id.Name()}, c.DropWarehouseFunc(t, id) +} + +func (c *WarehouseClient) DropWarehouseFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() { + t.Helper() + ctx := context.Background() + + return func() { + err := c.client().Drop(ctx, id, &sdk.DropWarehouseOptions{IfExists: sdk.Bool(true)}) + require.NoError(t, err) + err = c.context.client.Sessions.UseWarehouse(ctx, sdk.NewAccountObjectIdentifier(c.context.warehouse)) + require.NoError(t, err) + } +} + +func (c *WarehouseClient) UpdateMaxConcurrencyLevel(t *testing.T, name string, level int) { + t.Helper() + + ctx := context.Background() + + err := c.client().Alter(ctx, sdk.NewAccountObjectIdentifier(name), &sdk.AlterWarehouseOptions{Set: &sdk.WarehouseSet{MaxConcurrencyLevel: sdk.Int(level)}}) + require.NoError(t, err) +} diff --git a/pkg/datasources/grants_acceptance_test.go b/pkg/datasources/grants_acceptance_test.go index 134db6373e..f8e62e9dd2 100644 --- a/pkg/datasources/grants_acceptance_test.go +++ b/pkg/datasources/grants_acceptance_test.go @@ -192,7 +192,7 @@ func TestAcc_Grants_To_DatabaseRole(t *testing.T) { } func TestAcc_Grants_To_User(t *testing.T) { - user := getCurrentUser(t) + user := acc.TestClient().Context.CurrentUser(t) configVariables := config.Variables{ "user": config.StringVariable(user), } @@ -674,13 +674,3 @@ func checkAtLeastOneGrantPresentLimited() resource.TestCheckFunc { resource.TestCheckResourceAttrSet(datasourceName, "grants.0.granted_to"), ) } - -func getCurrentUser(t *testing.T) string { - t.Helper() - client := acc.Client(t) - user, err := client.ContextFunctions.CurrentUser(context.Background()) - if err != nil { - t.Fatal(err) - } - return user -} diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index a09456baa6..4c7651c7e7 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -18,7 +18,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/require" ) func TestAcc_DatabaseWithUnderscore(t *testing.T) { @@ -48,7 +47,7 @@ func TestAcc_Database(t *testing.T) { prefix := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) prefix2 := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - secondaryAccountName := getSecondaryAccount(t) + secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -156,7 +155,7 @@ func TestAcc_DatabaseRemovedOutsideOfTerraform(t *testing.T) { func TestAcc_Database_issue2021(t *testing.T) { name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - secondaryAccountName := getSecondaryAccount(t) + secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -369,18 +368,6 @@ resource "snowflake_database" "db" { return fmt.Sprintf(s, prefix, secondaryAccountName) } -func getSecondaryAccount(t *testing.T) string { - t.Helper() - - secondaryClient := acc.SecondaryClient(t) - ctx := context.Background() - - account, err := secondaryClient.ContextFunctions.CurrentAccount(ctx) - require.NoError(t, err) - - return account -} - // TODO [SNOW-936093]: this is used mostly as check for unsafe execute, not as normal check destroy in other resources. Handle with the helpers cleanup. func testAccCheckDatabaseExistence(t *testing.T, id string, shouldExist bool) func(state *terraform.State) error { t.Helper() diff --git a/pkg/resources/deprecated_helpers_test.go b/pkg/resources/deprecated_helpers_test.go new file mode 100644 index 0000000000..324f745d3a --- /dev/null +++ b/pkg/resources/deprecated_helpers_test.go @@ -0,0 +1,230 @@ +package resources_test + +import ( + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/stretchr/testify/require" +) + +/** + * Most functions from this file will be removed while removing old grant resources. + * The rest will be removed while adding security integrations to the SDK. + */ + +func databaseGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.DatabaseGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func schemaGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.SchemaGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func stageGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.StageGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func tableGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.TableGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func viewGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.ViewGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func materializedViewGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.MaterializedViewGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func resourceMonitorGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.ResourceMonitorGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func integrationGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.IntegrationGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func accountGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.AccountGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func roleGrants(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.RoleGrants().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func userOwnershipGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.UserOwnershipGrant().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func roleOwnershipGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.RoleOwnershipGrant().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func samlIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.SAMLIntegration().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func scimIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.SCIMIntegration().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func oauthIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.OAuthIntegration().Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func externalTableGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.ExternalTableGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func fileFormatGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.FileFormatGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func sequenceGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.SequenceGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func streamGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.StreamGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func maskingPolicyGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.MaskingPolicyGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func pipeGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.PipeGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func taskGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.TaskGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func rowAccessPolicyGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.RowAccessPolicyGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} + +func tagGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { + t.Helper() + r := require.New(t) + d := schema.TestResourceDataRaw(t, resources.TagGrant().Resource.Schema, params) + r.NotNil(d) + d.SetId(id) + return d +} diff --git a/pkg/resources/grant_ownership_acceptance_test.go b/pkg/resources/grant_ownership_acceptance_test.go index e32afb2b73..1f0d080e38 100644 --- a/pkg/resources/grant_ownership_acceptance_test.go +++ b/pkg/resources/grant_ownership_acceptance_test.go @@ -563,7 +563,8 @@ func TestAcc_GrantOwnership_TargetObjectRemovedOutsideTerraform(t *testing.T) { accountRoleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountRoleFullyQualifiedName := sdk.NewAccountObjectIdentifier(accountRoleName).FullyQualifiedName() - cleanupDatabase := createDatabase(t, databaseName) + _, cleanupDatabase := acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) + t.Cleanup(cleanupDatabase) configVariables := config.Variables{ "account_role_name": config.StringVariable(accountRoleName), @@ -619,7 +620,8 @@ func TestAcc_GrantOwnership_AccountRoleRemovedOutsideTerraform(t *testing.T) { accountRoleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountRoleFullyQualifiedName := sdk.NewAccountObjectIdentifier(accountRoleName).FullyQualifiedName() - cleanupAccountRole := createAccountRole(t, accountRoleName) + _, cleanupAccountRole := acc.TestClient().Role.CreateRoleWithName(t, accountRoleName) + t.Cleanup(cleanupAccountRole) configVariables := config.Variables{ "account_role_name": config.StringVariable(accountRoleName), @@ -721,7 +723,7 @@ func TestAcc_GrantOwnership_RoleBasedAccessControlUseCase(t *testing.T) { accountRoleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - userName := getCurrentUser(t) + userName := acc.TestClient().Context.CurrentUser(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -874,9 +876,13 @@ func TestAcc_GrantOwnership_ForceOwnershipTransferOnCreate(t *testing.T) { Steps: []resource.TestStep{ { PreConfig: func() { - t.Cleanup(createAccountRoleOutsideTerraform(t, accountRoleName)) - t.Cleanup(createAccountRoleOutsideTerraform(t, newDatabaseOwningAccountRoleName)) - t.Cleanup(createDatabaseWithRoleAsOwner(t, accountRoleName, databaseName)) + role, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, accountRoleName) + t.Cleanup(roleCleanup) + _, newRoleCleanup := acc.TestClient().Role.CreateRoleWithName(t, newDatabaseOwningAccountRoleName) + t.Cleanup(newRoleCleanup) + database, databaseCleanup := acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) + t.Cleanup(databaseCleanup) + acc.TestClient().Role.GrantOwnershipOnAccountObject(t, role.ID(), database.ID(), sdk.ObjectTypeDatabase) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantOwnership/ForceOwnershipTransferOnCreate"), ConfigVariables: configVariables, @@ -1119,33 +1125,6 @@ func TestAcc_GrantOwnership_OnDatabaseRole(t *testing.T) { }) } -func createDatabaseWithRoleAsOwner(t *testing.T, roleName string, databaseName string) func() { - t.Helper() - client := acc.Client(t) - ctx := context.Background() - databaseId := sdk.NewAccountObjectIdentifier(databaseName) - assert.NoError(t, client.Databases.Create(ctx, databaseId, &sdk.CreateDatabaseOptions{})) - - err := client.Grants.GrantOwnership( - ctx, - sdk.OwnershipGrantOn{ - Object: &sdk.Object{ - ObjectType: sdk.ObjectTypeDatabase, - Name: databaseId, - }, - }, - sdk.OwnershipGrantTo{ - AccountRoleName: sdk.Pointer(sdk.NewAccountObjectIdentifier(roleName)), - }, - new(sdk.GrantOwnershipOptions), - ) - assert.NoError(t, err) - - return func() { - assert.NoError(t, client.Databases.Drop(ctx, databaseId, &sdk.DropDatabaseOptions{})) - } -} - func moveResourceOwnershipToAccountRole(t *testing.T, objectType sdk.ObjectType, objectName sdk.ObjectIdentifier, accountRoleName sdk.AccountObjectIdentifier) { t.Helper() @@ -1195,40 +1174,14 @@ func checkResourceOwnershipIsGranted(opts *sdk.ShowGrantOptions, grantOn sdk.Obj } } -func createAccountRole(t *testing.T, name string) func() { - t.Helper() - client := acc.Client(t) - ctx := context.Background() - roleId := sdk.NewAccountObjectIdentifier(name) - assert.NoError(t, client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId))) - - return func() { - assert.NoError(t, client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId))) - } -} - -func createDatabase(t *testing.T, name string) func() { - t.Helper() - client := acc.Client(t) - - ctx := context.Background() - roleId := sdk.NewAccountObjectIdentifier(name) - assert.NoError(t, client.Databases.Create(ctx, roleId, new(sdk.CreateDatabaseOptions))) - - return func() { - assert.NoError(t, client.Databases.Drop(ctx, roleId, new(sdk.DropDatabaseOptions))) - } -} - func grantOwnershipToTheCurrentRole(t *testing.T, on sdk.OwnershipGrantOn) { t.Helper() client := acc.Client(t) ctx := context.Background() - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - assert.NoError(t, err) + currentRole := acc.TestClient().Context.CurrentRole(t) - err = client.Grants.GrantOwnership( + err := client.Grants.GrantOwnership( ctx, on, sdk.OwnershipGrantTo{ @@ -1238,11 +1191,3 @@ func grantOwnershipToTheCurrentRole(t *testing.T, on sdk.OwnershipGrantOn) { ) assert.NoError(t, err) } - -func getCurrentUser(t *testing.T) string { - t.Helper() - client := acc.Client(t) - currentUser, err := client.ContextFunctions.CurrentUser(context.Background()) - assert.NoError(t, err) - return currentUser -} diff --git a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go index a34d827190..7ac5e858f8 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -43,7 +43,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccount(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -89,7 +92,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccount_PrivilegesReversed(t *testin CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -137,7 +143,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccountObject(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -185,7 +194,10 @@ func TestAcc_GrantPrivilegesToApplicationRole_OnAccountObject_InfinitePlan(t *te CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject_InfinitePlan"), ConfigVariables: config.Variables{ "name": config.StringVariable(name), @@ -227,7 +239,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchema(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -263,7 +278,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchema_ExactlyOneOf(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema_ExactlyOneOf"), PlanOnly: true, ExpectError: regexp.MustCompile("Error: Invalid combination of arguments"), @@ -296,7 +314,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAllSchemasInDatabase(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAllSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -345,7 +366,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnFutureSchemasInDatabase(t *testing.T CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnFutureSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -397,7 +421,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnObject(t *testing.T) CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -446,7 +473,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnObject_OwnershipPrivi CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported privilege 'OWNERSHIP'"), @@ -480,7 +510,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAll_InDatabase(t *tes CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -530,7 +563,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAllPipes(t *testing.T CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -581,7 +617,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnFuture_InDatabase(t * CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -633,7 +672,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnFuture_Streamlits_InD CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported feature 'STREAMLIT'"), @@ -666,7 +708,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAll_Streamlits_InData CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -717,7 +762,10 @@ func TestAcc_GrantPrivilegesToAccountRole_UpdatePrivileges(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/UpdatePrivileges/privileges"), ConfigVariables: configVariables(false, []sdk.AccountObjectPrivilege{ sdk.AccountObjectPrivilegeCreateSchema, @@ -808,7 +856,10 @@ func TestAcc_GrantPrivilegesToAccountRole_UpdatePrivileges_SnowflakeChecked(t *t CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/UpdatePrivileges_SnowflakeChecked/privileges"), ConfigVariables: configVariables(false, []string{ sdk.AccountObjectPrivilegeCreateSchema.String(), @@ -883,7 +934,10 @@ func TestAcc_GrantPrivilegesToAccountRole_AlwaysApply(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/AlwaysApply"), ConfigVariables: configVariables(false), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -954,8 +1008,7 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivileges(t *testing.T) { sharedDatabaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) shareName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) roleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - secondaryAccountName, err := getSecondaryAccountName(t) - require.NoError(t, err) + secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) configVariables := config.Variables{ "role_name": config.StringVariable(roleName), "shared_database_name": config.StringVariable(sharedDatabaseName), @@ -1025,7 +1078,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivilegesOnSnowflakeDatabase( CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/ImportedPrivilegesOnSnowflakeDatabase"), ConfigVariables: configVariables, ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -1077,7 +1133,10 @@ func TestAcc_GrantPrivilegesToAccountRole_MultiplePartsInRoleName(t *testing.T) CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1113,7 +1172,8 @@ func TestAcc_GrantPrivilegesToAccountRole_OnExternalVolume(t *testing.T) { Steps: []resource.TestStep{ { PreConfig: func() { - t.Cleanup(createAccountRoleOutsideTerraform(t, name)) + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) cleanupExternalVolume := createExternalVolume(t, externalVolumeName) t.Cleanup(cleanupExternalVolume) }, @@ -1162,7 +1222,10 @@ func TestAcc_GrantPrivilegesToAccountRole_MLPrivileges(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1212,7 +1275,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ChangeWithGrantOptionsOutsideOfTerrafo CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1268,7 +1334,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ChangeWithGrantOptionsOutsideOfTerrafo CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1378,7 +1447,8 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveGrantedObjectOutsideTerraform(t { PreConfig: func() { _, databaseCleanup = acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) - t.Cleanup(createAccountRoleOutsideTerraform(t, name)) + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), ConfigVariables: configVariables, @@ -1422,7 +1492,7 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveAccountRoleOutsideTerraform(t *t PreConfig: func() { _, dbCleanup := acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) t.Cleanup(dbCleanup) - roleCleanup = createAccountRoleOutsideTerraform(t, name) + _, roleCleanup = acc.TestClient().Role.CreateRoleWithName(t, name) t.Cleanup(roleCleanup) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), @@ -1463,7 +1533,10 @@ func TestAcc_GrantPrivilegesToAccountRole_AlwaysApply_SetAfterCreate(t *testing. CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/AlwaysApply"), ConfigVariables: configVariables(true), ExpectNonEmptyPlan: true, @@ -1476,25 +1549,12 @@ func TestAcc_GrantPrivilegesToAccountRole_AlwaysApply_SetAfterCreate(t *testing. }) } -func getSecondaryAccountName(t *testing.T) (string, error) { - t.Helper() - secondaryClient := acc.SecondaryClient(t) - return secondaryClient.ContextFunctions.CurrentAccount(context.Background()) -} - -func getAccountName(t *testing.T) (string, error) { - t.Helper() - client := acc.Client(t) - return client.ContextFunctions.CurrentAccount(context.Background()) -} - func createSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, shareName string) error { t.Helper() secondaryClient := acc.SecondaryClient(t) ctx := context.Background() - accountName, err := getAccountName(t) + accountName := acc.TestClient().Context.CurrentAccount(t) return errors.Join( - err, secondaryClient.Databases.Create(ctx, sdk.NewAccountObjectIdentifier(databaseName), &sdk.CreateDatabaseOptions{}), secondaryClient.Shares.Create(ctx, sdk.NewAccountObjectIdentifier(shareName), &sdk.CreateShareOptions{}), secondaryClient.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}, &sdk.ShareGrantOn{Database: sdk.NewAccountObjectIdentifier(databaseName)}, sdk.NewAccountObjectIdentifier(shareName)), @@ -1514,22 +1574,6 @@ func dropSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, sha ) } -func createAccountRoleOutsideTerraform(t *testing.T, name string) func() { - t.Helper() - client := acc.Client(t) - ctx := context.Background() - roleId := sdk.NewAccountObjectIdentifier(name) - 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)) - } - - return func() { - if err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId).WithIfExists(true)); err != nil { - t.Errorf("error dropping account role (%s): %v", roleId.FullyQualifiedName(), err) - } - } -} - func queriedAccountRolePrivilegesEqualTo(roleName sdk.AccountObjectIdentifier, privileges ...string) func(s *terraform.State) error { return queriedPrivilegesEqualTo(func(client *sdk.Client, ctx context.Context) ([]sdk.Grant, error) { return client.Grants.Show(ctx, &sdk.ShowGrantOptions{ diff --git a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go index f1e9db1be1..e115afe658 100644 --- a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go @@ -44,7 +44,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase(t *testing.T) { CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -95,7 +98,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase_PrivilegesReversed(t *test CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -146,7 +152,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchema(t *testing.T) { CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -214,7 +223,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnAllSchemasInDatabase(t *testing.T) CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnAllSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -264,7 +276,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnFutureSchemasInDatabase(t *testing. CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnFutureSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -317,7 +332,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnObject(t *testing.T) CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -366,7 +384,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnObject_OwnershipPriv CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported privilege 'OWNERSHIP'"), @@ -401,7 +422,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAll_InDatabase(t *te CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -452,7 +476,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAllPipes(t *testing. CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -504,7 +531,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnFuture_InDatabase(t CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -554,7 +584,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnFuture_Streamlits_In CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported feature 'STREAMLIT'"), @@ -588,7 +621,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAll_Streamlits_InDat CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -640,7 +676,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_UpdatePrivileges(t *testing.T) { CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/UpdatePrivileges/privileges"), ConfigVariables: configVariables(false, []sdk.AccountObjectPrivilege{ sdk.AccountObjectPrivilegeCreateSchema, @@ -732,7 +771,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_UpdatePrivileges_SnowflakeChecked(t * CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/UpdatePrivileges_SnowflakeChecked/privileges"), ConfigVariables: configVariables(false, []string{ sdk.AccountObjectPrivilegeCreateSchema.String(), @@ -808,7 +850,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply(t *testing.T) { CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/AlwaysApply"), ConfigVariables: configVariables(false), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -903,7 +948,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_MLPrivileges(t *testing.T) { CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -948,7 +996,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_ChangeWithGrantOptionsOutsideOfTerraf CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -999,7 +1050,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_ChangeWithGrantOptionsOutsideOfTerraf CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1055,8 +1109,8 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveGrantedObjectOutsideTerraform(t PreConfig: func() { _, databaseCleanup = acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) t.Cleanup(databaseCleanup) - // no need to clean the role, because database will be dropped - createDatabaseRoleOutsideTerraform(t, databaseName, name) + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleInDatabaseWithName(t, sdk.NewAccountObjectIdentifier(databaseName), name) + t.Cleanup(databaseRoleCleanup) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, @@ -1098,7 +1152,8 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveDatabaseRoleOutsideTerraform(t PreConfig: func() { _, dbCleanup := acc.TestClient().Database.CreateDatabaseWithName(t, databaseName) t.Cleanup(dbCleanup) - databaseRoleCleanup = createDatabaseRoleOutsideTerraform(t, databaseName, name) + _, databaseRoleCleanup = acc.TestClient().DatabaseRole.CreateDatabaseRoleInDatabaseWithName(t, sdk.NewAccountObjectIdentifier(databaseName), name) + t.Cleanup(databaseRoleCleanup) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, @@ -1139,7 +1194,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply_SetAfterCreate(t *testing CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { + _, databaseRoleCleanup := acc.TestClient().DatabaseRole.CreateDatabaseRoleWithName(t, name) + t.Cleanup(databaseRoleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/AlwaysApply"), ConfigVariables: configVariables(true), ExpectNonEmptyPlan: true, @@ -1152,22 +1210,6 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply_SetAfterCreate(t *testing }) } -func createDatabaseRoleOutsideTerraform(t *testing.T, databaseName string, name string) func() { - t.Helper() - client := acc.Client(t) - ctx := context.Background() - databaseRoleId := sdk.NewDatabaseObjectIdentifier(databaseName, name) - if err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(databaseRoleId).WithOrReplace(true)); err != nil { - t.Fatal(fmt.Errorf("error database role (%s): %w", databaseRoleId.FullyQualifiedName(), err)) - } - - return func() { - if err := client.DatabaseRoles.Drop(ctx, sdk.NewDropDatabaseRoleRequest(databaseRoleId).WithIfExists(true)); err != nil { - t.Fatal(fmt.Errorf("error database role (%s): %w", databaseRoleId.FullyQualifiedName(), err)) - } - } -} - func queriedPrivilegesToDatabaseRoleEqualTo(databaseRoleName sdk.DatabaseObjectIdentifier, privileges ...string) func(s *terraform.State) error { return queriedPrivilegesEqualTo(func(client *sdk.Client, ctx context.Context) ([]sdk.Grant, error) { return client.Grants.Show(ctx, &sdk.ShowGrantOptions{ diff --git a/pkg/resources/grant_privileges_to_role_acceptance_test.go b/pkg/resources/grant_privileges_to_role_acceptance_test.go index 2bce101015..ce3fca0f46 100644 --- a/pkg/resources/grant_privileges_to_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_role_acceptance_test.go @@ -17,7 +17,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestAcc_GrantPrivilegesToRole_onAccount(t *testing.T) { @@ -987,7 +986,10 @@ func TestAcc_GrantPrivilegesToRole_OnAllPipes(t *testing.T) { CheckDestroy: nil, Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1083,8 +1085,7 @@ func TestAcc_GrantPrivilegesToRole_ImportedPrivileges(t *testing.T) { sharedDatabaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) shareName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) roleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - secondaryAccountName, err := getSecondaryAccountName(t) - require.NoError(t, err) + secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) configVariables := config.Variables{ "role_name": config.StringVariable(roleName), "shared_database_name": config.StringVariable(sharedDatabaseName), @@ -1158,7 +1159,10 @@ func TestAcc_GrantPrivilegesToRole_MultiplePartsInRoleName(t *testing.T) { CheckDestroy: nil, Steps: []resource.TestStep{ { - PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, + PreConfig: func() { + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, name) + t.Cleanup(roleCleanup) + }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( diff --git a/pkg/resources/helpers_test.go b/pkg/resources/helpers_test.go index d65409b83d..9ddee8930b 100644 --- a/pkg/resources/helpers_test.go +++ b/pkg/resources/helpers_test.go @@ -6,25 +6,17 @@ import ( "slices" "testing" - tfjson "github.com/hashicorp/terraform-json" - "github.com/hashicorp/terraform-plugin-testing/plancheck" + acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/stretchr/testify/assert" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/stretchr/testify/require" - - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" ) -// todo: remove the rest of these which are not used. also this file should be renamed for clarity to make it clear it is for testing only -// https://snowflakecomputing.atlassian.net/browse/SNOW-936093 type grantType int const ( @@ -60,429 +52,6 @@ func TestGetPropertyAsPointer(t *testing.T) { assert.Nil(t, resources.GetPropertyAsPointer[bool](d, "invalid")) } -func database(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Database().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func databaseGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.DatabaseGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func schemaGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.SchemaGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func stageGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.StageGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func tableGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.TableGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func viewGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ViewGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func materializedViewGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.MaterializedViewGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func resourceMonitorGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ResourceMonitorGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func integrationGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.IntegrationGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func accountGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.AccountGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func managedAccount(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ManagedAccount().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func networkPolicy(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.NetworkPolicy().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func pipe(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Pipe().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func resourceMonitor(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ResourceMonitor().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func sequence(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Sequence().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func share(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Share().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func stage(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Stage().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func stream(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Stream().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func tag(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Tag().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func roleGrants(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.RoleGrants().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func userOwnershipGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.UserOwnershipGrant().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func roleOwnershipGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.RoleOwnershipGrant().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func apiIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.APIIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func samlIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.SAMLIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func scimIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.SCIMIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func oauthIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.OAuthIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func externalFunction(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ExternalFunction().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func procedure(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Procedure().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func storageIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.StorageIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func notificationIntegration(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.NotificationIntegration().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func table(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Table().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func externalTable(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ExternalTable().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func user(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.User().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func view(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.View().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func materializedView(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.MaterializedView().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func warehouse(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Warehouse().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func externalTableGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.ExternalTableGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func fileFormatGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.FileFormatGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func sequenceGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.SequenceGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func streamGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.StreamGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func maskingPolicyGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.MaskingPolicyGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func pipeGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.PipeGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func taskGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.TaskGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func rowAccessPolicy(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.RowAccessPolicy().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func rowAccessPolicyGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.RowAccessPolicyGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func function(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.Function().Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - -func tagGrant(t *testing.T, id string, params map[string]interface{}) *schema.ResourceData { - t.Helper() - r := require.New(t) - d := schema.TestResourceDataRaw(t, resources.TagGrant().Resource.Schema, params) - r.NotNil(d) - d.SetId(id) - return d -} - // queriedAccountRolePrivilegesEqualTo will check if all the privileges specified in the argument are granted in Snowflake. func queriedPrivilegesEqualTo(query func(client *sdk.Client, ctx context.Context) ([]sdk.Grant, error), privileges ...string) func(s *terraform.State) error { return func(s *terraform.State) error { @@ -554,21 +123,3 @@ func updateAccountParameter(t *testing.T, client *sdk.Client, parameter sdk.Acco require.NoError(t, err) } } - -type planCheck func(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) - -func (fn planCheck) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) { - fn(ctx, req, resp) -} - -func expectsCreatePlan(resourceAddress string) planCheck { - return func(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) { - for _, rc := range req.Plan.ResourceChanges { - if rc.Address == resourceAddress && rc.Change != nil && slices.Contains(rc.Change.Actions, tfjson.ActionCreate) { - return - } - } - - resp.Error = fmt.Errorf("expected plan to contain create request for %s", resourceAddress) - } -} diff --git a/pkg/resources/user.go b/pkg/resources/user.go index ae5d53534f..fd539f0188 100644 --- a/pkg/resources/user.go +++ b/pkg/resources/user.go @@ -410,7 +410,9 @@ func DeleteUser(d *schema.ResourceData, meta interface{}) error { ctx := context.Background() objectIdentifier := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier) - err := client.Users.Drop(ctx, objectIdentifier) + err := client.Users.Drop(ctx, objectIdentifier, &sdk.DropUserOptions{ + IfExists: sdk.Bool(true), + }) if err != nil { return err } diff --git a/pkg/resources/user_acceptance_test.go b/pkg/resources/user_acceptance_test.go index 31d08cbef3..b02f09e4ec 100644 --- a/pkg/resources/user_acceptance_test.go +++ b/pkg/resources/user_acceptance_test.go @@ -1,7 +1,6 @@ package resources_test import ( - "context" "errors" "fmt" "log" @@ -157,7 +156,7 @@ resource "snowflake_user" "test" { }, }, { - PreConfig: removeUserOutsideOfTerraform(t, userName), + PreConfig: acc.TestClient().User.DropUserFunc(t, userName), Config: config, ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ @@ -180,17 +179,6 @@ resource "snowflake_user" "test" { }) } -func removeUserOutsideOfTerraform(t *testing.T, name sdk.AccountObjectIdentifier) func() { - t.Helper() - return func() { - client := acc.Client(t) - ctx := context.Background() - if err := client.Users.Drop(ctx, name); err != nil { - t.Fatalf("failed to drop user: %s", name.FullyQualifiedName()) - } - } -} - func uConfig(prefix, key1, key2, comment string) string { s := ` resource "snowflake_user" "w" { diff --git a/pkg/resources/view_acceptance_test.go b/pkg/resources/view_acceptance_test.go index 4aabc9a3cf..df4ba16dd0 100644 --- a/pkg/resources/view_acceptance_test.go +++ b/pkg/resources/view_acceptance_test.go @@ -446,7 +446,8 @@ func TestAcc_View_Issue2640(t *testing.T) { // try to import secure view without being its owner (proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2640) { PreConfig: func() { - t.Cleanup(createAccountRoleOutsideTerraform(t, roleName)) + _, roleCleanup := acc.TestClient().Role.CreateRoleWithName(t, roleName) + t.Cleanup(roleCleanup) alterViewOwnershipExternally(t, viewName, roleName) }, ResourceName: "snowflake_view.test", diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index a8b8e7c9a8..68e9b86640 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -1,7 +1,6 @@ package resources_test import ( - "context" "fmt" "strings" "testing" @@ -9,12 +8,10 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/require" ) func TestAcc_Warehouse(t *testing.T) { @@ -85,7 +82,7 @@ func TestAcc_Warehouse(t *testing.T) { }, // CHANGE max_concurrency_level EXTERNALLY (proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2318) { - PreConfig: func() { alterWarehouseMaxConcurrencyLevelExternally(t, prefix2, 10) }, + PreConfig: func() { acc.TestClient().Warehouse.UpdateMaxConcurrencyLevel(t, prefix2, 10) }, ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{plancheck.ExpectNonEmptyPlan()}, }, @@ -185,13 +182,3 @@ resource "snowflake_warehouse" "w2" { ` return fmt.Sprintf(s, prefix, prefix) } - -func alterWarehouseMaxConcurrencyLevelExternally(t *testing.T, warehouseId string, level int) { - t.Helper() - - client := acc.Client(t) - ctx := context.Background() - - err := client.Warehouses.Alter(ctx, sdk.NewAccountObjectIdentifier(warehouseId), &sdk.AlterWarehouseOptions{Set: &sdk.WarehouseSet{MaxConcurrencyLevel: sdk.Int(level)}}) - require.NoError(t, err) -} diff --git a/pkg/sdk/testint/accounts_integration_test.go b/pkg/sdk/testint/accounts_integration_test.go index c89da51407..d177dc53c0 100644 --- a/pkg/sdk/testint/accounts_integration_test.go +++ b/pkg/sdk/testint/accounts_integration_test.go @@ -18,13 +18,11 @@ import ( func TestInt_AccountShow(t *testing.T) { client := testClient(t) ctx := testContext(t) - ok, err := client.ContextFunctions.IsRoleInSession(ctx, sdk.NewAccountObjectIdentifier("ORGADMIN")) - require.NoError(t, err) + ok := testClientHelper().Context.IsRoleInSession(t, sdk.NewAccountObjectIdentifier("ORGADMIN")) if !ok { t.Skip("ORGADMIN role is not in current session") } - currentAccount, err := client.ContextFunctions.CurrentAccount(ctx) - require.NoError(t, err) + currentAccount := testClientHelper().Context.CurrentAccount(t) opts := &sdk.ShowAccountOptions{ Like: &sdk.Like{ Pattern: sdk.String(currentAccount), @@ -40,28 +38,24 @@ func TestInt_AccountShow(t *testing.T) { func TestInt_AccountShowByID(t *testing.T) { client := testClient(t) ctx := testContext(t) - ok, err := client.ContextFunctions.IsRoleInSession(ctx, sdk.NewAccountObjectIdentifier("ORGADMIN")) - require.NoError(t, err) + ok := testClientHelper().Context.IsRoleInSession(t, sdk.NewAccountObjectIdentifier("ORGADMIN")) if !ok { t.Skip("ORGADMIN role is not in current session") } - require.NoError(t, err) - _, err = client.Accounts.ShowByID(ctx, sdk.NewAccountObjectIdentifier("NOT_EXISTING_ACCOUNT")) + _, err := client.Accounts.ShowByID(ctx, sdk.NewAccountObjectIdentifier("NOT_EXISTING_ACCOUNT")) require.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) } func TestInt_AccountCreate(t *testing.T) { client := testClient(t) ctx := testContext(t) - ok, err := client.ContextFunctions.IsRoleInSession(ctx, sdk.NewAccountObjectIdentifier("ORGADMIN")) - require.NoError(t, err) + ok := testClientHelper().Context.IsRoleInSession(t, sdk.NewAccountObjectIdentifier("ORGADMIN")) if !ok { t.Skip("ORGADMIN role is not in current session") } t.Run("complete case", func(t *testing.T) { accountID := sdk.NewAccountObjectIdentifier("TF_" + strings.ToUpper(gofakeit.Fruit()) + "_" + fmt.Sprintf("%d", random.IntRange(100, 999))) - region, err := client.ContextFunctions.CurrentRegion(ctx) - require.NoError(t, err) + region := testClientHelper().Context.CurrentRegion(t) opts := &sdk.CreateAccountOptions{ AdminName: "someadmin", @@ -74,7 +68,7 @@ func TestInt_AccountCreate(t *testing.T) { Comment: sdk.String("Please delete me!"), Region: sdk.String(region), } - err = client.Accounts.Create(ctx, accountID, opts) + err := client.Accounts.Create(ctx, accountID, opts) require.NoError(t, err) var account *sdk.Account @@ -164,8 +158,7 @@ func TestInt_AccountCreate(t *testing.T) { func TestInt_AccountAlter(t *testing.T) { client := testClient(t) ctx := testContext(t) - ok, err := client.ContextFunctions.IsRoleInSession(ctx, sdk.NewAccountObjectIdentifier("ACCOUNTADMIN")) - require.NoError(t, err) + ok := testClientHelper().Context.IsRoleInSession(t, sdk.NewAccountObjectIdentifier("ACCOUNTADMIN")) if !ok { t.Skip("ACCOUNTADMIN role is not in current session") } @@ -229,7 +222,7 @@ func TestInt_AccountAlter(t *testing.T) { ResourceMonitor: resourceMonitorTest.ID(), }, } - err = client.Accounts.Alter(ctx, opts) + err := client.Accounts.Alter(ctx, opts) require.NoError(t, err) }) @@ -241,7 +234,7 @@ func TestInt_AccountAlter(t *testing.T) { PasswordPolicy: passwordPolicyTest.ID(), }, } - err = client.Accounts.Alter(ctx, opts) + err := client.Accounts.Alter(ctx, opts) require.NoError(t, err) // now unset @@ -262,7 +255,7 @@ func TestInt_AccountAlter(t *testing.T) { SessionPolicy: sessionPolicyTest.ID(), }, } - err = client.Accounts.Alter(ctx, opts) + err := client.Accounts.Alter(ctx, opts) require.NoError(t, err) // now unset @@ -293,10 +286,9 @@ func TestInt_AccountAlter(t *testing.T) { }, }, } - err = client.Accounts.Alter(ctx, opts) - require.NoError(t, err) - currentAccount, err := client.ContextFunctions.CurrentAccount(ctx) + err := client.Accounts.Alter(ctx, opts) require.NoError(t, err) + currentAccount := testClientHelper().Context.CurrentAccount(t) tagValue, err := client.SystemFunctions.GetTag(ctx, tagTest1.ID(), sdk.NewAccountObjectIdentifier(currentAccount), sdk.ObjectTypeAccount) require.NoError(t, err) assert.Equal(t, "abc", tagValue) diff --git a/pkg/sdk/testint/alerts_integration_test.go b/pkg/sdk/testint/alerts_integration_test.go index a8f91a1a23..1735bc5322 100644 --- a/pkg/sdk/testint/alerts_integration_test.go +++ b/pkg/sdk/testint/alerts_integration_test.go @@ -406,7 +406,7 @@ func TestInt_AlertsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/context_functions_integration_test.go b/pkg/sdk/testint/context_functions_integration_test.go index 5f7044e1d1..e280c3fe86 100644 --- a/pkg/sdk/testint/context_functions_integration_test.go +++ b/pkg/sdk/testint/context_functions_integration_test.go @@ -82,7 +82,7 @@ func TestInt_CurrentSchema(t *testing.T) { // new database and schema created on purpose databaseTest, databaseCleanup := testClientHelper().Database.CreateDatabase(t) t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchema(t, databaseTest) + schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchemaInDatabase(t, databaseTest.ID()) t.Cleanup(schemaCleanup) err := client.Sessions.UseSchema(ctx, schemaTest.ID()) require.NoError(t, err) @@ -96,7 +96,7 @@ func TestInt_CurrentWarehouse(t *testing.T) { ctx := testContext(t) // new warehouse created on purpose - warehouseTest, warehouseCleanup := createWarehouse(t, client) + warehouseTest, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) err := client.Sessions.UseWarehouse(ctx, warehouseTest.ID()) require.NoError(t, err) @@ -122,7 +122,7 @@ func TestInt_RolesUse(t *testing.T) { currentRoleID := sdk.NewAccountObjectIdentifier(currentRole) require.NoError(t, err) - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) require.NotEqual(t, currentRole, role.Name) @@ -147,7 +147,7 @@ func TestInt_RolesUseSecondaryRoles(t *testing.T) { currentRole, err := client.ContextFunctions.CurrentRole(ctx) require.NoError(t, err) - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) require.NotEqual(t, currentRole, role.Name) diff --git a/pkg/sdk/testint/conversion_functions_integration_test.go b/pkg/sdk/testint/conversion_functions_integration_test.go index 303e25e4bb..b9b37441e8 100644 --- a/pkg/sdk/testint/conversion_functions_integration_test.go +++ b/pkg/sdk/testint/conversion_functions_integration_test.go @@ -37,7 +37,7 @@ func TestInt_ToTimestampLTZ(t *testing.T) { require.NoError(t, err) }) // new warehouse created on purpose - warehouseTest, warehouseCleanup := createWarehouse(t, client) + warehouseTest, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) err = client.Sessions.UseWarehouse(ctx, warehouseTest.ID()) require.NoError(t, err) @@ -76,7 +76,7 @@ func TestInt_ToTimestampNTZ(t *testing.T) { require.NoError(t, err) }) // new warehouse created on purpose - warehouseTest, warehouseCleanup := createWarehouse(t, client) + warehouseTest, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) err = client.Sessions.UseWarehouse(ctx, warehouseTest.ID()) require.NoError(t, err) diff --git a/pkg/sdk/testint/database_role_integration_test.go b/pkg/sdk/testint/database_role_integration_test.go index 1f767a306f..17c969ce42 100644 --- a/pkg/sdk/testint/database_role_integration_test.go +++ b/pkg/sdk/testint/database_role_integration_test.go @@ -237,7 +237,7 @@ func TestInt_DatabaseRoles(t *testing.T) { role := createDatabaseRole(t) roleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, role.Name) - accountRole, accountRoleCleanup := createRole(t, client) + accountRole, accountRoleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(accountRoleCleanup) grantRequest := sdk.NewGrantDatabaseRoleRequest(roleId).WithAccountRole(accountRole.ID()) diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 07ad1f270a..b78d48f27d 100644 --- a/pkg/sdk/testint/databases_integration_test.go +++ b/pkg/sdk/testint/databases_integration_test.go @@ -56,7 +56,7 @@ func TestInt_DatabasesCreate(t *testing.T) { // new database and schema created on purpose databaseTest, databaseCleanup := testClientHelper().Database.CreateDatabase(t) t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchema(t, databaseTest) + schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchemaInDatabase(t, databaseTest.ID()) t.Cleanup(schemaCleanup) tagTest, tagCleanup := createTag(t, client, databaseTest, schemaTest) t.Cleanup(tagCleanup) @@ -198,7 +198,7 @@ func TestInt_DatabasesDescribe(t *testing.T) { // new database and schema created on purpose databaseTest, databaseCleanup := testClientHelper().Database.CreateDatabase(t) t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchema(t, databaseTest) + schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchemaInDatabase(t, databaseTest.ID()) t.Cleanup(schemaCleanup) ctx := testContext(t) databaseDetails, err := client.Databases.Describe(ctx, databaseTest.ID()) diff --git a/pkg/sdk/testint/dynamic_table_integration_test.go b/pkg/sdk/testint/dynamic_table_integration_test.go index 295256e020..e1cf60180a 100644 --- a/pkg/sdk/testint/dynamic_table_integration_test.go +++ b/pkg/sdk/testint/dynamic_table_integration_test.go @@ -14,7 +14,7 @@ import ( func TestInt_DynamicTableCreateAndDrop(t *testing.T) { client := testClient(t) - tableTest, tableCleanup := createTable(t, client, testDb(t), testSchema(t)) + tableTest, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) ctx := context.Background() @@ -209,7 +209,7 @@ func TestInt_DynamicTablesShowByID(t *testing.T) { createDynamicTableHandle := func(t *testing.T, id sdk.SchemaObjectIdentifier) { t.Helper() - tableTest, tableCleanup := createTable(t, client, databaseTest, schemaTest) + tableTest, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) targetLag := sdk.TargetLag{ MaximumDuration: sdk.String("2 minutes"), @@ -221,7 +221,7 @@ func TestInt_DynamicTablesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/event_tables_integration_test.go b/pkg/sdk/testint/event_tables_integration_test.go index 862ba950d3..19562a5b4b 100644 --- a/pkg/sdk/testint/event_tables_integration_test.go +++ b/pkg/sdk/testint/event_tables_integration_test.go @@ -220,7 +220,7 @@ func TestInt_EventTables(t *testing.T) { rowAccessPolicy2Id, rowAccessPolicy2Cleanup := createRowAccessPolicy(t, client, schemaTest) t.Cleanup(rowAccessPolicy2Cleanup) - table, tableCleanup := createTable(t, client, databaseTest, schemaTest) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) id := sdk.NewSchemaObjectIdentifier(table.DatabaseName, table.SchemaName, table.Name) @@ -299,7 +299,7 @@ func TestInt_EventTableShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/external_tables_integration_test.go b/pkg/sdk/testint/external_tables_integration_test.go index 3e1da02a62..ff4a5c31c7 100644 --- a/pkg/sdk/testint/external_tables_integration_test.go +++ b/pkg/sdk/testint/external_tables_integration_test.go @@ -447,7 +447,7 @@ func TestInt_ExternalTablesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/file_format_integration_test.go b/pkg/sdk/testint/file_format_integration_test.go index a55166ff44..2dc2c247c4 100644 --- a/pkg/sdk/testint/file_format_integration_test.go +++ b/pkg/sdk/testint/file_format_integration_test.go @@ -506,7 +506,7 @@ func TestInt_FileFormatsShowById(t *testing.T) { // new database and schema created on purpose databaseTest2, cleanupDatabase2 := testClientHelper().Database.CreateDatabase(t) t.Cleanup(cleanupDatabase2) - schemaTest2, cleanupSchema2 := testClientHelper().Schema.CreateSchema(t, databaseTest2) + schemaTest2, cleanupSchema2 := testClientHelper().Schema.CreateSchemaInDatabase(t, databaseTest2.ID()) t.Cleanup(cleanupSchema2) t.Run("show format in different schema", func(t *testing.T) { @@ -549,7 +549,7 @@ func TestInt_FileFormatsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index f334a4344c..ea72d19de1 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -492,7 +492,7 @@ func TestInt_FunctionsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/grants_integration_test.go b/pkg/sdk/testint/grants_integration_test.go index b7801eb92a..563630d756 100644 --- a/pkg/sdk/testint/grants_integration_test.go +++ b/pkg/sdk/testint/grants_integration_test.go @@ -42,7 +42,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { } t.Run("on account", func(t *testing.T) { - roleTest, roleCleanup := createRole(t, client) + roleTest, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) privileges := &sdk.AccountRoleGrantPrivileges{ GlobalPrivileges: []sdk.GlobalPrivilege{sdk.GlobalPrivilegeMonitorUsage, sdk.GlobalPrivilegeApplyTag}, @@ -87,7 +87,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("on account object", func(t *testing.T) { - roleTest, roleCleanup := createRole(t, client) + roleTest, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) resourceMonitorTest, resourceMonitorCleanup := createResourceMonitor(t, client) t.Cleanup(resourceMonitorCleanup) @@ -123,7 +123,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("on schema", func(t *testing.T) { - roleTest, roleCleanup := createRole(t, client) + roleTest, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) privileges := &sdk.AccountRoleGrantPrivileges{ SchemaPrivileges: []sdk.SchemaPrivilege{sdk.SchemaPrivilegeCreateAlert}, @@ -157,9 +157,9 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("on schema object", func(t *testing.T) { - roleTest, roleCleanup := createRole(t, client) + roleTest, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) - tableTest, tableTestCleanup := createTable(t, client, testDb(t), testSchema(t)) + tableTest, tableTestCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableTestCleanup) privileges := &sdk.AccountRoleGrantPrivileges{ SchemaObjectPrivileges: []sdk.SchemaObjectPrivilege{sdk.SchemaObjectPrivilegeSelect}, @@ -197,7 +197,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("on future schema object", func(t *testing.T) { - roleTest, roleCleanup := createRole(t, client) + roleTest, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) privileges := &sdk.AccountRoleGrantPrivileges{ SchemaObjectPrivileges: []sdk.SchemaObjectPrivilege{sdk.SchemaObjectPrivilegeSelect}, @@ -235,10 +235,10 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("grant and revoke on all pipes", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -250,7 +250,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { secondPipe, secondPipeCleanup := createPipe(t, client, testDb(t), schema, random.AlphaN(20), createPipeCopyStatement(t, table, stage)) t.Cleanup(secondPipeCleanup) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) err := client.Grants.GrantPrivilegesToAccountRole( @@ -295,10 +295,10 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { }) t.Run("grant and revoke on all pipes with multiple errors", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -310,7 +310,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { _, secondPipeCleanup := createPipe(t, client, testDb(t), schema, random.AlphaN(20), createPipeCopyStatement(t, table, stage)) t.Cleanup(secondPipeCleanup) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) err := client.Grants.GrantPrivilegesToAccountRole( @@ -388,7 +388,9 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { } t.Run("on database", func(t *testing.T) { - databaseRole, _ := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) + t.Cleanup(databaseRoleCleanup) + databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) privileges := &sdk.DatabaseRoleGrantPrivileges{ @@ -434,7 +436,9 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { }) t.Run("on schema", func(t *testing.T) { - databaseRole, _ := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) + t.Cleanup(databaseRoleCleanup) + databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) privileges := &sdk.DatabaseRoleGrantPrivileges{ @@ -482,9 +486,11 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { }) t.Run("on schema object", func(t *testing.T) { - databaseRole, _ := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) + t.Cleanup(databaseRoleCleanup) + databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) - table, _ := createTable(t, client, testDb(t), testSchema(t)) + table, _ := testClientHelper().Table.CreateTable(t) privileges := &sdk.DatabaseRoleGrantPrivileges{ SchemaObjectPrivileges: []sdk.SchemaObjectPrivilege{sdk.SchemaObjectPrivilegeSelect}, @@ -535,7 +541,9 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { }) t.Run("on future schema object", func(t *testing.T) { - databaseRole, _ := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) + t.Cleanup(databaseRoleCleanup) + databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) privileges := &sdk.DatabaseRoleGrantPrivileges{ @@ -580,10 +588,10 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { }) t.Run("grant and revoke on all pipes", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -595,7 +603,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { secondPipe, secondPipeCleanup := createPipe(t, client, testDb(t), schema, random.StringN(20), createPipeCopyStatement(t, table, stage)) t.Cleanup(secondPipeCleanup) - role, roleCleanup := createDatabaseRole(t, client, testDb(t)) + role, roleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) t.Cleanup(roleCleanup) err := client.Grants.GrantPrivilegesToDatabaseRole( @@ -640,10 +648,10 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { }) t.Run("grant and revoke on all pipes with multiple errors", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -655,7 +663,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { _, secondPipeCleanup := createPipe(t, client, testDb(t), schema, random.AlphaN(20), createPipeCopyStatement(t, table, stage)) t.Cleanup(secondPipeCleanup) - role, roleCleanup := createDatabaseRole(t, client, testDb(t)) + role, roleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) t.Cleanup(roleCleanup) err := client.Grants.GrantPrivilegesToDatabaseRole( @@ -736,7 +744,7 @@ func TestInt_GrantPrivilegeToShare(t *testing.T) { assert.NoError(t, err) }) - table, tableCleanup := createTable(t, client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) err = client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeSelect}, &sdk.ShareGrantOn{ @@ -816,7 +824,7 @@ func TestInt_GrantOwnership(t *testing.T) { client := testClient(t) ctx := testContext(t) - table, tableCleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, random.AlphaN(20))) @@ -1014,9 +1022,11 @@ func TestInt_GrantOwnership(t *testing.T) { } t.Run("on schema object to database role", func(t *testing.T) { - databaseRole, _ := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) + t.Cleanup(databaseRoleCleanup) + databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) - table, _ := createTable(t, client, testDb(t), testSchema(t)) + table, _ := testClientHelper().Table.CreateTable(t) on := sdk.OwnershipGrantOn{ Object: &sdk.Object{ @@ -1052,7 +1062,7 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on future schema object in database to role", func(t *testing.T) { - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) roleId := role.ID() @@ -1088,7 +1098,7 @@ func TestInt_GrantOwnership(t *testing.T) { // role is deliberately created after warehouse, so that cleanup is done in reverse // because after ownership grant we lose privilege to drop object // with first dropping the role, we reacquire rights to do it - a little hacky trick - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) roleId := role.ID() @@ -1130,7 +1140,7 @@ func TestInt_GrantOwnership(t *testing.T) { require.NoError(t, err) require.Equal(t, sdk.RunningPipeExecutionState, pipeExecutionState) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) err = client.Grants.GrantOwnership( @@ -1144,8 +1154,7 @@ func TestInt_GrantOwnership(t *testing.T) { require.NoError(t, err) checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(pipe), role.ID().Name()) - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + currentRole := testClientHelper().Context.CurrentRole(t) grantOwnershipToRole(t, currentRole, ownershipGrantOnPipe(pipe), nil) checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(pipe), currentRole) @@ -1156,10 +1165,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on pipe - with operate and monitor privileges granted", func(t *testing.T) { - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) - pipeRole, pipeRoleCleanup := createRoleGrantedToCurrentUser(t, client) + pipeRole, pipeRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(pipeRoleCleanup) // Role needs usage on the database and schema to later be able to remove pipe in the cleanup @@ -1167,15 +1176,14 @@ func TestInt_GrantOwnership(t *testing.T) { // grantPipeRole grants the necessary privileges to a role to be able to create pipe grantPipeRole(t, pipeRole, table, stage) - previousRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + previousRole := testClientHelper().Context.CurrentRole(t) // Use a previously prepared role to create a pipe and grant MONITOR + OPERATE to the previously used role (ACCOUNTADMIN). - usePreviousRole := useRole(t, client, pipeRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, pipeRole.Name) pipe, pipeCleanup := createPipe(t, client, testDb(t), testSchema(t), random.AlphaN(20), copyStatement) t.Cleanup(func() { - usePreviousRole = useRole(t, client, role.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, role.Name) defer usePreviousRole() pipeCleanup() }) @@ -1185,7 +1193,7 @@ func TestInt_GrantOwnership(t *testing.T) { usePreviousRole() - err = client.Pipes.Alter(ctx, pipe.ID(), &sdk.AlterPipeOptions{ + err := client.Pipes.Alter(ctx, pipe.ID(), &sdk.AlterPipeOptions{ Set: &sdk.PipeSet{ PipeExecutionPaused: sdk.Bool(false), }, @@ -1219,10 +1227,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on pipe - with operate privilege granted and copy current grants option", func(t *testing.T) { - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) - pipeRole, pipeRoleCleanup := createRoleGrantedToCurrentUser(t, client) + pipeRole, pipeRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(pipeRoleCleanup) // Role needs usage on the database and schema to later be able to remove pipe in the cleanup @@ -1230,15 +1238,14 @@ func TestInt_GrantOwnership(t *testing.T) { // grantPipeRole grants the necessary privileges to a role to be able to create pipe grantPipeRole(t, pipeRole, table, stage) - previousRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + previousRole := testClientHelper().Context.CurrentRole(t) // Use a previously prepared role to create a pipe and grant MONITOR + OPERATE to the previously used role (ACCOUNTADMIN). - usePreviousRole := useRole(t, client, pipeRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, pipeRole.Name) pipe, pipeCleanup := createPipe(t, client, testDb(t), testSchema(t), random.AlphaN(20), copyStatement) t.Cleanup(func() { - usePreviousRole = useRole(t, client, role.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, role.Name) defer usePreviousRole() pipeCleanup() }) @@ -1248,7 +1255,7 @@ func TestInt_GrantOwnership(t *testing.T) { usePreviousRole() - err = client.Pipes.Alter(ctx, pipe.ID(), &sdk.AlterPipeOptions{ + err := client.Pipes.Alter(ctx, pipe.ID(), &sdk.AlterPipeOptions{ Set: &sdk.PipeSet{ PipeExecutionPaused: sdk.Bool(false), }, @@ -1282,10 +1289,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on pipe - with neither ownership nor operate", func(t *testing.T) { - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) - pipeRole, pipeRoleCleanup := createRoleGrantedToCurrentUser(t, client) + pipeRole, pipeRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(pipeRoleCleanup) // Role needs usage on the database and schema to later be able to remove pipe in the cleanup @@ -1294,11 +1301,11 @@ func TestInt_GrantOwnership(t *testing.T) { grantPipeRole(t, pipeRole, table, stage) // Use a previously prepared role to create a pipe and grant MONITOR + OPERATE to the previously used role (ACCOUNTADMIN). - usePreviousRole := useRole(t, client, pipeRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, pipeRole.Name) pipe, pipeCleanup := createPipe(t, client, testDb(t), testSchema(t), random.AlphaN(20), copyStatement) t.Cleanup(func() { - usePreviousRole = useRole(t, client, pipeRole.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, pipeRole.Name) defer usePreviousRole() pipeCleanup() }) @@ -1328,10 +1335,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on pipe - with neither ownership nor operate on paused pipe", func(t *testing.T) { - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) - pipeRole, pipeRoleCleanup := createRoleGrantedToCurrentUser(t, client) + pipeRole, pipeRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(pipeRoleCleanup) // Role needs usage on the database and schema to later be able to remove pipe in the cleanup @@ -1340,11 +1347,11 @@ func TestInt_GrantOwnership(t *testing.T) { grantPipeRole(t, pipeRole, table, stage) // Use a previously prepared role to create a pipe and grant MONITOR + OPERATE to the previously used role (ACCOUNTADMIN). - usePreviousRole := useRole(t, client, pipeRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, pipeRole.Name) pipe, pipeCleanup := createPipe(t, client, testDb(t), testSchema(t), random.AlphaN(20), copyStatement) t.Cleanup(func() { - usePreviousRole = useRole(t, client, role.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, role.Name) defer usePreviousRole() pipeCleanup() }) @@ -1389,7 +1396,7 @@ func TestInt_GrantOwnership(t *testing.T) { require.NoError(t, err) require.Equal(t, sdk.RunningPipeExecutionState, secondPipeExecutionState) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) onAllPipesInSchema := sdk.OwnershipGrantOn{ @@ -1411,8 +1418,7 @@ func TestInt_GrantOwnership(t *testing.T) { checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(pipe), role.ID().Name()) checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(secondPipe), role.ID().Name()) - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + currentRole := testClientHelper().Context.CurrentRole(t) grantOwnershipToRole(t, currentRole, onAllPipesInSchema, nil) checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(pipe), currentRole) checkOwnershipOnObjectToRole(t, ownershipGrantOnPipe(secondPipe), currentRole) @@ -1433,7 +1439,7 @@ func TestInt_GrantOwnership(t *testing.T) { err := client.Tasks.Alter(ctx, sdk.NewAlterTaskRequest(task.ID()).WithResume(sdk.Bool(true))) require.NoError(t, err) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) task, err = client.Tasks.ShowByID(ctx, task.ID()) @@ -1457,10 +1463,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on task - without ownership and operate", func(t *testing.T) { - taskRole, taskRoleCleanup := createRoleGrantedToCurrentUser(t, client) + taskRole, taskRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(taskRoleCleanup) - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) // Role needs usage on the database and schema to later be able to remove task in the cleanup @@ -1470,13 +1476,13 @@ func TestInt_GrantOwnership(t *testing.T) { grantTaskRole(t, taskRole.ID()) // Use a previously prepared role to create a task - usePreviousRole := useRole(t, client, taskRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, taskRole.Name) taskId := sdk.NewSchemaObjectIdentifier(TestDatabaseName, TestSchemaName, random.AlphaN(20)) withWarehouseReq := sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(testWarehouse(t).ID())) task, taskCleanup := createTaskWithRequest(t, client, sdk.NewCreateTaskRequest(taskId, "SELECT CURRENT_TIMESTAMP").WithWarehouse(withWarehouseReq).WithSchedule(sdk.String("60 minutes"))) t.Cleanup(func() { - usePreviousRole := useRole(t, client, taskRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, taskRole.Name) defer usePreviousRole() taskCleanup() }) @@ -1502,10 +1508,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on task - with operate and execute task", func(t *testing.T) { - taskRole, taskRoleCleanup := createRoleGrantedToCurrentUser(t, client) + taskRole, taskRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(taskRoleCleanup) - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) // Role needs usage on the database and schema to later be able to remove task in the cleanup @@ -1514,20 +1520,18 @@ func TestInt_GrantOwnership(t *testing.T) { // grantTaskRole grants the necessary privileges to a role to be able to create task grantTaskRole(t, taskRole.ID()) - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) - + currentRole := testClientHelper().Context.CurrentRole(t) grantTaskRole(t, sdk.NewAccountObjectIdentifier(currentRole)) // Use a previously prepared role to create a task - usePreviousRole := useRole(t, client, taskRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, taskRole.Name) taskId := sdk.NewSchemaObjectIdentifier(TestDatabaseName, TestSchemaName, random.AlphaN(20)) withWarehouseReq := sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(testWarehouse(t).ID())) task, taskCleanup := createTaskWithRequest(t, client, sdk.NewCreateTaskRequest(taskId, "SELECT CURRENT_TIMESTAMP").WithWarehouse(withWarehouseReq).WithSchedule(sdk.String("60 minutes"))) t.Cleanup(taskCleanup) - err = client.Grants.GrantPrivilegesToAccountRole( + err := client.Grants.GrantPrivilegesToAccountRole( ctx, &sdk.AccountRoleGrantPrivileges{ SchemaObjectPrivileges: []sdk.SchemaObjectPrivilege{sdk.SchemaObjectPrivilegeOperate}, @@ -1551,9 +1555,7 @@ func TestInt_GrantOwnership(t *testing.T) { usePreviousRole() t.Cleanup(func() { - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) - + currentRole := testClientHelper().Context.CurrentRole(t) grantOwnershipToRole(t, currentRole, ownershipGrantOnTask(task), sdk.Pointer(sdk.Revoke)) }) @@ -1594,7 +1596,7 @@ func TestInt_GrantOwnership(t *testing.T) { err = client.Tasks.Alter(ctx, sdk.NewAlterTaskRequest(secondTask.ID()).WithResume(sdk.Bool(true))) require.NoError(t, err) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) currentTask, err := client.Tasks.ShowByID(ctx, task.ID()) @@ -1634,10 +1636,10 @@ func TestInt_GrantOwnership(t *testing.T) { }) t.Run("on all tasks - with operate", func(t *testing.T) { - taskRole, taskRoleCleanup := createRoleGrantedToCurrentUser(t, client) + taskRole, taskRoleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(taskRoleCleanup) - role, roleCleanup := createRoleGrantedToCurrentUser(t, client) + role, roleCleanup := testClientHelper().Role.CreateRoleGrantedToCurrentUser(t) t.Cleanup(roleCleanup) // Role needs usage on the database and schema to later be able to remove task in the cleanup @@ -1646,14 +1648,13 @@ func TestInt_GrantOwnership(t *testing.T) { // grantTaskRole grants the necessary privileges to a role to be able to create task grantTaskRole(t, taskRole.ID()) - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + currentRole := testClientHelper().Context.CurrentRole(t) grantTaskRole(t, role.ID()) grantTaskRole(t, sdk.NewAccountObjectIdentifier(currentRole)) // Use a previously prepared role to create a task - usePreviousRole := useRole(t, client, taskRole.Name) + usePreviousRole := testClientHelper().Role.UseRole(t, taskRole.Name) taskId := sdk.NewSchemaObjectIdentifier(TestDatabaseName, TestSchemaName, random.AlphaN(20)) withWarehouseReq := sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(testWarehouse(t).ID())) @@ -1664,7 +1665,7 @@ func TestInt_GrantOwnership(t *testing.T) { secondTask, secondTaskCleanup := createTaskWithRequest(t, client, sdk.NewCreateTaskRequest(secondTaskId, "SELECT CURRENT_TIMESTAMP").WithWarehouse(withWarehouseReq).WithAfter([]sdk.SchemaObjectIdentifier{task.ID()})) t.Cleanup(secondTaskCleanup) - err = client.Grants.GrantPrivilegesToAccountRole( + err := client.Grants.GrantPrivilegesToAccountRole( ctx, &sdk.AccountRoleGrantPrivileges{ SchemaObjectPrivileges: []sdk.SchemaObjectPrivilege{sdk.SchemaObjectPrivilegeOperate}, @@ -1709,16 +1710,14 @@ func TestInt_GrantOwnership(t *testing.T) { require.NoError(t, err) t.Cleanup(func() { - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) - - usePreviousRole := useRole(t, client, role.Name) + currentRole := testClientHelper().Context.CurrentRole(t) + usePreviousRole := testClientHelper().Role.UseRole(t, role.Name) grantOwnershipToRole(t, currentRole, ownershipGrantOnTask(task), sdk.Pointer(sdk.Revoke)) grantOwnershipToRole(t, currentRole, ownershipGrantOnTask(secondTask), sdk.Pointer(sdk.Revoke)) usePreviousRole() }) - usePreviousRole = useRole(t, client, taskRole.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, taskRole.Name) currentTask, err := client.Tasks.ShowByID(ctx, task.ID()) require.NoError(t, err) require.Equal(t, sdk.TaskStateStarted, currentTask.State) @@ -1751,7 +1750,7 @@ func TestInt_GrantOwnership(t *testing.T) { checkOwnershipOnObjectToRole(t, ownershipGrantOnTask(task), role.ID().Name()) checkOwnershipOnObjectToRole(t, ownershipGrantOnTask(secondTask), role.ID().Name()) - usePreviousRole = useRole(t, client, role.Name) + usePreviousRole = testClientHelper().Role.UseRole(t, role.Name) currentTask, err = client.Tasks.ShowByID(ctx, task.ID()) require.NoError(t, err) require.Equal(t, sdk.TaskStateSuspended, currentTask.State) diff --git a/pkg/sdk/testint/helpers_test.go b/pkg/sdk/testint/helpers_test.go index d1c88e7cda..15192b782f 100644 --- a/pkg/sdk/testint/helpers_test.go +++ b/pkg/sdk/testint/helpers_test.go @@ -22,6 +22,7 @@ const ( func getAccountIdentifier(t *testing.T, client *sdk.Client) sdk.AccountIdentifier { t.Helper() ctx := context.Background() + // TODO: replace later (incoming clients differ) currentAccountLocator, err := client.ContextFunctions.CurrentAccount(ctx) require.NoError(t, err) replicationAccounts, err := client.ReplicationFunctions.ShowReplicationAccounts(ctx) @@ -34,92 +35,6 @@ func getAccountIdentifier(t *testing.T, client *sdk.Client) sdk.AccountIdentifie return sdk.AccountIdentifier{} } -func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjectIdentifier) func() { - t.Helper() - ctx := context.Background() - err := client.Sessions.UseWarehouse(ctx, warehouseID) - require.NoError(t, err) - return func() { - err = client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID()) - require.NoError(t, err) - } -} - -func createWarehouse(t *testing.T, client *sdk.Client) (*sdk.Warehouse, func()) { - t.Helper() - return createWarehouseWithOptions(t, client, &sdk.CreateWarehouseOptions{}) -} - -func createWarehouseWithOptions(t *testing.T, client *sdk.Client, opts *sdk.CreateWarehouseOptions) (*sdk.Warehouse, func()) { - t.Helper() - name := random.StringRange(8, 28) - id := sdk.NewAccountObjectIdentifier(name) - ctx := context.Background() - err := client.Warehouses.Create(ctx, id, opts) - require.NoError(t, err) - return &sdk.Warehouse{ - Name: name, - }, func() { - err := client.Warehouses.Drop(ctx, id, nil) - require.NoError(t, err) - err = client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID()) - require.NoError(t, err) - } -} - -func createUser(t *testing.T, client *sdk.Client) (*sdk.User, func()) { - t.Helper() - name := random.StringRange(8, 28) - id := sdk.NewAccountObjectIdentifier(name) - return createUserWithOptions(t, client, id, &sdk.CreateUserOptions{}) -} - -func createUserWithName(t *testing.T, client *sdk.Client, name string) (*sdk.User, func()) { - t.Helper() - id := sdk.NewAccountObjectIdentifier(name) - return createUserWithOptions(t, client, id, &sdk.CreateUserOptions{}) -} - -func createUserWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier, opts *sdk.CreateUserOptions) (*sdk.User, func()) { - t.Helper() - ctx := context.Background() - err := client.Users.Create(ctx, id, opts) - require.NoError(t, err) - user, err := client.Users.ShowByID(ctx, id) - require.NoError(t, err) - return user, func() { - err := client.Users.Drop(ctx, id) - require.NoError(t, err) - } -} - -func createTable(t *testing.T, client *sdk.Client, database *sdk.Database, schema *sdk.Schema) (*sdk.Table, func()) { - t.Helper() - columns := []sdk.TableColumnRequest{ - *sdk.NewTableColumnRequest("id", sdk.DataTypeNumber), - } - return createTableWithColumns(t, client, database, schema, columns) -} - -func createTableWithColumns(t *testing.T, client *sdk.Client, database *sdk.Database, schema *sdk.Schema, columns []sdk.TableColumnRequest) (*sdk.Table, func()) { - t.Helper() - name := random.StringRange(8, 28) - id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) - ctx := context.Background() - - dbCreateRequest := sdk.NewCreateTableRequest(id, columns) - err := client.Tables.Create(ctx, dbCreateRequest) - require.NoError(t, err) - - table, err := client.Tables.ShowByID(ctx, id) - require.NoError(t, err) - - return table, func() { - dropErr := client.Tables.Drop(ctx, sdk.NewDropTableRequest(id)) - require.NoError(t, dropErr) - } -} - func createDynamicTable(t *testing.T, client *sdk.Client) (*sdk.DynamicTable, func()) { t.Helper() return createDynamicTableWithOptions(t, client, nil, nil, nil, nil) @@ -129,7 +44,7 @@ func createDynamicTableWithOptions(t *testing.T, client *sdk.Client, warehouse * t.Helper() var warehouseCleanup func() if warehouse == nil { - warehouse, warehouseCleanup = createWarehouse(t, client) + warehouse, warehouseCleanup = testClientHelper().Warehouse.CreateWarehouse(t) } var databaseCleanup func() if database == nil { @@ -137,11 +52,11 @@ func createDynamicTableWithOptions(t *testing.T, client *sdk.Client, warehouse * } var schemaCleanup func() if schema == nil { - schema, schemaCleanup = testClientHelper().Schema.CreateSchema(t, database) + schema, schemaCleanup = testClientHelper().Schema.CreateSchemaInDatabase(t, database.ID()) } var tableCleanup func() if table == nil { - table, tableCleanup = createTable(t, client, database, schema) + table, tableCleanup = testClientHelper().Table.CreateTableInSchema(t, schema.ID()) } name := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, random.String()) targetLag := sdk.TargetLag{ @@ -272,7 +187,7 @@ func createPasswordPolicyWithOptions(t *testing.T, client *sdk.Client, database } var schemaCleanup func() if schema == nil { - schema, schemaCleanup = testClientHelper().Schema.CreateSchema(t, database) + schema, schemaCleanup = testClientHelper().Schema.CreateSchemaInDatabase(t, database.ID()) } name := random.UUID() id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, name) @@ -409,7 +324,7 @@ func createMaskingPolicyWithOptions(t *testing.T, client *sdk.Client, database * } var schemaCleanup func() if schema == nil { - schema, schemaCleanup = testClientHelper().Schema.CreateSchema(t, database) + schema, schemaCleanup = testClientHelper().Schema.CreateSchemaInDatabase(t, database.ID()) } name := random.String() id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, name) @@ -456,11 +371,11 @@ func createAlertWithOptions(t *testing.T, client *sdk.Client, database *sdk.Data } var schemaCleanup func() if schema == nil { - schema, schemaCleanup = testClientHelper().Schema.CreateSchema(t, database) + schema, schemaCleanup = testClientHelper().Schema.CreateSchemaInDatabase(t, database.ID()) } var warehouseCleanup func() if warehouse == nil { - warehouse, warehouseCleanup = createWarehouse(t, client) + warehouse, warehouseCleanup = testClientHelper().Warehouse.CreateWarehouse(t) } name := random.String() @@ -495,86 +410,10 @@ func createAlertWithOptions(t *testing.T, client *sdk.Client, database *sdk.Data } } -func useRole(t *testing.T, client *sdk.Client, roleName string) func() { - t.Helper() - ctx := context.Background() - - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) - - err = client.Sessions.UseRole(ctx, sdk.NewAccountObjectIdentifier(roleName)) - require.NoError(t, err) - - return func() { - err = client.Sessions.UseRole(ctx, sdk.NewAccountObjectIdentifier(currentRole)) - require.NoError(t, err) - } -} - -func createRole(t *testing.T, client *sdk.Client) (*sdk.Role, func()) { - t.Helper() - return createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(sdk.RandomAccountObjectIdentifier())) -} - -func createRoleGrantedToCurrentUser(t *testing.T, client *sdk.Client) (*sdk.Role, func()) { - t.Helper() - role, roleCleanup := createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(sdk.RandomAccountObjectIdentifier())) - - ctx := context.Background() - currentUser, err := client.ContextFunctions.CurrentUser(ctx) - require.NoError(t, err) - - err = client.Roles.Grant(ctx, sdk.NewGrantRoleRequest(role.ID(), sdk.GrantRole{ - User: sdk.Pointer(sdk.NewAccountObjectIdentifier(currentUser)), - })) - require.NoError(t, err) - - return role, roleCleanup -} - -func createRoleWithRequest(t *testing.T, client *sdk.Client, req *sdk.CreateRoleRequest) (*sdk.Role, func()) { - t.Helper() - require.True(t, sdk.ValidObjectIdentifier(req.GetName())) - ctx := context.Background() - err := client.Roles.Create(ctx, req) - require.NoError(t, err) - role, err := client.Roles.ShowByID(ctx, req.GetName()) - require.NoError(t, err) - return role, func() { - err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(req.GetName())) - require.NoError(t, err) - } -} - -func createDatabaseRole(t *testing.T, client *sdk.Client, database *sdk.Database) (*sdk.DatabaseRole, func()) { - t.Helper() - name := random.String() - id := sdk.NewDatabaseObjectIdentifier(database.Name, name) - ctx := context.Background() - - err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) - require.NoError(t, err) - - databaseRole, err := client.DatabaseRoles.ShowByID(ctx, id) - require.NoError(t, err) - - return databaseRole, cleanupDatabaseRoleProvider(t, ctx, client, id) -} - -func cleanupDatabaseRoleProvider(t *testing.T, ctx context.Context, client *sdk.Client, id sdk.DatabaseObjectIdentifier) func() { - t.Helper() - return func() { - err := client.DatabaseRoles.Drop(ctx, sdk.NewDropDatabaseRoleRequest(id)) - require.NoError(t, err) - } -} - func createFailoverGroup(t *testing.T, client *sdk.Client) (*sdk.FailoverGroup, func()) { t.Helper() objectTypes := []sdk.PluralObjectType{sdk.PluralObjectTypeRoles} - ctx := context.Background() - currentAccount, err := client.ContextFunctions.CurrentAccount(ctx) - require.NoError(t, err) + currentAccount := testClientHelper().Context.CurrentAccount(t) accountID := sdk.NewAccountIdentifierFromAccountLocator(currentAccount) allowedAccounts := []sdk.AccountIdentifier{accountID} return createFailoverGroupWithOptions(t, client, objectTypes, allowedAccounts, nil) diff --git a/pkg/sdk/testint/masking_policy_integration_test.go b/pkg/sdk/testint/masking_policy_integration_test.go index 5dc9819594..bfde3fab4a 100644 --- a/pkg/sdk/testint/masking_policy_integration_test.go +++ b/pkg/sdk/testint/masking_policy_integration_test.go @@ -418,7 +418,7 @@ func TestInt_MaskingPoliciesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/materialized_views_gen_integration_test.go b/pkg/sdk/testint/materialized_views_gen_integration_test.go index 6384ec37a2..cf65c83719 100644 --- a/pkg/sdk/testint/materialized_views_gen_integration_test.go +++ b/pkg/sdk/testint/materialized_views_gen_integration_test.go @@ -17,7 +17,7 @@ func TestInt_MaterializedViews(t *testing.T) { client := testClient(t) ctx := testContext(t) - table, tableCleanup := createTable(t, client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) sql := fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName()) @@ -416,7 +416,7 @@ func TestInt_MaterializedViewsShowByID(t *testing.T) { ctx := testContext(t) databaseTest, schemaTest := testDb(t), testSchema(t) - table, tableCleanup := createTable(t, client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) sql := fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName()) @@ -440,7 +440,7 @@ func TestInt_MaterializedViewsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/network_rule_gen_integration_test.go b/pkg/sdk/testint/network_rule_gen_integration_test.go index 64c65f374f..2be850150c 100644 --- a/pkg/sdk/testint/network_rule_gen_integration_test.go +++ b/pkg/sdk/testint/network_rule_gen_integration_test.go @@ -164,7 +164,7 @@ func TestInt_NetworkRulesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/password_policy_integration_test.go b/pkg/sdk/testint/password_policy_integration_test.go index 1c2f7e4cc1..d503dc1501 100644 --- a/pkg/sdk/testint/password_policy_integration_test.go +++ b/pkg/sdk/testint/password_policy_integration_test.go @@ -336,7 +336,7 @@ func TestInt_PasswordPoliciesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/pipes_integration_test.go b/pkg/sdk/testint/pipes_integration_test.go index 763498d39c..e2221740cb 100644 --- a/pkg/sdk/testint/pipes_integration_test.go +++ b/pkg/sdk/testint/pipes_integration_test.go @@ -25,10 +25,10 @@ func TestInt_CreatePipeWithStrangeSchemaName(t *testing.T) { schemaIdentifier := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, "tcK1>AJ+") // creating a new schema on purpose - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), schemaIdentifier.Name()) + schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithName(t, schemaIdentifier.Name()) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stageName := random.AlphanumericN(20) @@ -67,10 +67,10 @@ func TestInt_CreatePipeWithStrangeSchemaName(t *testing.T) { } func TestInt_PipesShowAndDescribe(t *testing.T) { - table1, table1Cleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table1, table1Cleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(table1Cleanup) - table2, table2Cleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table2, table2Cleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(table2Cleanup) stageName := random.AlphanumericN(20) @@ -151,7 +151,7 @@ func TestInt_PipesShowAndDescribe(t *testing.T) { } func TestInt_PipeCreate(t *testing.T) { - table, tableCleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) stageName := random.AlphanumericN(20) @@ -223,7 +223,7 @@ func TestInt_PipeCreate(t *testing.T) { } func TestInt_PipeDrop(t *testing.T) { - table, tableCleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) stageName := random.AlphanumericN(20) @@ -251,7 +251,7 @@ func TestInt_PipeDrop(t *testing.T) { } func TestInt_PipeAlter(t *testing.T) { - table, tableCleanup := createTable(t, itc.client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) stageName := random.AlphanumericN(20) @@ -358,7 +358,7 @@ func TestInt_PipesShowByID(t *testing.T) { ctx := testContext(t) databaseTest, schemaTest := testDb(t), testSchema(t) - table, tableCleanup := createTable(t, client, databaseTest, schemaTest) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, client, sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.AlphaN(6))) t.Cleanup(stageCleanup) @@ -384,7 +384,7 @@ func TestInt_PipesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/policy_references_integration_test.go b/pkg/sdk/testint/policy_references_integration_test.go index 6871d87f6a..d8882d09c5 100644 --- a/pkg/sdk/testint/policy_references_integration_test.go +++ b/pkg/sdk/testint/policy_references_integration_test.go @@ -22,7 +22,7 @@ func TestInt_PolicyReferences(t *testing.T) { }) t.Run("user domain", func(t *testing.T) { - user, userCleanup := createUser(t, client) + user, userCleanup := testClientHelper().User.CreateUser(t) t.Cleanup(userCleanup) err = client.Users.Alter(ctx, user.ID(), &sdk.AlterUserOptions{ diff --git a/pkg/sdk/testint/procedures_integration_test.go b/pkg/sdk/testint/procedures_integration_test.go index dbd386982e..c0c899edab 100644 --- a/pkg/sdk/testint/procedures_integration_test.go +++ b/pkg/sdk/testint/procedures_integration_test.go @@ -1007,7 +1007,7 @@ func TestInt_ProceduresShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/replication_functions_integration_test.go b/pkg/sdk/testint/replication_functions_integration_test.go index dc4dfb6e4c..f89f9f3ef5 100644 --- a/pkg/sdk/testint/replication_functions_integration_test.go +++ b/pkg/sdk/testint/replication_functions_integration_test.go @@ -21,15 +21,12 @@ func TestInt_ShowReplicationFunctions(t *testing.T) { func TestInt_ShowReplicationDatabases(t *testing.T) { client := testClient(t) - secondaryClient := testSecondaryClient(t) ctx := testContext(t) - account, err := client.ContextFunctions.CurrentAccount(ctx) - require.NoError(t, err) + account := testClientHelper().Context.CurrentAccount(t) accountId := sdk.NewAccountIdentifierFromAccountLocator(account) - secondaryAccount, err := secondaryClient.ContextFunctions.CurrentAccount(ctx) - require.NoError(t, err) + secondaryAccount := secondaryTestClientHelper().Context.CurrentAccount(t) secondaryAccountId := sdk.NewAccountIdentifierFromAccountLocator(secondaryAccount) db1Name := random.AlphaN(10) @@ -40,7 +37,7 @@ func TestInt_ShowReplicationDatabases(t *testing.T) { db2, dbCleanup2 := testClientHelper().Database.CreateDatabaseWithName(t, db2Name) t.Cleanup(dbCleanup2) - err = client.Databases.AlterReplication(ctx, db.ID(), &sdk.AlterDatabaseReplicationOptions{EnableReplication: &sdk.EnableReplication{ToAccounts: []sdk.AccountIdentifier{secondaryAccountId}}}) + err := client.Databases.AlterReplication(ctx, db.ID(), &sdk.AlterDatabaseReplicationOptions{EnableReplication: &sdk.EnableReplication{ToAccounts: []sdk.AccountIdentifier{secondaryAccountId}}}) require.NoError(t, err) err = client.Databases.AlterReplication(ctx, db2.ID(), &sdk.AlterDatabaseReplicationOptions{EnableReplication: &sdk.EnableReplication{ToAccounts: []sdk.AccountIdentifier{secondaryAccountId}}}) require.NoError(t, err) diff --git a/pkg/sdk/testint/roles_integration_test.go b/pkg/sdk/testint/roles_integration_test.go index 04a863603c..48e607e80b 100644 --- a/pkg/sdk/testint/roles_integration_test.go +++ b/pkg/sdk/testint/roles_integration_test.go @@ -84,18 +84,14 @@ func TestInt_Roles(t *testing.T) { }) t.Run("alter rename to", func(t *testing.T) { - role, _ := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) + t.Cleanup(roleCleanup) + newName := sdk.RandomAccountObjectIdentifier() - t.Cleanup(func() { - err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(newName)) - if err != nil { - err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(role.ID())) - require.NoError(t, err) - } - }) err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithRenameTo(newName)) require.NoError(t, err) + t.Cleanup(testClientHelper().Role.DropRoleFunc(t, newName)) r, err := client.Roles.ShowByID(ctx, newName) require.NoError(t, err) @@ -103,7 +99,7 @@ func TestInt_Roles(t *testing.T) { }) t.Run("alter set tags", func(t *testing.T) { - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) _, err := client.SystemFunctions.GetTag(ctx, tag.ID(), role.ID(), "ROLE") @@ -126,7 +122,7 @@ func TestInt_Roles(t *testing.T) { t.Run("alter unset tags", func(t *testing.T) { tagValue := "tag-value" id := sdk.RandomAccountObjectIdentifier() - role, cleanup := createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(id). + role, cleanup := testClientHelper().Role.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(id). WithTag([]sdk.TagAssociation{ { Name: tag.ID(), @@ -147,7 +143,7 @@ func TestInt_Roles(t *testing.T) { }) t.Run("alter set comment", func(t *testing.T) { - role, cleanupRole := createRole(t, client) + role, cleanupRole := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanupRole) comment := random.Comment() @@ -162,7 +158,7 @@ func TestInt_Roles(t *testing.T) { t.Run("alter unset comment", func(t *testing.T) { comment := random.Comment() id := sdk.RandomAccountObjectIdentifier() - role, cleanup := createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(id).WithComment(comment)) + role, cleanup := testClientHelper().Role.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(id).WithComment(comment)) t.Cleanup(cleanup) err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithUnsetComment(true)) @@ -174,7 +170,9 @@ func TestInt_Roles(t *testing.T) { }) t.Run("drop no options", func(t *testing.T) { - role, _ := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) + t.Cleanup(roleCleanup) + err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(role.ID())) require.NoError(t, err) @@ -184,10 +182,10 @@ func TestInt_Roles(t *testing.T) { }) t.Run("show no options", func(t *testing.T) { - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) - role2, cleanup2 := createRole(t, client) + role2, cleanup2 := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup2) roles, err := client.Roles.Show(ctx, sdk.NewShowRoleRequest()) @@ -203,7 +201,7 @@ func TestInt_Roles(t *testing.T) { }) t.Run("show like", func(t *testing.T) { - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) roles, err := client.Roles.Show(ctx, sdk.NewShowRoleRequest().WithLike(sdk.NewLikeRequest(role.Name))) @@ -222,7 +220,7 @@ func TestInt_Roles(t *testing.T) { }) t.Run("show by id - same name in different schemas", func(t *testing.T) { - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) r, err := client.Roles.ShowByID(ctx, role.ID()) @@ -232,10 +230,10 @@ func TestInt_Roles(t *testing.T) { }) t.Run("grant and revoke role from user", func(t *testing.T) { - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) - user, cleanupUser := createUser(t, client) + user, cleanupUser := testClientHelper().User.CreateUser(t) t.Cleanup(cleanupUser) userID := user.ID() @@ -255,10 +253,10 @@ func TestInt_Roles(t *testing.T) { }) t.Run("grant and revoke role from role", func(t *testing.T) { - parentRole, cleanupParentRole := createRole(t, client) + parentRole, cleanupParentRole := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanupParentRole) - role, cleanup := createRole(t, client) + role, cleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(cleanup) parentRoleID := parentRole.ID() diff --git a/pkg/sdk/testint/row_access_policies_gen_integration_test.go b/pkg/sdk/testint/row_access_policies_gen_integration_test.go index 6ce243872b..c1de3baab2 100644 --- a/pkg/sdk/testint/row_access_policies_gen_integration_test.go +++ b/pkg/sdk/testint/row_access_policies_gen_integration_test.go @@ -340,7 +340,7 @@ func TestInt_RowAccessPoliciesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/schemas_integration_test.go b/pkg/sdk/testint/schemas_integration_test.go index 2abfe6d263..2fd4e5f1aa 100644 --- a/pkg/sdk/testint/schemas_integration_test.go +++ b/pkg/sdk/testint/schemas_integration_test.go @@ -15,7 +15,7 @@ func TestInt_SchemasCreate(t *testing.T) { ctx := testContext(t) // new schema created on purpose - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) t.Run("replace", func(t *testing.T) { @@ -118,7 +118,7 @@ func TestInt_SchemasAlter(t *testing.T) { t.Run("rename to", func(t *testing.T) { // new schema created on purpose - schema, _ := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, _ := testClientHelper().Schema.CreateSchema(t) t.Cleanup(func() { err := client.Sessions.UseSchema(ctx, testSchema(t).ID()) require.NoError(t, err) @@ -139,12 +139,12 @@ func TestInt_SchemasAlter(t *testing.T) { t.Run("swap with", func(t *testing.T) { // new schemas created on purpose - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) - swapSchema, cleanupSwapSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + swapSchema, cleanupSwapSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSwapSchema) - table, _ := createTable(t, client, testDb(t), schema) + table, _ := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(func() { newId := sdk.NewSchemaObjectIdentifier(testDb(t).Name, swapSchema.Name, table.Name) err := client.Tables.Drop(ctx, sdk.NewDropTableRequest(newId)) @@ -165,7 +165,7 @@ func TestInt_SchemasAlter(t *testing.T) { t.Run("set", func(t *testing.T) { // new schema created on purpose - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) comment := random.Comment() @@ -279,7 +279,7 @@ func TestInt_SchemasAlter(t *testing.T) { t.Run("enable managed access", func(t *testing.T) { // new schema created on purpose - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) err := client.Schemas.Alter(ctx, schema.ID(), &sdk.AlterSchemaOptions{ @@ -299,7 +299,7 @@ func TestInt_SchemasShow(t *testing.T) { ctx := testContext(t) // new schema created on purpose - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) t.Run("no options", func(t *testing.T) { @@ -341,7 +341,7 @@ func TestInt_SchemasDrop(t *testing.T) { ctx := testContext(t) // new schema created on purpose - schema, _ := testClientHelper().Schema.CreateSchema(t, testDb(t)) + schema, _ := testClientHelper().Schema.CreateSchema(t) t.Cleanup(func() { err := client.Sessions.UseSchema(ctx, testSchema(t).ID()) require.NoError(t, err) diff --git a/pkg/sdk/testint/sequences_integration_test.go b/pkg/sdk/testint/sequences_integration_test.go index 8790209052..33140ece7f 100644 --- a/pkg/sdk/testint/sequences_integration_test.go +++ b/pkg/sdk/testint/sequences_integration_test.go @@ -197,7 +197,7 @@ func TestInt_SequencesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/sessions_integration_test.go b/pkg/sdk/testint/sessions_integration_test.go index 8a2abc806a..10f705ede5 100644 --- a/pkg/sdk/testint/sessions_integration_test.go +++ b/pkg/sdk/testint/sessions_integration_test.go @@ -88,8 +88,7 @@ func TestInt_ShowObjectParameter(t *testing.T) { func TestInt_ShowUserParameter(t *testing.T) { client := testClient(t) ctx := testContext(t) - user, err := client.ContextFunctions.CurrentUser(ctx) - require.NoError(t, err) + user := testClientHelper().Context.CurrentUser(t) userID := sdk.NewAccountObjectIdentifier(user) parameter, err := client.Parameters.ShowUserParameter(ctx, sdk.UserParameterAutocommit, userID) require.NoError(t, err) @@ -105,7 +104,7 @@ func TestInt_UseWarehouse(t *testing.T) { require.NoError(t, err) }) // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) err := client.Sessions.UseWarehouse(ctx, warehouse.ID()) require.NoError(t, err) @@ -145,7 +144,7 @@ func TestInt_UseSchema(t *testing.T) { // new database and schema created on purpose database, databaseCleanup := testClientHelper().Database.CreateDatabase(t) t.Cleanup(databaseCleanup) - schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t, database) + schema, schemaCleanup := testClientHelper().Schema.CreateSchemaInDatabase(t, database.ID()) t.Cleanup(schemaCleanup) err := client.Sessions.UseSchema(ctx, schema.ID()) require.NoError(t, err) diff --git a/pkg/sdk/testint/stages_gen_integration_test.go b/pkg/sdk/testint/stages_gen_integration_test.go index 765f06eab2..3d4a488c63 100644 --- a/pkg/sdk/testint/stages_gen_integration_test.go +++ b/pkg/sdk/testint/stages_gen_integration_test.go @@ -580,7 +580,7 @@ func TestInt_StagesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/streamlits_integration_test.go b/pkg/sdk/testint/streamlits_integration_test.go index 41ff783687..fd9a85c3a8 100644 --- a/pkg/sdk/testint/streamlits_integration_test.go +++ b/pkg/sdk/testint/streamlits_integration_test.go @@ -76,7 +76,7 @@ func TestInt_Streamlits(t *testing.T) { stage, cleanupStage := createStage(t, client, sdk.NewSchemaObjectIdentifier(TestDatabaseName, TestSchemaName, random.AlphaN(4))) t.Cleanup(cleanupStage) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) comment := random.StringN(4) @@ -142,7 +142,7 @@ func TestInt_Streamlits(t *testing.T) { stage, cleanupStage := createStage(t, client, sdk.NewSchemaObjectIdentifier(TestDatabaseName, TestSchemaName, random.AlphaN(4))) t.Cleanup(cleanupStage) - databaseRole, databaseRoleCleanup := createDatabaseRole(t, client, testDb(t)) + databaseRole, databaseRoleCleanup := testClientHelper().DatabaseRole.CreateDatabaseRole(t) t.Cleanup(databaseRoleCleanup) databaseRoleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, databaseRole.Name) @@ -318,7 +318,7 @@ func TestInt_StreamlitsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/streams_gen_integration_test.go b/pkg/sdk/testint/streams_gen_integration_test.go index cd43f38d27..2048cf3bf1 100644 --- a/pkg/sdk/testint/streams_gen_integration_test.go +++ b/pkg/sdk/testint/streams_gen_integration_test.go @@ -18,7 +18,7 @@ func TestInt_Streams(t *testing.T) { db := testDb(t) - schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t, db) + schema, cleanupSchema := testClientHelper().Schema.CreateSchema(t) t.Cleanup(cleanupSchema) assertStream := func(t *testing.T, s *sdk.Stream, id sdk.SchemaObjectIdentifier, sourceType string, mode string) { @@ -34,7 +34,7 @@ func TestInt_Streams(t *testing.T) { } t.Run("CreateOnTable", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -104,7 +104,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("CreateOnView", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) tableId := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, table.Name) t.Cleanup(cleanupTable) @@ -128,7 +128,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Clone", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -156,7 +156,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Alter tags", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -197,7 +197,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Alter comment", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -232,7 +232,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Drop", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -251,7 +251,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show terse", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -280,7 +280,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show single with options", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -314,7 +314,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show multiple", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -350,7 +350,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show multiple with options", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) idPrefix := "stream_show_" @@ -396,7 +396,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Describe", func(t *testing.T) { - table, cleanupTable := createTable(t, client, db, schema) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(cleanupTable) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, random.AlphanumericN(32)) @@ -428,7 +428,7 @@ func TestInt_StreamsShowByID(t *testing.T) { ctx := testContext(t) databaseTest, schemaTest := testDb(t), testSchema(t) - table, cleanupTable := createTable(t, client, databaseTest, schemaTest) + table, cleanupTable := testClientHelper().Table.CreateTable(t) t.Cleanup(cleanupTable) cleanupStreamHandle := func(id sdk.SchemaObjectIdentifier) func() { @@ -450,7 +450,7 @@ func TestInt_StreamsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/system_functions_integration_test.go b/pkg/sdk/testint/system_functions_integration_test.go index d01c85c437..ce0b1f619e 100644 --- a/pkg/sdk/testint/system_functions_integration_test.go +++ b/pkg/sdk/testint/system_functions_integration_test.go @@ -50,10 +50,10 @@ func TestInt_GetTag(t *testing.T) { func TestInt_PipeStatus(t *testing.T) { client := testClient(t) - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -96,13 +96,13 @@ func TestInt_PipeStatus(t *testing.T) { func TestInt_PipeForceResume(t *testing.T) { client := testClient(t) - role, roleCleanup := createRole(t, client) + role, roleCleanup := testClientHelper().Role.CreateRole(t) t.Cleanup(roleCleanup) - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), random.AlphaN(20)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) - table, tableCleanup := createTable(t, itc.client, testDb(t), schema) + table, tableCleanup := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) t.Cleanup(tableCleanup) stage, stageCleanup := createStage(t, itc.client, sdk.NewSchemaObjectIdentifier(testDb(t).Name, schema.Name, random.AlphaN(20))) @@ -140,8 +140,7 @@ func TestInt_PipeForceResume(t *testing.T) { ) require.NoError(t, err) - currentRole, err := client.ContextFunctions.CurrentRole(ctx) - require.NoError(t, err) + currentRole := testClientHelper().Context.CurrentRole(t) err = client.Grants.GrantOwnership( ctx, diff --git a/pkg/sdk/testint/tables_integration_test.go b/pkg/sdk/testint/tables_integration_test.go index 34a6d25664..86514577d3 100644 --- a/pkg/sdk/testint/tables_integration_test.go +++ b/pkg/sdk/testint/tables_integration_test.go @@ -101,7 +101,7 @@ func TestInt_Table(t *testing.T) { Type: sdk.DataTypeVARCHAR, }, }, sdk.DataTypeVARCHAR, "REPLACE('X', 1, 2)", nil) - table2, _ := createTable(t, client, database, schema) + table2, _ := testClientHelper().Table.CreateTable(t) name := random.String() comment := random.String() @@ -258,7 +258,8 @@ func TestInt_Table(t *testing.T) { *sdk.NewTableColumnRequest("col2", "VARCHAR"), *sdk.NewTableColumnRequest("col3", "BOOLEAN"), } - sourceTable, sourceTableCleanup := createTableWithColumns(t, client, database, schema, columns) + sourceTableName := random.StringRange(8, 28) + sourceTable, sourceTableCleanup := testClientHelper().Table.CreateTableWithColumns(t, schema.ID(), sourceTableName, columns) t.Cleanup(sourceTableCleanup) name := random.String() @@ -290,7 +291,8 @@ func TestInt_Table(t *testing.T) { *sdk.NewTableColumnRequest("col2", "VARCHAR"), *sdk.NewTableColumnRequest("col3", "BOOLEAN"), } - sourceTable, sourceTableCleanup := createTableWithColumns(t, client, database, schema, columns) + sourceTableName := random.StringRange(8, 28) + sourceTable, sourceTableCleanup := testClientHelper().Table.CreateTableWithColumns(t, schema.ID(), sourceTableName, columns) t.Cleanup(sourceTableCleanup) name := random.String() @@ -940,7 +942,7 @@ func TestInt_Table(t *testing.T) { }) t.Run("drop table", func(t *testing.T) { - table, tableCleanup := createTable(t, client, database, schema) + table, tableCleanup := testClientHelper().Table.CreateTable(t) err := client.Tables.Drop(ctx, sdk.NewDropTableRequest(table.ID()).WithIfExists(sdk.Bool(true))) if err != nil { t.Cleanup(tableCleanup) @@ -952,9 +954,9 @@ func TestInt_Table(t *testing.T) { }) t.Run("show tables", func(t *testing.T) { - table, tableCleanup := createTable(t, client, database, schema) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) - table2, table2Cleanup := createTable(t, client, database, schema) + table2, table2Cleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(table2Cleanup) tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest()) @@ -970,7 +972,7 @@ func TestInt_Table(t *testing.T) { }) t.Run("with terse", func(t *testing.T) { - table, tableCleanup := createTable(t, client, database, schema) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithTerse(sdk.Bool(true)).WithLikePattern(table.ID().Name())) @@ -981,7 +983,7 @@ func TestInt_Table(t *testing.T) { }) t.Run("with starts with", func(t *testing.T) { - table, tableCleanup := createTable(t, client, database, schema) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithStartsWith(sdk.String(table.Name))) @@ -1025,7 +1027,7 @@ func TestInt_TablesShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/tags_integration_test.go b/pkg/sdk/testint/tags_integration_test.go index 068aae74c1..01f52658c1 100644 --- a/pkg/sdk/testint/tags_integration_test.go +++ b/pkg/sdk/testint/tags_integration_test.go @@ -18,7 +18,7 @@ func TestInt_Tags(t *testing.T) { databaseTest, databaseCleanup := testClientHelper().Database.CreateDatabase(t) t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchema(t, databaseTest) + schemaTest, schemaCleanup := testClientHelper().Schema.CreateSchemaInDatabase(t, databaseTest.ID()) t.Cleanup(schemaCleanup) assertTagHandle := func(t *testing.T, tag *sdk.Tag, expectedName string, expectedComment string, expectedAllowedValues []string) { @@ -302,7 +302,7 @@ func TestInt_TagsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/tasks_gen_integration_test.go b/pkg/sdk/testint/tasks_gen_integration_test.go index 03082b0908..1f10d9bced 100644 --- a/pkg/sdk/testint/tasks_gen_integration_test.go +++ b/pkg/sdk/testint/tasks_gen_integration_test.go @@ -751,7 +751,7 @@ func TestInt_TasksShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/users_integration_test.go b/pkg/sdk/testint/users_integration_test.go index c3ddde8dd1..18c9b516f4 100644 --- a/pkg/sdk/testint/users_integration_test.go +++ b/pkg/sdk/testint/users_integration_test.go @@ -14,10 +14,10 @@ func TestInt_UsersShow(t *testing.T) { client := testClient(t) ctx := testContext(t) - userTest, userCleanup := createUserWithName(t, client, "USER_FOO") + userTest, userCleanup := testClientHelper().User.CreateUserWithName(t, "USER_FOO") t.Cleanup(userCleanup) - userTest2, user2Cleanup := createUserWithName(t, client, "USER_BAR") + userTest2, user2Cleanup := testClientHelper().User.CreateUserWithName(t, "USER_BAR") t.Cleanup(user2Cleanup) t.Run("with like options", func(t *testing.T) { @@ -199,7 +199,7 @@ func TestInt_UserDescribe(t *testing.T) { client := testClient(t) ctx := testContext(t) - user, userCleanup := createUser(t, client) + user, userCleanup := testClientHelper().User.CreateUser(t) t.Cleanup(userCleanup) t.Run("when user exists", func(t *testing.T) { @@ -220,9 +220,11 @@ func TestInt_UserDrop(t *testing.T) { ctx := testContext(t) t.Run("when user exists", func(t *testing.T) { - user, _ := createUser(t, client) + user, userCleanup := testClientHelper().User.CreateUser(t) + t.Cleanup(userCleanup) + id := user.ID() - err := client.Users.Drop(ctx, id) + err := client.Users.Drop(ctx, id, &sdk.DropUserOptions{}) require.NoError(t, err) _, err = client.Users.Describe(ctx, id) assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) @@ -230,7 +232,7 @@ func TestInt_UserDrop(t *testing.T) { t.Run("when user does not exist", func(t *testing.T) { id := sdk.NewAccountObjectIdentifier("does_not_exist") - err := client.Users.Drop(ctx, id) + err := client.Users.Drop(ctx, id, &sdk.DropUserOptions{}) assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) }) } diff --git a/pkg/sdk/testint/views_gen_integration_test.go b/pkg/sdk/testint/views_gen_integration_test.go index eeeabfb41e..52ad9f93ff 100644 --- a/pkg/sdk/testint/views_gen_integration_test.go +++ b/pkg/sdk/testint/views_gen_integration_test.go @@ -18,7 +18,7 @@ func TestInt_Views(t *testing.T) { client := testClient(t) ctx := testContext(t) - table, tableCleanup := createTable(t, client, testDb(t), testSchema(t)) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) sql := fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName()) @@ -493,7 +493,7 @@ func TestInt_Views(t *testing.T) { t.Run("show view by id: same name in different schemas", func(t *testing.T) { // we assume that SF returns views alphabetically schemaName := "aaaa" + random.StringRange(8, 28) - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, testDb(t), schemaName) + schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithName(t, schemaName) t.Cleanup(schemaCleanup) name := random.String() @@ -537,7 +537,7 @@ func TestInt_ViewsShowByID(t *testing.T) { ctx := testContext(t) databaseTest, schemaTest := testDb(t), testSchema(t) - table, tableCleanup := createTable(t, client, databaseTest, schemaTest) + table, tableCleanup := testClientHelper().Table.CreateTable(t) t.Cleanup(tableCleanup) sql := fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName()) @@ -561,7 +561,7 @@ func TestInt_ViewsShowByID(t *testing.T) { } t.Run("show by id - same name in different schemas", func(t *testing.T) { - schema, schemaCleanup := testClientHelper().Schema.CreateSchemaWithIdentifier(t, databaseTest, random.AlphaN(8)) + schema, schemaCleanup := testClientHelper().Schema.CreateSchema(t) t.Cleanup(schemaCleanup) name := random.AlphaN(4) diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index 3b167945cc..e517c3d13c 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -13,12 +13,13 @@ func TestInt_WarehousesShow(t *testing.T) { client := testClient(t) ctx := testContext(t) + id := sdk.RandomAccountObjectIdentifier() // new warehouses created on purpose - testWarehouse, warehouseCleanup := createWarehouseWithOptions(t, client, &sdk.CreateWarehouseOptions{ + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouseWithOptions(t, id, &sdk.CreateWarehouseOptions{ WarehouseSize: &sdk.WarehouseSizeSmall, }) t.Cleanup(warehouseCleanup) - _, warehouse2Cleanup := createWarehouse(t, client) + _, warehouse2Cleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouse2Cleanup) t.Run("show without options", func(t *testing.T) { @@ -30,13 +31,13 @@ func TestInt_WarehousesShow(t *testing.T) { t.Run("show with options", func(t *testing.T) { showOptions := &sdk.ShowWarehouseOptions{ Like: &sdk.Like{ - Pattern: &testWarehouse.Name, + Pattern: &warehouse.Name, }, } warehouses, err := client.Warehouses.Show(ctx, showOptions) require.NoError(t, err) assert.Equal(t, 1, len(warehouses)) - assert.Equal(t, testWarehouse.Name, warehouses[0].Name) + assert.Equal(t, warehouse.Name, warehouses[0].Name) assert.Equal(t, sdk.WarehouseSizeSmall, warehouses[0].Size) }) @@ -163,7 +164,7 @@ func TestInt_WarehouseDescribe(t *testing.T) { ctx := testContext(t) // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) t.Run("when warehouse exists", func(t *testing.T) { @@ -249,7 +250,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("set", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) alterOptions := &sdk.AlterWarehouseOptions{ @@ -276,7 +277,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("rename", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) oldID := warehouse.ID() t.Cleanup(warehouseCleanup) @@ -303,10 +304,10 @@ func TestInt_WarehouseAlter(t *testing.T) { Comment: sdk.String("test comment"), MaxClusterCount: sdk.Int(10), } + id := sdk.RandomAccountObjectIdentifier() // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouseWithOptions(t, client, createOptions) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouseWithOptions(t, id, createOptions) t.Cleanup(warehouseCleanup) - id := warehouse.ID() alterOptions := &sdk.AlterWarehouseOptions{ Unset: &sdk.WarehouseUnset{ @@ -331,7 +332,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("suspend & resume", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) alterOptions := &sdk.AlterWarehouseOptions{ @@ -367,7 +368,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("resume without suspending", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) alterOptions := &sdk.AlterWarehouseOptions{ @@ -389,10 +390,10 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("abort all queries", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) - resetWarehouse := useWarehouse(t, client, warehouse.ID()) + resetWarehouse := testClientHelper().Warehouse.UseWarehouse(t, warehouse.ID()) t.Cleanup(resetWarehouse) // Start a long query @@ -436,7 +437,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("set tags", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) alterOptions := &sdk.AlterWarehouseOptions{ @@ -464,7 +465,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Run("unset tags", func(t *testing.T) { // new warehouse created on purpose - warehouse, warehouseCleanup := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) t.Cleanup(warehouseCleanup) alterOptions := &sdk.AlterWarehouseOptions{ @@ -512,7 +513,8 @@ func TestInt_WarehouseDrop(t *testing.T) { t.Run("when warehouse exists", func(t *testing.T) { // new warehouse created on purpose - warehouse, _ := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) + t.Cleanup(warehouseCleanup) err := client.Warehouses.Drop(ctx, warehouse.ID(), nil) require.NoError(t, err) @@ -528,7 +530,8 @@ func TestInt_WarehouseDrop(t *testing.T) { t.Run("when warehouse exists and if exists is true", func(t *testing.T) { // new warehouse created on purpose - warehouse, _ := createWarehouse(t, client) + warehouse, warehouseCleanup := testClientHelper().Warehouse.CreateWarehouse(t) + t.Cleanup(warehouseCleanup) dropOptions := &sdk.DropWarehouseOptions{IfExists: sdk.Bool(true)} err := client.Warehouses.Drop(ctx, warehouse.ID(), dropOptions) diff --git a/pkg/sdk/users.go b/pkg/sdk/users.go index b7f937c2d5..3a4f74b80b 100644 --- a/pkg/sdk/users.go +++ b/pkg/sdk/users.go @@ -19,7 +19,7 @@ var ( type Users interface { Create(ctx context.Context, id AccountObjectIdentifier, opts *CreateUserOptions) error Alter(ctx context.Context, id AccountObjectIdentifier, opts *AlterUserOptions) error - Drop(ctx context.Context, id AccountObjectIdentifier) error + Drop(ctx context.Context, id AccountObjectIdentifier, opts *DropUserOptions) error Describe(ctx context.Context, id AccountObjectIdentifier) (*UserDetails, error) Show(ctx context.Context, opts *ShowUserOptions) ([]User, error) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*User, error) @@ -378,9 +378,10 @@ func (opts *UserUnset) validate() error { // DropUserOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-user. type DropUserOptions struct { - drop bool `ddl:"static" sql:"DROP"` - user bool `ddl:"static" sql:"USER"` - name AccountObjectIdentifier `ddl:"identifier"` + drop bool `ddl:"static" sql:"DROP"` + user bool `ddl:"static" sql:"USER"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` } func (opts *DropUserOptions) validate() error { @@ -393,8 +394,10 @@ func (opts *DropUserOptions) validate() error { return nil } -func (v *users) Drop(ctx context.Context, id AccountObjectIdentifier) error { - opts := &DropUserOptions{} +func (v *users) Drop(ctx context.Context, id AccountObjectIdentifier, opts *DropUserOptions) error { + if opts == nil { + opts = &DropUserOptions{} + } opts.name = id if err := opts.validate(); err != nil { return fmt.Errorf("validate drop options: %w", err)