Skip to content

Commit

Permalink
chore: Cleanup helpers part 5 (#2744)
Browse files Browse the repository at this point in the history
Next batch of helper methods refactor:
- if exists added to multiple SDK objects (including application package
from the previous change)
- if exists added to some resources in delete
- some helpers were simplified
- some tests were simplified (e.g. usage of the default schema)
- helper methods for default ids introduced in test client
- missing cleanups added
- helpers:
  - pipe
  - tag
  - password policy
  - network policy
  - resource monitor
  - masking policy
  - alert
  - failover group
  - file format
  • Loading branch information
sfc-gh-asawicki committed Apr 25, 2024
1 parent 9475e35 commit 1f165bf
Show file tree
Hide file tree
Showing 74 changed files with 870 additions and 599 deletions.
2 changes: 1 addition & 1 deletion framework/provider/resource_monitor_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func (r *ResourceMonitorResource) delete(ctx context.Context, data *resourceMoni

diags := diag.Diagnostics{}
id := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(data.Id.ValueString())
err := client.ResourceMonitors.Drop(ctx, id)
err := client.ResourceMonitors.Drop(ctx, id, &sdk.DropResourceMonitorOptions{IfExists: sdk.Bool(true)})
if dryRun {
return data, client.TraceLogs(), diags
}
Expand Down
58 changes: 58 additions & 0 deletions pkg/acceptance/helpers/alert_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package helpers

import (
"context"
"testing"

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

type AlertClient struct {
context *TestClientContext
}

func NewAlertClient(context *TestClientContext) *AlertClient {
return &AlertClient{
context: context,
}
}

func (c *AlertClient) client() sdk.Alerts {
return c.context.client.Alerts
}

func (c *AlertClient) CreateAlert(t *testing.T) (*sdk.Alert, func()) {
t.Helper()
schedule := "USING CRON * * * * * UTC"
condition := "SELECT 1"
action := "SELECT 1"
return c.CreateAlertWithOptions(t, schedule, condition, action, &sdk.CreateAlertOptions{})
}

func (c *AlertClient) CreateAlertWithOptions(t *testing.T, schedule string, condition string, action string, opts *sdk.CreateAlertOptions) (*sdk.Alert, func()) {
t.Helper()
ctx := context.Background()

name := random.String()
id := c.context.newSchemaObjectIdentifier(name)

err := c.client().Create(ctx, id, c.context.warehouseId(), schedule, condition, action, opts)
require.NoError(t, err)

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

return alert, c.DropAlertFunc(t, id)
}

func (c *AlertClient) DropAlertFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, id, &sdk.DropAlertOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
}
}
6 changes: 2 additions & 4 deletions pkg/acceptance/helpers/application_package_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

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

Expand Down Expand Up @@ -43,9 +42,8 @@ func (c *ApplicationPackageClient) DropApplicationPackageFunc(t *testing.T, id s
ctx := context.Background()

return func() {
// no if exists supported based on the docs https://docs.snowflake.com/en/sql-reference/sql/drop-application-package#syntax
err := c.client().Drop(ctx, sdk.NewDropApplicationPackageRequest(id))
assert.NoError(t, err)
err := c.client().Drop(ctx, sdk.NewDropApplicationPackageRequest(id).WithIfExists(sdk.Bool(true)))
require.NoError(t, err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *DatabaseClient) DropDatabaseFunc(t *testing.T, id sdk.AccountObjectIden
return func() {
err := c.client().Drop(ctx, id, &sdk.DropDatabaseOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
err = c.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema))
err = c.context.client.Sessions.UseSchema(ctx, c.context.schemaId())
require.NoError(t, err)
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c *DatabaseClient) CreateSecondaryDatabaseWithOptions(t *testing.T, id sdk
// TODO [926148]: make this wait better with tests stabilization
// waiting because sometimes dropping primary db right after dropping the secondary resulted in error
time.Sleep(1 * time.Second)
err = c.context.client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema))
err = c.context.client.Sessions.UseSchema(ctx, c.context.schemaId())
require.NoError(t, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/acceptance/helpers/database_role_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *DatabaseRoleClient) client() sdk.DatabaseRoles {

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

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.DatabaseRole, func()) {
Expand All @@ -36,7 +36,7 @@ func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, database

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

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabaseWithName(t *testing.T, databaseId sdk.AccountObjectIdentifier, name string) (*sdk.DatabaseRole, func()) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/acceptance/helpers/dynamic_table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *DynamicTableClient) client() sdk.DynamicTables {

func (c *DynamicTableClient) CreateDynamicTable(t *testing.T, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
t.Helper()
return c.CreateDynamicTableWithOptions(t, sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema), random.AlphaN(12), sdk.NewAccountObjectIdentifier(c.context.warehouse), tableId)
return c.CreateDynamicTableWithOptions(t, c.context.schemaId(), random.AlphaN(12), c.context.warehouseId(), tableId)
}

func (c *DynamicTableClient) CreateDynamicTableWithOptions(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, name string, warehouseId sdk.AccountObjectIdentifier, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
Expand Down
58 changes: 58 additions & 0 deletions pkg/acceptance/helpers/failover_group_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package helpers

import (
"context"
"testing"

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

type FailoverGroupClient struct {
context *TestClientContext
}

func NewFailoverGroupClient(context *TestClientContext) *FailoverGroupClient {
return &FailoverGroupClient{
context: context,
}
}

func (c *FailoverGroupClient) client() sdk.FailoverGroups {
return c.context.client.FailoverGroups
}

func (c *FailoverGroupClient) CreateFailoverGroup(t *testing.T) (*sdk.FailoverGroup, func()) {
t.Helper()
objectTypes := []sdk.PluralObjectType{sdk.PluralObjectTypeRoles}
currentAccount, err := c.context.client.ContextFunctions.CurrentAccount(context.Background())
require.NoError(t, err)
accountID := sdk.NewAccountIdentifierFromAccountLocator(currentAccount)
allowedAccounts := []sdk.AccountIdentifier{accountID}
return c.CreateFailoverGroupWithOptions(t, objectTypes, allowedAccounts, nil)
}

func (c *FailoverGroupClient) CreateFailoverGroupWithOptions(t *testing.T, objectTypes []sdk.PluralObjectType, allowedAccounts []sdk.AccountIdentifier, opts *sdk.CreateFailoverGroupOptions) (*sdk.FailoverGroup, func()) {
t.Helper()
ctx := context.Background()

id := sdk.RandomAlphanumericAccountObjectIdentifier()

err := c.client().Create(ctx, id, objectTypes, allowedAccounts, opts)
require.NoError(t, err)

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

return failoverGroup, c.DropFailoverGroupFunc(t, id)
}

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

return func() {
err := c.client().Drop(ctx, id, &sdk.DropFailoverGroupOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
}
}
56 changes: 56 additions & 0 deletions pkg/acceptance/helpers/file_format_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package helpers

import (
"context"
"testing"

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

type FileFormatClient struct {
context *TestClientContext
}

func NewFileFormatClient(context *TestClientContext) *FileFormatClient {
return &FileFormatClient{
context: context,
}
}

func (c *FileFormatClient) client() sdk.FileFormats {
return c.context.client.FileFormats
}

func (c *FileFormatClient) CreateFileFormat(t *testing.T) (*sdk.FileFormat, func()) {
t.Helper()
return c.CreateFileFormatWithOptions(t, &sdk.CreateFileFormatOptions{
Type: sdk.FileFormatTypeCSV,
})
}

func (c *FileFormatClient) CreateFileFormatWithOptions(t *testing.T, opts *sdk.CreateFileFormatOptions) (*sdk.FileFormat, func()) {
t.Helper()
ctx := context.Background()

id := c.context.newSchemaObjectIdentifier(random.AlphanumericN(12))

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

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

return fileFormat, c.DropFileFormatFunc(t, id)
}

func (c *FileFormatClient) DropFileFormatFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, id, &sdk.DropFileFormatOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
}
}
85 changes: 85 additions & 0 deletions pkg/acceptance/helpers/masking_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package helpers

import (
"context"
"testing"

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

type MaskingPolicyClient struct {
context *TestClientContext
}

func NewMaskingPolicyClient(context *TestClientContext) *MaskingPolicyClient {
return &MaskingPolicyClient{
context: context,
}
}

func (c *MaskingPolicyClient) client() sdk.MaskingPolicies {
return c.context.client.MaskingPolicies
}

func (c *MaskingPolicyClient) CreateMaskingPolicy(t *testing.T) (*sdk.MaskingPolicy, func()) {
t.Helper()
return c.CreateMaskingPolicyInSchema(t, c.context.schemaId())
}

func (c *MaskingPolicyClient) CreateMaskingPolicyInSchema(t *testing.T, schemaId sdk.DatabaseObjectIdentifier) (*sdk.MaskingPolicy, func()) {
t.Helper()
signature := []sdk.TableColumnSignature{
{
Name: random.String(),
Type: sdk.DataTypeVARCHAR,
},
{
Name: random.String(),
Type: sdk.DataTypeVARCHAR,
},
}
expression := "REPLACE('X', 1, 2)"
return c.CreateMaskingPolicyWithOptions(t, schemaId, signature, sdk.DataTypeVARCHAR, expression, &sdk.CreateMaskingPolicyOptions{})
}

func (c *MaskingPolicyClient) CreateMaskingPolicyIdentity(t *testing.T, columnType sdk.DataType) (*sdk.MaskingPolicy, func()) {
t.Helper()
name := "a"
signature := []sdk.TableColumnSignature{
{
Name: name,
Type: columnType,
},
}
expression := "a"
return c.CreateMaskingPolicyWithOptions(t, c.context.schemaId(), signature, columnType, expression, &sdk.CreateMaskingPolicyOptions{})
}

func (c *MaskingPolicyClient) CreateMaskingPolicyWithOptions(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, signature []sdk.TableColumnSignature, returns sdk.DataType, expression string, options *sdk.CreateMaskingPolicyOptions) (*sdk.MaskingPolicy, func()) {
t.Helper()
ctx := context.Background()

name := random.String()
id := sdk.NewSchemaObjectIdentifier(schemaId.DatabaseName(), schemaId.Name(), name)

err := c.client().Create(ctx, id, signature, returns, expression, options)
require.NoError(t, err)

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

return maskingPolicy, c.DropMaskingPolicyFunc(t, id)
}

func (c *MaskingPolicyClient) DropMaskingPolicyFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, id, &sdk.DropMaskingPolicyOptions{IfExists: sdk.Bool(true)})
assert.NoError(t, err)
}
}
51 changes: 51 additions & 0 deletions pkg/acceptance/helpers/network_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package helpers

import (
"context"
"testing"

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

type NetworkPolicyClient struct {
context *TestClientContext
}

func NewNetworkPolicyClient(context *TestClientContext) *NetworkPolicyClient {
return &NetworkPolicyClient{
context: context,
}
}

func (c *NetworkPolicyClient) client() sdk.NetworkPolicies {
return c.context.client.NetworkPolicies
}

func (c *NetworkPolicyClient) CreateNetworkPolicy(t *testing.T) (*sdk.NetworkPolicy, func()) {
t.Helper()
return c.CreateNetworkPolicyWithRequest(t, sdk.NewCreateNetworkPolicyRequest(sdk.RandomAccountObjectIdentifier()))
}

func (c *NetworkPolicyClient) CreateNetworkPolicyWithRequest(t *testing.T, request *sdk.CreateNetworkPolicyRequest) (*sdk.NetworkPolicy, func()) {
t.Helper()
ctx := context.Background()

err := c.client().Create(ctx, request)
require.NoError(t, err)

networkPolicy, err := c.client().ShowByID(ctx, request.GetName())
require.NoError(t, err)

return networkPolicy, c.DropNetworkPolicyFunc(t, request.GetName())
}

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

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

0 comments on commit 1f165bf

Please sign in to comment.