-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
ecs_task_definition_equivalency.go
80 lines (70 loc) · 1.75 KB
/
ecs_task_definition_equivalency.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
package aws
import (
"bytes"
"encoding/json"
"reflect"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/mitchellh/copystructure"
)
func ecsContainerDefinitionsAreEquivalent(def1, def2 string) (bool, error) {
var obj1 containerDefinitions
err := json.Unmarshal([]byte(def1), &obj1)
if err != nil {
return false, err
}
err = obj1.Reduce()
if err != nil {
return false, err
}
canonicalJson1, err := jsonutil.BuildJSON(obj1)
if err != nil {
return false, err
}
var obj2 containerDefinitions
err = json.Unmarshal([]byte(def2), &obj2)
if err != nil {
return false, err
}
err = obj2.Reduce()
if err != nil {
return false, err
}
canonicalJson2, err := jsonutil.BuildJSON(obj2)
if err != nil {
return false, err
}
return bytes.Compare(canonicalJson1, canonicalJson2) == 0, nil
}
type containerDefinitions []*ecs.ContainerDefinition
func (cd containerDefinitions) Reduce() error {
for i, def := range cd {
// Deal with special fields which have defaults
for j, pm := range def.PortMappings {
if pm.Protocol != nil && *pm.Protocol == "tcp" {
cd[i].PortMappings[j].Protocol = nil
}
if pm.HostPort != nil && *pm.HostPort == 0 {
cd[i].PortMappings[j].HostPort = nil
}
}
// Create a mutable copy
defCopy, err := copystructure.Copy(def)
if err != nil {
return err
}
definition := reflect.ValueOf(defCopy).Elem()
for i := 0; i < definition.NumField(); i++ {
sf := definition.Field(i)
// Set all empty slices to nil
if sf.Kind() == reflect.Slice {
if sf.IsValid() && !sf.IsNil() && sf.Len() == 0 {
sf.Set(reflect.Zero(sf.Type()))
}
}
}
iface := definition.Interface().(ecs.ContainerDefinition)
cd[i] = &iface
}
return nil
}