forked from hashicorp/terraform-provider-aws
/
datasync_tags.go
63 lines (52 loc) · 1.54 KB
/
datasync_tags.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
package aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/datasync"
)
// dataSyncTagsDiff takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
func dataSyncTagsDiff(oldTags, newTags []*datasync.TagListEntry) ([]*datasync.TagListEntry, []*datasync.TagListEntry) {
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}
// Build the list of what to remove
var remove []*datasync.TagListEntry
for _, t := range oldTags {
old, ok := create[aws.StringValue(t.Key)]
if !ok || old != aws.StringValue(t.Value) {
// Delete it!
remove = append(remove, t)
}
}
return expandDataSyncTagListEntry(create), remove
}
func dataSyncTagsKeys(tags []*datasync.TagListEntry) []*string {
keys := make([]*string, 0)
for _, tag := range tags {
if tag == nil {
continue
}
keys = append(keys, tag.Key)
}
return keys
}
func expandDataSyncTagListEntry(m map[string]interface{}) []*datasync.TagListEntry {
result := []*datasync.TagListEntry{}
for k, v := range m {
result = append(result, &datasync.TagListEntry{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}
return result
}
func flattenDataSyncTagListEntry(ts []*datasync.TagListEntry) map[string]string {
result := map[string]string{}
for _, t := range ts {
result[aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}
return result
}