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

feat: Upgrade view sdk #2969

Merged
merged 10 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
47 changes: 47 additions & 0 deletions pkg/acceptance/helpers/aggregation_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"context"
"fmt"
"testing"

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

// TODO(SNOW-1564954): change raw sqls to proper client
type AggregationPolicyClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewAggregationPolicyClient(context *TestClientContext, idsGenerator *IdsGenerator) *AggregationPolicyClient {
return &AggregationPolicyClient{
context: context,
ids: idsGenerator,
}
}

func (c *AggregationPolicyClient) client() *sdk.Client {
return c.context.client
}

func (c *AggregationPolicyClient) CreateAggregationPolicy(t *testing.T) (sdk.SchemaObjectIdentifier, func()) {
t.Helper()
ctx := context.Background()

id := c.ids.RandomSchemaObjectIdentifier()
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`CREATE AGGREGATION POLICY %s AS () RETURNS AGGREGATION_CONSTRAINT -> AGGREGATION_CONSTRAINT(MIN_GROUP_SIZE => 5)`, id.Name()))
require.NoError(t, err)
return id, c.DropAggregationPolicyFunc(t, id)
}

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

return func() {
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`DROP AGGREGATION POLICY IF EXISTS %s`, id.Name()))
require.NoError(t, err)
}
}
18 changes: 18 additions & 0 deletions pkg/acceptance/helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"database/sql"
"fmt"
"log"
"testing"
Expand Down Expand Up @@ -70,3 +71,20 @@ func AssertErrorContainsPartsFunc(t *testing.T, parts []string) resource.ErrorCh
return nil
}
}

type PolicyReference struct {
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
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 sql.NullString `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"`
}
50 changes: 50 additions & 0 deletions pkg/acceptance/helpers/data_metric_function_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package helpers

import (
"context"
"fmt"
"testing"

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

// TODO(SNOW-1564959): change raw sqls to proper client
type DataMetricFunctionClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewDataMetricFunctionClient(context *TestClientContext, idsGenerator *IdsGenerator) *DataMetricFunctionClient {
return &DataMetricFunctionClient{
context: context,
ids: idsGenerator,
}
}

func (c *DataMetricFunctionClient) client() *sdk.Client {
return c.context.client
}

func (c *DataMetricFunctionClient) CreateDataMetricFunction(t *testing.T, viewID sdk.SchemaObjectIdentifier) (sdk.SchemaObjectIdentifier, func()) {
t.Helper()
ctx := context.Background()

id := c.ids.RandomSchemaObjectIdentifier()
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`CREATE DATA METRIC FUNCTION %s(arg_t TABLE (arg_c INT))
RETURNS NUMBER AS
'SELECT COUNT(*) FROM arg_t
WHERE arg_c IN (SELECT id FROM %s)'`, id.Name(), viewID.Name()))
require.NoError(t, err)
return id, c.DropDataMetricFunctionFunc(t, id)
}

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

return func() {
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`DROP FUNCTION IF EXISTS %s (TABLE (INT))`, id.Name()))
require.NoError(t, err)
}
}
47 changes: 47 additions & 0 deletions pkg/acceptance/helpers/data_metric_function_references_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"context"
"fmt"
"testing"

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

type DataMetricFunctionReferencesClient struct {
context *TestClientContext
}

func NewDataMetricFunctionReferencesClient(context *TestClientContext) *DataMetricFunctionReferencesClient {
return &DataMetricFunctionReferencesClient{
context: context,
}
}

// GetDataMetricFunctionReferences is based on https://docs.snowflake.com/en/sql-reference/functions/data_metric_function_references.
func (c *DataMetricFunctionReferencesClient) GetDataMetricFunctionReferences(t *testing.T, id sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) ([]DataMetricFunctionReference, error) {
t.Helper()
ctx := context.Background()

s := []DataMetricFunctionReference{}
dmfReferencesId := sdk.NewSchemaObjectIdentifier(id.DatabaseName(), "INFORMATION_SCHEMA", "DATA_METRIC_FUNCTION_REFERENCES")
err := c.context.client.QueryForTests(ctx, &s, fmt.Sprintf(`SELECT * FROM TABLE(%s(REF_ENTITY_NAME => '%s', REF_ENTITY_DOMAIN => '%v'))`, dmfReferencesId.FullyQualifiedName(), id.FullyQualifiedName(), objectType))

return s, err
}

