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

fix: diffs always occurring when multiple columns exist #2686

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions pkg/resources/masking_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,16 @@ func ReadMaskingPolicy(d *schema.ResourceData, meta interface{}) error {
return err
}

signature := []map[string]interface{}{}
columns := []map[string]interface{}{}
for _, s := range maskingPolicyDetails.Signature {
signature = append(signature, map[string]interface{}{
"column": []map[string]interface{}{
{
"name": s.Name,
"type": s.Type,
},
},
columns = append(columns, map[string]interface{}{
"name": s.Name,
"type": s.Type,
})
}
signature := []map[string]interface{}{
{"column": columns},
}
if err := d.Set("signature", signature); err != nil {
return err
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/resources/masking_policy_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources_test

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"strings"
"testing"

Expand Down Expand Up @@ -118,3 +119,62 @@ func maskingPolicyConfigMultiline(n string, name string, databaseName string, sc
}
`, name, databaseName, schemaName)
}

func TestAcc_MaskingPolicyMultiColumns(t *testing.T) {
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
PreCheck: func() { acc.TestAccPreCheck(t) },
CheckDestroy: nil,
Steps: []resource.TestStep{
{
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
Config: maskingPolicyConfigMultiColumn(accName, accName, acc.TestDatabaseName, acc.TestSchemaName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "masking_expression", "case when current_role() in ('ANALYST') then val else sha2(val, 512) end"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "return_data_type", "VARCHAR"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.#", "1"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, signature.# was 2

resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.#", "2"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.0.name", "val"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.0.type", "VARCHAR"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.1.name", "val2"),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.1.type", "VARCHAR"),
),
},
},
})
}

func maskingPolicyConfigMultiColumn(n string, name string, databaseName string, schemaName string) string {
return fmt.Sprintf(`
resource "snowflake_masking_policy" "test" {
name = "%s"
database = "%s"
schema = "%s"
signature {
column {
name = "val"
type = "VARCHAR"
}

column {
name = "val2"
type = "VARCHAR"
}
}
masking_expression = "case when current_role() in ('ANALYST') then val else sha2(val, 512) end"
return_data_type = "VARCHAR"
}
`, name, databaseName, schemaName)
}
Loading