Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmichalak committed Nov 20, 2024
1 parent a547fac commit 8aa6ea6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 80 deletions.
2 changes: 2 additions & 0 deletions docs/resources/tag_association.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ description: |-

!> **V1 release candidate** This resource was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the resource if needed. Any errors reported will be resolved with a higher priority. We encourage checking this resource out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0980--v0990) to use it.

-> **Note** For `ACCOUNT` object type, only identifiers with organization name are supported. See [account identifier docs](https://docs.snowflake.com/en/user-guide/admin-account-identifier#format-1-preferred-account-name-in-your-organization) for more details.

# snowflake_tag_association (Resource)

Resource used to manage tag associations. For more information, check [object tagging documentation](https://docs.snowflake.com/en/user-guide/object-tagging).
Expand Down
24 changes: 5 additions & 19 deletions pkg/resources/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,16 @@ func JoinDiags(diagnostics ...diag.Diagnostics) diag.Diagnostics {
return result
}

// ListDiff Compares two lists (before and after), then compares and returns two lists that include
// ListDiff compares two lists (before and after), then compares and returns two lists that include
// added and removed items between those lists.
func ListDiff[T comparable](beforeList []T, afterList []T) (added []T, removed []T) {
added = make([]T, 0)
removed = make([]T, 0)

for _, beforeItem := range beforeList {
if !slices.Contains(afterList, beforeItem) {
removed = append(removed, beforeItem)
}
}

for _, afterItem := range afterList {
if !slices.Contains(beforeList, afterItem) {
added = append(added, afterItem)
}
}

return added, removed
added, removed, _ = ListDiffWithCommonItems(beforeList, afterList)
return
}

// ListDiffWithCommon Compares two lists (before and after), then compares and returns three lists that include
// ListDiffWithCommonItems compares two lists (before and after), then compares and returns three lists that include
// added, removed and common items between those lists.
func ListDiffWithCommon[T comparable](beforeList []T, afterList []T) (added []T, removed []T, common []T) {
func ListDiffWithCommonItems[T comparable](beforeList []T, afterList []T) (added []T, removed []T, common []T) {
added = make([]T, 0)
removed = make([]T, 0)
common = make([]T, 0)
Expand Down
77 changes: 77 additions & 0 deletions pkg/resources/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,83 @@ func TestListDiff(t *testing.T) {
}
}

func TestListDiffWithCommonItems(t *testing.T) {
testCases := []struct {
Name string
Before []any
After []any
Added []any
Removed []any
Common []any
}{
{
Name: "no changes",
Before: []any{1, 2, 3, 4},
After: []any{1, 2, 3, 4},
Removed: []any{},
Added: []any{},
Common: []any{1, 2, 3, 4},
},
{
Name: "only removed",
Before: []any{1, 2, 3, 4},
After: []any{},
Removed: []any{1, 2, 3, 4},
Added: []any{},
Common: []any{},
},
{
Name: "only added",
Before: []any{},
After: []any{1, 2, 3, 4},
Removed: []any{},
Added: []any{1, 2, 3, 4},
Common: []any{},
},
{
Name: "added repeated items",
Before: []any{2},
After: []any{1, 2, 1},
Removed: []any{},
Added: []any{1, 1},
Common: []any{2},
},
{
Name: "removed repeated items",
Before: []any{1, 2, 1},
After: []any{2},
Removed: []any{1, 1},
Added: []any{},
Common: []any{2},
},
{
Name: "simple diff: ints",
Before: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
After: []any{1, 3, 5, 7, 9, 12, 13, 14},
Removed: []any{2, 4, 6, 8},
Added: []any{12, 13, 14},
Common: []any{1, 3, 5, 7, 9},
},
{
Name: "simple diff: strings",
Before: []any{"one", "two", "three", "four"},
After: []any{"five", "two", "four", "six"},
Removed: []any{"one", "three"},
Added: []any{"five", "six"},
Common: []any{"two", "four"},
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
added, removed, common := resources.ListDiffWithCommonItems(tc.Before, tc.After)
assert.Equal(t, tc.Added, added)
assert.Equal(t, tc.Removed, removed)
assert.Equal(t, tc.Common, common)
})
}
}

func Test_DataTypeIssue3007DiffSuppressFunc(t *testing.T) {
testCases := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/tag_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func UpdateContextTagAssociation(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

_, _, commonIds := ListDiffWithCommon(oldIds, newIds)
_, _, commonIds := ListDiffWithCommonItems(oldIds, newIds)

for _, id := range commonIds {
request := sdk.NewSetTagRequest(objectType, id).WithSetTags([]sdk.TagAssociation{
Expand Down
92 changes: 32 additions & 60 deletions pkg/sdk/testint/tags_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,28 @@ func TestInt_TagsAssociations(t *testing.T) {
tag.ID(),
}

testTagSet := func(id sdk.ObjectIdentifier, objectType sdk.ObjectType) {
err := client.Tags.Set(ctx, sdk.NewSetTagRequest(objectType, id).WithSetTags(tags))
assertTagSet := func(id sdk.ObjectIdentifier, objectType sdk.ObjectType) {
returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, objectType)
require.NoError(t, err)
assert.Equal(t, sdk.Pointer(tagValue), returnedTagValue)
}

assertTagUnset := func(id sdk.ObjectIdentifier, objectType sdk.ObjectType) {
returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, objectType)
require.NoError(t, err)
assert.Equal(t, sdk.Pointer(tagValue), returnedTagValue)
assert.Nil(t, returnedTagValue)
}

err = client.Tags.Unset(ctx, sdk.NewUnsetTagRequest(objectType, id).WithUnsetTags(unsetTags))
testTagSet := func(id sdk.ObjectIdentifier, objectType sdk.ObjectType) {
err := client.Tags.Set(ctx, sdk.NewSetTagRequest(objectType, id).WithSetTags(tags))
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, objectType)
assertTagSet(id, objectType)

err = client.Tags.Unset(ctx, sdk.NewUnsetTagRequest(objectType, id).WithUnsetTags(unsetTags))
require.NoError(t, err)
assert.Nil(t, returnedTagValue)

assertTagUnset(id, objectType)
}

t.Run("TestInt_TagAssociationForAccount_locator", func(t *testing.T) {
Expand All @@ -343,33 +351,25 @@ func TestInt_TagsAssociations(t *testing.T) {
})
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, sdk.ObjectTypeAccount)

err = client.Accounts.Alter(ctx, &sdk.AlterAccountOptions{
UnsetTag: unsetTags,
})
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, sdk.ObjectTypeAccount)

// test tag sdk method
err = client.Tags.SetOnCurrentAccount(ctx, sdk.NewSetTagOnCurrentAccountRequest().WithSetTags(tags))
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, sdk.ObjectTypeAccount)

err = client.Tags.UnsetOnCurrentAccount(ctx, sdk.NewUnsetTagOnCurrentAccountRequest().WithUnsetTags(unsetTags))
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, sdk.ObjectTypeAccount)
})

