Skip to content

Commit

Permalink
chore: Cleanup helpers part6 (#2745)
Browse files Browse the repository at this point in the history
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
sfc-gh-asawicki authored Apr 25, 2024
1 parent 1f165bf commit eba3029
Show file tree
Hide file tree
Showing 42 changed files with 726 additions and 589 deletions.
44 changes: 44 additions & 0 deletions pkg/acceptance/helpers/account_client.go
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{}
}
52 changes: 52 additions & 0 deletions pkg/acceptance/helpers/api_integration_client.go
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)
}
}
15 changes: 15 additions & 0 deletions pkg/acceptance/helpers/application_package_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
Expand Down Expand Up @@ -56,3 +57,17 @@ func (c *ApplicationPackageClient) AddApplicationPackageVersion(t *testing.T, id
err := c.client().Alter(ctx, sdk.NewAlterApplicationPackageRequest(id).WithAddVersion(sdk.NewAddVersionRequest(using).WithVersionIdentifier(sdk.String(versionName))))
require.NoError(t, err)
}

func (c *ApplicationPackageClient) ShowVersions(t *testing.T, id sdk.AccountObjectIdentifier) []ApplicationPackageVersion {
t.Helper()

var versions []ApplicationPackageVersion
err := c.context.client.QueryForTests(context.Background(), &versions, fmt.Sprintf(`SHOW VERSIONS IN APPLICATION PACKAGE %s`, id.FullyQualifiedName()))
require.NoError(t, err)
return versions
}

type ApplicationPackageVersion struct {
Version string `json:"version"`
Patch int `json:"patch"`
}
3 changes: 3 additions & 0 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ func (c *DatabaseClient) CreateDatabaseWithName(t *testing.T, name string) (*sdk
func (c *DatabaseClient) CreateDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) {
t.Helper()
ctx := context.Background()

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

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

return database, c.DropDatabaseFunc(t, id)
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/acceptance/helpers/parameter_client.go
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)
}
}
9 changes: 9 additions & 0 deletions pkg/acceptance/helpers/role_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ func (c *RoleClient) GrantOwnershipOnAccountObject(t *testing.T, roleId sdk.Acco
)
require.NoError(t, err)
}

// TODO: move later to grants client
func (c *RoleClient) GrantPrivilegeOnDatabaseToShare(t *testing.T, databaseId sdk.AccountObjectIdentifier, shareId sdk.AccountObjectIdentifier) {
t.Helper()
ctx := context.Background()

err := c.context.client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}, &sdk.ShareGrantOn{Database: databaseId}, shareId)
require.NoError(t, err)
}
84 changes: 84 additions & 0 deletions pkg/acceptance/helpers/row_access_policy_client.go
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"`
}
70 changes: 70 additions & 0 deletions pkg/acceptance/helpers/share_client.go
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)
}
8 changes: 6 additions & 2 deletions pkg/acceptance/helpers/stage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
nycWeatherDataURL = "s3://snowflake-workshop-lab/weather-nyc"
)

type StageClient struct {
context *TestClientContext
}
Expand All @@ -26,11 +30,11 @@ func (c *StageClient) client() sdk.Stages {
return c.context.client.Stages
}

func (c *StageClient) CreateStageWithURL(t *testing.T, id sdk.SchemaObjectIdentifier, url string) (*sdk.Stage, func()) {
func (c *StageClient) CreateStageWithURL(t *testing.T, id sdk.SchemaObjectIdentifier) (*sdk.Stage, func()) {
t.Helper()
ctx := context.Background()
err := c.client().CreateOnS3(ctx, sdk.NewCreateOnS3StageRequest(id).
WithExternalStageParams(sdk.NewExternalS3StageParamsRequest(url)))
WithExternalStageParams(sdk.NewExternalS3StageParamsRequest(nycWeatherDataURL)))
require.NoError(t, err)

stage, err := c.client().ShowByID(ctx, id)
Expand Down
Loading

0 comments on commit eba3029

Please sign in to comment.