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: View rework part 3 #3023

Merged
merged 17 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package objectassert

import (
"fmt"
"slices"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers"

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

Expand All @@ -28,3 +32,71 @@ func (v *ViewAssert) HasNonEmptyText() *ViewAssert {
})
return v
}

func (v *ViewAssert) HasNoRowAccessPolicyReferences() *ViewAssert {
return v.hasNoPolicyReference(sdk.PolicyKindRowAccessPolicy)
}

func (v *ViewAssert) HasNoAggregationPolicyReferences() *ViewAssert {
return v.hasNoPolicyReference(sdk.PolicyKindAggregationPolicy)
}

func (v *ViewAssert) HasNoMaskingPolicyReferences() *ViewAssert {
return v.hasNoPolicyReference(sdk.PolicyKindMaskingPolicy)
}

func (v *ViewAssert) HasNoProjectionPolicyReferences() *ViewAssert {
return v.hasNoPolicyReference(sdk.PolicyKindProjectionPolicy)
}

func (v *ViewAssert) hasNoPolicyReference(kind sdk.PolicyKind) *ViewAssert {
v.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
refs, err := acc.TestClient().PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.ObjectTypeView)
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
refs = slices.DeleteFunc(refs, func(reference helpers.PolicyReference) bool {
return reference.PolicyKind != string(kind)
})
if len(refs) > 0 {
return fmt.Errorf("expected no %s policy references; got: %v", kind, refs)
}
return nil
})
return v
}

func (v *ViewAssert) HasRowAccessPolicyReferences(n int) *ViewAssert {
return v.hasPolicyReference(sdk.PolicyKindRowAccessPolicy, n)
}

func (v *ViewAssert) HasAggregationPolicyReferences(n int) *ViewAssert {
return v.hasPolicyReference(sdk.PolicyKindAggregationPolicy, n)
}

func (v *ViewAssert) HasMaskingPolicyReferences(n int) *ViewAssert {
return v.hasPolicyReference(sdk.PolicyKindMaskingPolicy, n)
}

func (v *ViewAssert) HasProjectionPolicyReferences(n int) *ViewAssert {
return v.hasPolicyReference(sdk.PolicyKindProjectionPolicy, n)
}

func (v *ViewAssert) hasPolicyReference(kind sdk.PolicyKind, n int) *ViewAssert {
v.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
refs, err := acc.TestClient().PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.ObjectTypeView)
if err != nil {
return err
}
refs = slices.DeleteFunc(refs, func(reference helpers.PolicyReference) bool {
return reference.PolicyKind != string(kind)
})
if len(refs) != n {
return fmt.Errorf("expected %d %s policy references; got: %d, %v", n, kind, len(refs), refs)
}
return nil
})
return v
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package resourceassert

import (
"strconv"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
)

func (v *ViewResourceAssert) HasColumnLength(len int) *ViewResourceAssert {
v.AddAssertion(assert.ValueSet("column.#", strconv.FormatInt(int64(len), 10)))
return v
}

func (v *ViewResourceAssert) HasAggregationPolicyLength(len int) *ViewResourceAssert {
v.AddAssertion(assert.ValueSet("aggregation_policy.#", strconv.FormatInt(int64(len), 10)))
return v
}

func (v *ViewResourceAssert) HasRowAccessPolicyLength(len int) *ViewResourceAssert {
v.AddAssertion(assert.ValueSet("row_access_policy.#", strconv.FormatInt(int64(len), 10)))
return v
}

func (v *ViewResourceAssert) HasDataMetricScheduleLength(len int) *ViewResourceAssert {
v.AddAssertion(assert.ValueSet("data_metric_schedule.#", strconv.FormatInt(int64(len), 10)))
return v
}

func (v *ViewResourceAssert) HasDataMetricFunctionLength(len int) *ViewResourceAssert {
v.AddAssertion(assert.ValueSet("data_metric_function.#", strconv.FormatInt(int64(len), 10)))
return v
}

func (v *ViewResourceAssert) HasNoAggregationPolicyByLength() *ViewResourceAssert {
v.AddAssertion(assert.ValueNotSet("aggregation_policy.#"))
return v
}

func (v *ViewResourceAssert) HasNoRowAccessPolicyByLength() *ViewResourceAssert {
v.AddAssertion(assert.ValueNotSet("row_access_policy.#"))
return v
}

func (v *ViewResourceAssert) HasNoDataMetricScheduleByLength() *ViewResourceAssert {
v.AddAssertion(assert.ValueNotSet("data_metric_schedule.#"))
return v
}

func (v *ViewResourceAssert) HasNoDataMetricFunctionByLength() *ViewResourceAssert {
v.AddAssertion(assert.ValueNotSet("data_metric_function.#"))
return v
}
4 changes: 4 additions & 0 deletions pkg/resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ResourceValueSetter interface {
Set(string, any) error
}

func DeleteResource(t string, builder func(string) *snowflake.Builder) func(*schema.ResourceData, interface{}) error {
return func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*provider.Context).Client
Expand Down
23 changes: 23 additions & 0 deletions pkg/resources/testdata/TestAcc_View/columns/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "snowflake_view" "test" {
name = var.name
database = var.database
schema = var.schema
statement = var.statement

column {
column_name = "ID"

projection_policy {
policy_name = var.projection_name
}

masking_policy {
policy_name = var.masking_name
using = var.masking_using
}
}

column {
column_name = "FOO"
}
}
27 changes: 27 additions & 0 deletions pkg/resources/testdata/TestAcc_View/columns/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "name" {
type = string
}

