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 4 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
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
}
35 changes: 0 additions & 35 deletions pkg/acceptance/helpers/references_client.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/acceptance/helpers/row_access_policy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package helpers

import (
"context"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
Expand Down Expand Up @@ -52,16 +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) GetOneRowAccessPolicyFor(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
}
168 changes: 86 additions & 82 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,49 @@ type TestClient struct {

Ids *IdsGenerator

Account *AccountClient
AggregationPolicy *AggregationPolicyClient
Alert *AlertClient
ApiIntegration *ApiIntegrationClient
Application *ApplicationClient
ApplicationPackage *ApplicationPackageClient
Context *ContextClient
CortexSearchService *CortexSearchServiceClient
CatalogIntegration *CatalogIntegrationClient
Database *DatabaseClient
DatabaseRole *DatabaseRoleClient
DynamicTable *DynamicTableClient
ExternalAccessIntegration *ExternalAccessIntegrationClient
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
Grant *GrantClient
MaskingPolicy *MaskingPolicyClient
MaterializedView *MaterializedViewClient
NetworkPolicy *NetworkPolicyClient
NetworkRule *NetworkRuleClient
Parameter *ParameterClient
PasswordPolicy *PasswordPolicyClient
Pipe *PipeClient
ProjectionPolicy *ProjectionPolicyClient
References *ReferencesClient
ResourceMonitor *ResourceMonitorClient
Role *RoleClient
RowAccessPolicy *RowAccessPolicyClient
Schema *SchemaClient
SecurityIntegration *SecurityIntegrationClient
SessionPolicy *SessionPolicyClient
Share *ShareClient
Stage *StageClient
Streamlit *StreamlitClient
Table *TableClient
Tag *TagClient
Task *TaskClient
User *UserClient
View *ViewClient
Warehouse *WarehouseClient
Account *AccountClient
AggregationPolicy *AggregationPolicyClient
Alert *AlertClient
ApiIntegration *ApiIntegrationClient
Application *ApplicationClient
ApplicationPackage *ApplicationPackageClient
Context *ContextClient
CortexSearchService *CortexSearchServiceClient
CatalogIntegration *CatalogIntegrationClient
Database *DatabaseClient
DatabaseRole *DatabaseRoleClient
DataMetricFunctionClient *DataMetricFunctionClient
DataMetricFunctionReferences *DataMetricFunctionReferencesClient
DynamicTable *DynamicTableClient
ExternalAccessIntegration *ExternalAccessIntegrationClient
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
Grant *GrantClient
MaskingPolicy *MaskingPolicyClient
MaterializedView *MaterializedViewClient
NetworkPolicy *NetworkPolicyClient
NetworkRule *NetworkRuleClient
Parameter *ParameterClient
PasswordPolicy *PasswordPolicyClient
Pipe *PipeClient
ProjectionPolicy *ProjectionPolicyClient
PolicyReferences *PolicyReferencesClient
ResourceMonitor *ResourceMonitorClient
Role *RoleClient
RowAccessPolicy *RowAccessPolicyClient
Schema *SchemaClient
SecurityIntegration *SecurityIntegrationClient
SessionPolicy *SessionPolicyClient
Share *ShareClient
Stage *StageClient
Streamlit *StreamlitClient
Table *TableClient
Tag *TagClient
Task *TaskClient
User *UserClient
View *ViewClient
Warehouse *WarehouseClient
}

