diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go new file mode 100644 index 0000000000..eee566d005 --- /dev/null +++ b/pkg/acceptance/check_destroy.go @@ -0,0 +1,365 @@ +package acceptance + +import ( + "context" + "errors" + "fmt" + "reflect" + "strings" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" + "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/terraform" +) + +func CheckDestroy(t *testing.T, resource resources.Resource) func(*terraform.State) error { + t.Helper() + client := Client(t) + t.Logf("running check destroy for resource %s", resource) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != resource.String() { + continue + } + t.Logf("found resource %s in state", resource) + ctx := context.Background() + id := decodeSnowflakeId(rs, resource) + if id == nil { + return fmt.Errorf("could not get the id of %s", resource) + } + showById, ok := showByIdFunctions[resource] + if !ok { + return fmt.Errorf("unsupported show by id in cleanup for %s, with id %v", resource, id.FullyQualifiedName()) + } + if showById(ctx, client, id) == nil { + return fmt.Errorf("%s %v still exists", resource, id.FullyQualifiedName()) + } else { + t.Logf("resource %s (%v) was dropped successfully in Snowflake", resource, id.FullyQualifiedName()) + } + } + return nil + } +} + +func decodeSnowflakeId(rs *terraform.ResourceState, resource resources.Resource) sdk.ObjectIdentifier { + switch resource { + case resources.ExternalFunction: + return sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(rs.Primary.ID) + case resources.Function: + return sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(rs.Primary.ID) + case resources.Procedure: + return sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(rs.Primary.ID) + default: + return helpers.DecodeSnowflakeID(rs.Primary.ID) + } +} + +type showByIdFunc func(context.Context, *sdk.Client, sdk.ObjectIdentifier) error + +var showByIdFunctions = map[resources.Resource]showByIdFunc{ + resources.Account: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Accounts.ShowByID) + }, + resources.Alert: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Alerts.ShowByID) + }, + resources.ApiIntegration: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.ApiIntegrations.ShowByID) + }, + resources.Database: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Databases.ShowByID) + }, + resources.DatabaseRole: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.DatabaseRoles.ShowByID) + }, + resources.DynamicTable: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.DynamicTables.ShowByID) + }, + resources.EmailNotificationIntegration: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.NotificationIntegrations.ShowByID) + }, + resources.ExternalFunction: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.ExternalFunctions.ShowByID) + }, + resources.ExternalTable: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.ExternalTables.ShowByID) + }, + resources.FailoverGroup: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.FailoverGroups.ShowByID) + }, + resources.FileFormat: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.FileFormats.ShowByID) + }, + resources.Function: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Functions.ShowByID) + }, + resources.ManagedAccount: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.ManagedAccounts.ShowByID) + }, + resources.MaskingPolicy: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.MaskingPolicies.ShowByID) + }, + resources.MaterializedView: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.MaterializedViews.ShowByID) + }, + resources.NetworkPolicy: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.NetworkPolicies.ShowByID) + }, + resources.NotificationIntegration: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.NotificationIntegrations.ShowByID) + }, + resources.PasswordPolicy: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.PasswordPolicies.ShowByID) + }, + resources.Pipe: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Pipes.ShowByID) + }, + resources.Procedure: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Procedures.ShowByID) + }, + resources.ResourceMonitor: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.ResourceMonitors.ShowByID) + }, + resources.Role: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Roles.ShowByID) + }, + resources.RowAccessPolicy: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.RowAccessPolicies.ShowByID) + }, + resources.Schema: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Schemas.ShowByID) + }, + resources.Sequence: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Sequences.ShowByID) + }, + resources.Share: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Shares.ShowByID) + }, + resources.Stage: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Stages.ShowByID) + }, + resources.StorageIntegration: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.StorageIntegrations.ShowByID) + }, + resources.Stream: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Streams.ShowByID) + }, + resources.Table: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Tables.ShowByID) + }, + resources.Tag: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Tags.ShowByID) + }, + resources.Task: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Tasks.ShowByID) + }, + resources.User: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Users.ShowByID) + }, + resources.View: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Views.ShowByID) + }, + resources.Warehouse: func(ctx context.Context, client *sdk.Client, id sdk.ObjectIdentifier) error { + return runShowById(ctx, id, client.Warehouses.ShowByID) + }, +} + +func runShowById[T any, U sdk.AccountObjectIdentifier | sdk.DatabaseObjectIdentifier | sdk.SchemaObjectIdentifier | sdk.TableColumnIdentifier](ctx context.Context, id sdk.ObjectIdentifier, show func(ctx context.Context, id U) (T, error)) error { + idCast, err := asId[U](id) + if err != nil { + return err + } + _, err = show(ctx, *idCast) + return err +} + +func asId[T sdk.AccountObjectIdentifier | sdk.DatabaseObjectIdentifier | sdk.SchemaObjectIdentifier | sdk.TableColumnIdentifier](id sdk.ObjectIdentifier) (*T, error) { + if idCast, ok := id.(T); !ok { + return nil, fmt.Errorf("expected %s identifier type, but got: %T", reflect.TypeOf(new(T)).Elem().Name(), id) + } else { + return &idCast, nil + } +} + +// CheckGrantAccountRoleDestroy is a custom checks that should be later incorporated into generic CheckDestroy +func CheckGrantAccountRoleDestroy(t *testing.T) func(*terraform.State) error { + t.Helper() + client := Client(t) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_grant_account_role" { + continue + } + ctx := context.Background() + parts := strings.Split(rs.Primary.ID, "|") + roleName := parts[0] + roleIdentifier := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(roleName) + objectType := parts[1] + targetIdentifier := parts[2] + grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ + Of: &sdk.ShowGrantsOf{ + Role: roleIdentifier, + }, + }) + if err != nil { + return nil + } + + var found bool + for _, grant := range grants { + if grant.GrantedTo == sdk.ObjectType(objectType) { + if grant.GranteeName.FullyQualifiedName() == targetIdentifier { + found = true + break + } + } + } + if found { + return fmt.Errorf("role grant %v still exists", rs.Primary.ID) + } + } + return nil + } +} + +// CheckGrantDatabaseRoleDestroy is a custom checks that should be later incorporated into generic CheckDestroy +func CheckGrantDatabaseRoleDestroy(t *testing.T) func(*terraform.State) error { + t.Helper() + client := Client(t) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_grant_database_role" { + continue + } + ctx := context.Background() + id := rs.Primary.ID + ids := strings.Split(id, "|") + databaseRoleName := ids[0] + objectType := ids[1] + parentRoleName := ids[2] + grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ + Of: &sdk.ShowGrantsOf{ + DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRoleName), + }, + }) + if err != nil { + continue + } + for _, grant := range grants { + if grant.GrantedTo == sdk.ObjectType(objectType) { + if grant.GranteeName.FullyQualifiedName() == parentRoleName { + return fmt.Errorf("database role grant %v still exists", grant) + } + } + } + } + return nil + } +} + +// CheckAccountRolePrivilegesRevoked is a custom checks that should be later incorporated into generic CheckDestroy +func CheckAccountRolePrivilegesRevoked(t *testing.T) func(*terraform.State) error { + t.Helper() + client := Client(t) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_grant_privileges_to_account_role" { + continue + } + ctx := context.Background() + + id := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["account_role_name"]) + grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ + To: &sdk.ShowGrantsTo{ + Role: id, + }, + }) + if err != nil { + if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { + continue + } + return err + } + var grantedPrivileges []string + for _, grant := range grants { + grantedPrivileges = append(grantedPrivileges, grant.Privilege) + } + if len(grantedPrivileges) > 0 { + return fmt.Errorf("account role (%s) is still granted, granted privileges %v", id.FullyQualifiedName(), grantedPrivileges) + } + } + return nil + } +} + +// CheckDatabaseRolePrivilegesRevoked is a custom checks that should be later incorporated into generic CheckDestroy +func CheckDatabaseRolePrivilegesRevoked(t *testing.T) func(*terraform.State) error { + t.Helper() + client := Client(t) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_grant_privileges_to_database_role" { + continue + } + ctx := context.Background() + + id := sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["database_role_name"]) + grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ + To: &sdk.ShowGrantsTo{ + DatabaseRole: id, + }, + }) + if err != nil { + return err + } + var grantedPrivileges []string + for _, grant := range grants { + // usage is the default privilege available after creation (it won't be revoked) + if grant.Privilege != "USAGE" { + grantedPrivileges = append(grantedPrivileges, grant.Privilege) + } + } + if len(grantedPrivileges) > 0 { + return fmt.Errorf("database role (%s) is still granted, granted privileges %v", id.FullyQualifiedName(), grantedPrivileges) + } + } + return nil + } +} + +// CheckUserPasswordPolicyAttachmentDestroy is a custom checks that should be later incorporated into generic CheckDestroy +func CheckUserPasswordPolicyAttachmentDestroy(t *testing.T) func(*terraform.State) error { + t.Helper() + client := Client(t) + + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "snowflake_user_password_policy_attachment" { + continue + } + ctx := context.Background() + policyReferences, err := client.PolicyReferences.GetForEntity(ctx, sdk.NewGetForEntityPolicyReferenceRequest( + sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["user_name"]), + sdk.PolicyEntityDomainUser, + )) + if err != nil { + if strings.Contains(err.Error(), "does not exist or not authorized") { + // Note: this can happen if the Policy Reference or the User has been deleted as well; in this case, ignore the error + continue + } + return err + } + if len(policyReferences) > 0 { + return fmt.Errorf("user password policy attachment %v still exists", policyReferences[0].PolicyName) + } + } + return nil + } +} diff --git a/pkg/datasources/dynamic_tables_acceptance_test.go b/pkg/datasources/dynamic_tables_acceptance_test.go index d551f9f3ec..cf7ecc6457 100644 --- a/pkg/datasources/dynamic_tables_acceptance_test.go +++ b/pkg/datasources/dynamic_tables_acceptance_test.go @@ -1,21 +1,16 @@ package datasources_test import ( - "context" "fmt" "regexp" "strings" "testing" - "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/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -42,7 +37,7 @@ func TestAcc_DynamicTables_complete(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: nil, Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -145,20 +140,3 @@ data "snowflake_dynamic_tables" "dts" { } ` } - -func testAccCheckDynamicTableDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_dynamic_table" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - dynamicTable, err := client.DynamicTables.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("dynamic table %v still exists", dynamicTable.Name) - } - } - return nil -} diff --git a/pkg/datasources/role.go b/pkg/datasources/role.go index 7e61223d4c..2629ade57e 100644 --- a/pkg/datasources/role.go +++ b/pkg/datasources/role.go @@ -42,7 +42,7 @@ func ReadRole(d *schema.ResourceData, meta interface{}) error { roleName := d.Get("name").(string) - role, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(sdk.NewAccountObjectIdentifier(roleName))) + role, err := client.Roles.ShowByID(ctx, sdk.NewAccountObjectIdentifier(roleName)) if err != nil { log.Printf("[DEBUG] role (%s) not found", roleName) d.SetId("") diff --git a/pkg/provider/resources/resources.go b/pkg/provider/resources/resources.go new file mode 100644 index 0000000000..f0d03daf6d --- /dev/null +++ b/pkg/provider/resources/resources.go @@ -0,0 +1,52 @@ +package resources + +type resource string + +const ( + Account resource = "snowflake_account" + Alert resource = "snowflake_alert" + ApiIntegration resource = "snowflake_api_integration" + Database resource = "snowflake_database" + DatabaseRole resource = "snowflake_database_role" + DynamicTable resource = "snowflake_dynamic_table" + EmailNotificationIntegration resource = "snowflake_email_notification_integration" + ExternalFunction resource = "snowflake_external_function" + ExternalTable resource = "snowflake_external_table" + FailoverGroup resource = "snowflake_failover_group" + FileFormat resource = "snowflake_file_format" + Function resource = "snowflake_function" + ManagedAccount resource = "snowflake_managed_account" + MaskingPolicy resource = "snowflake_masking_policy" + MaterializedView resource = "snowflake_materialized_view" + NetworkPolicy resource = "snowflake_network_policy" + NotificationIntegration resource = "snowflake_notification_integration" + PasswordPolicy resource = "snowflake_password_policy" + Pipe resource = "snowflake_pipe" + Procedure resource = "snowflake_procedure" + ResourceMonitor resource = "snowflake_resource_monitor" + Role resource = "snowflake_role" + RowAccessPolicy resource = "snowflake_row_access_policy" + Schema resource = "snowflake_schema" + Sequence resource = "snowflake_sequence" + Share resource = "snowflake_share" + Stage resource = "snowflake_stage" + StorageIntegration resource = "snowflake_storage_integration" + Stream resource = "snowflake_stream" + Table resource = "snowflake_table" + Tag resource = "snowflake_tag" + Task resource = "snowflake_task" + User resource = "snowflake_user" + View resource = "snowflake_view" + Warehouse resource = "snowflake_warehouse" +) + +type Resource interface { + xxxProtected() + String() string +} + +func (r resource) xxxProtected() {} + +func (r resource) String() string { + return string(r) +} diff --git a/pkg/resources/account_acceptance_test.go b/pkg/resources/account_acceptance_test.go index c7d3933af0..cb97c03ca7 100644 --- a/pkg/resources/account_acceptance_test.go +++ b/pkg/resources/account_acceptance_test.go @@ -8,6 +8,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -25,7 +26,7 @@ func TestAcc_Account_complete(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Account), // this errors with: Error running post-test destroy, there may be dangling resources: exit status 1 // unless we change the resource to return nil on destroy then this is unavoidable Steps: []resource.TestStep{ diff --git a/pkg/resources/alert_acceptance_test.go b/pkg/resources/alert_acceptance_test.go index 162890636f..c768454b82 100644 --- a/pkg/resources/alert_acceptance_test.go +++ b/pkg/resources/alert_acceptance_test.go @@ -10,6 +10,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -102,7 +103,7 @@ func TestAcc_Alert(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Alert), Steps: []resource.TestStep{ { Config: alertConfig(alertInitialState), diff --git a/pkg/resources/api_integration_acceptance_test.go b/pkg/resources/api_integration_acceptance_test.go index 1b357d83e1..250a2c5f01 100644 --- a/pkg/resources/api_integration_acceptance_test.go +++ b/pkg/resources/api_integration_acceptance_test.go @@ -1,20 +1,15 @@ package resources_test import ( - "context" - "fmt" "strings" "testing" - "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/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -58,7 +53,7 @@ func TestAcc_ApiIntegration_aws(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckApiIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ApiIntegration), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -151,7 +146,7 @@ func TestAcc_ApiIntegration_azure(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckApiIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ApiIntegration), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -241,7 +236,7 @@ func TestAcc_ApiIntegration_google(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckApiIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ApiIntegration), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -342,7 +337,7 @@ func TestAcc_ApiIntegration_changeApiProvider(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckApiIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ApiIntegration), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -385,19 +380,3 @@ func TestAcc_ApiIntegration_changeApiProvider(t *testing.T) { }, }) } - -func testAccCheckApiIntegrationDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_api_integration" { - continue - } - ctx := context.Background() - id := sdk.NewAccountObjectIdentifier(rs.Primary.Attributes["name"]) - existingApiIntegration, err := client.ApiIntegrations.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("api integration %v still exists", existingApiIntegration.ID().FullyQualifiedName()) - } - } - return nil -} diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 4afa8f7e51..ee56b5bcac 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -7,10 +7,10 @@ import ( "strings" "testing" - "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/internal/provider" + "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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -30,7 +30,7 @@ func TestAcc_DatabaseWithUnderscore(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { Config: dbConfig(prefix), @@ -56,7 +56,7 @@ func TestAcc_Database(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { Config: dbConfig(prefix), @@ -117,7 +117,7 @@ func TestAcc_DatabaseRemovedOutsideOfTerraform(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseExistence(t, id, false), + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -164,7 +164,7 @@ func TestAcc_Database_issue2021(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { Config: dbConfigWithReplication(name, secondaryAccountName), @@ -205,7 +205,7 @@ func TestAcc_Database_DefaultDataRetentionTime(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, true, "5"), @@ -294,7 +294,7 @@ func TestAcc_Database_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing. TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Database), Steps: []resource.TestStep{ { PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, true, "5"), @@ -398,6 +398,7 @@ func getSecondaryAccount(t *testing.T) string { 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() return func(state *terraform.State) error { @@ -489,7 +490,9 @@ func createDatabaseOutsideTerraform(t *testing.T, name string) func() { } return func() { - if err := client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(name), new(sdk.DropDatabaseOptions)); err != nil { + opts := new(sdk.DropDatabaseOptions) + opts.IfExists = sdk.Bool(true) + if err := client.Databases.Drop(ctx, sdk.NewAccountObjectIdentifier(name), opts); err != nil { if err != nil { t.Fatal(err) } diff --git a/pkg/resources/database_role_acceptance_test.go b/pkg/resources/database_role_acceptance_test.go index a50480b4f6..928e14c060 100644 --- a/pkg/resources/database_role_acceptance_test.go +++ b/pkg/resources/database_role_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -24,7 +25,7 @@ func TestAcc_DatabaseRole(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.DatabaseRole), Steps: []resource.TestStep{ { Config: databaseRoleConfig(dbRoleName, acc.TestDatabaseName, comment), diff --git a/pkg/resources/dynamic_table_acceptance_test.go b/pkg/resources/dynamic_table_acceptance_test.go index 85f35154e6..00b727f10c 100644 --- a/pkg/resources/dynamic_table_acceptance_test.go +++ b/pkg/resources/dynamic_table_acceptance_test.go @@ -6,16 +6,14 @@ import ( "strings" "testing" - "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/provider/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "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/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -54,7 +52,7 @@ func TestAcc_DynamicTable_basic(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -183,7 +181,7 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -257,7 +255,7 @@ func TestAcc_DynamicTable_issue2134(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ /* * Before the fix the first step resulted in not empty plan (as expected) @@ -320,7 +318,7 @@ func TestAcc_DynamicTable_issue2276(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -366,7 +364,7 @@ func TestAcc_DynamicTable_issue2329(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -412,7 +410,7 @@ func TestAcc_DynamicTable_issue2329_with_matching_comment(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.DynamicTable), Steps: []resource.TestStep{ // If we match more than one time (in this case in comment) we raise an explanation error. { @@ -427,22 +425,6 @@ func TestAcc_DynamicTable_issue2329_with_matching_comment(t *testing.T) { }) } -func testAccCheckDynamicTableDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_dynamic_table" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - dynamicTable, err := client.DynamicTables.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("dynamic table %v still exists", dynamicTable.Name) - } - } - return nil -} - // 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() diff --git a/pkg/resources/email_notification_integration_acceptance_test.go b/pkg/resources/email_notification_integration_acceptance_test.go index 489dadc2de..593b07732a 100644 --- a/pkg/resources/email_notification_integration_acceptance_test.go +++ b/pkg/resources/email_notification_integration_acceptance_test.go @@ -1,19 +1,15 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -28,7 +24,7 @@ func TestAcc_EmailNotificationIntegration(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckEmailNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.EmailNotificationIntegration), Steps: []resource.TestStep{ { Config: emailNotificationIntegrationConfig(emailIntegrationName, verifiedEmail), @@ -70,7 +66,7 @@ func TestAcc_EmailNotificationIntegration_issue2223(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckEmailNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.EmailNotificationIntegration), Steps: []resource.TestStep{ { Config: emailNotificationIntegrationWithoutRecipientsConfig(emailIntegrationName), @@ -105,19 +101,3 @@ resource "snowflake_email_notification_integration" "test" { }` return fmt.Sprintf(s, name) } - -func testAccCheckEmailNotificationIntegrationDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_email_notification_integration" { - continue - } - ctx := context.Background() - id := sdk.NewAccountObjectIdentifier(rs.Primary.Attributes["name"]) - existingNotificationIntegration, err := client.NotificationIntegrations.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("notification integration %v still exists", existingNotificationIntegration.ID().FullyQualifiedName()) - } - } - return nil -} diff --git a/pkg/resources/external_function.go b/pkg/resources/external_function.go index 1817967411..5028dac4fb 100644 --- a/pkg/resources/external_function.go +++ b/pkg/resources/external_function.go @@ -326,7 +326,7 @@ func ReadContextExternalFunction(ctx context.Context, d *schema.ResourceData, me client := meta.(*provider.Context).Client id := sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(d.Id()) - externalFunction, err := client.ExternalFunctions.ShowByID(ctx, id.WithoutArguments(), id.Arguments()) + externalFunction, err := client.ExternalFunctions.ShowByID(ctx, id) if err != nil { d.SetId("") return nil diff --git a/pkg/resources/external_function_acceptance_test.go b/pkg/resources/external_function_acceptance_test.go index dcd2783781..d3ef224080 100644 --- a/pkg/resources/external_function_acceptance_test.go +++ b/pkg/resources/external_function_acceptance_test.go @@ -7,6 +7,7 @@ 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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -40,7 +41,7 @@ func TestAcc_ExternalFunction_basic(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_ExternalFunction/basic"), @@ -111,7 +112,7 @@ func TestAcc_ExternalFunction_no_arguments(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_ExternalFunction/no_arguments"), @@ -178,7 +179,7 @@ func TestAcc_ExternalFunction_complete(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_ExternalFunction/complete"), @@ -235,7 +236,7 @@ func TestAcc_ExternalFunction_migrateFromVersion085(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { @@ -282,7 +283,7 @@ func TestAcc_ExternalFunction_migrateFromVersion085_issue2694_previousValuePrese TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { @@ -321,7 +322,7 @@ func TestAcc_ExternalFunction_migrateFromVersion085_issue2694_previousValueRemov TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { @@ -379,7 +380,7 @@ func TestAcc_ExternalFunction_issue2528(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalFunction), Steps: []resource.TestStep{ { Config: externalFunctionConfigIssue2528(acc.TestDatabaseName, acc.TestSchemaName, accName, secondSchema), diff --git a/pkg/resources/external_stage_acceptance_test.go b/pkg/resources/external_stage_acceptance_test.go index 469d2abc53..6f2c22f8a2 100644 --- a/pkg/resources/external_stage_acceptance_test.go +++ b/pkg/resources/external_stage_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -21,7 +22,7 @@ func TestAcc_ExternalStage(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stage), Steps: []resource.TestStep{ { Config: externalStageConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), diff --git a/pkg/resources/external_table.go b/pkg/resources/external_table.go index 386e4f15af..2732ea713c 100644 --- a/pkg/resources/external_table.go +++ b/pkg/resources/external_table.go @@ -256,7 +256,7 @@ func ReadExternalTable(d *schema.ResourceData, meta any) error { ctx := context.Background() id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id)) + externalTable, err := client.ExternalTables.ShowByID(ctx, id) if err != nil { log.Printf("[DEBUG] external table (%s) not found", d.Id()) d.SetId("") diff --git a/pkg/resources/external_table_acceptance_test.go b/pkg/resources/external_table_acceptance_test.go index e1b5ac3e3f..2dfc86a9ed 100644 --- a/pkg/resources/external_table_acceptance_test.go +++ b/pkg/resources/external_table_acceptance_test.go @@ -9,11 +9,11 @@ import ( "strings" "testing" - "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/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" + "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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -65,7 +65,7 @@ func TestAcc_ExternalTable_basic(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckExternalTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -149,7 +149,7 @@ func TestAcc_ExternalTable_CorrectDataTypes(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckExternalTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -207,7 +207,7 @@ func TestAcc_ExternalTable_CanCreateWithPartitions(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckExternalTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -266,7 +266,7 @@ func TestAcc_ExternalTable_DeltaLake(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckExternalTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.ExternalTable), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -297,7 +297,7 @@ func TestAcc_ExternalTable_DeltaLake(t *testing.T) { client := acc.TestAccProvider.Meta().(*provider.Context).Client ctx := context.Background() id := sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, acc.TestSchemaName, name) - result, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id)) + result, err := client.ExternalTables.ShowByID(ctx, id) if err != nil { return err } @@ -425,19 +425,3 @@ func expectTableDDLContains(tableName string, substr string) func(s *terraform.S return nil } } - -func testAccCheckExternalTableDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_external_table" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - dynamicTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id)) - if err == nil { - return fmt.Errorf("external table %v still exists", dynamicTable.Name) - } - } - return nil -} diff --git a/pkg/resources/failover_group_acceptance_test.go b/pkg/resources/failover_group_acceptance_test.go index 74a37fecb2..2f2881ec47 100644 --- a/pkg/resources/failover_group_acceptance_test.go +++ b/pkg/resources/failover_group_acceptance_test.go @@ -8,6 +8,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -26,7 +27,7 @@ func TestAcc_FailoverGroupBasic(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FailoverGroup), Steps: []resource.TestStep{ { Config: failoverGroupBasic(randomCharacters, accountName, acc.TestDatabaseName), @@ -64,7 +65,7 @@ func TestAcc_FailoverGroupRemoveObjectTypes(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FailoverGroup), Steps: []resource.TestStep{ { Config: failoverGroupWithInterval(randomCharacters, accountName, 20, acc.TestDatabaseName), @@ -105,7 +106,7 @@ func TestAcc_FailoverGroupInterval(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FailoverGroup), Steps: []resource.TestStep{ { Config: failoverGroupWithInterval(randomCharacters, accountName, 10, acc.TestDatabaseName), @@ -204,7 +205,7 @@ func TestAcc_FailoverGroup_issue2517(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FailoverGroup), Steps: []resource.TestStep{ { Config: failoverGroupWithAccountParameters(randomCharacters, accountName, acc.TestDatabaseName), @@ -235,7 +236,7 @@ func TestAcc_FailoverGroup_issue2544(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FailoverGroup), Steps: []resource.TestStep{ { Config: failoverGroupBasic(randomCharacters, accountName, acc.TestDatabaseName), diff --git a/pkg/resources/file_format_acceptance_test.go b/pkg/resources/file_format_acceptance_test.go index cb6281e911..a7fc9e4e0f 100644 --- a/pkg/resources/file_format_acceptance_test.go +++ b/pkg/resources/file_format_acceptance_test.go @@ -5,6 +5,8 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -19,7 +21,7 @@ func TestAcc_FileFormatCSV(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigCSV(accName, acc.TestDatabaseName, acc.TestSchemaName, ";", "'", "Terraform acceptance test"), @@ -87,7 +89,7 @@ func TestAcc_FileFormatJSON(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigJSON(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -127,7 +129,7 @@ func TestAcc_FileFormatAvro(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigAvro(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -156,7 +158,7 @@ func TestAcc_FileFormatORC(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigORC(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -184,7 +186,7 @@ func TestAcc_FileFormatParquet(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigParquet(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -214,7 +216,7 @@ func TestAcc_FileFormatXML(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigXML(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -247,7 +249,7 @@ func TestAcc_FileFormatCSVDefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "CSV", acc.TestDatabaseName, acc.TestSchemaName), @@ -276,7 +278,7 @@ func TestAcc_FileFormatJSONDefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "JSON", acc.TestDatabaseName, acc.TestSchemaName), @@ -305,7 +307,7 @@ func TestAcc_FileFormatAVRODefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "AVRO", acc.TestDatabaseName, acc.TestSchemaName), @@ -334,7 +336,7 @@ func TestAcc_FileFormatORCDefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "ORC", acc.TestDatabaseName, acc.TestSchemaName), @@ -363,7 +365,7 @@ func TestAcc_FileFormatPARQUETDefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "PARQUET", acc.TestDatabaseName, acc.TestSchemaName), @@ -392,7 +394,7 @@ func TestAcc_FileFormatXMLDefaults(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(accName, "XML", acc.TestDatabaseName, acc.TestSchemaName), @@ -422,7 +424,7 @@ func TestAcc_FileFormat_issue1947(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.FileFormat), Steps: []resource.TestStep{ { Config: fileFormatConfigFullDefaults(name, "XML", acc.TestDatabaseName, acc.TestSchemaName), diff --git a/pkg/resources/function_acceptance_test.go b/pkg/resources/function_acceptance_test.go index 06ab8d2854..5ecd526463 100644 --- a/pkg/resources/function_acceptance_test.go +++ b/pkg/resources/function_acceptance_test.go @@ -1,18 +1,17 @@ package resources_test import ( - "context" "fmt" "strings" "testing" 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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -43,7 +42,7 @@ func testAccFunction(t *testing.T, configDirectory string) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy(t), + CheckDestroy: acc.CheckDestroy(t, resources.Function), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory(configDirectory), @@ -130,7 +129,7 @@ func TestAcc_Function_complex(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy(t), + CheckDestroy: acc.CheckDestroy(t, resources.Function), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Function/complex"), @@ -193,7 +192,7 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckFunctionDestroy(t), + CheckDestroy: acc.CheckDestroy(t, resources.Function), // Using the string config because of the validation in teststep_validate.go: // teststep.Config.HasConfigurationFiles() returns true both for ConfigFile and ConfigDirectory. @@ -246,22 +245,3 @@ resource "snowflake_function" "f" { } `, database, schema, 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 - } -} diff --git a/pkg/resources/grant_account_role_acceptance_test.go b/pkg/resources/grant_account_role_acceptance_test.go index 3f92dc6a0a..19d44cdb07 100644 --- a/pkg/resources/grant_account_role_acceptance_test.go +++ b/pkg/resources/grant_account_role_acceptance_test.go @@ -1,20 +1,15 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -31,7 +26,7 @@ func TestAcc_GrantAccountRole_accountRole(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: testAccCheckGrantAccountRoleDestroy, + CheckDestroy: acc.CheckGrantAccountRoleDestroy(t), TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, @@ -70,7 +65,7 @@ func TestAcc_GrantAccountRole_user(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: testAccCheckGrantAccountRoleDestroy, + CheckDestroy: acc.CheckGrantAccountRoleDestroy(t), TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, @@ -95,40 +90,3 @@ func TestAcc_GrantAccountRole_user(t *testing.T) { }, }) } - -func testAccCheckGrantAccountRoleDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_grant_account_role" { - continue - } - ctx := context.Background() - parts := strings.Split(rs.Primary.ID, "|") - roleName := parts[0] - roleIdentifier := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(roleName) - objectType := parts[1] - targetIdentifier := parts[2] - grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ - Of: &sdk.ShowGrantsOf{ - Role: roleIdentifier, - }, - }) - if err != nil { - return nil - } - - var found bool - for _, grant := range grants { - if grant.GrantedTo == sdk.ObjectType(objectType) { - if grant.GranteeName.FullyQualifiedName() == targetIdentifier { - found = true - break - } - } - } - if found { - return fmt.Errorf("role grant %v still exists", rs.Primary.ID) - } - } - return nil -} diff --git a/pkg/resources/grant_database_role_acceptance_test.go b/pkg/resources/grant_database_role_acceptance_test.go index e512dcd416..0734d0b90b 100644 --- a/pkg/resources/grant_database_role_acceptance_test.go +++ b/pkg/resources/grant_database_role_acceptance_test.go @@ -1,20 +1,16 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -35,7 +31,7 @@ func TestAcc_GrantDatabaseRole_databaseRole(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckGrantDatabaseRoleDestroy, + CheckDestroy: acc.CheckGrantDatabaseRoleDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/database_role"), @@ -77,7 +73,7 @@ func TestAcc_GrantDatabaseRole_issue2402(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckGrantDatabaseRoleDestroy, + CheckDestroy: acc.CheckGrantDatabaseRoleDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantDatabaseRole/issue2402"), @@ -109,7 +105,7 @@ func TestAcc_GrantDatabaseRole_accountRole(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckGrantDatabaseRoleDestroy, + CheckDestroy: acc.CheckGrantDatabaseRoleDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/account_role"), @@ -153,7 +149,7 @@ func TestAcc_GrantDatabaseRole_share(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckGrantDatabaseRoleDestroy(t), Steps: []resource.TestStep{ { ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/share"), @@ -175,34 +171,3 @@ func TestAcc_GrantDatabaseRole_share(t *testing.T) { }, }) } - -func testAccCheckGrantDatabaseRoleDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_grant_database_role" { - continue - } - ctx := context.Background() - id := rs.Primary.ID - ids := strings.Split(id, "|") - databaseRoleName := ids[0] - objectType := ids[1] - parentRoleName := ids[2] - grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ - Of: &sdk.ShowGrantsOf{ - DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRoleName), - }, - }) - if err != nil { - continue - } - for _, grant := range grants { - if grant.GrantedTo == sdk.ObjectType(objectType) { - if grant.GranteeName.FullyQualifiedName() == parentRoleName { - return fmt.Errorf("database role grant %v still exists", grant) - } - } - } - } - return nil -} diff --git a/pkg/resources/grant_ownership_acceptance_test.go b/pkg/resources/grant_ownership_acceptance_test.go index 3a47df8c72..e32afb2b73 100644 --- a/pkg/resources/grant_ownership_acceptance_test.go +++ b/pkg/resources/grant_ownership_acceptance_test.go @@ -874,10 +874,8 @@ func TestAcc_GrantOwnership_ForceOwnershipTransferOnCreate(t *testing.T) { Steps: []resource.TestStep{ { PreConfig: func() { - createAccountRoleOutsideTerraform(t, accountRoleName) - registerAccountRoleCleanup(t, accountRoleName) - createAccountRoleOutsideTerraform(t, newDatabaseOwningAccountRoleName) - registerAccountRoleCleanup(t, newDatabaseOwningAccountRoleName) + t.Cleanup(createAccountRoleOutsideTerraform(t, accountRoleName)) + t.Cleanup(createAccountRoleOutsideTerraform(t, newDatabaseOwningAccountRoleName)) t.Cleanup(createDatabaseWithRoleAsOwner(t, accountRoleName, databaseName)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantOwnership/ForceOwnershipTransferOnCreate"), diff --git a/pkg/resources/grant_privileges_to_account_role.go b/pkg/resources/grant_privileges_to_account_role.go index fc4a4cc2cd..fbe53ae1e7 100644 --- a/pkg/resources/grant_privileges_to_account_role.go +++ b/pkg/resources/grant_privileges_to_account_role.go @@ -742,7 +742,7 @@ func ReadGrantPrivilegesToAccountRole(ctx context.Context, d *schema.ResourceDat client := meta.(*provider.Context).Client // TODO(SNOW-891217): Use custom error. Right now, "object does not exist" error is hidden in sdk/internal/collections package - if _, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(id.RoleName)); err != nil && err.Error() == "object does not exist" { + if _, err := client.Roles.ShowByID(ctx, id.RoleName); err != nil && err.Error() == "object does not exist" { d.SetId("") return diag.Diagnostics{ diag.Diagnostic{ 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 88a497ede9..d8575f5d8f 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -4,13 +4,10 @@ import ( "context" "errors" "fmt" - "log" "regexp" "strings" "testing" - "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/sdk" @@ -43,10 +40,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccount(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -89,10 +86,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccount_PrivilegesReversed(t *testin TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -137,10 +134,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAccountObject(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -185,10 +182,10 @@ func TestAcc_GrantPrivilegesToApplicationRole_OnAccountObject_InfinitePlan(t *te TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject_InfinitePlan"), ConfigVariables: config.Variables{ "name": config.StringVariable(name), @@ -227,10 +224,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchema(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -263,10 +260,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchema_ExactlyOneOf(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema_ExactlyOneOf"), PlanOnly: true, ExpectError: regexp.MustCompile("Error: Invalid combination of arguments"), @@ -296,10 +293,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnAllSchemasInDatabase(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAllSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -345,10 +342,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnFutureSchemasInDatabase(t *testing.T TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnFutureSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -397,10 +394,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnObject(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -446,10 +443,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnObject_OwnershipPrivi TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported privilege 'OWNERSHIP'"), @@ -480,10 +477,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAll_InDatabase(t *tes TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -530,10 +527,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAllPipes(t *testing.T TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -581,10 +578,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnFuture_InDatabase(t * TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -633,10 +630,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnFuture_Streamlits_InD TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported feature 'STREAMLIT'"), @@ -666,10 +663,10 @@ func TestAcc_GrantPrivilegesToAccountRole_OnSchemaObject_OnAll_Streamlits_InData TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -717,10 +714,10 @@ func TestAcc_GrantPrivilegesToAccountRole_UpdatePrivileges(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/UpdatePrivileges/privileges"), ConfigVariables: configVariables(false, []sdk.AccountObjectPrivilege{ sdk.AccountObjectPrivilegeCreateSchema, @@ -808,10 +805,10 @@ func TestAcc_GrantPrivilegesToAccountRole_UpdatePrivileges_SnowflakeChecked(t *t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/UpdatePrivileges_SnowflakeChecked/privileges"), ConfigVariables: configVariables(false, []string{ sdk.AccountObjectPrivilegeCreateSchema.String(), @@ -883,10 +880,10 @@ func TestAcc_GrantPrivilegesToAccountRole_AlwaysApply(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/AlwaysApply"), ConfigVariables: configVariables(false), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -978,7 +975,7 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivileges(t *testing.T) { }, CheckDestroy: func(state *terraform.State) error { return errors.Join( - testAccCheckAccountRolePrivilegesRevoked(roleName)(state), + acc.CheckAccountRolePrivilegesRevoked(t)(state), dropSharedDatabaseOnSecondaryAccount(t, sharedDatabaseName, shareName), ) }, @@ -1010,9 +1007,9 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivileges(t *testing.T) { // proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/1998 is fixed func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivilegesOnSnowflakeDatabase(t *testing.T) { - roleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) configVariables := config.Variables{ - "role_name": config.StringVariable(roleName), + "role_name": config.StringVariable(name), "privileges": config.ListVariable( config.StringVariable(sdk.AccountObjectPrivilegeImportedPrivileges.String()), ), @@ -1025,10 +1022,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivilegesOnSnowflakeDatabase( TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(roleName), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, roleName) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/ImportedPrivilegesOnSnowflakeDatabase"), ConfigVariables: configVariables, ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -1077,10 +1074,10 @@ func TestAcc_GrantPrivilegesToAccountRole_MultiplePartsInRoleName(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1112,11 +1109,11 @@ func TestAcc_GrantPrivilegesToAccountRole_OnExternalVolume(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { - createAccountRoleOutsideTerraform(t, name) + t.Cleanup(createAccountRoleOutsideTerraform(t, name)) cleanupExternalVolume := createExternalVolume(t, externalVolumeName) t.Cleanup(cleanupExternalVolume) }, @@ -1162,10 +1159,10 @@ func TestAcc_GrantPrivilegesToAccountRole_MLPrivileges(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1212,10 +1209,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ChangeWithGrantOptionsOutsideOfTerrafo TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1268,10 +1265,10 @@ func TestAcc_GrantPrivilegesToAccountRole_ChangeWithGrantOptionsOutsideOfTerrafo TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1376,12 +1373,12 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveGrantedObjectOutsideTerraform(t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { databaseCleanup = createDatabaseOutsideTerraform(t, databaseName) - createAccountRoleOutsideTerraform(t, name) + t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), ConfigVariables: configVariables, @@ -1419,12 +1416,13 @@ func TestAcc_GrantPrivilegesToAccountRole_RemoveAccountRoleOutsideTerraform(t *t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { t.Cleanup(createDatabaseOutsideTerraform(t, databaseName)) roleCleanup = createAccountRoleOutsideTerraform(t, name) + t.Cleanup(roleCleanup) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnAccountObject"), ConfigVariables: configVariables, @@ -1461,7 +1459,7 @@ func TestAcc_GrantPrivilegesToAccountRole_AlwaysApply_SetAfterCreate(t *testing. TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, @@ -1526,49 +1524,8 @@ func createAccountRoleOutsideTerraform(t *testing.T, name string) func() { return func() { if err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId).WithIfExists(true)); err != nil { - t.Fatal(fmt.Errorf("error account role (%s): %w", roleId.FullyQualifiedName(), err)) - } - } -} - -func testAccCheckAccountRolePrivilegesRevoked(name string) func(*terraform.State) error { - return func(state *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - - defer func() { - err := client.Roles.Drop(context.Background(), sdk.NewDropRoleRequest(sdk.NewAccountObjectIdentifier(name)).WithIfExists(true)) - if err != nil { - log.Printf("failed to drop account role (%s), err = %s\n", name, err.Error()) - } - }() - - for _, rs := range state.RootModule().Resources { - if rs.Type != "snowflake_grant_privileges_to_account_role" { - continue - } - ctx := context.Background() - - id := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["account_role_name"]) - grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ - To: &sdk.ShowGrantsTo{ - Role: id, - }, - }) - if err != nil { - if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { - continue - } - return err - } - var grantedPrivileges []string - for _, grant := range grants { - grantedPrivileges = append(grantedPrivileges, grant.Privilege) - } - if len(grantedPrivileges) > 0 { - return fmt.Errorf("account role (%s) is still granted, granted privileges %v", id.FullyQualifiedName(), grantedPrivileges) - } + t.Errorf("error dropping account role (%s): %v", roleId.FullyQualifiedName(), err) } - return nil } } 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 6a8b4a8e0a..d879d40ef3 100644 --- a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go @@ -7,13 +7,11 @@ import ( "strings" "testing" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" - - "github.com/hashicorp/terraform-plugin-testing/helper/acctest" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" + "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/terraform" @@ -43,10 +41,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -94,10 +92,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase_PrivilegesReversed(t *test TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -145,10 +143,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchema(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -180,7 +178,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchema_ExactlyOneOf(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema_ExactlyOneOf"), @@ -213,10 +211,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnAllSchemasInDatabase(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnAllSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -263,10 +261,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnFutureSchemasInDatabase(t *testing. TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnFutureSchemasInDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -316,10 +314,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnObject(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -365,10 +363,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnObject_OwnershipPriv TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnObject"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported privilege 'OWNERSHIP'"), @@ -400,10 +398,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAll_InDatabase(t *te TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -451,10 +449,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAllPipes(t *testing. TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -503,10 +501,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnFuture_InDatabase(t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -553,10 +551,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnFuture_Streamlits_In TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnFuture_InDatabase"), ConfigVariables: configVariables, ExpectError: regexp.MustCompile("Unsupported feature 'STREAMLIT'"), @@ -587,10 +585,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnSchemaObject_OnAll_Streamlits_InDat TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchemaObject_OnAll_InDatabase"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -639,10 +637,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_UpdatePrivileges(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/UpdatePrivileges/privileges"), ConfigVariables: configVariables(false, []sdk.AccountObjectPrivilege{ sdk.AccountObjectPrivilegeCreateSchema, @@ -731,10 +729,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_UpdatePrivileges_SnowflakeChecked(t * TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/UpdatePrivileges_SnowflakeChecked/privileges"), ConfigVariables: configVariables(false, []string{ sdk.AccountObjectPrivilegeCreateSchema.String(), @@ -807,10 +805,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/AlwaysApply"), ConfigVariables: configVariables(false), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -902,10 +900,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_MLPrivileges(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnSchema"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -947,10 +945,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_ChangeWithGrantOptionsOutsideOfTerraf TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -998,10 +996,10 @@ func TestAcc_GrantPrivilegesToDatabaseRole_ChangeWithGrantOptionsOutsideOfTerraf TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, + PreConfig: func() { t.Cleanup(createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name)) }, ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ plancheck.ExpectEmptyPlan(), @@ -1051,11 +1049,13 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveGrantedObjectOutsideTerraform(t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { databaseCleanup = createDatabaseOutsideTerraform(t, databaseName) + t.Cleanup(databaseCleanup) + // no need to clean the role, because database will be dropped createDatabaseRoleOutsideTerraform(t, databaseName, name) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole/OnDatabase"), @@ -1092,7 +1092,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_RemoveDatabaseRoleOutsideTerraform(t TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { @@ -1135,7 +1135,7 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply_SetAfterCreate(t *testing TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked, + CheckDestroy: acc.CheckDatabaseRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { PreConfig: func() { createDatabaseRoleOutsideTerraform(t, acc.TestDatabaseName, name) }, @@ -1187,37 +1187,6 @@ func queriedPrivilegesToDatabaseRoleContainAtLeast(databaseRoleName sdk.Database }, databaseRoleName, privileges...) } -func testAccCheckDatabaseRolePrivilegesRevoked(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_grant_privileges_to_database_role" { - continue - } - ctx := context.Background() - - id := sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["database_role_name"]) - grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{ - To: &sdk.ShowGrantsTo{ - DatabaseRole: id, - }, - }) - if err != nil { - return err - } - var grantedPrivileges []string - for _, grant := range grants { - // usage is the default privilege available after creation (it won't be revoked) - if grant.Privilege != "USAGE" { - grantedPrivileges = append(grantedPrivileges, grant.Privilege) - } - } - if len(grantedPrivileges) > 0 { - return fmt.Errorf("database role (%s) is still granted, granted privileges %v", id.FullyQualifiedName(), grantedPrivileges) - } - } - return nil -} - func revokeAndGrantPrivilegesOnDatabaseToDatabaseRole( t *testing.T, databaseRoleName sdk.DatabaseObjectIdentifier, diff --git a/pkg/resources/grant_privileges_to_role.go b/pkg/resources/grant_privileges_to_role.go index 31f69e3660..21b5ac2e30 100644 --- a/pkg/resources/grant_privileges_to_role.go +++ b/pkg/resources/grant_privileges_to_role.go @@ -828,7 +828,7 @@ func setRolePrivilegeOptions(privileges []string, allPrivileges bool, onAccount } func readRoleGrantPrivileges(ctx context.Context, client *sdk.Client, grantedOn sdk.ObjectType, id GrantPrivilegesToRoleID, opts *sdk.ShowGrantOptions, d *schema.ResourceData) error { - if _, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(sdk.NewAccountObjectIdentifier(id.RoleName))); err != nil && err.Error() == "object does not exist" { + if _, err := client.Roles.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.RoleName)); err != nil && err.Error() == "object does not exist" { d.SetId("") log.Printf("[DEBUG] Failed to retrieve account role. Marking the resource as removed.") return nil diff --git a/pkg/resources/grant_privileges_to_role_acceptance_test.go b/pkg/resources/grant_privileges_to_role_acceptance_test.go index 40a11ee89d..2bce101015 100644 --- a/pkg/resources/grant_privileges_to_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_role_acceptance_test.go @@ -984,10 +984,10 @@ func TestAcc_GrantPrivilegesToRole_OnAllPipes(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: nil, Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToRole/OnAllPipes"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( @@ -1104,7 +1104,7 @@ func TestAcc_GrantPrivilegesToRole_ImportedPrivileges(t *testing.T) { }, CheckDestroy: func(state *terraform.State) error { return errors.Join( - testAccCheckAccountRolePrivilegesRevoked(roleName)(state), + acc.CheckAccountRolePrivilegesRevoked(t)(state), dropSharedDatabaseOnSecondaryAccount(t, sharedDatabaseName, shareName), ) }, @@ -1155,10 +1155,10 @@ func TestAcc_GrantPrivilegesToRole_MultiplePartsInRoleName(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRolePrivilegesRevoked(name), + CheckDestroy: nil, Steps: []resource.TestStep{ { - PreConfig: func() { createAccountRoleOutsideTerraform(t, name) }, + PreConfig: func() { t.Cleanup(createAccountRoleOutsideTerraform(t, name)) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToRole/OnAccount"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( diff --git a/pkg/resources/internal_stage_acceptance_test.go b/pkg/resources/internal_stage_acceptance_test.go index 57f4c62af7..51d30afe7c 100644 --- a/pkg/resources/internal_stage_acceptance_test.go +++ b/pkg/resources/internal_stage_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -21,7 +22,7 @@ func TestAcc_InternalStage(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stage), Steps: []resource.TestStep{ { Config: internalStageConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), diff --git a/pkg/resources/managed_account_acceptance_test.go b/pkg/resources/managed_account_acceptance_test.go index 4bacd652ae..0a6fdab809 100644 --- a/pkg/resources/managed_account_acceptance_test.go +++ b/pkg/resources/managed_account_acceptance_test.go @@ -8,6 +8,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -31,7 +32,7 @@ func TestAcc_ManagedAccount(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ManagedAccount), Steps: []resource.TestStep{ { Config: managedAccountConfig(accName, adminName, adminPass), diff --git a/pkg/resources/masking_policy_acceptance_test.go b/pkg/resources/masking_policy_acceptance_test.go index 2a3b5650e8..2d270a343e 100644 --- a/pkg/resources/masking_policy_acceptance_test.go +++ b/pkg/resources/masking_policy_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" @@ -24,7 +25,7 @@ func TestAcc_MaskingPolicy(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.MaskingPolicy), Steps: []resource.TestStep{ { Config: maskingPolicyConfig(accName, accName, comment, acc.TestDatabaseName, acc.TestSchemaName), @@ -129,7 +130,7 @@ func TestAcc_MaskingPolicyMultiColumns(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.MaskingPolicy), Steps: []resource.TestStep{ { Config: maskingPolicyConfigMultiColumn(accName, accName, acc.TestDatabaseName, acc.TestSchemaName), diff --git a/pkg/resources/materialized_view_acceptance_test.go b/pkg/resources/materialized_view_acceptance_test.go index 5d30e52ea8..c3939e23ee 100644 --- a/pkg/resources/materialized_view_acceptance_test.go +++ b/pkg/resources/materialized_view_acceptance_test.go @@ -6,14 +6,12 @@ import ( "strings" "testing" - "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/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/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" "github.com/stretchr/testify/require" ) @@ -33,7 +31,7 @@ func TestAcc_MaterializedView(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckMaterializedViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.MaterializedView), Steps: []resource.TestStep{ { Config: materializedViewConfig(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "Terraform test resource", true, false), @@ -114,7 +112,7 @@ func TestAcc_MaterializedView_Tags(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckMaterializedViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.MaterializedView), Steps: []resource.TestStep{ // create tags { @@ -158,7 +156,7 @@ func TestAcc_MaterializedView_Rename(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckMaterializedViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.MaterializedView), Steps: []resource.TestStep{ { Config: materializedViewConfig(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "Terraform test resource", true, false), @@ -258,22 +256,6 @@ resource "snowflake_materialized_view" "test" { `, tableName, databaseName, schemaName, viewName, warehouseName, q, tag, tag1Name, tag2Name) } -func testAccCheckMaterializedViewDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_materialized_view" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - existingMaterializedView, err := client.MaterializedViews.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("materialized view %v still exists", existingMaterializedView.ID().FullyQualifiedName()) - } - } - return nil -} - func alterMaterializedViewQueryExternally(t *testing.T, id sdk.SchemaObjectIdentifier, query string, warehouse string) { t.Helper() diff --git a/pkg/resources/network_policy_acceptance_test.go b/pkg/resources/network_policy_acceptance_test.go index 4434279005..d428828bf9 100644 --- a/pkg/resources/network_policy_acceptance_test.go +++ b/pkg/resources/network_policy_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -25,7 +26,7 @@ func TestAcc_NetworkPolicy(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.NetworkPolicy), Steps: []resource.TestStep{ { Config: networkPolicyConfig(name), diff --git a/pkg/resources/notification_integration_acceptance_test.go b/pkg/resources/notification_integration_acceptance_test.go index 649d45c32b..52b13f8560 100644 --- a/pkg/resources/notification_integration_acceptance_test.go +++ b/pkg/resources/notification_integration_acceptance_test.go @@ -1,20 +1,16 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "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/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -30,7 +26,7 @@ func TestAcc_NotificationIntegration_AutoGoogle(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { Config: googleAutoConfig(accName, gcpPubsubSubscriptionName), @@ -78,7 +74,7 @@ func TestAcc_NotificationIntegration_AutoAzure(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { Config: azureAutoConfig(accName, azureStorageQueuePrimaryUri, azureTenantId), @@ -128,7 +124,7 @@ func TestAcc_NotificationIntegration_PushAmazon(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { Config: amazonPushConfig(accName, awsSnsTopicArn, awsSnsRoleArn), @@ -180,7 +176,7 @@ func TestAcc_NotificationIntegration_changeNotificationProvider(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { Config: googleAutoConfig(accName, gcpPubsubSubscriptionName), @@ -233,7 +229,7 @@ func TestAcc_NotificationIntegration_migrateFromVersion085(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { ExternalProviders: map[string]resource.ExternalProvider{ @@ -277,7 +273,7 @@ func TestAcc_NotificationIntegration_migrateFromVersion085_explicitType(t *testi TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckNotificationIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.NotificationIntegration), Steps: []resource.TestStep{ { ExternalProviders: map[string]resource.ExternalProvider{ @@ -367,19 +363,3 @@ resource "snowflake_notification_integration" "test" { ` return fmt.Sprintf(s, name, "AWS_SNS", awsSnsTopicArn, awsSnsRoleArn) } - -func testAccCheckNotificationIntegrationDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_notification_integration" { - continue - } - ctx := context.Background() - id := sdk.NewAccountObjectIdentifier(rs.Primary.Attributes["name"]) - existingNotificationIntegration, err := client.NotificationIntegrations.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("notification integration %v still exists", existingNotificationIntegration.ID().FullyQualifiedName()) - } - } - return nil -} diff --git a/pkg/resources/password_policy_acceptance_test.go b/pkg/resources/password_policy_acceptance_test.go index e3b4e25a4e..ccf44d2878 100644 --- a/pkg/resources/password_policy_acceptance_test.go +++ b/pkg/resources/password_policy_acceptance_test.go @@ -5,6 +5,8 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -42,7 +44,7 @@ func TestAcc_PasswordPolicy(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.PasswordPolicy), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -116,7 +118,7 @@ func TestAcc_PasswordPolicyMaxAgeDays(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.PasswordPolicy), Steps: []resource.TestStep{ // Creation sets zero properly { diff --git a/pkg/resources/pipe_acceptance_test.go b/pkg/resources/pipe_acceptance_test.go index 9d5509d417..63808fce20 100644 --- a/pkg/resources/pipe_acceptance_test.go +++ b/pkg/resources/pipe_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -21,7 +22,7 @@ func TestAcc_Pipe(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Pipe), Steps: []resource.TestStep{ { Config: pipeConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index b886296e2e..89e77c0bed 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -7,6 +7,7 @@ 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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -44,7 +45,7 @@ func testAccProcedure(t *testing.T, configDirectory string) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Procedure), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory(configDirectory), @@ -135,7 +136,7 @@ func TestAcc_Procedure_complex(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckDynamicTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Procedure), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Procedure/complex"), @@ -198,7 +199,7 @@ func TestAcc_Procedure_migrateFromVersion085(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Procedure), Steps: []resource.TestStep{ { diff --git a/pkg/resources/resource_monitor_acceptance_test.go b/pkg/resources/resource_monitor_acceptance_test.go index 8b9d2490be..77162d27e4 100644 --- a/pkg/resources/resource_monitor_acceptance_test.go +++ b/pkg/resources/resource_monitor_acceptance_test.go @@ -10,6 +10,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -26,7 +27,7 @@ func TestAcc_ResourceMonitor(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ResourceMonitor), Steps: []resource.TestStep{ { Config: resourceMonitorConfig(name, acc.TestWarehouseName), @@ -82,7 +83,7 @@ func TestAcc_ResourceMonitorChangeStartEndTimestamp(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ResourceMonitor), Steps: []resource.TestStep{ { Config: resourceMonitorConfigInitialTimestamp(name), @@ -176,7 +177,7 @@ func TestAcc_ResourceMonitorUpdateNotifyUsers(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ResourceMonitor), Steps: []resource.TestStep{ { Config: emptyUsersConfig, @@ -242,7 +243,7 @@ func TestAcc_ResourceMonitor_issue2167(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ResourceMonitor), Steps: []resource.TestStep{ { Config: configNoUsers, @@ -279,7 +280,7 @@ func TestAcc_ResourceMonitorNotifyUsers(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.ResourceMonitor), Steps: []resource.TestStep{ { Config: config, diff --git a/pkg/resources/role.go b/pkg/resources/role.go index 856b6806ac..a149046f00 100644 --- a/pkg/resources/role.go +++ b/pkg/resources/role.go @@ -75,7 +75,7 @@ func ReadAccountRole(ctx context.Context, d *schema.ResourceData, meta any) diag client := meta.(*provider.Context).Client id := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier) - accountRole, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(id)) + accountRole, err := client.Roles.ShowByID(ctx, id) if err != nil { if err.Error() == "object does not exist" { d.SetId("") diff --git a/pkg/resources/role_acceptance_test.go b/pkg/resources/role_acceptance_test.go index a9345c1280..8b0c3781c7 100644 --- a/pkg/resources/role_acceptance_test.go +++ b/pkg/resources/role_acceptance_test.go @@ -1,21 +1,17 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" - "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/hashicorp/terraform-plugin-testing/tfversion" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" ) func TestAcc_Role(t *testing.T) { @@ -28,7 +24,7 @@ func TestAcc_Role(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Role), Steps: []resource.TestStep{ { Config: roleBasicConfig(name, "test comment"), @@ -78,7 +74,7 @@ func TestAcc_AccountRole_basic(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRoleDestroy(name), + CheckDestroy: acc.CheckDestroy(t, resources.Role), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -121,7 +117,7 @@ func TestAcc_AccountRole_updates(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckAccountRoleDestroy(name), + CheckDestroy: acc.CheckDestroy(t, resources.Role), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -153,24 +149,6 @@ func TestAcc_AccountRole_updates(t *testing.T) { }) } -func testAccCheckAccountRoleDestroy(accountRoleName string) func(state *terraform.State) error { - return func(state *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range state.RootModule().Resources { - if rs.Type != "snowflake_role" { - continue - } - ctx := context.Background() - id := sdk.NewAccountObjectIdentifier(rs.Primary.Attributes["name"]) - _, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(id)) - if err == nil { - return fmt.Errorf("account role %v still exists", accountRoleName) - } - } - return nil - } -} - func roleBasicConfig(name, comment string) string { s := ` resource "snowflake_role" "role" { diff --git a/pkg/resources/row_access_policy_acceptance_test.go b/pkg/resources/row_access_policy_acceptance_test.go index 472e05f872..74bc7d0061 100644 --- a/pkg/resources/row_access_policy_acceptance_test.go +++ b/pkg/resources/row_access_policy_acceptance_test.go @@ -1,20 +1,15 @@ package resources_test import ( - "context" - "fmt" "strings" "testing" - "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/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -34,7 +29,7 @@ func TestAcc_RowAccessPolicy(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckRowAccessPolicyDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.RowAccessPolicy), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -87,19 +82,3 @@ func TestAcc_RowAccessPolicy(t *testing.T) { }, }) } - -func testAccCheckRowAccessPolicyDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_row_access_policy" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - existingRowAccessPolicy, err := client.RowAccessPolicies.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("row access policy %v still exists", existingRowAccessPolicy.ID().FullyQualifiedName()) - } - } - return nil -} diff --git a/pkg/resources/schema_acceptance_test.go b/pkg/resources/schema_acceptance_test.go index 2abd3ec539..926861a967 100644 --- a/pkg/resources/schema_acceptance_test.go +++ b/pkg/resources/schema_acceptance_test.go @@ -8,18 +8,18 @@ import ( "strings" "testing" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" - - "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/stretchr/testify/require" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" + "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/config" "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/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" + "github.com/stretchr/testify/require" ) func TestAcc_Schema(t *testing.T) { @@ -32,7 +32,7 @@ func TestAcc_Schema(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -80,7 +80,7 @@ func TestAcc_Schema_Rename(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), @@ -128,7 +128,7 @@ func TestAcc_Schema_TwoSchemasWithTheSameNameOnDifferentDatabases(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -185,7 +185,7 @@ func TestAcc_Schema_DefaultDataRetentionTime(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), @@ -273,7 +273,7 @@ func TestAcc_Schema_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), @@ -322,7 +322,7 @@ func TestAcc_Schema_RemoveDatabaseOutsideOfTerraform(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_RemoveOutsideOfTerraform"), @@ -360,7 +360,7 @@ func TestAcc_Schema_RemoveSchemaOutsideOfTerraform(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckSchemaDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Schema), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_RemoveOutsideOfTerraform"), @@ -436,22 +436,6 @@ func setSchemaDataRetentionTime(t *testing.T, id sdk.DatabaseObjectIdentifier, d } } -func testAccCheckSchemaDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_schema" { - continue - } - ctx := context.Background() - id := sdk.NewDatabaseObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["name"]) - schema, err := client.Schemas.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("schema %v still exists", schema.Name) - } - } - return nil -} - func removeSchemaOutsideOfTerraform(t *testing.T, databaseName string, schemaName string) { t.Helper() diff --git a/pkg/resources/sequence_acceptance_test.go b/pkg/resources/sequence_acceptance_test.go index 2e0534d15b..e8e5dda575 100644 --- a/pkg/resources/sequence_acceptance_test.go +++ b/pkg/resources/sequence_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -22,7 +23,7 @@ func TestAcc_Sequence(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Sequence), Steps: []resource.TestStep{ // CREATE { diff --git a/pkg/resources/share_acceptance_test.go b/pkg/resources/share_acceptance_test.go index 486ad449d7..6c6274ef98 100644 --- a/pkg/resources/share_acceptance_test.go +++ b/pkg/resources/share_acceptance_test.go @@ -7,6 +7,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -27,7 +28,7 @@ func TestAcc_Share(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Share), Steps: []resource.TestStep{ { Config: shareConfig(name, shareComment), diff --git a/pkg/resources/stage_acceptance_test.go b/pkg/resources/stage_acceptance_test.go index f1938e8786..6b3a271f39 100644 --- a/pkg/resources/stage_acceptance_test.go +++ b/pkg/resources/stage_acceptance_test.go @@ -8,6 +8,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -23,7 +24,7 @@ func TestAcc_StageAlterWhenBothURLAndStorageIntegrationChange(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stage), Steps: []resource.TestStep{ { Config: stageIntegrationConfig(name, "si1", "s3://foo/", acc.TestDatabaseName, acc.TestSchemaName), @@ -84,6 +85,7 @@ func TestAcc_Stage_CreateAndAlter(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, + CheckDestroy: acc.CheckDestroy(t, resources.Stage), Steps: []resource.TestStep{ { ConfigDirectory: config.TestNameDirectory(), diff --git a/pkg/resources/storage_integration_acceptance_test.go b/pkg/resources/storage_integration_acceptance_test.go index dc0aa9b4e6..da35931a0c 100644 --- a/pkg/resources/storage_integration_acceptance_test.go +++ b/pkg/resources/storage_integration_acceptance_test.go @@ -1,24 +1,19 @@ package resources_test import ( - "context" - "fmt" "regexp" "testing" - "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/acceptance/testenvs" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/go-uuid" "github.com/hashicorp/terraform-plugin-testing/config" - "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/require" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" + "github.com/stretchr/testify/require" ) func TestAcc_StorageIntegration_Empty_StorageAllowedLocations(t *testing.T) { @@ -28,7 +23,7 @@ func TestAcc_StorageIntegration_Empty_StorageAllowedLocations(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckStorageIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.StorageIntegration), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_StorageIntegration/Empty_StorageAllowedLocations"), @@ -59,7 +54,7 @@ func TestAcc_StorageIntegration_AWSObjectACL_Update(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckStorageIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.StorageIntegration), Steps: []resource.TestStep{ { ConfigVariables: configVariables(false), @@ -124,7 +119,7 @@ func TestAcc_StorageIntegration_AWS_Update(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckStorageIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.StorageIntegration), Steps: []resource.TestStep{ { ConfigVariables: configVariables(false), @@ -209,7 +204,7 @@ func TestAcc_StorageIntegration_Azure_Update(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckStorageIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.StorageIntegration), Steps: []resource.TestStep{ { ConfigVariables: configVariables(false), @@ -286,7 +281,7 @@ func TestAcc_StorageIntegration_GCP_Update(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckStorageIntegrationDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.StorageIntegration), Steps: []resource.TestStep{ { ConfigVariables: configVariables(false), @@ -330,19 +325,3 @@ func TestAcc_StorageIntegration_GCP_Update(t *testing.T) { }, }) } - -func testAccCheckStorageIntegrationDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_storage_integration" { - continue - } - ctx := context.Background() - id := sdk.NewAccountObjectIdentifier(rs.Primary.Attributes["name"]) - storageInt, err := client.StorageIntegrations.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("storage integration %v still exists", storageInt.Name) - } - } - return nil -} diff --git a/pkg/resources/stream.go b/pkg/resources/stream.go index da8b2ae833..294b95a9cf 100644 --- a/pkg/resources/stream.go +++ b/pkg/resources/stream.go @@ -223,7 +223,7 @@ func ReadStream(d *schema.ResourceData, meta interface{}) error { client := meta.(*provider.Context).Client ctx := context.Background() id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier) - stream, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + stream, err := client.Streams.ShowByID(ctx, id) if err != nil { log.Printf("[DEBUG] stream (%s) not found", d.Id()) d.SetId("") diff --git a/pkg/resources/stream_acceptance_test.go b/pkg/resources/stream_acceptance_test.go index a979835967..b9a7ae19fd 100644 --- a/pkg/resources/stream_acceptance_test.go +++ b/pkg/resources/stream_acceptance_test.go @@ -9,6 +9,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" @@ -24,7 +25,7 @@ func TestAcc_StreamCreateOnStageWithoutDirectoryEnabled(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stream), Steps: []resource.TestStep{ { Config: stageStreamConfig(accName, false), @@ -42,7 +43,7 @@ func TestAcc_StreamCreateOnStage(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stream), Steps: []resource.TestStep{ { Config: stageStreamConfig(accName, true), @@ -72,7 +73,7 @@ func TestAcc_Stream_OnTable(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stream), Steps: []resource.TestStep{ { Config: streamConfigOnTable(acc.TestDatabaseName, acc.TestSchemaName, tableName, name), @@ -116,7 +117,7 @@ func TestAcc_Stream_OnView(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stream), Steps: []resource.TestStep{ { Config: streamConfigOnView(acc.TestDatabaseName, acc.TestSchemaName, tableName, viewName, name), @@ -150,7 +151,7 @@ func TestAcc_Stream(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Stream), Steps: []resource.TestStep{ { Config: streamConfig(accName, false), diff --git a/pkg/resources/table_acceptance_test.go b/pkg/resources/table_acceptance_test.go index 0356dd8c20..cfe494f821 100644 --- a/pkg/resources/table_acceptance_test.go +++ b/pkg/resources/table_acceptance_test.go @@ -8,19 +8,18 @@ import ( "strings" "testing" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" - - "github.com/hashicorp/terraform-plugin-testing/config" - "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/stretchr/testify/require" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" + "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/config" "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/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" + "github.com/stretchr/testify/require" ) func TestAcc_TableWithSeparateDataRetentionObjectParameterWithoutLifecycle(t *testing.T) { @@ -32,7 +31,7 @@ func TestAcc_TableWithSeparateDataRetentionObjectParameterWithoutLifecycle(t *te TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -81,7 +80,7 @@ func TestAcc_TableWithSeparateDataRetentionObjectParameterWithLifecycle(t *testi TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -149,7 +148,7 @@ func TestAcc_Table(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfig(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -893,7 +892,7 @@ func TestAcc_TableDefaults(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableColumnWithDefaults(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -1034,7 +1033,7 @@ func TestAcc_TableTags(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableWithTags(accName, tagName, tag2Name, acc.TestDatabaseName, acc.TestSchemaName), @@ -1112,7 +1111,7 @@ func TestAcc_TableIdentity(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableColumnWithIdentityDefault(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -1249,7 +1248,7 @@ func TestAcc_TableCollate(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableColumnWithCollate(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -1409,7 +1408,7 @@ func TestAcc_TableRename(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfigWithName(oldTableName, acc.TestDatabaseName, acc.TestSchemaName), @@ -1465,7 +1464,7 @@ func TestAcc_Table_MaskingPolicy(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableWithMaskingPolicy(accName, acc.TestDatabaseName, acc.TestSchemaName, "policy1"), @@ -1520,7 +1519,7 @@ func TestAcc_Table_DefaultDataRetentionTime(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), @@ -1619,7 +1618,7 @@ func TestAcc_Table_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), @@ -1689,7 +1688,7 @@ func TestAcc_Table_DefaultDataRetentionTimeSettingUnsetting(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), @@ -1781,22 +1780,6 @@ resource "snowflake_table" "test_table" { return fmt.Sprintf(s, name, databaseName, schemaName, policy) } -func testAccCheckTableDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_table" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - existingTable, err := client.Tables.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("table %v still exists", existingTable.ID().FullyQualifiedName()) - } - } - return nil -} - // proves issues https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2110 and https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2495 func TestAcc_Table_ClusterBy(t *testing.T) { accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) @@ -1807,7 +1790,7 @@ func TestAcc_Table_ClusterBy(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfigWithComplexClusterBy(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -1854,7 +1837,7 @@ func TestAcc_ColumnTypeChangeWithNonTextType(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckTableDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.Table), Steps: []resource.TestStep{ { Config: tableConfigWithNumberColumnType(accName, acc.TestDatabaseName, acc.TestSchemaName, "NUMBER(38,0)"), diff --git a/pkg/resources/tag_acceptance_test.go b/pkg/resources/tag_acceptance_test.go index 727f4f8823..8d54ddd1fc 100644 --- a/pkg/resources/tag_acceptance_test.go +++ b/pkg/resources/tag_acceptance_test.go @@ -6,6 +6,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -37,8 +38,7 @@ func TestAcc_Tag(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - // todo: implement CheckDestroy (SNOW-1165865) - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Tag), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Tag/basic"), diff --git a/pkg/resources/tag_masking_policy_association_acceptance_test.go b/pkg/resources/tag_masking_policy_association_acceptance_test.go index 65df3c3981..f3627c240e 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(t), + CheckDestroy: nil, Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_TagMaskingPolicyAssociation/basic"), diff --git a/pkg/resources/task_acceptance_test.go b/pkg/resources/task_acceptance_test.go index 8450caf84c..65ed39f57f 100644 --- a/pkg/resources/task_acceptance_test.go +++ b/pkg/resources/task_acceptance_test.go @@ -9,6 +9,7 @@ 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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -197,7 +198,7 @@ func TestAcc_Task(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Task), Steps: []resource.TestStep{ { Config: taskConfig(initialState), @@ -427,7 +428,7 @@ todo: this test is failing due to error message below. Need to figure out why th tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Task), Steps: []resource.TestStep{ { Config: taskConfigManaged1(accName, acc.TestDatabaseName, acc.TestSchemaName), @@ -555,7 +556,7 @@ func TestAcc_Task_SwitchScheduled(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Task), Steps: []resource.TestStep{ { Config: taskConfigManagedScheduled(accName, taskRootName, acc.TestDatabaseName, acc.TestSchemaName), @@ -705,7 +706,7 @@ func TestAcc_Task_issue2207(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Task), Steps: []resource.TestStep{ { ConfigDirectory: config.TestStepDirectory(), @@ -751,7 +752,7 @@ func TestAcc_Task_issue2036(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Task), Steps: []resource.TestStep{ // create without when { diff --git a/pkg/resources/user_acceptance_test.go b/pkg/resources/user_acceptance_test.go index 8d94738bf9..a6865de0d8 100644 --- a/pkg/resources/user_acceptance_test.go +++ b/pkg/resources/user_acceptance_test.go @@ -9,16 +9,16 @@ import ( "strings" "testing" - "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/provider/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" - "github.com/hashicorp/terraform-plugin-testing/tfversion" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/testhelpers" "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/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfversion" "github.com/stretchr/testify/require" ) @@ -52,7 +52,7 @@ func TestAcc_User(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.User), Steps: []resource.TestStep{ { Config: uConfig(prefix, sshkey1, sshkey2), @@ -249,7 +249,7 @@ func TestAcc_User_issue2058(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.User), Steps: []resource.TestStep{ { Config: uConfig(prefix, sshkey1, sshkey2), diff --git a/pkg/resources/user_password_policy_attachment_acceptance_test.go b/pkg/resources/user_password_policy_attachment_acceptance_test.go index 285593ba1d..156b8d2e4f 100644 --- a/pkg/resources/user_password_policy_attachment_acceptance_test.go +++ b/pkg/resources/user_password_policy_attachment_acceptance_test.go @@ -1,19 +1,15 @@ package resources_test import ( - "context" "fmt" "strings" "testing" - "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/sdk" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" ) func TestAcc_UserPasswordPolicyAttachment(t *testing.T) { @@ -25,7 +21,7 @@ func TestAcc_UserPasswordPolicyAttachment(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, PreCheck: func() { acc.TestAccPreCheck(t) }, - CheckDestroy: testAccCheckUserPasswordPolicyAttachmentDestroy, + CheckDestroy: acc.CheckUserPasswordPolicyAttachmentDestroy(t), Steps: []resource.TestStep{ // CREATE { @@ -55,31 +51,6 @@ func TestAcc_UserPasswordPolicyAttachment(t *testing.T) { }) } -func testAccCheckUserPasswordPolicyAttachmentDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - ctx := context.Background() - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_user_password_policy_attachment" { - continue - } - policyReferences, err := client.PolicyReferences.GetForEntity(ctx, sdk.NewGetForEntityPolicyReferenceRequest( - sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["user_name"]), - sdk.PolicyEntityDomainUser, - )) - if err != nil { - if strings.Contains(err.Error(), "does not exist or not authorized") { - // Note: this can happen if the Policy Reference or the User has been deleted as well; in this case, ignore the error - continue - } - return err - } - if len(policyReferences) > 0 { - return fmt.Errorf("user password policy attachment %v still exists", policyReferences[0].PolicyName) - } - } - return nil -} - func userPasswordPolicyAttachmentConfig(userName, databaseName, schemaName, passwordPolicyName string) string { return fmt.Sprintf(` resource "snowflake_user" "user" { diff --git a/pkg/resources/view_acceptance_test.go b/pkg/resources/view_acceptance_test.go index fd6887bd48..85f4d9040d 100644 --- a/pkg/resources/view_acceptance_test.go +++ b/pkg/resources/view_acceptance_test.go @@ -9,14 +9,12 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" + "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/config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -51,7 +49,7 @@ func TestAcc_View(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_View_basic"), @@ -148,7 +146,7 @@ func TestAcc_View_Tags(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ // create tags { @@ -211,7 +209,7 @@ func TestAcc_View_Rename(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_View_basic"), @@ -260,7 +258,7 @@ func TestAcc_ViewChangeCopyGrants(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_View_basic"), @@ -321,7 +319,7 @@ func TestAcc_ViewChangeCopyGrantsReversed(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { ConfigDirectory: acc.ConfigurationDirectory("TestAcc_View_basic"), @@ -362,7 +360,7 @@ func TestAcc_ViewStatementUpdate(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { Config: viewConfigWithGrants(acc.TestDatabaseName, acc.TestSchemaName, tableName, viewName, `\"name\"`), @@ -393,7 +391,7 @@ func TestAcc_View_copyGrants(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { Config: viewConfigWithCopyGrants(acc.TestDatabaseName, acc.TestSchemaName, accName, query, true), @@ -427,7 +425,7 @@ func TestAcc_View_Issue2640(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: testAccCheckViewDestroy, + CheckDestroy: acc.CheckDestroy(t, resources.View), Steps: []resource.TestStep{ { Config: viewConfigWithMultilineUnionStatement(acc.TestDatabaseName, acc.TestSchemaName, viewName, part1, part2), @@ -441,8 +439,7 @@ 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() { - createAccountRoleOutsideTerraform(t, roleName) - registerAccountRoleCleanup(t, roleName) + t.Cleanup(createAccountRoleOutsideTerraform(t, roleName)) alterViewOwnershipExternally(t, viewName, roleName) }, ResourceName: "snowflake_view.test", @@ -564,22 +561,6 @@ SQL `, databaseName, schemaName, name, part1, part2) } -func testAccCheckViewDestroy(s *terraform.State) error { - client := acc.TestAccProvider.Meta().(*provider.Context).Client - for _, rs := range s.RootModule().Resources { - if rs.Type != "snowflake_view" { - continue - } - ctx := context.Background() - id := sdk.NewSchemaObjectIdentifier(rs.Primary.Attributes["database"], rs.Primary.Attributes["schema"], rs.Primary.Attributes["name"]) - existingView, err := client.Views.ShowByID(ctx, id) - if err == nil { - return fmt.Errorf("view %v still exists", existingView.ID().FullyQualifiedName()) - } - } - return nil -} - func alterViewQueryExternally(t *testing.T, id sdk.SchemaObjectIdentifier, query string) { t.Helper() @@ -590,25 +571,6 @@ func alterViewQueryExternally(t *testing.T, id sdk.SchemaObjectIdentifier, query require.NoError(t, err) } -func registerAccountRoleCleanup(t *testing.T, roleName string) { - t.Helper() - - roleId := sdk.NewAccountObjectIdentifier(roleName) - - 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)) - if err != nil { - t.Logf("failed to drop account role (%s), err = %s\n", roleName, err.Error()) - } - assert.Nil(t, err) - }) -} - func alterViewOwnershipExternally(t *testing.T, viewName string, roleName string) { t.Helper() diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 7e732d1b3c..a027e6c5be 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -8,6 +8,7 @@ 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" @@ -26,7 +27,7 @@ func TestAcc_Warehouse(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Warehouse), Steps: []resource.TestStep{ { Config: wConfig(prefix), @@ -118,7 +119,7 @@ func TestAcc_WarehousePattern(t *testing.T) { TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.RequireAbove(tfversion.Version1_5_0), }, - CheckDestroy: nil, + CheckDestroy: acc.CheckDestroy(t, resources.Warehouse), Steps: []resource.TestStep{ { Config: wConfigPattern(prefix), diff --git a/pkg/sdk/external_functions_gen.go b/pkg/sdk/external_functions_gen.go index c671a237e4..d201f65dfe 100644 --- a/pkg/sdk/external_functions_gen.go +++ b/pkg/sdk/external_functions_gen.go @@ -9,7 +9,7 @@ type ExternalFunctions interface { Create(ctx context.Context, request *CreateExternalFunctionRequest) error Alter(ctx context.Context, request *AlterExternalFunctionRequest) error Show(ctx context.Context, request *ShowExternalFunctionRequest) ([]ExternalFunction, error) - ShowByID(ctx context.Context, id SchemaObjectIdentifier, arguments []DataType) (*ExternalFunction, error) + ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*ExternalFunction, error) Describe(ctx context.Context, request *DescribeExternalFunctionRequest) ([]ExternalFunctionProperty, error) } diff --git a/pkg/sdk/external_functions_impl_gen.go b/pkg/sdk/external_functions_impl_gen.go index 03b94e514e..3f1838e270 100644 --- a/pkg/sdk/external_functions_impl_gen.go +++ b/pkg/sdk/external_functions_impl_gen.go @@ -33,7 +33,8 @@ func (v *externalFunctions) Show(ctx context.Context, request *ShowExternalFunct return resultList, nil } -func (v *externalFunctions) ShowByID(ctx context.Context, id SchemaObjectIdentifier, arguments []DataType) (*ExternalFunction, error) { +func (v *externalFunctions) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*ExternalFunction, error) { + arguments := id.Arguments() externalFunctions, err := v.Show(ctx, NewShowExternalFunctionRequest(). WithIn(&In{Schema: NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())}). WithLike(&Like{Pattern: String(id.Name())})) @@ -72,7 +73,7 @@ func (r *CreateExternalFunctionRequest) toOpts() *CreateExternalFunctionOptions opts := &CreateExternalFunctionOptions{ OrReplace: r.OrReplace, Secure: r.Secure, - name: r.name, + name: r.name.WithoutArguments(), ResultDataType: r.ResultDataType, ReturnNullValues: r.ReturnNullValues, @@ -114,7 +115,7 @@ func (r *CreateExternalFunctionRequest) toOpts() *CreateExternalFunctionOptions func (r *AlterExternalFunctionRequest) toOpts() *AlterExternalFunctionOptions { opts := &AlterExternalFunctionOptions{ IfExists: r.IfExists, - name: r.name, + name: r.name.WithoutArguments(), ArgumentDataTypes: r.ArgumentDataTypes, } if r.Set != nil { diff --git a/pkg/sdk/external_tables.go b/pkg/sdk/external_tables.go index e6ebff779e..387fe0067e 100644 --- a/pkg/sdk/external_tables.go +++ b/pkg/sdk/external_tables.go @@ -22,7 +22,7 @@ type ExternalTables interface { AlterPartitions(ctx context.Context, req *AlterExternalTablePartitionRequest) error Drop(ctx context.Context, req *DropExternalTableRequest) error Show(ctx context.Context, req *ShowExternalTableRequest) ([]ExternalTable, error) - ShowByID(ctx context.Context, req *ShowExternalTableByIDRequest) (*ExternalTable, error) + ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*ExternalTable, error) DescribeColumns(ctx context.Context, req *DescribeExternalTableColumnsRequest) ([]ExternalTableColumnDetails, error) DescribeStage(ctx context.Context, req *DescribeExternalTableStageRequest) ([]ExternalTableStageDetails, error) } diff --git a/pkg/sdk/external_tables_dto.go b/pkg/sdk/external_tables_dto.go index 5aceccbc24..77a5caaa36 100644 --- a/pkg/sdk/external_tables_dto.go +++ b/pkg/sdk/external_tables_dto.go @@ -680,10 +680,6 @@ func (v *ShowExternalTableRequest) toOpts() *ShowExternalTableOptions { } } -type ShowExternalTableByIDRequest struct { - id SchemaObjectIdentifier // required -} - type DescribeExternalTableColumnsRequest struct { id SchemaObjectIdentifier // required } diff --git a/pkg/sdk/external_tables_dto_builders_gen.go b/pkg/sdk/external_tables_dto_builders_gen.go index a466ac8b54..e43109efe3 100644 --- a/pkg/sdk/external_tables_dto_builders_gen.go +++ b/pkg/sdk/external_tables_dto_builders_gen.go @@ -674,14 +674,6 @@ func (s *ShowExternalTableInRequest) WithSchema(schema DatabaseObjectIdentifier) return s } -func NewShowExternalTableByIDRequest( - id SchemaObjectIdentifier, -) *ShowExternalTableByIDRequest { - s := ShowExternalTableByIDRequest{} - s.id = id - return &s -} - func NewDescribeExternalTableColumnsRequest( id SchemaObjectIdentifier, ) *DescribeExternalTableColumnsRequest { diff --git a/pkg/sdk/external_tables_impl.go b/pkg/sdk/external_tables_impl.go index 1afc0b6619..039d1f4aec 100644 --- a/pkg/sdk/external_tables_impl.go +++ b/pkg/sdk/external_tables_impl.go @@ -49,19 +49,19 @@ func (v *externalTables) Show(ctx context.Context, req *ShowExternalTableRequest return resultList, nil } -func (v *externalTables) ShowByID(ctx context.Context, req *ShowExternalTableByIDRequest) (*ExternalTable, error) { - if !ValidObjectIdentifier(req.id) { +func (v *externalTables) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*ExternalTable, error) { + if !ValidObjectIdentifier(id) { return nil, ErrInvalidObjectIdentifier } externalTables, err := v.client.ExternalTables.Show(ctx, NewShowExternalTableRequest(). - WithIn(NewShowExternalTableInRequest().WithSchema(NewDatabaseObjectIdentifier(req.id.DatabaseName(), req.id.SchemaName()))). - WithLike(String(req.id.Name()))) + WithIn(NewShowExternalTableInRequest().WithSchema(NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName()))). + WithLike(String(id.Name()))) if err != nil { return nil, err } - return collections.FindOne(externalTables, func(t ExternalTable) bool { return t.ID().FullyQualifiedName() == req.id.FullyQualifiedName() }) + return collections.FindOne(externalTables, func(t ExternalTable) bool { return t.ID().FullyQualifiedName() == id.FullyQualifiedName() }) } func (v *externalTables) DescribeColumns(ctx context.Context, req *DescribeExternalTableColumnsRequest) ([]ExternalTableColumnDetails, error) { diff --git a/pkg/sdk/roles.go b/pkg/sdk/roles.go index d0c32d2acc..46a6831e3c 100644 --- a/pkg/sdk/roles.go +++ b/pkg/sdk/roles.go @@ -11,7 +11,7 @@ type Roles interface { Alter(ctx context.Context, req *AlterRoleRequest) error Drop(ctx context.Context, req *DropRoleRequest) error Show(ctx context.Context, req *ShowRoleRequest) ([]Role, error) - ShowByID(ctx context.Context, req *ShowRoleByIdRequest) (*Role, error) + ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Role, error) Grant(ctx context.Context, req *GrantRoleRequest) error Revoke(ctx context.Context, req *RevokeRoleRequest) error Use(ctx context.Context, req *UseRoleRequest) error diff --git a/pkg/sdk/roles_dto.go b/pkg/sdk/roles_dto.go index 0300d36d6c..47ef8bff79 100644 --- a/pkg/sdk/roles_dto.go +++ b/pkg/sdk/roles_dto.go @@ -176,16 +176,6 @@ func (s *ShowRoleRequest) toOpts() *ShowRoleOptions { } } -type ShowRoleByIdRequest struct { - id AccountObjectIdentifier // required -} - -func NewShowByIdRoleRequest(id AccountObjectIdentifier) *ShowRoleByIdRequest { - return &ShowRoleByIdRequest{ - id: id, - } -} - type GrantRoleRequest struct { name AccountObjectIdentifier // required Grant GrantRole // required diff --git a/pkg/sdk/roles_impl.go b/pkg/sdk/roles_impl.go index c6ce1dbabb..43c848734b 100644 --- a/pkg/sdk/roles_impl.go +++ b/pkg/sdk/roles_impl.go @@ -36,12 +36,12 @@ func (v *roles) Show(ctx context.Context, req *ShowRoleRequest) ([]Role, error) return resultList, nil } -func (v *roles) ShowByID(ctx context.Context, req *ShowRoleByIdRequest) (*Role, error) { - roleList, err := v.client.Roles.Show(ctx, NewShowRoleRequest().WithLike(NewLikeRequest(req.id.Name()))) +func (v *roles) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Role, error) { + roleList, err := v.client.Roles.Show(ctx, NewShowRoleRequest().WithLike(NewLikeRequest(id.Name()))) if err != nil { return nil, err } - return collections.FindOne(roleList, func(r Role) bool { return r.ID().name == req.id.Name() }) + return collections.FindOne(roleList, func(r Role) bool { return r.ID().name == id.Name() }) } func (v *roles) Grant(ctx context.Context, req *GrantRoleRequest) error { diff --git a/pkg/sdk/streams_dto_builders_gen.go b/pkg/sdk/streams_dto_builders_gen.go index 95407a110f..7e32402fab 100644 --- a/pkg/sdk/streams_dto_builders_gen.go +++ b/pkg/sdk/streams_dto_builders_gen.go @@ -300,14 +300,6 @@ func (s *ShowStreamRequest) WithLimit(Limit *LimitFrom) *ShowStreamRequest { return s } -func NewShowByIdStreamRequest( - name SchemaObjectIdentifier, -) *ShowByIdStreamRequest { - s := ShowByIdStreamRequest{} - s.name = name - return &s -} - func NewDescribeStreamRequest( name SchemaObjectIdentifier, ) *DescribeStreamRequest { diff --git a/pkg/sdk/streams_gen.go b/pkg/sdk/streams_gen.go index 2980b75d48..301c9afeed 100644 --- a/pkg/sdk/streams_gen.go +++ b/pkg/sdk/streams_gen.go @@ -15,7 +15,7 @@ type Streams interface { Alter(ctx context.Context, request *AlterStreamRequest) error Drop(ctx context.Context, request *DropStreamRequest) error Show(ctx context.Context, request *ShowStreamRequest) ([]Stream, error) - ShowByID(ctx context.Context, request *ShowByIdStreamRequest) (*Stream, error) + ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Stream, error) Describe(ctx context.Context, request *DescribeStreamRequest) (*Stream, error) } diff --git a/pkg/sdk/streams_impl_gen.go b/pkg/sdk/streams_impl_gen.go index fcbbf46f15..689a1b4f4d 100644 --- a/pkg/sdk/streams_impl_gen.go +++ b/pkg/sdk/streams_impl_gen.go @@ -57,16 +57,16 @@ func (v *streams) Show(ctx context.Context, request *ShowStreamRequest) ([]Strea return resultList, nil } -func (v *streams) ShowByID(ctx context.Context, request *ShowByIdStreamRequest) (*Stream, error) { +func (v *streams) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Stream, error) { streams, err := v.Show(ctx, NewShowStreamRequest(). WithIn(&In{ - Schema: NewDatabaseObjectIdentifier(request.name.DatabaseName(), request.name.SchemaName()), + Schema: NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName()), }). - WithLike(&Like{Pattern: String(request.name.Name())})) + WithLike(&Like{Pattern: String(id.Name())})) if err != nil { return nil, err } - return collections.FindOne(streams, func(r Stream) bool { return r.Name == request.name.Name() }) + return collections.FindOne(streams, func(r Stream) bool { return r.Name == id.Name() }) } func (v *streams) Describe(ctx context.Context, request *DescribeStreamRequest) (*Stream, error) { diff --git a/pkg/sdk/testint/external_functions_integration_test.go b/pkg/sdk/testint/external_functions_integration_test.go index b996c1e73a..dfd3fe6f35 100644 --- a/pkg/sdk/testint/external_functions_integration_test.go +++ b/pkg/sdk/testint/external_functions_integration_test.go @@ -21,17 +21,17 @@ func TestInt_ExternalFunctions(t *testing.T) { integration, integrationCleanup := createApiIntegration(t, client) t.Cleanup(integrationCleanup) - cleanupExternalFuncionHandle := func(id sdk.SchemaObjectIdentifier, dts []sdk.DataType) func() { + cleanupExternalFunctionHandle := func(id sdk.SchemaObjectIdentifier, dts []sdk.DataType) func() { return func() { - err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id, dts).WithIfExists(sdk.Bool(true))) + err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id.WithoutArguments(), dts).WithIfExists(sdk.Bool(true))) require.NoError(t, err) } } - createExternalFunction := func(t *testing.T, dt sdk.DataType) *sdk.ExternalFunction { + createExternalFunction := func(t *testing.T) *sdk.ExternalFunction { t.Helper() - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(4)) - argument := sdk.NewExternalFunctionArgumentRequest("x", dt) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, random.StringN(4), defaultDataTypes) + argument := sdk.NewExternalFunctionArgumentRequest("x", defaultDataTypes[0]) as := "https://xyz.execute-api.us-west-2.amazonaws.com/production/remote_echo" request := sdk.NewCreateExternalFunctionRequest(id, sdk.DataTypeVariant, &integration, as). WithOrReplace(sdk.Bool(true)). @@ -39,17 +39,18 @@ func TestInt_ExternalFunctions(t *testing.T) { WithArguments([]sdk.ExternalFunctionArgumentRequest{*argument}) err := client.ExternalFunctions.Create(ctx, request) require.NoError(t, err) - t.Cleanup(cleanupExternalFuncionHandle(id, []sdk.DataType{sdk.DataTypeVariant})) + t.Cleanup(cleanupExternalFunctionHandle(id.WithoutArguments(), []sdk.DataType{sdk.DataTypeVariant})) - e, err := client.ExternalFunctions.ShowByID(ctx, id, defaultDataTypes) + e, err := client.ExternalFunctions.ShowByID(ctx, id) require.NoError(t, err) return e } - assertExternalFunction := func(t *testing.T, id sdk.SchemaObjectIdentifier, secure bool, dts []sdk.DataType) { + assertExternalFunction := func(t *testing.T, id sdk.SchemaObjectIdentifier, secure bool) { t.Helper() + dts := id.Arguments() - e, err := client.ExternalFunctions.ShowByID(ctx, id, dts) + e, err := client.ExternalFunctions.ShowByID(ctx, id) require.NoError(t, err) require.NotEmpty(t, e.CreatedOn) @@ -78,7 +79,7 @@ func TestInt_ExternalFunctions(t *testing.T) { } t.Run("create external function", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(4)) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, random.StringN(4), defaultDataTypes) argument := sdk.NewExternalFunctionArgumentRequest("x", sdk.DataTypeVARCHAR) headers := []sdk.ExternalFunctionHeaderRequest{ { @@ -106,38 +107,38 @@ func TestInt_ExternalFunctions(t *testing.T) { WithCompression(sdk.String("GZIP")) err := client.ExternalFunctions.Create(ctx, request) require.NoError(t, err) - t.Cleanup(cleanupExternalFuncionHandle(id, []sdk.DataType{sdk.DataTypeVariant})) + t.Cleanup(cleanupExternalFunctionHandle(id.WithoutArguments(), []sdk.DataType{sdk.DataTypeVariant})) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("create external function without arguments", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(4)) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, random.StringN(4), nil) as := "https://xyz.execute-api.us-west-2.amazonaws.com/production/remote_echo" request := sdk.NewCreateExternalFunctionRequest(id, sdk.DataTypeVariant, &integration, as) err := client.ExternalFunctions.Create(ctx, request) require.NoError(t, err) - t.Cleanup(cleanupExternalFuncionHandle(id, nil)) + t.Cleanup(cleanupExternalFunctionHandle(id, nil)) - assertExternalFunction(t, id, false, nil) + assertExternalFunction(t, id, false) }) t.Run("alter external function: set api integration", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + e := createExternalFunction(t) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) set := sdk.NewExternalFunctionSetRequest(). WithApiIntegration(&integration) request := sdk.NewAlterExternalFunctionRequest(id, defaultDataTypes).WithSet(set) err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("alter external function: set headers", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) headers := []sdk.ExternalFunctionHeaderRequest{ { Name: "measure", @@ -148,13 +149,13 @@ func TestInt_ExternalFunctions(t *testing.T) { request := sdk.NewAlterExternalFunctionRequest(id, defaultDataTypes).WithSet(set) err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("alter external function: set context headers", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) ch := []sdk.ExternalFunctionContextHeaderRequest{ { ContextFunction: "CURRENT_DATE", @@ -167,35 +168,35 @@ func TestInt_ExternalFunctions(t *testing.T) { request := sdk.NewAlterExternalFunctionRequest(id, defaultDataTypes).WithSet(set) err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("alter external function: set compression", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) set := sdk.NewExternalFunctionSetRequest().WithCompression(sdk.String("AUTO")) request := sdk.NewAlterExternalFunctionRequest(id, defaultDataTypes).WithSet(set) err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("alter external function: set max batch rows", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) set := sdk.NewExternalFunctionSetRequest().WithMaxBatchRows(sdk.Int(20)) request := sdk.NewAlterExternalFunctionRequest(id, defaultDataTypes).WithSet(set) err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("alter external function: unset", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) unset := sdk.NewExternalFunctionUnsetRequest(). WithComment(sdk.Bool(true)). WithHeaders(sdk.Bool(true)) @@ -203,12 +204,12 @@ func TestInt_ExternalFunctions(t *testing.T) { err := client.ExternalFunctions.Alter(ctx, request) require.NoError(t, err) - assertExternalFunction(t, id, true, defaultDataTypes) + assertExternalFunction(t, id, true) }) t.Run("show external function: with like", func(t *testing.T) { - e1 := createExternalFunction(t, sdk.DataTypeVARCHAR) - e2 := createExternalFunction(t, sdk.DataTypeVARCHAR) + e1 := createExternalFunction(t) + e2 := createExternalFunction(t) es, err := client.ExternalFunctions.Show(ctx, sdk.NewShowExternalFunctionRequest().WithLike(&sdk.Like{Pattern: sdk.String(e1.Name)})) require.NoError(t, err) @@ -222,7 +223,7 @@ func TestInt_ExternalFunctions(t *testing.T) { otherDb, otherDbCleanup := createDatabase(t, testClient(t)) t.Cleanup(otherDbCleanup) - e1 := createExternalFunction(t, sdk.DataTypeVARCHAR) + e1 := createExternalFunction(t) es, err := client.ExternalFunctions.Show(ctx, sdk.NewShowExternalFunctionRequest().WithIn(&sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(databaseTest.Name, schemaTest.Name)})) require.NoError(t, err) @@ -247,19 +248,19 @@ func TestInt_ExternalFunctions(t *testing.T) { }) t.Run("show external function by id", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) - es, err := client.ExternalFunctions.ShowByID(ctx, id, []sdk.DataType{sdk.DataTypeVARCHAR}) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, e.Name, defaultDataTypes) + es, err := client.ExternalFunctions.ShowByID(ctx, id) require.NoError(t, err) require.Equal(t, *e, *es) - _, err = client.ExternalFunctions.ShowByID(ctx, id, nil) + _, err = client.ExternalFunctions.ShowByID(ctx, id.WithoutArguments()) require.Error(t, err, sdk.ErrObjectNotExistOrAuthorized) }) t.Run("describe external function", func(t *testing.T) { - e := createExternalFunction(t, sdk.DataTypeVARCHAR) + e := createExternalFunction(t) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, e.Name) request := sdk.NewDescribeExternalFunctionRequest(id, []sdk.DataType{sdk.DataTypeVARCHAR}) diff --git a/pkg/sdk/testint/external_tables_integration_test.go b/pkg/sdk/testint/external_tables_integration_test.go index 6acd43fb00..e4510712a6 100644 --- a/pkg/sdk/testint/external_tables_integration_test.go +++ b/pkg/sdk/testint/external_tables_integration_test.go @@ -64,7 +64,7 @@ func TestInt_ExternalTables(t *testing.T) { err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + externalTable, err := client.ExternalTables.ShowByID(ctx, externalTableID) require.NoError(t, err) assert.Equal(t, name, externalTable.Name) }) @@ -78,7 +78,7 @@ func TestInt_ExternalTables(t *testing.T) { ) require.NoError(t, err) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + externalTable, err := client.ExternalTables.ShowByID(ctx, externalTableID) require.NoError(t, err) assert.Equal(t, name, externalTable.Name) }) @@ -105,7 +105,7 @@ func TestInt_ExternalTables(t *testing.T) { ) require.NoError(t, err) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + externalTable, err := client.ExternalTables.ShowByID(ctx, externalTableID) require.NoError(t, err) assert.Equal(t, name, externalTable.Name) }) @@ -130,7 +130,7 @@ func TestInt_ExternalTables(t *testing.T) { WithAutoRefresh(sdk.Bool(false))) require.NoError(t, err) - _, err = client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id)) + _, err = client.ExternalTables.ShowByID(ctx, id) require.NoError(t, err) }) @@ -140,7 +140,7 @@ func TestInt_ExternalTables(t *testing.T) { err := client.ExternalTables.CreateWithManualPartitioning(ctx, createExternalTableWithManualPartitioningReq(name)) require.NoError(t, err) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + externalTable, err := client.ExternalTables.ShowByID(ctx, externalTableID) require.NoError(t, err) assert.Equal(t, name, externalTable.Name) }) @@ -166,7 +166,7 @@ func TestInt_ExternalTables(t *testing.T) { ) require.NoError(t, err) - externalTable, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + externalTable, err := client.ExternalTables.ShowByID(ctx, externalTableID) require.NoError(t, err) assert.Equal(t, name, externalTable.Name) }) @@ -347,7 +347,7 @@ func TestInt_ExternalTables(t *testing.T) { ) require.NoError(t, err) - _, err = client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) + _, err = client.ExternalTables.ShowByID(ctx, externalTableID) require.ErrorIs(t, err, collections.ErrObjectNotFound) }) @@ -457,11 +457,11 @@ func TestInt_ExternalTablesShowByID(t *testing.T) { createExternalTableHandle(t, id1) createExternalTableHandle(t, id2) - e1, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id1)) + e1, err := client.ExternalTables.ShowByID(ctx, id1) require.NoError(t, err) require.Equal(t, id1, e1.ID()) - e2, err := client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(id2)) + e2, err := client.ExternalTables.ShowByID(ctx, id2) require.NoError(t, err) require.Equal(t, id2, e2.ID()) }) diff --git a/pkg/sdk/testint/helpers_test.go b/pkg/sdk/testint/helpers_test.go index 2a080f3271..fa4677288d 100644 --- a/pkg/sdk/testint/helpers_test.go +++ b/pkg/sdk/testint/helpers_test.go @@ -608,7 +608,7 @@ func createRoleWithRequest(t *testing.T, client *sdk.Client, req *sdk.CreateRole ctx := context.Background() err := client.Roles.Create(ctx, req) require.NoError(t, err) - role, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(req.GetName())) + role, err := client.Roles.ShowByID(ctx, req.GetName()) require.NoError(t, err) return role, func() { err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(req.GetName())) diff --git a/pkg/sdk/testint/roles_integration_test.go b/pkg/sdk/testint/roles_integration_test.go index 682468d36e..ec90885608 100644 --- a/pkg/sdk/testint/roles_integration_test.go +++ b/pkg/sdk/testint/roles_integration_test.go @@ -25,7 +25,7 @@ func TestInt_Roles(t *testing.T) { require.NoError(t, err) }) - role, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(roleID)) + role, err := client.Roles.ShowByID(ctx, roleID) require.NoError(t, err) assert.Equal(t, roleID.Name(), role.Name) @@ -40,7 +40,7 @@ func TestInt_Roles(t *testing.T) { require.NoError(t, err) }) - role, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(roleID)) + role, err := client.Roles.ShowByID(ctx, roleID) require.NoError(t, err) assert.Equal(t, roleID.Name(), role.Name) }) @@ -68,7 +68,7 @@ func TestInt_Roles(t *testing.T) { require.NoError(t, err) }) - role, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(roleID)) + role, err := client.Roles.ShowByID(ctx, roleID) require.NoError(t, err) assert.Equal(t, roleID.Name(), role.Name) assert.Equal(t, comment, role.Comment) @@ -97,7 +97,7 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithRenameTo(newName)) require.NoError(t, err) - r, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(newName)) + r, err := client.Roles.ShowByID(ctx, newName) require.NoError(t, err) assert.Equal(t, newName.Name(), r.Name) }) @@ -154,7 +154,7 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithSetComment(comment)) require.NoError(t, err) - r, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + r, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) assert.Equal(t, comment, r.Comment) }) @@ -168,7 +168,7 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithUnsetComment(true)) require.NoError(t, err) - r, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + r, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) assert.Equal(t, "", r.Comment) }) @@ -178,7 +178,7 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(role.ID())) require.NoError(t, err) - r, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + r, err := client.Roles.ShowByID(ctx, role.ID()) require.Nil(t, r) require.Error(t, err) }) @@ -225,7 +225,7 @@ func TestInt_Roles(t *testing.T) { role, cleanup := createRole(t, client) t.Cleanup(cleanup) - r, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + r, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) require.NotNil(t, r) assert.Equal(t, role.Name, r.Name) @@ -242,14 +242,14 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Grant(ctx, sdk.NewGrantRoleRequest(role.ID(), sdk.GrantRole{User: &userID})) require.NoError(t, err) - roleBefore, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + roleBefore, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) assert.Equal(t, 1, roleBefore.AssignedToUsers) err = client.Roles.Revoke(ctx, sdk.NewRevokeRoleRequest(role.ID(), sdk.RevokeRole{User: &userID})) require.NoError(t, err) - roleAfter, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + roleAfter, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) assert.Equal(t, 0, roleAfter.AssignedToUsers) }) @@ -265,10 +265,10 @@ func TestInt_Roles(t *testing.T) { err := client.Roles.Grant(ctx, sdk.NewGrantRoleRequest(role.ID(), sdk.GrantRole{Role: &parentRoleID})) require.NoError(t, err) - roleBefore, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + roleBefore, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) - parentRoleBefore, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(parentRole.ID())) + parentRoleBefore, err := client.Roles.ShowByID(ctx, parentRole.ID()) require.NoError(t, err) require.Equal(t, 1, roleBefore.GrantedToRoles) @@ -277,10 +277,10 @@ func TestInt_Roles(t *testing.T) { err = client.Roles.Revoke(ctx, sdk.NewRevokeRoleRequest(role.ID(), sdk.RevokeRole{Role: &parentRoleID})) require.NoError(t, err) - roleAfter, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(role.ID())) + roleAfter, err := client.Roles.ShowByID(ctx, role.ID()) require.NoError(t, err) - parentRoleAfter, err := client.Roles.ShowByID(ctx, sdk.NewShowByIdRoleRequest(parentRole.ID())) + parentRoleAfter, err := client.Roles.ShowByID(ctx, parentRole.ID()) require.NoError(t, err) assert.Equal(t, 0, roleAfter.GrantedToRoles) diff --git a/pkg/sdk/testint/streams_gen_integration_test.go b/pkg/sdk/testint/streams_gen_integration_test.go index 286798acf2..3b1074f47c 100644 --- a/pkg/sdk/testint/streams_gen_integration_test.go +++ b/pkg/sdk/testint/streams_gen_integration_test.go @@ -48,7 +48,7 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err := client.Streams.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, table.ID().FullyQualifiedName(), *s.TableName) @@ -78,7 +78,7 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err := client.Streams.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, externalTableId.FullyQualifiedName(), *s.TableName) @@ -99,7 +99,7 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err := client.Streams.ShowByID(ctx, id) require.NoError(t, err) assertStream(t, s, id, "Stage", "DEFAULT") @@ -123,7 +123,7 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err := client.Streams.ShowByID(ctx, id) require.NoError(t, err) assertStream(t, s, id, "View", "DEFAULT") @@ -150,7 +150,7 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(cloneId)) + s, err := client.Streams.ShowByID(ctx, cloneId) require.NoError(t, err) assertStream(t, s, cloneId, "Table", "DEFAULT") @@ -194,7 +194,7 @@ func TestInt_Streams(t *testing.T) { _, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeStream) require.Error(t, err) - _, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + _, err = client.Streams.ShowByID(ctx, id) require.NoError(t, err) }) @@ -211,25 +211,25 @@ func TestInt_Streams(t *testing.T) { require.NoError(t, err) }) - s, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err := client.Streams.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, "", *s.Comment) err = client.Streams.Alter(ctx, sdk.NewAlterStreamRequest(id).WithSetComment(sdk.String("some_comment"))) require.NoError(t, err) - s, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err = client.Streams.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, "some_comment", *s.Comment) err = client.Streams.Alter(ctx, sdk.NewAlterStreamRequest(id).WithUnsetComment(sdk.Bool(true))) require.NoError(t, err) - s, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + s, err = client.Streams.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, "", *s.Comment) - _, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + _, err = client.Streams.ShowByID(ctx, id) require.NoError(t, err) }) @@ -242,13 +242,13 @@ func TestInt_Streams(t *testing.T) { err := client.Streams.CreateOnTable(ctx, req) require.NoError(t, err) - _, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + _, err = client.Streams.ShowByID(ctx, id) require.NoError(t, err) err = client.Streams.Drop(ctx, sdk.NewDropStreamRequest(id)) require.NoError(t, err) - _, err = client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id)) + _, err = client.Streams.ShowByID(ctx, id) require.Error(t, err) }) @@ -462,11 +462,11 @@ func TestInt_StreamsShowByID(t *testing.T) { createStreamHandle(t, id1) createStreamHandle(t, id2) - e1, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id1)) + e1, err := client.Streams.ShowByID(ctx, id1) require.NoError(t, err) require.Equal(t, id1, e1.ID()) - e2, err := client.Streams.ShowByID(ctx, sdk.NewShowByIdStreamRequest(id2)) + e2, err := client.Streams.ShowByID(ctx, id2) require.NoError(t, err) require.Equal(t, id2, e2.ID()) })