forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
196 lines (153 loc) · 4.28 KB
/
show.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 tfexec
import (
"bytes"
"context"
"fmt"
"os/exec"
tfjson "github.com/hashicorp/terraform-json"
)
type showConfig struct {
reattachInfo ReattachInfo
}
var defaultShowOptions = showConfig{}
type ShowOption interface {
configureShow(*showConfig)
}
func (opt *ReattachOption) configureShow(conf *showConfig) {
conf.reattachInfo = opt.info
}
// Show reads the default state path and outputs the state.
// To read a state or plan file, ShowState or ShowPlan must be used instead.
func (tf *Terraform) Show(ctx context.Context, opts ...ShowOption) (*tfjson.State, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform show -json was added in 0.12.0: %w", err)
}
c := defaultShowOptions
for _, o := range opts {
o.configureShow(&c)
}
mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return nil, err
}
mergeEnv[reattachEnvVar] = reattachStr
}
showCmd := tf.showCmd(ctx, true, mergeEnv)
var ret tfjson.State
ret.UseJSONNumber(true)
err = tf.runTerraformCmdJSON(ctx, showCmd, &ret)
if err != nil {
return nil, err
}
err = ret.Validate()
if err != nil {
return nil, err
}
return &ret, nil
}
// ShowStateFile reads a given state file and outputs the state.
func (tf *Terraform) ShowStateFile(ctx context.Context, statePath string, opts ...ShowOption) (*tfjson.State, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform show -json was added in 0.12.0: %w", err)
}
if statePath == "" {
return nil, fmt.Errorf("statePath cannot be blank: use Show() if not passing statePath")
}
c := defaultShowOptions
for _, o := range opts {
o.configureShow(&c)
}
mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return nil, err
}
mergeEnv[reattachEnvVar] = reattachStr
}
showCmd := tf.showCmd(ctx, true, mergeEnv, statePath)
var ret tfjson.State
ret.UseJSONNumber(true)
err = tf.runTerraformCmdJSON(ctx, showCmd, &ret)
if err != nil {
return nil, err
}
err = ret.Validate()
if err != nil {
return nil, err
}
return &ret, nil
}
// ShowPlanFile reads a given plan file and outputs the plan.
func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string, opts ...ShowOption) (*tfjson.Plan, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform show -json was added in 0.12.0: %w", err)
}
if planPath == "" {
return nil, fmt.Errorf("planPath cannot be blank: use Show() if not passing planPath")
}
c := defaultShowOptions
for _, o := range opts {
o.configureShow(&c)
}
mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return nil, err
}
mergeEnv[reattachEnvVar] = reattachStr
}
showCmd := tf.showCmd(ctx, true, mergeEnv, planPath)
var ret tfjson.Plan
err = tf.runTerraformCmdJSON(ctx, showCmd, &ret)
if err != nil {
return nil, err
}
err = ret.Validate()
if err != nil {
return nil, err
}
return &ret, nil
}
// ShowPlanFileRaw reads a given plan file and outputs the plan in a
// human-friendly, opaque format.
func (tf *Terraform) ShowPlanFileRaw(ctx context.Context, planPath string, opts ...ShowOption) (string, error) {
if planPath == "" {
return "", fmt.Errorf("planPath cannot be blank: use Show() if not passing planPath")
}
c := defaultShowOptions
for _, o := range opts {
o.configureShow(&c)
}
mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return "", err
}
mergeEnv[reattachEnvVar] = reattachStr
}
showCmd := tf.showCmd(ctx, false, mergeEnv, planPath)
var ret bytes.Buffer
showCmd.Stdout = &ret
err := tf.runTerraformCmd(ctx, showCmd)
if err != nil {
return "", err
}
return ret.String(), nil
}
func (tf *Terraform) showCmd(ctx context.Context, jsonOutput bool, mergeEnv map[string]string, args ...string) *exec.Cmd {
allArgs := []string{"show"}
if jsonOutput {
allArgs = append(allArgs, "-json")
}
allArgs = append(allArgs, "-no-color")
allArgs = append(allArgs, args...)
return tf.buildTerraformCmd(ctx, mergeEnv, allArgs...)
}