/
output.go
140 lines (116 loc) · 3.83 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
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
}
// 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))
}