type DataMetricFunctionReference struct {
MetricDatabaseName string `db:"METRIC_DATABASE_NAME"`
MetricSchemaName string `db:"METRIC_SCHEMA_NAME"`
MetricName string `db:"METRIC_NAME"`
MetricSignature string `db:"METRIC_SIGNATURE"`
MetricDataType string `db:"METRIC_DATA_TYPE"`
RefEntityDatabaseName string `db:"REF_ENTITY_DATABASE_NAME"`
RefEntitySchemaName string `db:"REF_ENTITY_SCHEMA_NAME"`
RefEntityName string `db:"REF_ENTITY_NAME"`
RefEntityDomain string `db:"REF_ENTITY_DOMAIN"`
RefArguments string `db:"REF_ARGUMENTS"`
RefId string `db:"REF_ID"`
Schedule string `db:"SCHEDULE"`
ScheduleStatus string `db:"SCHEDULE_STATUS"`
}
47 changes: 47 additions & 0 deletions pkg/acceptance/helpers/policy_references_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"context"
"fmt"
"testing"

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

type PolicyReferencesClient struct {
context *TestClientContext
}

func NewPolicyReferencesClient(context *TestClientContext) *PolicyReferencesClient {
return &PolicyReferencesClient{
context: context,
}
}

func (c *PolicyReferencesClient) client() sdk.RowAccessPolicies {
return c.context.client.RowAccessPolicies
}

// GetPolicyReferences is based on https://docs.snowflake.com/en/sql-reference/functions/policy_references.
func (c *PolicyReferencesClient) GetPolicyReferences(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.QueryForTests(ctx, &s, fmt.Sprintf(`SELECT * FROM TABLE(%s(REF_ENTITY_NAME => '%s', REF_ENTITY_DOMAIN => '%v'))`, policyReferencesId.FullyQualifiedName(), id.FullyQualifiedName(), objectType))

return s, err
}

// GetPolicyReference is based on https://docs.snowflake.com/en/sql-reference/functions/policy_references.
func (c *PolicyReferencesClient) GetPolicyReference(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
}
47 changes: 47 additions & 0 deletions pkg/acceptance/helpers/projection_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"context"
"fmt"
"testing"

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

// TODO(SNOW-1564959): change raw sqls to proper client
type ProjectionPolicyClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewProjectionPolicyClient(context *TestClientContext, idsGenerator *IdsGenerator) *ProjectionPolicyClient {
return &ProjectionPolicyClient{
context: context,
ids: idsGenerator,
}
}

func (c *ProjectionPolicyClient) client() *sdk.Client {
return c.context.client
}

func (c *ProjectionPolicyClient) CreateProjectionPolicy(t *testing.T) (sdk.SchemaObjectIdentifier, func()) {
t.Helper()
ctx := context.Background()

id := c.ids.RandomSchemaObjectIdentifier()
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`CREATE PROJECTION POLICY %s AS () RETURNS PROJECTION_CONSTRAINT -> PROJECTION_CONSTRAINT(ALLOW => false)`, id.Name()))
require.NoError(t, err)
return id, c.DropProjectionPolicyFunc(t, id)
}

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

return func() {
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`DROP PROJECTION POLICY IF EXISTS %s`, id.Name()))
require.NoError(t, err)
}
}
32 changes: 0 additions & 32 deletions pkg/acceptance/helpers/row_access_policy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package helpers

import (
"context"
"database/sql"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
Expand Down Expand Up @@ -53,33 +51,3 @@ func (c *RowAccessPolicyClient) DropRowAccessPolicyFunc(t *testing.T, id sdk.Sch
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"`
}
Loading
Loading