-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreview.go
More file actions
276 lines (247 loc) · 8.25 KB
/
preview.go
File metadata and controls
276 lines (247 loc) · 8.25 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package preview
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"log/slog"
"slices"
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/coder/preview/hclext"
"github.com/coder/preview/tfvars"
"github.com/coder/preview/types"
)
type Input struct {
// PlanJSONPath is an optional path to a plan file. If PlanJSON isn't
// specified, and PlanJSONPath is, then the file will be read and treated
// as if the contents were passed in directly.
PlanJSONPath string
PlanJSON json.RawMessage
ParameterValues map[string]string
Owner types.WorkspaceOwner
Logger *slog.Logger
// TFVars will override any variables set in '.tfvars' files.
// The value set must be a cty.Value, as the type can be anything.
TFVars map[string]cty.Value
}
type Output struct {
// ModuleOutput is any 'output' values from the terraform files. This has 0
// effect on the parameters, tags, etc. It can be helpful for debugging, as it
// allows exporting some terraform values to the caller to review.
//
// JSON marshalling is handled in the custom methods.
ModuleOutput cty.Value `json:"-"`
Parameters []types.Parameter `json:"parameters"`
WorkspaceTags types.TagBlocks `json:"workspace_tags"`
Presets []types.Preset `json:"presets"`
Variables []types.Variable `json:"variables"`
// Files is included for printing diagnostics.
// They can be marshalled, but not unmarshalled. This is a limitation
// of the HCL library.
Files map[string]*hcl.File `json:"-"`
}
// MarshalJSON includes the ModuleOutput and files in the JSON output. Output
// should never be unmarshalled. Marshalling to JSON is strictly useful for
// debugging information.
func (o Output) MarshalJSON() ([]byte, error) {
// Do not make this a fatal error, as it is supplementary information.
modOutput, _ := ctyjson.Marshal(o.ModuleOutput, o.ModuleOutput.Type())
type Alias Output
return json.Marshal(&struct {
ModuleOutput json.RawMessage `json:"module_output"`
Files map[string]*hcl.File `json:"files"`
Alias
}{
ModuleOutput: modOutput,
Files: o.Files,
Alias: (Alias)(o),
})
}
// ValidatePrebuilds will iterate over each preset, validate the inputs as a set,
// and attach any diagnostics to the preset if there are issues. This must be done
// because prebuilds have to be valid without user input.
//
// This will only validate presets that have prebuilds configured and have no
// existing error diagnostics. This should only be used when doing Template
// Imports as a protection against invalid presets.
//
// A preset doesn't need to specify all required parameters, as users can provide
// the remaining values when creating a workspace. However, since prebuild
// creation has no user input, presets used for prebuilds must provide all
// required parameter values.
func ValidatePrebuilds(ctx context.Context, input Input, preValid []types.Preset, dir fs.FS) {
for i := range preValid {
pre := &preValid[i]
if pre.Prebuilds == nil || pre.Prebuilds.Instances <= 0 {
// No prebuilds, so presets do not need to be valid without user input
continue
}
if hcl.Diagnostics(pre.Diagnostics).HasErrors() {
// Piling on diagnostics is not helpful, if an error exists, leave it at that.
continue
}
// Diagnostics are added to the existing preset.
input.ParameterValues = pre.Parameters
output, diagnostics := Preview(ctx, input, dir)
if diagnostics.HasErrors() {
pre.Diagnostics = append(pre.Diagnostics, diagnostics...)
// Do not pile on more diagnostics for individual params, it already failed
continue
}
if output == nil {
continue
}
// Check all parameters in the preset are defined by the template.
for paramName, _ := range pre.Parameters {
templateParamIndex := slices.IndexFunc(output.Parameters, func(p types.Parameter) bool {
return p.Name == paramName
})
if templateParamIndex == -1 {
pre.Diagnostics = append(pre.Diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Undefined Parameter",
Detail: fmt.Sprintf("Preset parameter %q is not defined by the template.", paramName),
})
continue
}
}
// If any parameter is invalid, then the preset is invalid.
// A value must be specified for this failing parameter.
for _, param := range output.Parameters {
if hcl.Diagnostics(param.Diagnostics).HasErrors() {
for _, paramDiag := range param.Diagnostics {
if paramDiag.Severity != hcl.DiagError {
continue // Only care about errors here
}
orig := paramDiag.Summary
paramDiag.Summary = fmt.Sprintf("Parameter %s: %s", param.Name, orig)
pre.Diagnostics = append(pre.Diagnostics, paramDiag)
}
}
}
}
}
func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagnostics hcl.Diagnostics) {
// The trivy package works with `github.com/zclconf/go-cty`. This package is
// similar to `reflect` in its usage. This package can panic if types are
// misused. To protect the caller, a general `recover` is used to catch any
// mistakes. If this happens, there is a developer bug that needs to be resolved.
defer func() {
if r := recover(); r != nil {
diagnostics = hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Panic occurred in preview. This should not happen, please report this to Coder.",
Detail: fmt.Sprintf("panic in preview: %+v", r),
},
}
}
}()
varFiles, err := tfvars.TFVarFiles("", dir)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Files not found",
Detail: err.Error(),
},
}
}
variableValues, err := tfvars.LoadTFVars(dir, varFiles)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to load tfvars from files",
Detail: err.Error(),
},
}
}
planHook, err := planJSONHook(dir, input)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Parsing plan JSON",
Detail: err.Error(),
},
}
}
ownerHook, err := workspaceOwnerHook(dir, input)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Workspace owner hook",
Detail: err.Error(),
},
}
}
logger := input.Logger
if logger == nil { // Default to discarding logs
logger = slog.New(slog.DiscardHandler)
}
// Override with user-supplied variables
for k, v := range input.TFVars {
variableValues[k] = v
}
// moduleSource is "" for a local module
p := parser.New(dir, "",
parser.OptionWithLogger(logger),
parser.OptionStopOnHCLError(false),
parser.OptionWithDownloads(false),
parser.OptionWithSkipCachedModules(true),
parser.OptionWithEvalHook(planHook),
parser.OptionWithEvalHook(ownerHook),
parser.OptionWithWorkingDirectoryPath("/"),
parser.OptionWithEvalHook(parameterContextsEvalHook(input)),
// 'OptionsWithTfVars' cannot be set with 'OptionWithTFVarsPaths'. So load the
// tfvars from the files ourselves and merge with the user-supplied tf vars.
parser.OptionsWithTfVars(variableValues),
)
err = p.ParseFS(ctx, ".")
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Parse terraform files",
Detail: err.Error(),
},
}
}
modules, err := p.EvaluateAll(ctx)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Evaluate terraform files",
Detail: err.Error(),
},
}
}
outputs := hclext.ExportOutputs(modules)
diags := make(hcl.Diagnostics, 0)
rp, rpDiags := parameters(modules)
// preValidPresets are extracted as written in terraform. Each individual
// param value is checked, however, the preset as a whole might not be valid.
preValidPresets := presets(modules, rp)
tags, tagDiags := workspaceTags(modules, p.Files())
vars := variables(modules)
// Add warnings
diags = diags.Extend(warnings(modules))
return &Output{
ModuleOutput: outputs,
Parameters: rp,
WorkspaceTags: tags,
Presets: preValidPresets,
Files: p.Files(),
Variables: vars,
}, diags.Extend(rpDiags).Extend(tagDiags)
}
func (i Input) RichParameterValue(key string) (string, bool) {
p, ok := i.ParameterValues[key]
return p, ok
}