Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Cleanup helpers part 5 #2744

Merged
merged 25 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a9ef23b
Prepare pipe client
sfc-gh-asawicki Apr 24, 2024
d3c8556
Use pipe helper in integration tests
sfc-gh-asawicki Apr 24, 2024
f9cde89
Add if exists to pipes
sfc-gh-asawicki Apr 24, 2024
1a8ae22
Add if exists to application package
sfc-gh-asawicki Apr 24, 2024
decece7
Prepare tag client
sfc-gh-asawicki Apr 24, 2024
e292fd3
Use tag helper throughout integration tests
sfc-gh-asawicki Apr 24, 2024
8a2edc5
Prepare password policy client
sfc-gh-asawicki Apr 24, 2024
a0e3692
Use password policy helpers
sfc-gh-asawicki Apr 24, 2024
bec43b8
Prepare network policy client
sfc-gh-asawicki Apr 24, 2024
deb90f5
Use network policy helper
sfc-gh-asawicki Apr 24, 2024
6be438a
Introduce resource monitor helper
sfc-gh-asawicki Apr 24, 2024
6deef15
Support if exists in resource monitor drop
sfc-gh-asawicki Apr 24, 2024
b186eb6
Introduce and use masking policy helper
sfc-gh-asawicki Apr 24, 2024
0218527
Prepare alert helper
sfc-gh-asawicki Apr 24, 2024
4477399
Use alert helper
sfc-gh-asawicki Apr 24, 2024
dd813c3
Prepare failover group helper
sfc-gh-asawicki Apr 24, 2024
7812cc8
Prepare failover group helper methods
sfc-gh-asawicki Apr 24, 2024
83734d0
Use failover group helpers
sfc-gh-asawicki Apr 24, 2024
117307b
Support if exists in alerts
sfc-gh-asawicki Apr 24, 2024
ea49508
Prepare file format helper
sfc-gh-asawicki Apr 24, 2024
751e75b
Prepare file format helper methods
sfc-gh-asawicki Apr 24, 2024
3b3ecf4
Use file format helper
sfc-gh-asawicki Apr 24, 2024
b792bc6
Use common context functions
sfc-gh-asawicki Apr 24, 2024
27ed52e
Fix network policy test
sfc-gh-asawicki Apr 24, 2024
f25355b
Add if exists to masking policy drop
sfc-gh-asawicki Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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
Loading