Skip to content

Commit e48b106

Browse files
committed
fix(provisioner/terraform/tfparse): skip evaluating variables/parameters not referenced by workspace_tags
1 parent e00c3f5 commit e48b106

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

coderd/templateversions_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
358358
name: "main.tf with workspace tags and request tags",
359359
files: map[string]string{
360360
`main.tf`: `
361+
// This file is the same as the above, except for this comment.
361362
variable "a" {
362363
type = string
363364
default = "1"

provisioner/terraform/tfparse/tfparse.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ func (p *Parser) WorkspaceTagDefaults(ctx context.Context) (map[string]string, e
172172

173173
// To evaluate the expressions, we need to load the default values for
174174
// variables and parameters.
175-
varsDefaults, err := p.VariableDefaults(ctx)
175+
varsDefaults, err := p.VariableDefaults(ctx, tags)
176176
if err != nil {
177177
return nil, xerrors.Errorf("load variable defaults: %w", err)
178178
}
179-
paramsDefaults, err := p.CoderParameterDefaults(ctx, varsDefaults)
179+
paramsDefaults, err := p.CoderParameterDefaults(ctx, varsDefaults, tags)
180180
if err != nil {
181181
return nil, xerrors.Errorf("load parameter defaults: %w", err)
182182
}
@@ -251,28 +251,39 @@ func WriteArchive(bs []byte, mimetype string, path string) error {
251251
return nil
252252
}
253253

254-
// VariableDefaults returns the default values for all variables passed to it.
255-
func (p *Parser) VariableDefaults(ctx context.Context) (map[string]string, error) {
254+
// VariableDefaults returns the default values for all variables referenced in the values of tags.
255+
func (p *Parser) VariableDefaults(ctx context.Context, tags map[string]string) (map[string]string, error) {
256+
var skipped []string
256257
// iterate through vars to get the default values for all
257-
// variables.
258+
// required variables.
258259
m := make(map[string]string)
259260
for _, v := range p.module.Variables {
260261
if v == nil {
261262
continue
262263
}
264+
var found bool
265+
for _, tv := range tags {
266+
if strings.Contains(tv, v.Name) {
267+
found = true
268+
}
269+
}
270+
if !found {
271+
skipped = append(skipped, v.Name)
272+
continue
273+
}
263274
sv, err := interfaceToString(v.Default)
264275
if err != nil {
265276
return nil, xerrors.Errorf("can't convert variable default value to string: %v", err)
266277
}
267278
m[v.Name] = strings.Trim(sv, `"`)
268279
}
269-
p.logger.Debug(ctx, "found default values for variables", slog.F("defaults", m))
280+
p.logger.Debug(ctx, "found default values for variables", slog.F("defaults", m), slog.F("skipped", skipped))
270281
return m, nil
271282
}
272283

273284
// CoderParameterDefaults returns the default values of all coder_parameter data sources
274285
// in the parsed module.
275-
func (p *Parser) CoderParameterDefaults(ctx context.Context, varsDefaults map[string]string) (map[string]string, error) {
286+
func (p *Parser) CoderParameterDefaults(ctx context.Context, varsDefaults map[string]string, tags map[string]string) (map[string]string, error) {
276287
defaultsM := make(map[string]string)
277288
var (
278289
skipped []string
@@ -294,6 +305,17 @@ func (p *Parser) CoderParameterDefaults(ctx context.Context, varsDefaults map[st
294305
continue
295306
}
296307

308+
var found bool
309+
for _, tv := range tags {
310+
if strings.Contains(tv, dataResource.Name) {
311+
found = true
312+
}
313+
}
314+
if !found {
315+
skipped = append(skipped, dataResource.Name)
316+
continue
317+
}
318+
297319
// We know in which HCL file is the data resource defined.
298320
// NOTE: hclparse.Parser will cache multiple successive calls to parse the same file.
299321
file, diags = p.underlying.ParseHCLFile(dataResource.Pos.Filename)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy