forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
196 lines (166 loc) · 5.6 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
package terraform
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
// 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)
if err != nil {
t.Fatal(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 := RunTerraformCommandE(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)
if err != nil {
t.Fatal(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)
if err != nil {
t.Fatal(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 := RunTerraformCommandE(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
}
value, containsValue := outputMap["value"]
if !containsValue {
return nil, fmt.Errorf("Output doesn't contain a value for the key %q", key)
}
list := []string{}
switch t := value.(type) {
case []interface{}:
for _, item := range t {
list = append(list, fmt.Sprintf("%v", item))
}
default:
return nil, fmt.Errorf("Output value %q is not a list", 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)
if err != nil {
t.Fatal(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 := RunTerraformCommandE(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
}
value, containsValue := outputMap["value"]
if !containsValue {
return nil, fmt.Errorf("Output doesn't contain a value for the key %q", key)
}
valueMap, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Output value %q is not a map", value)
}
resultMap := make(map[string]string)
for k, v := range valueMap {
resultMap[k] = fmt.Sprintf("%v", v)
}
return resultMap, nil
}
// OutputForKeysE 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)
if err != nil {
t.Fatal(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 := RunTerraformCommandE(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, fmt.Errorf("output doesn't contain a value for the key %q", 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)
if err != nil {
t.Fatal(err)
}
return out
}
// OutputListE calls terraform output and returns all the outputs as a map
func OutputAllE(t *testing.T, options *Options) (map[string]interface{}, error) {
return OutputForKeysE(t, options, nil)
}
// EmptyOutput is an error that occurs when an output is empty.
type EmptyOutput string
func (outputName EmptyOutput) Error() string {
return fmt.Sprintf("Required output %s was empty", string(outputName))
}