-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
plan.go
135 lines (112 loc) · 5.02 KB
/
plan.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
package terraform
import (
"fmt"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
// InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
// This will fail the test if there is an error in the command.
func InitAndPlan(t testing.TestingT, options *Options) string {
out, err := InitAndPlanE(t, options)
require.NoError(t, err)
return out
}
// InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
func InitAndPlanE(t testing.TestingT, options *Options) (string, error) {
if _, err := InitE(t, options); err != nil {
return "", err
}
if _, err := GetE(t, options); err != nil {
return "", err
}
return PlanE(t, options)
}
// Plan runs terraform plan with the given options and returns stdout/stderr.
// This will fail the test if there is an error in the command.
func Plan(t testing.TestingT, options *Options) string {
out, err := PlanE(t, options)
require.NoError(t, err)
return out
}
// PlanE runs terraform plan with the given options and returns stdout/stderr.
func PlanE(t testing.TestingT, options *Options) (string, error) {
return RunTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-lock=false")...)
}
// InitAndPlanAndShow runs terraform init, then terraform plan, and then terraform show with the given options, and
// returns the json output of the plan file. This will fail the test if there is an error in the command.
func InitAndPlanAndShow(t testing.TestingT, options *Options) string {
jsonOut, err := InitAndPlanAndShowE(t, options)
require.NoError(t, err)
return jsonOut
}
// InitAndPlanAndShowE runs terraform init, then terraform plan, and then terraform show with the given options, and
// returns the json output of the plan file.
func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error) {
if options.PlanFilePath == "" {
return "", PlanFilePathRequired
}
_, err := InitAndPlanE(t, options)
if err != nil {
return "", err
}
return ShowE(t, options)
}
// InitAndPlanAndShowWithStruct runs terraform init, then terraform plan, and then terraform show with the given
// options, and parses the json result into a go struct. This will fail the test if there is an error in the command.
func InitAndPlanAndShowWithStruct(t testing.TestingT, options *Options) *PlanStruct {
plan, err := InitAndPlanAndShowWithStructE(t, options)
require.NoError(t, err)
return plan
}
// InitAndPlanAndShowWithStructE runs terraform init, then terraform plan, and then terraform show with the given options, and
// parses the json result into a go struct.
func InitAndPlanAndShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error) {
jsonOut, err := InitAndPlanAndShowE(t, options)
if err != nil {
return nil, err
}
return parsePlanJson(jsonOut)
}
// InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command.
// This will fail the test if there is an error in the command.
func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int {
exitCode, err := InitAndPlanWithExitCodeE(t, options)
require.NoError(t, err)
return exitCode
}
// InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.
func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error) {
if _, err := InitE(t, options); err != nil {
return DefaultErrorExitCode, err
}
return PlanExitCodeE(t, options)
}
// PlanExitCode runs terraform plan with the given options and returns the detailed exitcode.
// This will fail the test if there is an error in the command.
func PlanExitCode(t testing.TestingT, options *Options) int {
exitCode, err := PlanExitCodeE(t, options)
require.NoError(t, err)
return exitCode
}
// PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode.
func PlanExitCodeE(t testing.TestingT, options *Options) (int, error) {
return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-detailed-exitcode")...)
}
// TgPlanAllExitCode runs terragrunt plan-all with the given options and returns the detailed exitcode.
// This will fail the test if there is an error in the command.
func TgPlanAllExitCode(t testing.TestingT, options *Options) int {
exitCode, err := TgPlanAllExitCodeE(t, options)
require.NoError(t, err)
return exitCode
}
// TgPlanAllExitCodeE runs terragrunt plan-all with the given options and returns the detailed exitcode.
func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error) {
if options.TerraformBinary != "terragrunt" {
return 1, fmt.Errorf("terragrunt must be set as TerraformBinary to use this method")
}
return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "plan-all", "--input=false", "--lock=true", "--detailed-exitcode")...)
}
// Custom errors
var (
PlanFilePathRequired = fmt.Errorf("You must set PlanFilePath on options struct to use this function.")
)