t.Run("TestInt_TagAssociationForAccount", func(t *testing.T) {
Expand All @@ -381,16 +381,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := client.Tags.Set(ctx, sdk.NewSetTagRequest(sdk.ObjectTypeAccount, id).WithSetTags(tags))
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Equal(t, sdk.Pointer(tagValue), returnedTagValue)
assertTagSet(id, sdk.ObjectTypeAccount)

err = client.Tags.UnsetOnCurrentAccount(ctx, sdk.NewUnsetTagOnCurrentAccountRequest().WithUnsetTags(unsetTags))
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeAccount)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, sdk.ObjectTypeAccount)
})

accountObjectTestCases := []struct {
Expand Down Expand Up @@ -653,16 +649,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := tc.setTags(id, tags)
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, tc.objectType)

err = tc.unsetTags(id, unsetTags)
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, tc.objectType)

// test object methods
testTagSet(id, tc.objectType)
Expand Down Expand Up @@ -745,16 +737,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := tc.setTags(id, tags)
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, tc.objectType)

err = tc.unsetTags(id, unsetTags)
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, tc.objectType)

// test object methods
testTagSet(id, tc.objectType)
Expand Down Expand Up @@ -933,16 +921,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := tc.setTags(id, tags)
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, tc.objectType)

err = tc.unsetTags(id, unsetTags)
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, tc.objectType)

// test object methods
testTagSet(id, tc.objectType)
Expand All @@ -958,9 +942,7 @@ func TestInt_TagsAssociations(t *testing.T) {
})
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeMaskingPolicy)
require.NoError(t, err)
assert.Equal(t, sdk.Pointer(tagValue), returnedTagValue)
assertTagSet(id, sdk.ObjectTypeMaskingPolicy)

// assert that setting masking policy does not apply the tag on the masking policy
refs, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, tag.ID(), sdk.PolicyEntityDomainTag)
Expand All @@ -972,9 +954,7 @@ func TestInt_TagsAssociations(t *testing.T) {
})
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeMaskingPolicy)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, sdk.ObjectTypeMaskingPolicy)

// test object methods
testTagSet(id, sdk.ObjectTypeMaskingPolicy)
Expand Down Expand Up @@ -1030,16 +1010,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := tc.setTags(id, tags)
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeColumn)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, sdk.ObjectTypeColumn)

err = tc.unsetTags(id, unsetTags)
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, sdk.ObjectTypeColumn)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, sdk.ObjectTypeColumn)

// test object methods
testTagSet(id, sdk.ObjectTypeColumn)
Expand Down Expand Up @@ -1108,16 +1084,12 @@ func TestInt_TagsAssociations(t *testing.T) {
err := tc.setTags(id, tags)
require.NoError(t, err)

returnedTagValue, err := client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Equal(t, tagValue, returnedTagValue)
assertTagSet(id, tc.objectType)

err = tc.unsetTags(id, unsetTags)
require.NoError(t, err)

returnedTagValue, err = client.SystemFunctions.GetTag(ctx, tag.ID(), id, tc.objectType)
require.NoError(t, err)
assert.Nil(t, returnedTagValue)
assertTagUnset(id, tc.objectType)

// test object methods
testTagSet(id, tc.objectType)
Expand Down
2 changes: 2 additions & 0 deletions templates/resources/tag_association.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ description: |-

!> **V1 release candidate** This resource was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the resource if needed. Any errors reported will be resolved with a higher priority. We encourage checking this resource out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0980--v0990) to use it.

-> **Note** For `ACCOUNT` object type, only identifiers with organization name are supported. See [account identifier docs](https://docs.snowflake.com/en/user-guide/admin-account-identifier#format-1-preferred-account-name-in-your-organization) for more details.

# {{.Name}} ({{.Type}})

{{ .Description | trimspace }}
Expand Down

0 comments on commit 8aa6ea6

Please sign in to comment.