forked from gruntwork-io/terratest
/
output.go
57 lines (46 loc) · 1.43 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
package terraform
import (
"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
}
// 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))
}