From 382bfc1f805851b3b944437128cdf9da74e8d6f2 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 16 Apr 2024 09:11:22 +0200 Subject: [PATCH] chore: Initialize the SDK client in fewer places (#2710) Result of SNOW-935947. - the SDK default client was already setup earlier in the acceptance tests context; used in all valid places - added secondary client setup to acceptance tests context and used in valid places --- pkg/acceptance/testing.go | 20 +++++- pkg/datasources/grants_acceptance_test.go | 20 +----- pkg/resources/database_acceptance_test.go | 36 +++------- .../dynamic_table_acceptance_test.go | 5 +- .../external_table_acceptance_test.go | 12 ++-- pkg/resources/function_acceptance_test.go | 38 +++++------ .../grant_ownership_acceptance_test.go | 25 +++---- ...vileges_to_account_role_acceptance_test.go | 65 +++++-------------- ...ileges_to_database_role_acceptance_test.go | 12 +--- ...ant_privileges_to_share_acceptance_test.go | 5 +- .../materialized_view_acceptance_test.go | 5 +- pkg/resources/schema_acceptance_test.go | 10 ++- pkg/resources/table_acceptance_test.go | 5 +- ...king_policy_association_acceptance_test.go | 2 +- .../unsafe_execute_acceptance_test.go | 13 ++-- pkg/resources/user_acceptance_test.go | 5 +- pkg/resources/view_acceptance_test.go | 15 ++--- pkg/resources/warehouse_acceptance_test.go | 5 +- pkg/sdk/helper_test.go | 15 ++--- 19 files changed, 111 insertions(+), 202 deletions(-) diff --git a/pkg/acceptance/testing.go b/pkg/acceptance/testing.go index d600249420..f43923522b 100644 --- a/pkg/acceptance/testing.go +++ b/pkg/acceptance/testing.go @@ -62,11 +62,22 @@ func init() { log.Panicf("Cannot instantiate new client, err: %v", err) } atc.client = client + + cfg, err := sdk.ProfileConfig(testprofiles.Secondary) + if err != nil { + log.Panicf("Config for the secondary client is needed to run acceptance tests, err: %v", err) + } + secondaryClient, err := sdk.NewClient(cfg) + if err != nil { + log.Panicf("Cannot instantiate new secondary client, err: %v", err) + } + atc.secondaryClient = secondaryClient } type acceptanceTestContext struct { - config *gosnowflake.Config - client *sdk.Client + config *gosnowflake.Config + client *sdk.Client + secondaryClient *sdk.Client } var TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ @@ -144,6 +155,11 @@ func Client(t *testing.T) *sdk.Client { return atc.client } +func SecondaryClient(t *testing.T) *sdk.Client { + t.Helper() + return atc.secondaryClient +} + func DefaultConfig(t *testing.T) *gosnowflake.Config { t.Helper() return atc.config diff --git a/pkg/datasources/grants_acceptance_test.go b/pkg/datasources/grants_acceptance_test.go index 5389832530..134db6373e 100644 --- a/pkg/datasources/grants_acceptance_test.go +++ b/pkg/datasources/grants_acceptance_test.go @@ -8,7 +8,6 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -373,18 +372,8 @@ func TestAcc_Grants_Of_Share(t *testing.T) { getSecondaryAccountIdentifier := func(t *testing.T) *sdk.AccountIdentifier { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } - cfg, err := sdk.ProfileConfig(testprofiles.Secondary) - if err != nil { - t.Fatal(err) - } - secondaryClient, err := sdk.NewClient(cfg) - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) + secondaryClient := acc.SecondaryClient(t) ctx := context.Background() replicationAccounts, err := client.ReplicationFunctions.ShowReplicationAccounts(ctx) @@ -688,10 +677,7 @@ func checkAtLeastOneGrantPresentLimited() resource.TestCheckFunc { func getCurrentUser(t *testing.T) string { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) user, err := client.ContextFunctions.CurrentUser(context.Background()) if err != nil { t.Fatal(err) diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 96e3f936e3..4afa8f7e51 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -11,7 +11,6 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -198,8 +197,7 @@ func TestAcc_Database_DefaultDataRetentionTime(t *testing.T) { return vars } - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -288,8 +286,7 @@ func TestAcc_Database_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing. return vars } - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -310,7 +307,7 @@ func TestAcc_Database_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing. }, { PreConfig: func() { - err = client.Databases.Alter(context.Background(), id, &sdk.AlterDatabaseOptions{ + err := client.Databases.Alter(context.Background(), id, &sdk.AlterDatabaseOptions{ Set: &sdk.DatabaseSet{ DataRetentionTimeInDays: sdk.Int(20), }, @@ -382,23 +379,17 @@ resource "snowflake_database" "db" { func dropDatabaseOutsideTerraform(t *testing.T, id string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(id), &sdk.DropDatabaseOptions{}) + err := client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(id), &sdk.DropDatabaseOptions{}) require.NoError(t, err) } func getSecondaryAccount(t *testing.T) string { t.Helper() - secondaryConfig, err := sdk.ProfileConfig(testprofiles.Secondary) - require.NoError(t, err) - - secondaryClient, err := sdk.NewClient(secondaryConfig) - require.NoError(t, err) - + secondaryClient := acc.SecondaryClient(t) ctx := context.Background() account, err := secondaryClient.ContextFunctions.CurrentAccount(ctx) @@ -410,11 +401,10 @@ func getSecondaryAccount(t *testing.T) string { func testAccCheckDatabaseExistence(t *testing.T, id string, shouldExist bool) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - _, err = client.Databases.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id)) + _, err := client.Databases.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id)) if shouldExist { if err != nil { return fmt.Errorf("error while retrieving database %s, err = %w", id, err) @@ -431,10 +421,7 @@ func testAccCheckDatabaseExistence(t *testing.T, id string, shouldExist bool) fu func testAccCheckIfDatabaseIsReplicated(t *testing.T, id string) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { - client, err := sdk.NewDefaultClient() - if err != nil { - return err - } + client := acc.Client(t) ctx := context.Background() replicationDatabases, err := client.ReplicationFunctions.ShowReplicationDatabases(ctx, nil) @@ -492,10 +479,7 @@ func checkAccountAndDatabaseDataRetentionTime(id sdk.AccountObjectIdentifier, ex func createDatabaseOutsideTerraform(t *testing.T, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() if err := client.Databases.Create(ctx, sdk.NewAccountObjectIdentifier(name), new(sdk.CreateDatabaseOptions)); err != nil { diff --git a/pkg/resources/dynamic_table_acceptance_test.go b/pkg/resources/dynamic_table_acceptance_test.go index 58dd24fc93..85f35154e6 100644 --- a/pkg/resources/dynamic_table_acceptance_test.go +++ b/pkg/resources/dynamic_table_acceptance_test.go @@ -446,10 +446,7 @@ func testAccCheckDynamicTableDestroy(s *terraform.State) error { // TODO [SNOW-926148]: currently this dynamic table is not cleaned in the test; it is removed when the whole database is removed - this currently happens in a sweeper func createDynamicTableOutsideTerraform(t *testing.T, schemaName string, dynamicTableName string, query string) { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() dynamicTableId := sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, schemaName, dynamicTableName) diff --git a/pkg/resources/external_table_acceptance_test.go b/pkg/resources/external_table_acceptance_test.go index d4f530531d..e1b5ac3e3f 100644 --- a/pkg/resources/external_table_acceptance_test.go +++ b/pkg/resources/external_table_acceptance_test.go @@ -73,7 +73,7 @@ func TestAcc_ExternalTable_basic(t *testing.T) { }, { PreConfig: func() { - publishExternalTablesTestData(sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, acc.TestSchemaName, name), data) + publishExternalTablesTestData(t, sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, acc.TestSchemaName, name), data) }, ConfigDirectory: config.TestStepDirectory(), ConfigVariables: configVariables, @@ -344,14 +344,12 @@ func externalTableContainsData(name string, contains func(rows []map[string]*any } } -func publishExternalTablesTestData(stageName sdk.SchemaObjectIdentifier, data []byte) { - client, err := sdk.NewDefaultClient() - if err != nil { - log.Fatal(err) - } +func publishExternalTablesTestData(t *testing.T, stageName sdk.SchemaObjectIdentifier, data []byte) { + t.Helper() + client := acc.Client(t) ctx := context.Background() - _, err = client.ExecForTests(ctx, fmt.Sprintf(`copy into @%s/external_tables_test_data/test_data from (select parse_json('%s')) overwrite = true`, stageName.FullyQualifiedName(), string(data))) + _, err := client.ExecForTests(ctx, fmt.Sprintf(`copy into @%s/external_tables_test_data/test_data from (select parse_json('%s')) overwrite = true`, stageName.FullyQualifiedName(), string(data))) if err != nil { log.Fatal(err) } diff --git a/pkg/resources/function_acceptance_test.go b/pkg/resources/function_acceptance_test.go index 2c01c496f3..06ab8d2854 100644 --- a/pkg/resources/function_acceptance_test.go +++ b/pkg/resources/function_acceptance_test.go @@ -2,7 +2,6 @@ package resources_test import ( "context" - "errors" "fmt" "strings" "testing" @@ -44,7 +43,7 @@ func testAccFunction(t *testing.T, configDirectory string) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy, + CheckDestroy: testAccCheckFunctionDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory(configDirectory), @@ -131,7 +130,7 @@ func TestAcc_Function_complex(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy, + CheckDestroy: testAccCheckFunctionDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Function/complex"), @@ -194,7 +193,7 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy, + CheckDestroy: testAccCheckFunctionDestroy(t), // Using the string config because of the validation in teststep_validate.go: // teststep.Config.HasConfigurationFiles() returns true both for ConfigFile and ConfigDirectory. @@ -248,22 +247,21 @@ resource "snowflake_function" "f" { `, database, schema, name) } -func testAccCheckFunctionDestroy(s *terraform.State) error { - client, err := sdk.NewDefaultClient() - if err != nil { - return errors.New("client could not be instantiated") - } - - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_function" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - function, err := client.Functions.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("function %v still exists", function.Name) +func testAccCheckFunctionDestroy(t *testing.T) func(s *terraform.State) error { + t.Helper() + client := acc.Client(t) + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_function" { + continue + } + ctx := context.Background() + id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) + function, err := client.Functions.ShowByID(ctx, id) + if err == nil { + return fmt.Errorf("function %v still exists", function.Name) + } } + return nil } - return nil } diff --git a/pkg/resources/grant_ownership_acceptance_test.go b/pkg/resources/grant_ownership_acceptance_test.go index fcaa054d58..14972620cf 100644 --- a/pkg/resources/grant_ownership_acceptance_test.go +++ b/pkg/resources/grant_ownership_acceptance_test.go @@ -1078,14 +1078,12 @@ func TestAcc_GrantOwnership_OnAllTasks(t *testing.T) { func createDatabaseWithRoleAsOwner(t *testing.T, roleName string, databaseName string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) - + 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( + err := client.Grants.GrantOwnership( ctx, sdk.OwnershipGrantOn{ Object: &sdk.Object{ @@ -1108,11 +1106,9 @@ func createDatabaseWithRoleAsOwner(t *testing.T, roleName string, databaseName s func moveResourceOwnershipToAccountRole(t *testing.T, objectType sdk.ObjectType, objectName sdk.ObjectIdentifier, accountRoleName sdk.AccountObjectIdentifier) { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) - + client := acc.Client(t) ctx := context.Background() - err = client.Grants.GrantOwnership( + err := client.Grants.GrantOwnership( ctx, sdk.OwnershipGrantOn{ Object: &sdk.Object{ @@ -1158,9 +1154,7 @@ func checkResourceOwnershipIsGranted(opts *sdk.ShowGrantOptions, grantOn sdk.Obj func createAccountRole(t *testing.T, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) - + client := acc.Client(t) ctx := context.Background() roleId := sdk.NewAccountObjectIdentifier(name) assert.NoError(t, client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId))) @@ -1172,8 +1166,7 @@ func createAccountRole(t *testing.T, name string) func() { func createDatabase(t *testing.T, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) + client := acc.Client(t) ctx := context.Background() roleId := sdk.NewAccountObjectIdentifier(name) @@ -1186,8 +1179,7 @@ func createDatabase(t *testing.T, name string) func() { func grantOwnershipToTheCurrentRole(t *testing.T, on sdk.OwnershipGrantOn) { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) + client := acc.Client(t) ctx := context.Background() currentRole, err := client.ContextFunctions.CurrentRole(ctx) @@ -1206,8 +1198,7 @@ func grantOwnershipToTheCurrentRole(t *testing.T, on sdk.OwnershipGrantOn) { func getCurrentUser(t *testing.T) string { t.Helper() - client, err := sdk.NewDefaultClient() - assert.NoError(t, err) + 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 e843630959..0c8c464a8a 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -13,7 +13,6 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -1310,12 +1309,9 @@ func revokeAndGrantPrivilegesOnTableToAccountRole( withGrantOption bool, ) { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() - err = client.Grants.RevokePrivilegesFromAccountRole( + err := client.Grants.RevokePrivilegesFromAccountRole( ctx, &sdk.AccountRoleGrantPrivileges{ SchemaObjectPrivileges: privileges, @@ -1446,44 +1442,27 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveAccountRoleOutsideTerraform(t *t func getSecondaryAccountName(t *testing.T) (string, error) { t.Helper() - config, err := sdk.ProfileConfig(testprofiles.Secondary) - if err != nil { - t.Fatal(err) - } - client, err := sdk.NewClient(config) - if err != nil { - t.Fatal(err) - } - return client.ContextFunctions.CurrentAccount(context.Background()) + secondaryClient := acc.SecondaryClient(t) + return secondaryClient.ContextFunctions.CurrentAccount(context.Background()) } func getAccountName(t *testing.T) (string, error) { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) return client.ContextFunctions.CurrentAccount(context.Background()) } func createSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, shareName string) error { t.Helper() - config, err := sdk.ProfileConfig(testprofiles.Secondary) - if err != nil { - t.Fatal(err) - } - client, err := sdk.NewClient(config) - if err != nil { - t.Fatal(err) - } + secondaryClient := acc.SecondaryClient(t) ctx := context.Background() accountName, err := getAccountName(t) return errors.Join( err, - client.Databases.Create(ctx, sdk.NewAccountObjectIdentifier(databaseName), &sdk.CreateDatabaseOptions{}), - client.Shares.Create(ctx, sdk.NewAccountObjectIdentifier(shareName), &sdk.CreateShareOptions{}), - client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}, &sdk.ShareGrantOn{Database: sdk.NewAccountObjectIdentifier(databaseName)}, sdk.NewAccountObjectIdentifier(shareName)), - client.Shares.Alter(ctx, sdk.NewAccountObjectIdentifier(shareName), &sdk.AlterShareOptions{Set: &sdk.ShareSet{ + 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)), + secondaryClient.Shares.Alter(ctx, sdk.NewAccountObjectIdentifier(shareName), &sdk.AlterShareOptions{Set: &sdk.ShareSet{ Accounts: []sdk.AccountIdentifier{sdk.NewAccountIdentifierFromAccountLocator(accountName)}, }}), ) @@ -1491,27 +1470,17 @@ func createSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, s func dropSharedDatabaseOnSecondaryAccount(t *testing.T, databaseName string, shareName string) error { t.Helper() - config, err := sdk.ProfileConfig(testprofiles.Secondary) - if err != nil { - t.Fatal(err) - } - client, err := sdk.NewClient(config) - if err != nil { - t.Fatal(err) - } + secondaryClient := acc.SecondaryClient(t) ctx := context.Background() return errors.Join( - client.Shares.Drop(ctx, sdk.NewAccountObjectIdentifier(shareName)), - client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(databaseName), &sdk.DropDatabaseOptions{}), + secondaryClient.Shares.Drop(ctx, sdk.NewAccountObjectIdentifier(shareName)), + secondaryClient.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(databaseName), &sdk.DropDatabaseOptions{}), ) } func createAccountRoleOutsideTerraform(t *testing.T, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() roleId := sdk.NewAccountObjectIdentifier(name) if err := client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId).WithOrReplace(true)); err != nil { @@ -1589,11 +1558,9 @@ func queriedAccountRolePrivilegesContainAtLeast(roleName sdk.AccountObjectIdenti func createExternalVolume(t *testing.T, externalVolumeName string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) - + client := acc.Client(t) ctx := context.Background() - _, err = client.ExecForTests(ctx, fmt.Sprintf(`create external volume "%s" storage_locations = ( + _, err := client.ExecForTests(ctx, fmt.Sprintf(`create external volume "%s" storage_locations = ( ( name = 'test' storage_provider = 's3' 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 0697c6dc2e..20849ba6a4 100644 --- a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go @@ -1115,10 +1115,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveDatabaseRoleOutsideTerraform(t func createDatabaseRoleOutsideTerraform(t *testing.T, databaseName string, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + 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 { @@ -1191,12 +1188,9 @@ func revokeAndGrantPrivilegesOnDatabaseToDatabaseRole( withGrantOption bool, ) { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() - err = client.Grants.RevokePrivilegesFromDatabaseRole( + err := client.Grants.RevokePrivilegesFromDatabaseRole( ctx, &sdk.DatabaseRoleGrantPrivileges{ DatabasePrivileges: privileges, diff --git a/pkg/resources/grant_privileges_to_share_acceptance_test.go b/pkg/resources/grant_privileges_to_share_acceptance_test.go index 17bdbb4c8c..0d248e940b 100644 --- a/pkg/resources/grant_privileges_to_share_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_share_acceptance_test.go @@ -594,10 +594,7 @@ func testAccCheckSharePrivilegesRevoked() func(*terraform.State) error { func createShareOutsideTerraform(t *testing.T, name string) func() { t.Helper() - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + client := acc.Client(t) ctx := context.Background() if err := client.Shares.Create(ctx, sdk.NewAccountObjectIdentifier(name), new(sdk.CreateShareOptions)); err != nil { diff --git a/pkg/resources/materialized_view_acceptance_test.go b/pkg/resources/materialized_view_acceptance_test.go index 47954d9773..5d30e52ea8 100644 --- a/pkg/resources/materialized_view_acceptance_test.go +++ b/pkg/resources/materialized_view_acceptance_test.go @@ -277,11 +277,10 @@ func testAccCheckMaterializedViewDestroy(s *terraform.State) error { func alterMaterializedViewQueryExternally(t *testing.T, id sdk.SchemaObjectIdentifier, query string, warehouse string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Sessions.UseWarehouse(ctx, sdk.NewAccountObjectIdentifier(warehouse)) + err := client.Sessions.UseWarehouse(ctx, sdk.NewAccountObjectIdentifier(warehouse)) require.NoError(t, err) err = client.MaterializedViews.Create(ctx, sdk.NewCreateMaterializedViewRequest(id, query).WithOrReplace(sdk.Bool(true))) diff --git a/pkg/resources/schema_acceptance_test.go b/pkg/resources/schema_acceptance_test.go index bafc1c6f61..2abd3ec539 100644 --- a/pkg/resources/schema_acceptance_test.go +++ b/pkg/resources/schema_acceptance_test.go @@ -424,11 +424,10 @@ func setSchemaDataRetentionTime(t *testing.T, id sdk.DatabaseObjectIdentifier, d t.Helper() return func() { - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Schemas.Alter(ctx, id, &sdk.AlterSchemaOptions{ + err := client.Schemas.Alter(ctx, id, &sdk.AlterSchemaOptions{ Set: &sdk.SchemaSet{ DataRetentionTimeInDays: sdk.Int(days), }, @@ -456,10 +455,9 @@ func testAccCheckSchemaDestroy(s *terraform.State) error { func removeSchemaOutsideOfTerraform(t *testing.T, databaseName string, schemaName string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Schemas.Drop(ctx, sdk.NewDatabaseObjectIdentifier(databaseName, schemaName), new(sdk.DropSchemaOptions)) + err := client.Schemas.Drop(ctx, sdk.NewDatabaseObjectIdentifier(databaseName, schemaName), new(sdk.DropSchemaOptions)) require.NoError(t, err) } diff --git a/pkg/resources/table_acceptance_test.go b/pkg/resources/table_acceptance_test.go index eb786115af..0356dd8c20 100644 --- a/pkg/resources/table_acceptance_test.go +++ b/pkg/resources/table_acceptance_test.go @@ -1949,11 +1949,10 @@ func setTableDataRetentionTime(t *testing.T, id sdk.SchemaObjectIdentifier, days t.Helper() return func() { - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSet(sdk.NewTableSetRequest().WithDataRetentionTimeInDays(sdk.Int(days)))) + err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSet(sdk.NewTableSetRequest().WithDataRetentionTimeInDays(sdk.Int(days)))) require.NoError(t, err) } } diff --git a/pkg/resources/tag_masking_policy_association_acceptance_test.go b/pkg/resources/tag_masking_policy_association_acceptance_test.go index d56a8052b1..65df3c3981 100644 --- a/pkg/resources/tag_masking_policy_association_acceptance_test.go +++ b/pkg/resources/tag_masking_policy_association_acceptance_test.go @@ -53,7 +53,7 @@ func TestAcc_TagMaskingPolicyAssociationsystem_functions_integration_testComplet TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy, + CheckDestroy: testAccCheckFunctionDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_TagMaskingPolicyAssociation/basic"), diff --git a/pkg/resources/unsafe_execute_acceptance_test.go b/pkg/resources/unsafe_execute_acceptance_test.go index b0120b83b0..662d8ae76b 100644 --- a/pkg/resources/unsafe_execute_acceptance_test.go +++ b/pkg/resources/unsafe_execute_acceptance_test.go @@ -718,11 +718,10 @@ func generateUnsafeExecuteTestRoleName(t *testing.T) string { func createResourcesForExecuteUnsafeTestCaseForGrants(t *testing.T, dbId string, roleId string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Databases.Create(ctx, sdk.NewAccountObjectIdentifier(dbId), &sdk.CreateDatabaseOptions{}) + err := client.Databases.Create(ctx, sdk.NewAccountObjectIdentifier(dbId), &sdk.CreateDatabaseOptions{}) require.NoError(t, err) err = client.Roles.Create(ctx, sdk.NewCreateRoleRequest(sdk.NewAccountObjectIdentifier(roleId))) @@ -732,11 +731,10 @@ func createResourcesForExecuteUnsafeTestCaseForGrants(t *testing.T, dbId string, func dropResourcesForUnsafeExecuteTestCaseForGrants(t *testing.T, dbId string, roleId string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(dbId), &sdk.DropDatabaseOptions{}) + err := client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(dbId), &sdk.DropDatabaseOptions{}) assert.NoError(t, err) err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(sdk.NewAccountObjectIdentifier(roleId))) @@ -746,8 +744,7 @@ func dropResourcesForUnsafeExecuteTestCaseForGrants(t *testing.T, dbId string, r func verifyGrantExists(t *testing.T, roleId string, privilege sdk.AccountObjectPrivilege, shouldExist bool) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ diff --git a/pkg/resources/user_acceptance_test.go b/pkg/resources/user_acceptance_test.go index a6eeb3f09e..8d94738bf9 100644 --- a/pkg/resources/user_acceptance_test.go +++ b/pkg/resources/user_acceptance_test.go @@ -173,10 +173,7 @@ resource "snowflake_user" "test" { func removeUserOutsideOfTerraform(t *testing.T, name sdk.AccountObjectIdentifier) func() { t.Helper() return func() { - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } + 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()) diff --git a/pkg/resources/view_acceptance_test.go b/pkg/resources/view_acceptance_test.go index ade2be600b..fd6887bd48 100644 --- a/pkg/resources/view_acceptance_test.go +++ b/pkg/resources/view_acceptance_test.go @@ -583,11 +583,10 @@ func testAccCheckViewDestroy(s *terraform.State) error { func alterViewQueryExternally(t *testing.T, id sdk.SchemaObjectIdentifier, query string) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Views.Create(ctx, sdk.NewCreateViewRequest(id, query).WithOrReplace(sdk.Bool(true))) + err := client.Views.Create(ctx, sdk.NewCreateViewRequest(id, query).WithOrReplace(sdk.Bool(true))) require.NoError(t, err) } @@ -596,14 +595,13 @@ func registerAccountRoleCleanup(t *testing.T, roleName string) { roleId := sdk.NewAccountObjectIdentifier(roleName) - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() t.Cleanup(func() { t.Logf("dropping account role (%s)", roleName) // We remove the role, so the ownership will be changed back. The view will be deleted with db cleanup. - err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId).WithIfExists(true)) + err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId).WithIfExists(true)) if err != nil { t.Logf("failed to drop account role (%s), err = %s\n", roleName, err.Error()) } @@ -617,8 +615,7 @@ func alterViewOwnershipExternally(t *testing.T, viewName string, roleName string viewId := sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, acc.TestSchemaName, viewName) roleId := sdk.NewAccountObjectIdentifier(roleName) - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() on := sdk.OwnershipGrantOn{ @@ -633,6 +630,6 @@ func alterViewOwnershipExternally(t *testing.T, viewName string, roleName string currentGrants := sdk.OwnershipCurrentGrants{ OutboundPrivileges: sdk.Revoke, } - err = client.Grants.GrantOwnership(ctx, on, to, &sdk.GrantOwnershipOptions{CurrentGrants: ¤tGrants}) + err := client.Grants.GrantOwnership(ctx, on, to, &sdk.GrantOwnershipOptions{CurrentGrants: ¤tGrants}) require.NoError(t, err) } diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 871a4a26c8..7e732d1b3c 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -182,10 +182,9 @@ resource "snowflake_warehouse" "w2" { func alterWarehouseMaxConcurrencyLevelExternally(t *testing.T, warehouseId string, level int) { t.Helper() - client, err := sdk.NewDefaultClient() - require.NoError(t, err) + client := acc.Client(t) ctx := context.Background() - err = client.Warehouses.Alter(ctx, sdk.NewAccountObjectIdentifier(warehouseId), &sdk.AlterWarehouseOptions{Set: &sdk.WarehouseSet{MaxConcurrencyLevel: sdk.Int(level)}}) + 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/helper_test.go b/pkg/sdk/helper_test.go index 2de6670556..adfe806f45 100644 --- a/pkg/sdk/helper_test.go +++ b/pkg/sdk/helper_test.go @@ -20,19 +20,14 @@ func testClient(t *testing.T) *Client { func testSecondaryClient(t *testing.T) *Client { t.Helper() - client, err := testClientFromProfile(t, testprofiles.Secondary) + config, err := ProfileConfig(testprofiles.Secondary) if err != nil { t.Skipf("Snowflake secondary account not configured. Must be set in ~./snowflake/config.yml with profile name: %s", testprofiles.Secondary) } - - return client -} - -func testClientFromProfile(t *testing.T, profile string) (*Client, error) { - t.Helper() - config, err := ProfileConfig(profile) + client, err := NewClient(config) if err != nil { - return nil, err + t.Skipf("Snowflake secondary account not configured. Must be set in ~./snowflake/config.yml with profile name: %s", testprofiles.Secondary) } - return NewClient(config) + + return client }