@@ -68,6 +68,61 @@ func (o Output) MarshalJSON() ([]byte, error) {
6868 })
6969}
7070
71+ // ValidatePrebuilds will iterate over each preset, validate the inputs as a set,
72+ // and attach any diagnostics to the preset if there are issues. This must be done
73+ // because prebuilds have to be valid without user input.
74+ //
75+ // This will only validate presets that have prebuilds configured and have no
76+ // existing error diagnostics. This should only be used when doing Template
77+ // Imports as a protection against invalid presets.
78+ //
79+ // A preset doesn't need to specify all required parameters, as users can provide
80+ // the remaining values when creating a workspace. However, since prebuild
81+ // creation has no user input, presets used for prebuilds must provide all
82+ // required parameter values.
83+ func ValidatePrebuilds (ctx context.Context , input Input , preValid []types.Preset , dir fs.FS ) {
84+ for i := range preValid {
85+ pre := & preValid [i ]
86+ if pre .Prebuilds == nil || pre .Prebuilds .Instances <= 0 {
87+ // No prebuilds, so presets do not need to be valid without user input
88+ continue
89+ }
90+
91+ if hcl .Diagnostics (pre .Diagnostics ).HasErrors () {
92+ // Piling on diagnostics is not helpful, if an error exists, leave it at that.
93+ continue
94+ }
95+
96+ // Diagnostics are added to the existing preset.
97+ input .ParameterValues = pre .Parameters
98+ output , diagnostics := Preview (ctx , input , dir )
99+ if diagnostics .HasErrors () {
100+ pre .Diagnostics = append (pre .Diagnostics , diagnostics ... )
101+ // Do not pile on more diagnostics for individual params, it already failed
102+ continue
103+ }
104+
105+ if output == nil {
106+ continue
107+ }
108+
109+ // If any parameter is invalid, then the preset is invalid.
110+ // A value must be specified for this failing parameter.
111+ for _ , param := range output .Parameters {
112+ if hcl .Diagnostics (param .Diagnostics ).HasErrors () {
113+ for _ , paramDiag := range param .Diagnostics {
114+ if paramDiag .Severity != hcl .DiagError {
115+ continue // Only care about errors here
116+ }
117+ orig := paramDiag .Summary
118+ paramDiag .Summary = fmt .Sprintf ("Parameter %s: %s" , param .Name , orig )
119+ pre .Diagnostics = append (pre .Diagnostics , paramDiag )
120+ }
121+ }
122+ }
123+ }
124+ }
125+
71126func Preview (ctx context.Context , input Input , dir fs.FS ) (output * Output , diagnostics hcl.Diagnostics ) {
72127 // The trivy package works with `github.com/zclconf/go-cty`. This package is
73128 // similar to `reflect` in its usage. This package can panic if types are
@@ -180,7 +235,9 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagn
180235
181236 diags := make (hcl.Diagnostics , 0 )
182237 rp , rpDiags := parameters (modules )
183- presets := presets (modules , rp )
238+ // preValidPresets are extracted as written in terraform. Each individual
239+ // param value is checked, however, the preset as a whole might not be valid.
240+ preValidPresets := presets (modules , rp )
184241 tags , tagDiags := workspaceTags (modules , p .Files ())
185242 vars := variables (modules )
186243
@@ -191,7 +248,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagn
191248 ModuleOutput : outputs ,
192249 Parameters : rp ,
193250 WorkspaceTags : tags ,
194- Presets : presets ,
251+ Presets : preValidPresets ,
195252 Files : p .Files (),
196253 Variables : vars ,
197254 }, diags .Extend (rpDiags ).Extend (tagDiags )
0 commit comments