/
output.go
350 lines (289 loc) · 11 KB
/
output.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package terraform
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
// Output calls terraform output for the given variable and return its string value representation.
// It only designed to work with primitive terraform types: string, number and bool.
// Please use OutputStruct for anything else.
func Output(t testing.TestingT, options *Options, key string) string {
out, err := OutputE(t, options, key)
require.NoError(t, err)
return out
}
// OutputE calls terraform output for the given variable and return its string value representation.
// It only designed to work with primitive terraform types: string, number and bool.
// Please use OutputStructE for anything else.
func OutputE(t testing.TestingT, options *Options, key string) (string, error) {
var val interface{}
err := OutputStructE(t, options, key, &val)
return fmt.Sprintf("%v", val), err
}
// OutputRequired calls terraform output for the given variable and return its value. If the value is empty, fail the test.
func OutputRequired(t testing.TestingT, options *Options, key string) string {
out, err := OutputRequiredE(t, options, key)
require.NoError(t, err)
return out
}
// OutputRequiredE calls terraform output for the given variable and return its value. If the value is empty, return an error.
func OutputRequiredE(t testing.TestingT, options *Options, key string) (string, error) {
out, err := OutputE(t, options, key)
if err != nil {
return "", err
}
if out == "" {
return "", EmptyOutput(key)
}
return out, nil
}
// parseListOfMaps takes a list of maps and parses the types.
// It is mainly a wrapper for parseMap to support lists.
func parseListOfMaps(l []interface{}) ([]map[string]interface{}, error) {
var result []map[string]interface{}
for _, v := range l {
asMap, isMap := v.(map[string]interface{})
if !isMap {
err := errors.New("Type switching to map[string]interface{} failed.")
return nil, err
}
m, err := parseMap(asMap)
if err != nil {
return nil, err
}
result = append(result, m)
}
return result, nil
}
// parseMap takes a map of interfaces and parses the types.
// It is recursive which allows it to support complex nested structures.
// At this time, this function uses https://golang.org/pkg/strconv/#ParseInt
// to determine if a number should be a float or an int. For this reason, if you are
// expecting a float with a zero as the "tenth" you will need to manually convert
// the return value to a float.
//
// This function exists to map return values of the terraform outputs to intuitive
// types. ie, if you are expecting a value of "1" you are implicitly expecting an int.
//
// This also allows the work to be executed recursively to support complex data types.
func parseMap(m map[string]interface{}) (map[string]interface{}, error) {
result := make(map[string]interface{})
for k, v := range m {
switch vt := v.(type) {
case map[string]interface{}:
nestedMap, err := parseMap(vt)
if err != nil {
return nil, err
}
result[k] = nestedMap
case []interface{}:
nestedList, err := parseListOfMaps(vt)
if err != nil {
return nil, err
}
result[k] = nestedList
case float64:
testInt, err := strconv.ParseInt((fmt.Sprintf("%v", vt)), 10, 0)
if err == nil {
result[k] = int(testInt)
} else {
result[k] = vt
}
default:
result[k] = vt
}
}
return result, nil
}
// OutputMapOfObjects calls terraform output for the given variable and returns its value as a map of lists/maps.
// If the output value is not a map of lists/maps, then it fails the test.
func OutputMapOfObjects(t testing.TestingT, options *Options, key string) map[string]interface{} {
out, err := OutputMapOfObjectsE(t, options, key)
require.NoError(t, err)
return out
}
// OutputMapOfObjectsE calls terraform output for the given variable and returns its value as a map of lists/maps.
// Also returns an error object if an error was generated.
// If the output value is not a map of lists/maps, then it fails the test.
func OutputMapOfObjectsE(t testing.TestingT, options *Options, key string) (map[string]interface{}, error) {
out, err := OutputJsonE(t, options, key)
if err != nil {
return nil, err
}
var output map[string]interface{}
if err := json.Unmarshal([]byte(out), &output); err != nil {
return nil, err
}
return parseMap(output)
}
// OutputListOfObjects calls terraform output for the given variable and returns its value as a list of maps/lists.
// If the output value is not a list of maps/lists, then it fails the test.
func OutputListOfObjects(t testing.TestingT, options *Options, key string) []map[string]interface{} {
out, err := OutputListOfObjectsE(t, options, key)
require.NoError(t, err)
return out
}
// OutputListOfObjectsE calls terraform output for the given variable and returns its value as a list of maps/lists.
// Also returns an error object if an error was generated.
// If the output value is not a list of maps/lists, then it fails the test.
func OutputListOfObjectsE(t testing.TestingT, options *Options, key string) ([]map[string]interface{}, error) {
out, err := OutputJsonE(t, options, key)
if err != nil {
return nil, err
}
var output []map[string]interface{}
if err := json.Unmarshal([]byte(out), &output); err != nil {
return nil, err
}
var result []map[string]interface{}
for _, m := range output {
newMap, err := parseMap(m)
if err != nil {
return nil, err
}
result = append(result, newMap)
}
return result, nil
}
// OutputList calls terraform output for the given variable and returns its value as a list.
// If the output value is not a list type, then it fails the test.
func OutputList(t testing.TestingT, options *Options, key string) []string {
out, err := OutputListE(t, options, key)
require.NoError(t, err)
return out
}
// OutputListE calls terraform output for the given variable and returns its value as a list.
// If the output value is not a list type, then it returns an error.
func OutputListE(t testing.TestingT, options *Options, key string) ([]string, error) {
out, err := OutputJsonE(t, options, key)
if err != nil {
return nil, err
}
var output interface{}
if err := json.Unmarshal([]byte(out), &output); err != nil {
return nil, err
}
if outputList, isList := output.([]interface{}); isList {
return parseListOutputTerraform(outputList, key)
}
return nil, UnexpectedOutputType{Key: key, ExpectedType: "map or list", ActualType: reflect.TypeOf(output).String()}
}
// Parse a list output in the format it is returned by Terraform 0.12 and newer versions
func parseListOutputTerraform(outputList []interface{}, key string) ([]string, error) {
list := []string{}
for _, item := range outputList {
list = append(list, fmt.Sprintf("%v", item))
}
return list, nil
}
// OutputMap calls terraform output for the given variable and returns its value as a map.
// If the output value is not a map type, then it fails the test.
func OutputMap(t testing.TestingT, options *Options, key string) map[string]string {
out, err := OutputMapE(t, options, key)
require.NoError(t, err)
return out
}
// OutputMapE calls terraform output for the given variable and returns its value as a map.
// If the output value is not a map type, then it returns an error.
func OutputMapE(t testing.TestingT, options *Options, key string) (map[string]string, error) {
out, err := OutputJsonE(t, options, key)
if err != nil {
return nil, err
}
outputMap := map[string]interface{}{}
if err := json.Unmarshal([]byte(out), &outputMap); err != nil {
return nil, err
}
resultMap := make(map[string]string)
for k, v := range outputMap {
resultMap[k] = fmt.Sprintf("%v", v)
}
return resultMap, nil
}
// OutputForKeys calls terraform output for the given key list and returns values as a map.
// If keys not found in the output, fails the test
func OutputForKeys(t testing.TestingT, options *Options, keys []string) map[string]interface{} {
out, err := OutputForKeysE(t, options, keys)
require.NoError(t, err)
return out
}
// OutputJson calls terraform output for the given variable and returns the
// result as the json string.
// If key is an empty string, it will return all the output variables.
func OutputJson(t testing.TestingT, options *Options, key string) string {
str, err := OutputJsonE(t, options, key)
require.NoError(t, err)
return str
}
// OutputJsonE calls terraform output for the given variable and returns the
// result as the json string.
// If key is an empty string, it will return all the output variables.
func OutputJsonE(t testing.TestingT, options *Options, key string) (string, error) {
args := []string{"output", "-no-color", "-json"}
if key != "" {
args = append(args, key)
}
return RunTerraformCommandAndGetStdoutE(t, options, args...)
}
// OutputStruct calls terraform output for the given variable and stores the
// result in the value pointed to by v. If v is nil or not a pointer, or if
// the value returned by Terraform is not appropriate for a given target type,
// it fails the test.
func OutputStruct(t testing.TestingT, options *Options, key string, v interface{}) {
err := OutputStructE(t, options, key, v)
require.NoError(t, err)
}
// OutputStructE calls terraform output for the given variable and stores the
// result in the value pointed to by v. If v is nil or not a pointer, or if
// the value returned by Terraform is not appropriate for a given target type,
// it returns an error.
func OutputStructE(t testing.TestingT, options *Options, key string, v interface{}) error {
out, err := OutputJsonE(t, options, key)
if err != nil {
return err
}
return json.Unmarshal([]byte(out), &v)
}
// OutputForKeysE calls terraform output for the given key list and returns values as a map.
// The returned values are of type interface{} and need to be type casted as necessary. Refer to output_test.go
func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]interface{}, error) {
out, err := OutputJsonE(t, options, "")
if err != nil {
return nil, err
}
outputMap := map[string]map[string]interface{}{}
if err := json.Unmarshal([]byte(out), &outputMap); err != nil {
return nil, err
}
if keys == nil {
outputKeys := make([]string, 0, len(outputMap))
for k := range outputMap {
outputKeys = append(outputKeys, k)
}
keys = outputKeys
}
resultMap := make(map[string]interface{})
for _, key := range keys {
value, containsValue := outputMap[key]["value"]
if !containsValue {
return nil, OutputKeyNotFound(string(key))
}
resultMap[key] = value
}
return resultMap, nil
}
// OutputAll calls terraform output returns all values as a map.
// If there is error fetching the output, fails the test
func OutputAll(t testing.TestingT, options *Options) map[string]interface{} {
out, err := OutputAllE(t, options)
require.NoError(t, err)
return out
}
// OutputAllE calls terraform and returns all the outputs as a map
func OutputAllE(t testing.TestingT, options *Options) (map[string]interface{}, error) {
return OutputForKeysE(t, options, nil)
}