Skip to content

Commit 7152103

Browse files
Add ackcompare.GetTagsDifference helper method (#109)
Extends aws-controllers-k8s/code-generator#399 Description of changes: Adds a `GetTagsDifference` helper method that can be used to determine which tags should be added and removed based on what they are going "from" and "to" - typically needed for services with `TagResource` and `UntagResource` operations. For example: ```go aTags := ToACKTags(a.ko.Spec.Tags) bTags := ToACKTags(b.ko.Spec.Tags) added, _, removed := ackcompare.GetTagsDifference(aTags, bTags) // optionally, convert back to service Tag type toAdd := FromACKTags(added) toRemove := FromACKTags(removed) ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent b98d322 commit 7152103

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ require (
4444
github.com/prometheus/client_model v0.2.0 // indirect
4545
github.com/prometheus/common v0.28.0 // indirect
4646
github.com/prometheus/procfs v0.6.0 // indirect
47+
github.com/samber/lo v1.37.0 // indirect
4748
github.com/stretchr/objx v0.1.1 // indirect
4849
go.uber.org/atomic v1.7.0 // indirect
4950
go.uber.org/multierr v1.6.0 // indirect
51+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
5052
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
5153
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
5254
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
411411
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
412412
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
413413
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
414+
github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw=
415+
github.com/samber/lo v1.37.0/go.mod h1:9vaz2O4o8oOnK23pd2TrXufcbdbJIa3b6cstBWKpopA=
414416
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
415417
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
416418
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
@@ -520,6 +522,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
520522
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
521523
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
522524
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
525+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
526+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
523527
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
524528
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
525529
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

pkg/compare/tags.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package compare
15+
16+
import (
17+
"github.com/samber/lo"
18+
19+
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
20+
)
21+
22+
// GetTagsDifference determines which tags have been added, unchanged or have
23+
// been removed between the `from` and the `to` inputs. Tags that are in `from`
24+
// but not in `to` are added to `removed`, whereas tags that are in `to` but not
25+
// in `from` are added to `added`.
26+
func GetTagsDifference(from, to acktags.Tags) (added, unchanged, removed acktags.Tags) {
27+
// we need to convert the tag tuples to a comparable interface type
28+
fromPairs := lo.ToPairs(from)
29+
toPairs := lo.ToPairs(to)
30+
31+
left, right := lo.Difference(fromPairs, toPairs)
32+
middle := lo.Intersect(fromPairs, toPairs)
33+
34+
removed = lo.FromPairs(left)
35+
added = lo.FromPairs(right)
36+
unchanged = lo.FromPairs(middle)
37+
38+
return added, unchanged, removed
39+
}

pkg/compare/tags_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package compare_test
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/require"
20+
21+
"github.com/aws-controllers-k8s/runtime/pkg/compare"
22+
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
23+
)
24+
25+
func TestTagsDifference(t *testing.T) {
26+
require := require.New(t)
27+
28+
a := acktags.NewTags()
29+
b := acktags.NewTags()
30+
31+
a["tag1"] = "value1"
32+
33+
a["tag2"] = "value2"
34+
b["tag2"] = "value2"
35+
36+
b["tag3"] = "value3"
37+
b["tag4"] = "value4"
38+
39+
added, unchanged, removed := compare.GetTagsDifference(a, b)
40+
41+
require.Len(added, 2)
42+
require.Len(unchanged, 1)
43+
require.Len(removed, 1)
44+
45+
require.Contains(added, "tag3")
46+
require.Contains(added, "tag4")
47+
require.Contains(unchanged, "tag2")
48+
require.Contains(removed, "tag1")
49+
50+
// add test for updating the value of an existing tag
51+
a["tag2"] = "oldvalue"
52+
b["tag2"] = "newvalue"
53+
54+
added, unchanged, removed = compare.GetTagsDifference(a, b)
55+
56+
require.Len(added, 3)
57+
require.Len(unchanged, 0)
58+
require.Len(removed, 2)
59+
60+
require.Contains(added, "tag2")
61+
require.Equal(added["tag2"], "newvalue")
62+
}

0 commit comments

Comments
 (0)