-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfn_util.go
91 lines (78 loc) · 2.44 KB
/
cfn_util.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
package main
import (
"sort"
"strings"
)
func resourcesByService(spec *CloudFormationResourceSpecification, serviceNames []string) *map[string]map[string]Resource {
resourcesByService := map[string]map[string]Resource{}
for _, service := range serviceNames {
resourcesByService[service] = map[string]Resource{}
}
for resourceName, resource := range spec.Resources {
splits := strings.Split(resourceName, "::")
service := splits[1]
resourcesByService[service][resourceName] = resource
}
return &resourcesByService
}
func serviceNames(spec *CloudFormationResourceSpecification) []string {
servicesMap := map[string]bool{}
for resourceName := range spec.Resources {
splits := strings.Split(resourceName, "::")
servicesMap[splits[1]] = true
}
serviceNames := make([]string, 0, len(servicesMap))
for serviceName := range servicesMap {
serviceNames = append(serviceNames, serviceName)
}
sort.Strings(serviceNames)
return serviceNames
}
func propertiesByResource(spec *CloudFormationResourceSpecification) *map[string]map[string]Resource {
// Find weird/broken properties
// fmt.Println("weird/broken properties")
// weirdProps := []string{}
// for resourcePropertyName, property := range spec.Properties {
// if len(property.Properties) == 0 {
// weirdProps = append(weirdProps, resourcePropertyName)
// }
// }
// sort.Strings(weirdProps)
// for _, prop := range weirdProps {
// fmt.Println(prop)
// }
// panic(0)
propertiesByResource := map[string]map[string]Resource{}
for resourcePropertyName, property := range spec.Properties {
splits := strings.Split(resourcePropertyName, ".")
if len(splits) > 1 {
resourceName := splits[0]
if propertiesByResource[resourceName] == nil {
propertiesByResource[resourceName] = map[string]Resource{}
}
propertyName := splits[1]
propertiesByResource[resourceName][propertyName] = property
}
}
for resourceName := range spec.Resources {
if propertiesByResource[resourceName] == nil {
propertiesByResource[resourceName] = map[string]Resource{}
}
propertiesByResource[resourceName]["Tag"] = spec.Properties["Tag"]
}
return &propertiesByResource
}
func resourceNamesSlice(p map[string]Resource) (keys []string) {
keys = make([]string, 0, len(p))
for key := range p {
keys = append(keys, key)
}
return keys
}
func propertyNames(p map[string]Property) (keys []string) {
keys = make([]string, 0, len(p))
for key := range p {
keys = append(keys, key)
}
return keys
}