/
resource_block.go
160 lines (141 loc) · 3.3 KB
/
resource_block.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
package terraform
import (
"bytes"
"fmt"
"strings"
"text/template"
)
type PlanReference struct {
Value interface{}
}
type PlanBlock struct {
Type string
Name string
BlockType string
Blocks map[string]map[string]interface{}
Attributes map[string]interface{}
}
func NewPlanBlock(blockType, resourceType, resourceName string) *PlanBlock {
if blockType == "managed" {
blockType = "resource"
}
return &PlanBlock{
Type: resourceType,
Name: resourceName,
BlockType: blockType,
Blocks: make(map[string]map[string]interface{}),
Attributes: make(map[string]interface{}),
}
}
func (rb *PlanBlock) HasAttribute(attribute string) bool {
for k := range rb.Attributes {
if k == attribute {
return true
}
}
return false
}
func (rb *PlanBlock) ToHCL() string {
resourceTmpl, err := template.New("resource").Funcs(template.FuncMap{
"RenderValue": renderTemplateValue,
"RenderPrimitive": renderPrimitive,
}).Parse(resourceTemplate)
if err != nil {
panic(err)
}
var res bytes.Buffer
if err := resourceTmpl.Execute(&res, map[string]interface{}{
"BlockType": rb.BlockType,
"Type": rb.Type,
"Name": rb.Name,
"Attributes": rb.Attributes,
"Blocks": rb.Blocks,
}); err != nil {
return ""
}
return res.String()
}
var resourceTemplate = `{{ .BlockType }} "{{ .Type }}" "{{ .Name }}" {
{{ range $name, $value := .Attributes }}{{ if $value }}{{ $name }} {{ RenderValue $value }}
{{end}}{{ end }}{{ range $name, $block := .Blocks }}{{ $name }} {
{{ range $name, $value := $block }}{{ if $value }}{{ $name }} {{ RenderValue $value }}
{{end}}{{ end }}}
{{end}}}`
func renderTemplateValue(val interface{}) string {
switch t := val.(type) {
case map[string]interface{}:
return fmt.Sprintf("= %s", renderMap(t))
case []interface{}:
if isMapSlice(t) {
return renderSlice(t)
}
return fmt.Sprintf("= %s", renderSlice(t))
default:
return fmt.Sprintf("= %s", renderPrimitive(val))
}
}
func renderPrimitive(val interface{}) string {
switch t := val.(type) {
case PlanReference:
return fmt.Sprintf("%v", t.Value)
case string:
if strings.Contains(t, "\n") {
return fmt.Sprintf(`<<EOF
%s
EOF
`, t)
}
return fmt.Sprintf("%q", t)
case map[string]interface{}:
return renderMap(t)
case []interface{}:
return renderSlice(t)
default:
return fmt.Sprintf("%#v", t)
}
}
func isMapSlice(vars []interface{}) bool {
if len(vars) == 0 {
return false
}
val := vars[0]
switch val.(type) {
case map[string]interface{}:
return true
default:
return false
}
}
func renderSlice(vals []interface{}) string {
if len(vals) == 0 {
return "[]"
}
val := vals[0]
switch t := val.(type) {
// if vals[0] is a map[string]interface this is a block, so render it as a map
case map[string]interface{}:
return renderMap(t)
// otherwise its going to be just a list of primitives
default:
result := "[\n"
for _, v := range vals {
result = fmt.Sprintf("%s\t%v,\n", result, renderPrimitive(v))
}
result = fmt.Sprintf("%s]", result)
return result
}
}
func renderMap(val map[string]interface{}) string {
if len(val) == 0 {
return "{}"
}
result := "{\n"
for k, v := range val {
if v == nil {
continue
}
result = fmt.Sprintf("%s\t%s = %s\n", result, k, renderPrimitive(v))
}
result = fmt.Sprintf("%s}", result)
return result
}