9
9
"time"
10
10
11
11
"github.com/google/uuid"
12
+ "github.com/zclconf/go-cty/cty"
12
13
"golang.org/x/xerrors"
13
14
14
15
"github.com/coder/coder/v2/apiversion"
@@ -41,9 +42,10 @@ type loader struct {
41
42
templateVersionID uuid.UUID
42
43
43
44
// cache of objects
44
- templateVersion * database.TemplateVersion
45
- job * database.ProvisionerJob
46
- terraformValues * database.TemplateVersionTerraformValue
45
+ templateVersion * database.TemplateVersion
46
+ job * database.ProvisionerJob
47
+ terraformValues * database.TemplateVersionTerraformValue
48
+ templateVariableValues * []database.TemplateVersionVariable
47
49
}
48
50
49
51
// Prepare is the entrypoint for this package. It loads the necessary objects &
@@ -61,6 +63,12 @@ func Prepare(ctx context.Context, db database.Store, cache files.FileAcquirer, v
61
63
return l .Renderer (ctx , db , cache )
62
64
}
63
65
66
+ func WithTemplateVariableValues (vals []database.TemplateVersionVariable ) func (r * loader ) {
67
+ return func (r * loader ) {
68
+ r .templateVariableValues = & vals
69
+ }
70
+ }
71
+
64
72
func WithTemplateVersion (tv database.TemplateVersion ) func (r * loader ) {
65
73
return func (r * loader ) {
66
74
if tv .ID == r .templateVersionID {
@@ -127,6 +135,14 @@ func (r *loader) loadData(ctx context.Context, db database.Store) error {
127
135
r .terraformValues = & values
128
136
}
129
137
138
+ if r .templateVariableValues == nil {
139
+ vals , err := db .GetTemplateVersionVariables (ctx , r .templateVersion .ID )
140
+ if err != nil && ! xerrors .Is (err , sql .ErrNoRows ) {
141
+ return xerrors .Errorf ("template version variables: %w" , err )
142
+ }
143
+ r .templateVariableValues = & vals
144
+ }
145
+
130
146
return nil
131
147
}
132
148
@@ -160,13 +176,17 @@ func (r *loader) dynamicRenderer(ctx context.Context, db database.Store, cache *
160
176
}
161
177
}()
162
178
179
+ tfVarValues , err := VariableValues (* r .templateVariableValues )
180
+ if err != nil {
181
+ return nil , xerrors .Errorf ("parse variable values: %w" , err )
182
+ }
183
+
163
184
// If they can read the template version, then they can read the file for
164
185
// parameter loading purposes.
165
186
//nolint:gocritic
166
187
fileCtx := dbauthz .AsFileReader (ctx )
167
188
168
189
var templateFS fs.FS
169
- var err error
170
190
171
191
templateFS , err = cache .Acquire (fileCtx , db , r .job .FileID )
172
192
if err != nil {
@@ -189,6 +209,7 @@ func (r *loader) dynamicRenderer(ctx context.Context, db database.Store, cache *
189
209
db : db ,
190
210
ownerErrors : make (map [uuid.UUID ]error ),
191
211
close : cache .Close ,
212
+ tfvarValues : tfVarValues ,
192
213
}, nil
193
214
}
194
215
@@ -199,6 +220,7 @@ type dynamicRenderer struct {
199
220
200
221
ownerErrors map [uuid.UUID ]error
201
222
currentOwner * previewtypes.WorkspaceOwner
223
+ tfvarValues map [string ]cty.Value
202
224
203
225
once sync.Once
204
226
close func ()
@@ -229,6 +251,7 @@ func (r *dynamicRenderer) Render(ctx context.Context, ownerID uuid.UUID, values
229
251
PlanJSON : r .data .terraformValues .CachedPlan ,
230
252
ParameterValues : values ,
231
253
Owner : * r .currentOwner ,
254
+ TFVars : r .tfvarValues ,
232
255
// Do not emit parser logs to coderd output logs.
233
256
// TODO: Returning this logs in the output would benefit the caller.
234
257
// Unsure how large the logs can be, so for now we just discard them.
0 commit comments