Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8396c67

Browse files
committed
feat: include template variables in dynamic parameter rendering
1 parent00ba027 commit8396c67

File tree

5 files changed

+94
-7
lines changed

5 files changed

+94
-7
lines changed

‎coderd/dynamicparameters/render.go‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/google/uuid"
12+
"github.com/zclconf/go-cty/cty"
1213
"golang.org/x/xerrors"
1314

1415
"github.com/coder/coder/v2/apiversion"
@@ -41,9 +42,10 @@ type loader struct {
4142
templateVersionID uuid.UUID
4243

4344
// 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
4749
}
4850

4951
// 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
6163
returnl.Renderer(ctx,db,cache)
6264
}
6365

66+
funcWithTemplateVariableValues(vals []database.TemplateVersionVariable)func(r*loader) {
67+
returnfunc(r*loader) {
68+
r.templateVariableValues=&vals
69+
}
70+
}
71+
6472
funcWithTemplateVersion(tv database.TemplateVersion)func(r*loader) {
6573
returnfunc(r*loader) {
6674
iftv.ID==r.templateVersionID {
@@ -127,6 +135,14 @@ func (r *loader) loadData(ctx context.Context, db database.Store) error {
127135
r.terraformValues=&values
128136
}
129137

138+
ifr.templateVariableValues==nil {
139+
vals,err:=db.GetTemplateVersionVariables(ctx,r.templateVersion.ID)
140+
iferr!=nil&&!xerrors.Is(err,sql.ErrNoRows) {
141+
returnxerrors.Errorf("template version variables: %w",err)
142+
}
143+
r.templateVariableValues=&vals
144+
}
145+
130146
returnnil
131147
}
132148

@@ -160,13 +176,17 @@ func (r *loader) dynamicRenderer(ctx context.Context, db database.Store, cache *
160176
}
161177
}()
162178

179+
tfVarValues,err:=VariableValues(*r.templateVariableValues)
180+
iferr!=nil {
181+
returnnil,xerrors.Errorf("parse variable values: %w",err)
182+
}
183+
163184
// If they can read the template version, then they can read the file for
164185
// parameter loading purposes.
165186
//nolint:gocritic
166187
fileCtx:=dbauthz.AsFileReader(ctx)
167188

168189
vartemplateFS fs.FS
169-
varerrerror
170190

171191
templateFS,err=cache.Acquire(fileCtx,db,r.job.FileID)
172192
iferr!=nil {
@@ -189,6 +209,7 @@ func (r *loader) dynamicRenderer(ctx context.Context, db database.Store, cache *
189209
db:db,
190210
ownerErrors:make(map[uuid.UUID]error),
191211
close:cache.Close,
212+
tfvarValues:tfVarValues,
192213
},nil
193214
}
194215

@@ -199,6 +220,7 @@ type dynamicRenderer struct {
199220

200221
ownerErrorsmap[uuid.UUID]error
201222
currentOwner*previewtypes.WorkspaceOwner
223+
tfvarValuesmap[string]cty.Value
202224

203225
once sync.Once
204226
closefunc()
@@ -229,6 +251,7 @@ func (r *dynamicRenderer) Render(ctx context.Context, ownerID uuid.UUID, values
229251
PlanJSON:r.data.terraformValues.CachedPlan,
230252
ParameterValues:values,
231253
Owner:*r.currentOwner,
254+
TFVars:r.tfvarValues,
232255
// Do not emit parser logs to coderd output logs.
233256
// TODO: Returning this logs in the output would benefit the caller.
234257
// Unsure how large the logs can be, so for now we just discard them.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dynamicparameters
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/zclconf/go-cty/cty"
7+
"github.com/zclconf/go-cty/cty/json"
8+
"golang.org/x/xerrors"
9+
10+
"github.com/coder/coder/v2/coderd/database"
11+
)
12+
13+
funcVariableValues(vals []database.TemplateVersionVariable) (map[string]cty.Value,error) {
14+
ctyVals:=make(map[string]cty.Value,len(vals))
15+
for_,v:=rangevals {
16+
value:=v.Value
17+
ifvalue==""&&v.DefaultValue!="" {
18+
value=v.DefaultValue
19+
}
20+
21+
ifvalue=="" {
22+
// Empty strings are unsupported I guess?
23+
continue// omit non-set vals
24+
}
25+
26+
varerrerror
27+
switchv.Type {
28+
case"string":
29+
ctyVals[v.Name]=cty.StringVal(value)
30+
case"number":
31+
ctyVals[v.Name],err=cty.ParseNumberVal(value)
32+
iferr!=nil {
33+
returnnil,xerrors.Errorf("parse variable %q: %w",v.Name,err)
34+
}
35+
case"bool":
36+
parsed,err:=strconv.ParseBool(value)
37+
iferr!=nil {
38+
returnnil,xerrors.Errorf("parse variable %q: %w",v.Name,err)
39+
}
40+
ctyVals[v.Name]=cty.BoolVal(parsed)
41+
default:
42+
// If it is a complex type, let the cty json code give it a try.
43+
// TODO: Ideally we parse `list` & `map` and build the type ourselves.
44+
ty,err:=json.ImpliedType([]byte(value))
45+
iferr!=nil {
46+
returnnil,xerrors.Errorf("implied type for variable %q: %w",v.Name,err)
47+
}
48+
49+
jv,err:=json.Unmarshal([]byte(value),ty)
50+
iferr!=nil {
51+
returnnil,xerrors.Errorf("unmarshal variable %q: %w",v.Name,err)
52+
}
53+
ctyVals[v.Name]=jv
54+
}
55+
}
56+
57+
returnctyVals,nil
58+
}

‎coderd/wsbuilder/wsbuilder.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,16 @@ func (b *Builder) getDynamicParameterRenderer() (dynamicparameters.Renderer, err
633633
returnnil,xerrors.Errorf("get template version terraform values: %w",err)
634634
}
635635

636+
variableValues,err:=b.getTemplateVersionVariables()
637+
iferr!=nil {
638+
returnnil,xerrors.Errorf("get template version variables: %w",err)
639+
}
640+
636641
renderer,err:=dynamicparameters.Prepare(b.ctx,b.store,b.fileCache,tv.ID,
637642
dynamicparameters.WithTemplateVersion(*tv),
638643
dynamicparameters.WithProvisionerJob(*job),
639644
dynamicparameters.WithTerraformValues(*tfVals),
645+
dynamicparameters.WithTemplateVariableValues(variableValues),
640646
)
641647
iferr!=nil {
642648
returnnil,xerrors.Errorf("get template version renderer: %w",err)

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ require (
483483
require (
484484
github.com/coder/agentapi-sdk-gov0.0.0-20250505131810-560d1d88d225
485485
github.com/coder/aisdk-gov0.0.9
486-
github.com/coder/previewv1.0.3-0.20250701142654-c3d6e86b9393
486+
github.com/coder/previewv1.0.3-0.20250709160236-8ddde200cd69
487487
github.com/fsnotify/fsnotifyv1.9.0
488488
github.com/mark3labs/mcp-gov0.32.0
489489
)

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ github.com/coder/pq v1.10.5-0.20250630052411-a259f96b6102 h1:ahTJlTRmTogsubgRVGO
916916
github.com/coder/pqv1.10.5-0.20250630052411-a259f96b6102/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
917917
github.com/coder/prettyv0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
918918
github.com/coder/prettyv0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
919-
github.com/coder/previewv1.0.3-0.20250701142654-c3d6e86b9393 h1:l+m2liikn8JoEv6C22QIV4qseolUfvNsyUNA6JJsD6Y=
920-
github.com/coder/previewv1.0.3-0.20250701142654-c3d6e86b9393/go.mod h1:efDWGlO/PZPrvdt5QiDhMtTUTkPxejXo9c0wmYYLLjM=
919+
github.com/coder/previewv1.0.3-0.20250709160236-8ddde200cd69 h1:bQ3r5Y22V1heD6Ah4kN/wMJ8gflyGPhzNtiFefytBVs=
920+
github.com/coder/previewv1.0.3-0.20250709160236-8ddde200cd69/go.mod h1:efDWGlO/PZPrvdt5QiDhMtTUTkPxejXo9c0wmYYLLjM=
921921
github.com/coder/quartzv0.2.1 h1:QgQ2Vc1+mvzewg2uD/nj8MJ9p9gE+QhGJm+Z+NGnrSE=
922922
github.com/coder/quartzv0.2.1/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
923923
github.com/coder/retryv1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp