/
output.go
207 lines (173 loc) · 6.42 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
package terraform
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// Output calls terraform output for the given variable and return its value.
func Output(t *testing.T, 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 value.
func OutputE(t *testing.T, options *Options, key string) (string, error) {
output, err := RunTerraformCommandAndGetStdoutE(t, options, "output", "-no-color", key)
if err != nil {
return "", err
}
return strings.TrimSpace(output), nil
}
// OutputRequired calls terraform output for the given variable and return its value. If the value is empty, fail the test.
func OutputRequired(t *testing.T, 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.T, 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
}
// 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.T, 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.T, options *Options, key string) ([]string, error) {
out, err := RunTerraformCommandAndGetStdoutE(t, options, "output", "-no-color", "-json", key)
if err != nil {
return nil, err
}
var output interface{}
if err := json.Unmarshal([]byte(out), &output); err != nil {
return nil, err
}
if outputMap, isMap := output.(map[string]interface{}); isMap {
return parseListOutputTerraform11OrOlder(outputMap, key)
} else if outputList, isList := output.([]interface{}); isList {
return parseListOutputTerraform12OrNewer(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 parseListOutputTerraform12OrNewer(outputList []interface{}, key string) ([]string, error) {
list := []string{}
for _, item := range outputList {
list = append(list, fmt.Sprintf("%v", item))
}
return list, nil
}
// Parse a list output in the format it is returned by Terraform 0.11 and older versions
func parseListOutputTerraform11OrOlder(outputMap map[string]interface{}, key string) ([]string, error) {
value, containsValue := outputMap["value"]
if !containsValue {
return nil, OutputKeyNotFound(key)
}
list := []string{}
switch t := value.(type) {
case []interface{}:
for _, item := range t {
list = append(list, fmt.Sprintf("%v", item))
}
default:
return nil, OutputValueNotList{Value: value}
}
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.T, 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.T, options *Options, key string) (map[string]string, error) {
out, err := RunTerraformCommandAndGetStdoutE(t, options, "output", "-no-color", "-json", key)
if err != nil {
return nil, err
}
outputMap := map[string]interface{}{}
if err := json.Unmarshal([]byte(out), &outputMap); err != nil {
return nil, err
}
// Terraform 0.11 or older return an object where the value we want is under the key "value". Terraform 0.12 and
// older return the value we want directly.
value, containsValue := outputMap["value"]
_, containsSensitive := outputMap["sensitive"]
_, containsType := outputMap["type"]
if containsValue && containsSensitive && containsType {
// Handle Terraform 0.11 and older
valueMap, ok := value.(map[string]interface{})
if !ok {
return nil, OutputValueNotMap{Value: value}
}
outputMap = valueMap
}
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.T, options *Options, keys []string) map[string]interface{} {
out, err := OutputForKeysE(t, options, keys)
require.NoError(t, err)
return out
}
// 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.T, options *Options, keys []string) (map[string]interface{}, error) {
out, err := RunTerraformCommandAndGetStdoutE(t, options, "output", "-no-color", "-json")
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.T, 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.T, options *Options) (map[string]interface{}, error) {
return OutputForKeysE(t, options, nil)
}