/
hooks.go
193 lines (174 loc) · 5.11 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package event_data_store
import (
"context"
"fmt"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
svcsdk "github.com/aws/aws-sdk-go/service/cloudtrail"
svcapitypes "github.com/aws-controllers-k8s/cloudtrail-controller/apis/v1alpha1"
)
// setResourceAdditionalFields will set the fields that are not returned by
// GetEventDataStore calls
func (rm *resourceManager) setResourceAdditionalFields(
ctx context.Context,
ko *svcapitypes.EventDataStore,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.setResourceAdditionalFields")
defer func() { exit(err) }()
if ko.Status.ACKResourceMetadata != nil && ko.Status.ACKResourceMetadata.ARN != nil &&
*ko.Status.ACKResourceMetadata.ARN != "" {
// Set event data store tags
ko.Spec.Tags, err = rm.getTags(ctx, string(*ko.Status.ACKResourceMetadata.ARN))
if err != nil {
return err
}
}
return nil
}
// syncTags updates a EDS list of tags.
func (rm *resourceManager) syncTags(
ctx context.Context,
latest *resource,
desired *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncTags")
defer func() { exit(err) }()
added, removed := computeTagsDelta(latest.ko.Spec.Tags, desired.ko.Spec.Tags)
// Tags to create/update
if len(removed) > 0 {
_, err = rm.sdkapi.RemoveTagsWithContext(
ctx,
&svcsdk.RemoveTagsInput{
ResourceId: (*string)(latest.ko.Status.ACKResourceMetadata.ARN),
TagsList: sdkTagsFromResourceTags(removed),
},
)
rm.metrics.RecordAPICall("UPDATE", "RemoveTags", err)
if err != nil {
return err
}
}
if len(added) > 0 {
_, err = rm.sdkapi.AddTagsWithContext(
ctx,
&svcsdk.AddTagsInput{
ResourceId: (*string)(latest.ko.Status.ACKResourceMetadata.ARN),
TagsList: sdkTagsFromResourceTags(added),
},
)
rm.metrics.RecordAPICall("UPDATE", "AddTags", err)
if err != nil {
return err
}
}
return nil
}
// getTags retrieves a resource list of tags.
func (rm *resourceManager) getTags(
ctx context.Context,
resourceARN string,
) ([]*svcapitypes.Tag, error) {
tags := []*svcapitypes.Tag{}
listTagsResponse, err := rm.sdkapi.ListTagsWithContext(
ctx,
&svcsdk.ListTagsInput{
ResourceIdList: []*string{&resourceARN},
},
)
rm.metrics.RecordAPICall("GET", "ListTags", err)
if err != nil {
return nil, err
}
if len(listTagsResponse.ResourceTagList) != 1 {
return nil, fmt.Errorf("expected to receive one exact resource tags in ListTags response")
}
for _, tag := range listTagsResponse.ResourceTagList[0].TagsList {
tags = append(tags, &svcapitypes.Tag{
Key: tag.Key,
Value: tag.Value,
})
}
return tags, nil
}
func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
} else if len(a.ko.Spec.Tags) > 0 {
if !equalTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
}
// equalTags returns true if two Tag arrays are equal regardless of the order
// of their elements.
func equalTags(
a []*svcapitypes.Tag,
b []*svcapitypes.Tag,
) bool {
added, removed := computeTagsDelta(a, b)
return len(added) == 0 && len(removed) == 0
}
// computeTagsDelta compares two Tag arrays and return two different lists
// containing the added and removed tags.
// The removed tags list only contains the Key of tags
func computeTagsDelta(
a []*svcapitypes.Tag,
b []*svcapitypes.Tag,
) (added, removed []*svcapitypes.Tag) {
var visitedIndexes []string
mainLoop:
for _, aElement := range a {
visitedIndexes = append(visitedIndexes, *aElement.Key)
for _, bElement := range b {
if equalStrings(aElement.Key, bElement.Key) {
if !equalStrings(aElement.Value, bElement.Value) {
added = append(added, bElement)
}
continue mainLoop
}
}
removed = append(removed, aElement)
}
for _, bElement := range b {
if !ackutil.InStrings(*bElement.Key, visitedIndexes) {
added = append(added, bElement)
}
}
return added, removed
}
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag array.
func sdkTagsFromResourceTags(rTags []*svcapitypes.Tag) []*svcsdk.Tag {
tags := make([]*svcsdk.Tag, len(rTags))
for i := range rTags {
tags[i] = &svcsdk.Tag{
Key: rTags[i].Key,
Value: rTags[i].Value,
}
}
return tags
}
func equalStrings(a, b *string) bool {
if a == nil {
return b == nil || *b == ""
}
return (*a == "" && b == nil) || *a == *b
}