-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Cleanup helpers part6 (#2745)
Continuation for #2744: - Moved helpers: - share - view - row access policy - api integration - task - parameter - columns (added to the existing table helper) - account - Add missing if exists (share is missing the IF EXISTS in the docs) - what's left: - going through acceptance tests and replacing the funcs sitting there - check every invocation of `acc.TestAccProvider.Meta().(*provider.Context).Client` - check every invocation of `func Client(t *testing.T) *sdk.Client`
- Loading branch information
1 parent
1f165bf
commit eba3029
Showing
42 changed files
with
726 additions
and
589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type AccountClient struct { | ||
context *TestClientContext | ||
} | ||
|
||
func NewAccountClient(context *TestClientContext) *AccountClient { | ||
return &AccountClient{ | ||
context: context, | ||
} | ||
} | ||
|
||
func (c *AccountClient) client() sdk.Accounts { | ||
return c.context.client.Accounts | ||
} | ||
|
||
// GetAccountIdentifier gets the account identifier from Snowflake API, by fetching the account locator | ||
// and by filtering the list of accounts in replication accounts by it (because there is no direct way to get). | ||
func (c *AccountClient) GetAccountIdentifier(t *testing.T) sdk.AccountIdentifier { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
currentAccountLocator, err := c.context.client.ContextFunctions.CurrentAccount(ctx) | ||
require.NoError(t, err) | ||
|
||
replicationAccounts, err := c.context.client.ReplicationFunctions.ShowReplicationAccounts(ctx) | ||
require.NoError(t, err) | ||
|
||
for _, replicationAccount := range replicationAccounts { | ||
if replicationAccount.AccountLocator == currentAccountLocator { | ||
return sdk.NewAccountIdentifier(replicationAccount.OrganizationName, replicationAccount.AccountName) | ||
} | ||
} | ||
t.Fatal("could not find the account identifier for the locator") | ||
return sdk.AccountIdentifier{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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 ApiIntegrationClient struct { | ||
context *TestClientContext | ||
} | ||
|
||
func NewApiIntegrationClient(context *TestClientContext) *ApiIntegrationClient { | ||
return &ApiIntegrationClient{ | ||
context: context, | ||
} | ||
} | ||
|
||
func (c *ApiIntegrationClient) client() sdk.ApiIntegrations { | ||
return c.context.client.ApiIntegrations | ||
} | ||
|
||
func (c *ApiIntegrationClient) CreateApiIntegration(t *testing.T) (*sdk.ApiIntegration, func()) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
id := sdk.NewAccountObjectIdentifier(random.AlphanumericN(12)) | ||
apiAllowedPrefixes := []sdk.ApiIntegrationEndpointPrefix{{Path: "https://xyz.execute-api.us-west-2.amazonaws.com/production"}} | ||
req := sdk.NewCreateApiIntegrationRequest(id, apiAllowedPrefixes, true) | ||
req.WithAwsApiProviderParams(sdk.NewAwsApiParamsRequest(sdk.ApiIntegrationAwsApiGateway, "arn:aws:iam::123456789012:role/hello_cloud_account_role")) | ||
|
||
err := c.client().Create(ctx, req) | ||
require.NoError(t, err) | ||
|
||
apiIntegration, err := c.client().ShowByID(ctx, id) | ||
require.NoError(t, err) | ||
|
||
return apiIntegration, c.DropApiIntegrationFunc(t, id) | ||
} | ||
|
||
func (c *ApiIntegrationClient) DropApiIntegrationFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
return func() { | ||
err := c.client().Drop(ctx, sdk.NewDropApiIntegrationRequest(id).WithIfExists(sdk.Bool(true))) | ||
require.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type ParameterClient struct { | ||
context *TestClientContext | ||
} | ||
|
||
func NewParameterClient(context *TestClientContext) *ParameterClient { | ||
return &ParameterClient{ | ||
context: context, | ||
} | ||
} | ||
|
||
func (c *ParameterClient) client() sdk.Parameters { | ||
return c.context.client.Parameters | ||
} | ||
|
||
func (c *ParameterClient) UpdateAccountParameterTemporarily(t *testing.T, parameter sdk.AccountParameter, newValue string) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
param, err := c.client().ShowAccountParameter(ctx, parameter) | ||
require.NoError(t, err) | ||
oldValue := param.Value | ||
|
||
err = c.client().SetAccountParameter(ctx, parameter, newValue) | ||
require.NoError(t, err) | ||
|
||
return func() { | ||
err = c.client().SetAccountParameter(ctx, parameter, oldValue) | ||
require.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"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 RowAccessPolicyClient struct { | ||
context *TestClientContext | ||
} | ||
|
||
func NewRowAccessPolicyClient(context *TestClientContext) *RowAccessPolicyClient { | ||
return &RowAccessPolicyClient{ | ||
context: context, | ||
} | ||
} | ||
|
||
func (c *RowAccessPolicyClient) client() sdk.RowAccessPolicies { | ||
return c.context.client.RowAccessPolicies | ||
} | ||
|
||
func (c *RowAccessPolicyClient) CreateRowAccessPolicy(t *testing.T) (*sdk.RowAccessPolicy, func()) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
id := c.context.newSchemaObjectIdentifier(random.AlphanumericN(12)) | ||
arg := sdk.NewCreateRowAccessPolicyArgsRequest("A", sdk.DataTypeNumber) | ||
body := "true" | ||
createRequest := sdk.NewCreateRowAccessPolicyRequest(id, []sdk.CreateRowAccessPolicyArgsRequest{*arg}, body) | ||
|
||
err := c.client().Create(ctx, createRequest) | ||
require.NoError(t, err) | ||
|
||
rowAccessPolicy, err := c.client().ShowByID(ctx, id) | ||
require.NoError(t, err) | ||
|
||
return rowAccessPolicy, c.DropRowAccessPolicyFunc(t, id) | ||
} | ||
|
||
func (c *RowAccessPolicyClient) DropRowAccessPolicyFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
return func() { | ||
err := c.client().Drop(ctx, sdk.NewDropRowAccessPolicyRequest(id).WithIfExists(sdk.Bool(true))) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
// GetRowAccessPolicyFor is based on https://docs.snowflake.com/en/user-guide/security-row-intro#obtain-database-objects-with-a-row-access-policy. | ||
// TODO: extract getting row access policies as resource (like getting tag in system functions) | ||
func (c *RowAccessPolicyClient) GetRowAccessPolicyFor(t *testing.T, id sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) (*PolicyReference, error) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
s := &PolicyReference{} | ||
policyReferencesId := sdk.NewSchemaObjectIdentifier(id.DatabaseName(), "INFORMATION_SCHEMA", "POLICY_REFERENCES") | ||
err := c.context.client.QueryOneForTests(ctx, s, fmt.Sprintf(`SELECT * FROM TABLE(%s(REF_ENTITY_NAME => '%s', REF_ENTITY_DOMAIN => '%v'))`, policyReferencesId.FullyQualifiedName(), id.FullyQualifiedName(), objectType)) | ||
|
||
return s, err | ||
} | ||
|
||
type PolicyReference struct { | ||
PolicyDb string `db:"POLICY_DB"` | ||
PolicySchema string `db:"POLICY_SCHEMA"` | ||
PolicyName string `db:"POLICY_NAME"` | ||
PolicyKind string `db:"POLICY_KIND"` | ||
RefDatabaseName string `db:"REF_DATABASE_NAME"` | ||
RefSchemaName string `db:"REF_SCHEMA_NAME"` | ||
RefEntityName string `db:"REF_ENTITY_NAME"` | ||
RefEntityDomain string `db:"REF_ENTITY_DOMAIN"` | ||
RefColumnName sql.NullString `db:"REF_COLUMN_NAME"` | ||
RefArgColumnNames string `db:"REF_ARG_COLUMN_NAMES"` | ||
TagDatabase sql.NullString `db:"TAG_DATABASE"` | ||
TagSchema sql.NullString `db:"TAG_SCHEMA"` | ||
TagName sql.NullString `db:"TAG_NAME"` | ||
PolicyStatus string `db:"POLICY_STATUS"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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 ShareClient struct { | ||
context *TestClientContext | ||
} | ||
|
||
func NewShareClient(context *TestClientContext) *ShareClient { | ||
return &ShareClient{ | ||
context: context, | ||
} | ||
} | ||
|
||
func (c *ShareClient) client() sdk.Shares { | ||
return c.context.client.Shares | ||
} | ||
|
||
func (c *ShareClient) CreateShare(t *testing.T) (*sdk.Share, func()) { | ||
t.Helper() | ||
// TODO(SNOW-1058419): Try with identifier containing dot during identifiers rework | ||
return c.CreateShareWithName(t, random.AlphanumericN(12)) | ||
} | ||
|
||
func (c *ShareClient) CreateShareWithName(t *testing.T, name string) (*sdk.Share, func()) { | ||
t.Helper() | ||
return c.CreateShareWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateShareOptions{}) | ||
} | ||
|
||
func (c *ShareClient) CreateShareWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateShareOptions) (*sdk.Share, func()) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.client().Create(ctx, id, opts) | ||
require.NoError(t, err) | ||
|
||
share, err := c.client().ShowByID(ctx, id) | ||
require.NoError(t, err) | ||
|
||
return share, c.DropShareFunc(t, id) | ||
} | ||
|
||
func (c *ShareClient) DropShareFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
return func() { | ||
err := c.client().Drop(ctx, id, &sdk.DropShareOptions{IfExists: sdk.Bool(true)}) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
func (c *ShareClient) SetAccountOnShare(t *testing.T, accountId sdk.AccountIdentifier, shareId sdk.AccountObjectIdentifier) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.client().Alter(ctx, shareId, &sdk.AlterShareOptions{ | ||
Set: &sdk.ShareSet{ | ||
Accounts: []sdk.AccountIdentifier{accountId}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.