func NewTestClient(c *sdk.Client, database string, schema string, warehouse string, testObjectSuffix string) *TestClient {
Expand All @@ -66,47 +68,49 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri

Ids: idsGenerator,

Account: NewAccountClient(context),
AggregationPolicy: NewAggregationPolicyClient(context, idsGenerator),
Alert: NewAlertClient(context, idsGenerator),
ApiIntegration: NewApiIntegrationClient(context, idsGenerator),
Application: NewApplicationClient(context, idsGenerator),
ApplicationPackage: NewApplicationPackageClient(context, idsGenerator),
Context: NewContextClient(context),
CortexSearchService: NewCortexSearchServiceClient(context, idsGenerator),
CatalogIntegration: NewCatalogIntegrationClient(context, idsGenerator),
Database: NewDatabaseClient(context, idsGenerator),
DatabaseRole: NewDatabaseRoleClient(context, idsGenerator),
DynamicTable: NewDynamicTableClient(context, idsGenerator),
ExternalAccessIntegration: NewExternalAccessIntegrationClient(context, idsGenerator),
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
Grant: NewGrantClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
MaterializedView: NewMaterializedViewClient(context, idsGenerator),
NetworkPolicy: NewNetworkPolicyClient(context, idsGenerator),
NetworkRule: NewNetworkRuleClient(context, idsGenerator),
Parameter: NewParameterClient(context),
PasswordPolicy: NewPasswordPolicyClient(context, idsGenerator),
Pipe: NewPipeClient(context, idsGenerator),
ProjectionPolicy: NewProjectionPolicyClient(context, idsGenerator),
References: NewReferencesClient(context),
ResourceMonitor: NewResourceMonitorClient(context, idsGenerator),
Role: NewRoleClient(context, idsGenerator),
RowAccessPolicy: NewRowAccessPolicyClient(context, idsGenerator),
Schema: NewSchemaClient(context, idsGenerator),
SecurityIntegration: NewSecurityIntegrationClient(context, idsGenerator),
SessionPolicy: NewSessionPolicyClient(context, idsGenerator),
Share: NewShareClient(context, idsGenerator),
Streamlit: NewStreamlitClient(context, idsGenerator),
Stage: NewStageClient(context, idsGenerator),
Table: NewTableClient(context, idsGenerator),
Tag: NewTagClient(context, idsGenerator),
Task: NewTaskClient(context, idsGenerator),
User: NewUserClient(context, idsGenerator),
View: NewViewClient(context, idsGenerator),
Warehouse: NewWarehouseClient(context, idsGenerator),
Account: NewAccountClient(context),
AggregationPolicy: NewAggregationPolicyClient(context, idsGenerator),
Alert: NewAlertClient(context, idsGenerator),
ApiIntegration: NewApiIntegrationClient(context, idsGenerator),
Application: NewApplicationClient(context, idsGenerator),
ApplicationPackage: NewApplicationPackageClient(context, idsGenerator),
Context: NewContextClient(context),
CortexSearchService: NewCortexSearchServiceClient(context, idsGenerator),
CatalogIntegration: NewCatalogIntegrationClient(context, idsGenerator),
Database: NewDatabaseClient(context, idsGenerator),
DatabaseRole: NewDatabaseRoleClient(context, idsGenerator),
DataMetricFunctionClient: NewDataMetricFunctionClient(context, idsGenerator),
DataMetricFunctionReferences: NewDataMetricFunctionReferencesClient(context),
DynamicTable: NewDynamicTableClient(context, idsGenerator),
ExternalAccessIntegration: NewExternalAccessIntegrationClient(context, idsGenerator),
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
Grant: NewGrantClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
MaterializedView: NewMaterializedViewClient(context, idsGenerator),
NetworkPolicy: NewNetworkPolicyClient(context, idsGenerator),
NetworkRule: NewNetworkRuleClient(context, idsGenerator),
Parameter: NewParameterClient(context),
PasswordPolicy: NewPasswordPolicyClient(context, idsGenerator),
Pipe: NewPipeClient(context, idsGenerator),
ProjectionPolicy: NewProjectionPolicyClient(context, idsGenerator),
PolicyReferences: NewPolicyReferencesClient(context),
ResourceMonitor: NewResourceMonitorClient(context, idsGenerator),
Role: NewRoleClient(context, idsGenerator),
RowAccessPolicy: NewRowAccessPolicyClient(context, idsGenerator),
Schema: NewSchemaClient(context, idsGenerator),
SecurityIntegration: NewSecurityIntegrationClient(context, idsGenerator),
SessionPolicy: NewSessionPolicyClient(context, idsGenerator),
Share: NewShareClient(context, idsGenerator),
Streamlit: NewStreamlitClient(context, idsGenerator),
Stage: NewStageClient(context, idsGenerator),
Table: NewTableClient(context, idsGenerator),
Tag: NewTagClient(context, idsGenerator),
Task: NewTaskClient(context, idsGenerator),
User: NewUserClient(context, idsGenerator),
View: NewViewClient(context, idsGenerator),
Warehouse: NewWarehouseClient(context, idsGenerator),
}
}

Expand Down
Loading
Loading