variable "database" {
type = string
}

variable "schema" {
type = string
}

variable "statement" {
type = string
}

variable "projection_name" {
type = string
}

variable "masking_name" {
type = string
}

variable "masking_using" {
type = list(string)
}
49 changes: 36 additions & 13 deletions pkg/resources/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,27 +502,46 @@ func CreateView(orReplace bool) schema.CreateContextFunc {
}

func extractColumns(v any) ([]sdk.ViewColumnRequest, error) {
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
_, ok := v.([]any)
if v == nil || !ok {
return nil, fmt.Errorf("unable to extract columns, input is either nil or non expected type (%T): %v", v, v)
}
columns := make([]sdk.ViewColumnRequest, len(v.([]any)))
for i, columnConfigRaw := range v.([]any) {
columnConfig := columnConfigRaw.(map[string]any)
columnsReq := *sdk.NewViewColumnRequest(columnConfig["column_name"].(string))
if len(columnConfig["projection_policy"].([]any)) > 0 {
projectionPolicyId, _, err := extractPolicyWithColumnsSet(columnConfig["projection_policy"], "")
columnConfig, ok := columnConfigRaw.(map[string]any)
if !ok {
return nil, fmt.Errorf("unable to extract column, non expected type of %T: %v", columnConfigRaw, columnConfigRaw)
}

columnName, ok := columnConfig["column_name"]
if !ok {
return nil, fmt.Errorf("unable to extract column, missing column_name key in column")
}
columnsReq := *sdk.NewViewColumnRequest(columnName.(string))

projectionPolicy, ok := columnConfig["projection_policy"]
if ok && len(projectionPolicy.([]any)) > 0 {
projectionPolicyId, _, err := extractPolicyWithColumnsSet(projectionPolicy, "")
if err != nil {
return nil, err
}
columnsReq.WithProjectionPolicy(*sdk.NewViewColumnProjectionPolicyRequest(projectionPolicyId))
}
if len(columnConfig["masking_policy"].([]any)) > 0 {
maskingPolicyId, maskingPolicyColumns, err := extractPolicyWithColumnsList(columnConfig["masking_policy"], "using")

maskingPolicy, ok := columnConfig["masking_policy"]
if ok && len(maskingPolicy.([]any)) > 0 {
maskingPolicyId, maskingPolicyColumns, err := extractPolicyWithColumnsList(maskingPolicy, "using")
if err != nil {
return nil, err
}
columnsReq.WithMaskingPolicy(*sdk.NewViewColumnMaskingPolicyRequest(maskingPolicyId).WithUsing(maskingPolicyColumns))
}
if commentRaw := columnConfig["comment"].(string); len(commentRaw) > 0 {
columnsReq.WithComment(commentRaw)

comment, ok := columnConfig["comment"]
if ok && len(comment.(string)) > 0 {
columnsReq.WithComment(comment.(string))
}

columns[i] = columnsReq
}
return columns, nil
Expand Down Expand Up @@ -552,7 +571,7 @@ func extractPolicyWithColumnsList(v any, columnsKey string) (sdk.SchemaObjectIde
return sdk.SchemaObjectIdentifier{}, nil, err
}
if policyConfig[columnsKey] == nil {
return id, nil, nil
return id, nil, fmt.Errorf("unable to extract policy with column list, unable to find columnsKey: %s", columnsKey)
}
columnsRaw := expandStringList(policyConfig[columnsKey].([]any))
columns := make([]sdk.Column, len(columnsRaw))
Expand Down Expand Up @@ -746,17 +765,21 @@ func handleDataMetricFunctions(ctx context.Context, client *sdk.Client, id sdk.S
})
}

func handleColumns(d *schema.ResourceData, columns []sdk.ViewDetails, policyRefs []sdk.PolicyReference) error {
func handleColumns(d ResourceValueSetter, columns []sdk.ViewDetails, policyRefs []sdk.PolicyReference) error {
if len(columns) == 0 {
return d.Set("column", nil)
}
columnsRaw := make([]map[string]any, len(columns))
for i, column := range columns {
columnsRaw[i] = map[string]any{
"column_name": column.Name,
"comment": column.Comment,
}
projectionPolicy, err := collections.FindOne(policyRefs, func(r sdk.PolicyReference) bool {
if column.Comment != nil {
columnsRaw[i]["comment"] = *column.Comment
} else {
columnsRaw[i]["comment"] = nil
}
projectionPolicy, err := collections.FindFirst(policyRefs, func(r sdk.PolicyReference) bool {
return r.PolicyKind == sdk.PolicyKindProjectionPolicy && r.RefColumnName != nil && *r.RefColumnName == column.Name
})
if err == nil {
Expand All @@ -770,7 +793,7 @@ func handleColumns(d *schema.ResourceData, columns []sdk.ViewDetails, policyRefs
log.Printf("could not store projection policy name: policy db and schema can not be empty")
}
}
maskingPolicy, err := collections.FindOne(policyRefs, func(r sdk.PolicyReference) bool {
maskingPolicy, err := collections.FindFirst(policyRefs, func(r sdk.PolicyReference) bool {
return r.PolicyKind == sdk.PolicyKindMaskingPolicy && r.RefColumnName != nil && *r.RefColumnName == column.Name
})
if err == nil {
Expand Down
Loading
Loading