-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
writer.go
176 lines (142 loc) · 4.32 KB
/
writer.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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 terraformWriter
import (
"fmt"
"path"
"strconv"
"strings"
"sync"
)
type TerraformWriter struct {
// mutex protects the following items (resources & Files)
mutex sync.Mutex
// resources is a list of TF items that should be created
resources []*terraformResource
// outputs is a list of our TF output variables
outputs map[string]*terraformOutputVariable
// Files is a map of TF resource Files that should be created
Files map[string][]byte
}
type OutputValue struct {
Value *Literal
ValueArray []*Literal
}
type terraformResource struct {
ResourceType string
ResourceName string
Item interface{}
}
type terraformOutputVariable struct {
Key string
Value *Literal
ValueArray []*Literal
}
// sanitizeName ensures terraform resource names don't start with digits or contain any invalid characters
func sanitizeName(name string) string {
// Terraform resource names cannot start with a digit
if _, err := strconv.Atoi(string(name[0])); err == nil {
name = fmt.Sprintf("prefix_%v", name)
}
return strings.NewReplacer(".", "-", "/", "--", ":", "_").Replace(name)
}
func (t *TerraformWriter) InitTerraformWriter() {
t.Files = make(map[string][]byte)
t.outputs = make(map[string]*terraformOutputVariable)
}
func (t *TerraformWriter) AddFileBytes(resourceType string, resourceName string, key string, data []byte, base64 bool) (*Literal, error) {
id := resourceType + "_" + resourceName + "_" + key
t.mutex.Lock()
defer t.mutex.Unlock()
p := path.Join("data", id)
t.Files[p] = data
modulePath := fmt.Sprintf("%q", path.Join("${path.module}", p))
fn := "file"
if base64 {
fn = "filebase64"
}
return LiteralFunctionExpression(fn, []string{modulePath}), nil
}
func (t *TerraformWriter) RenderResource(resourceType string, resourceName string, e interface{}) error {
res := &terraformResource{
ResourceType: resourceType,
ResourceName: resourceName,
Item: e,
}
t.mutex.Lock()
defer t.mutex.Unlock()
t.resources = append(t.resources, res)
return nil
}
func (t *TerraformWriter) AddOutputVariable(key string, literal *Literal) error {
v := &terraformOutputVariable{
Key: key,
Value: literal,
}
t.mutex.Lock()
defer t.mutex.Unlock()
if t.outputs[key] != nil {
return fmt.Errorf("duplicate variable: %q", key)
}
t.outputs[key] = v
return nil
}
func (t *TerraformWriter) AddOutputVariableArray(key string, literal *Literal) error {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.outputs[key] == nil {
v := &terraformOutputVariable{
Key: key,
}
t.outputs[key] = v
}
if t.outputs[key].Value != nil {
return fmt.Errorf("variable %q is both an array and a scalar", key)
}
t.outputs[key].ValueArray = append(t.outputs[key].ValueArray, literal)
return nil
}
func (t *TerraformWriter) GetResourcesByType() (map[string]map[string]interface{}, error) {
resourcesByType := make(map[string]map[string]interface{})
for _, res := range t.resources {
resources := resourcesByType[res.ResourceType]
if resources == nil {
resources = make(map[string]interface{})
resourcesByType[res.ResourceType] = resources
}
tfName := sanitizeName(res.ResourceName)
if resources[tfName] != nil {
return nil, fmt.Errorf("duplicate resource found: %s.%s", res.ResourceType, tfName)
}
resources[tfName] = res.Item
}
return resourcesByType, nil
}
func (t *TerraformWriter) GetOutputs() (map[string]OutputValue, error) {
values := map[string]OutputValue{}
for _, v := range t.outputs {
tfName := sanitizeName(v.Key)
if _, found := values[tfName]; found {
return nil, fmt.Errorf("duplicate variable found: %s", tfName)
}
deduped, err := dedupLiterals(v.ValueArray)
if err != nil {
return nil, err
}
values[tfName] = OutputValue{
Value: v.Value,
ValueArray: deduped,
}
}
return values, nil
}