Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ab10cf

Browse files
authored
fix(provisioner/terraform/tfparse): skip evaluation of unrelated parameters (#16023)
* Improves tfparse test coverage to include more parameter types and values * Adds tests with unrelated parameters that should be ignored by tfparse * Modifies tfparse to only attempt evaluation of parameters referenced by coder_workspace_tags
1 parent 4e09639 commit 1ab10cf

File tree

5 files changed

+329
-77
lines changed

5 files changed

+329
-77
lines changed

coderd/templateversions_test.go

+86-27
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
293293
type = string
294294
default = "2"
295295
}
296+
data "coder_parameter" "unrelated" {
297+
name = "unrelated"
298+
type = "list(string)"
299+
default = jsonencode(["a", "b"])
300+
}
296301
resource "null_resource" "test" {}`,
297302
},
298303
wantTags: map[string]string{"owner": "", "scope": "organization"},
@@ -301,18 +306,23 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
301306
name: "main.tf with empty workspace tags",
302307
files: map[string]string{
303308
`main.tf`: `
304-
variable "a" {
305-
type = string
306-
default = "1"
307-
}
308-
data "coder_parameter" "b" {
309-
type = string
310-
default = "2"
311-
}
312-
resource "null_resource" "test" {}
313-
data "coder_workspace_tags" "tags" {
314-
tags = {}
315-
}`,
309+
variable "a" {
310+
type = string
311+
default = "1"
312+
}
313+
data "coder_parameter" "b" {
314+
type = string
315+
default = "2"
316+
}
317+
data "coder_parameter" "unrelated" {
318+
name = "unrelated"
319+
type = "list(string)"
320+
default = jsonencode(["a", "b"])
321+
}
322+
resource "null_resource" "test" {}
323+
data "coder_workspace_tags" "tags" {
324+
tags = {}
325+
}`,
316326
},
317327
wantTags: map[string]string{"owner": "", "scope": "organization"},
318328
},
@@ -328,6 +338,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
328338
type = string
329339
default = "2"
330340
}
341+
data "coder_parameter" "unrelated" {
342+
name = "unrelated"
343+
type = "list(string)"
344+
default = jsonencode(["a", "b"])
345+
}
331346
resource "null_resource" "test" {}
332347
data "coder_workspace_tags" "tags" {
333348
tags = {
@@ -343,22 +358,28 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
343358
name: "main.tf with workspace tags and request tags",
344359
files: map[string]string{
345360
`main.tf`: `
346-
variable "a" {
347-
type = string
348-
default = "1"
349-
}
350-
data "coder_parameter" "b" {
351-
type = string
352-
default = "2"
353-
}
354-
resource "null_resource" "test" {}
355-
data "coder_workspace_tags" "tags" {
356-
tags = {
357-
"foo": "bar",
358-
"a": var.a,
359-
"b": data.coder_parameter.b.value,
361+
// This file is the same as the above, except for this comment.
362+
variable "a" {
363+
type = string
364+
default = "1"
365+
}
366+
data "coder_parameter" "b" {
367+
type = string
368+
default = "2"
360369
}
361-
}`,
370+
data "coder_parameter" "unrelated" {
371+
name = "unrelated"
372+
type = "list(string)"
373+
default = jsonencode(["a", "b"])
374+
}
375+
resource "null_resource" "test" {}
376+
data "coder_workspace_tags" "tags" {
377+
tags = {
378+
"foo": "bar",
379+
"a": var.a,
380+
"b": data.coder_parameter.b.value,
381+
}
382+
}`,
362383
},
363384
reqTags: map[string]string{"baz": "zap", "foo": "noclobber"},
364385
wantTags: map[string]string{"owner": "", "scope": "organization", "foo": "bar", "baz": "zap", "a": "1", "b": "2"},
@@ -375,6 +396,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
375396
type = string
376397
default = "2"
377398
}
399+
data "coder_parameter" "unrelated" {
400+
name = "unrelated"
401+
type = "list(string)"
402+
default = jsonencode(["a", "b"])
403+
}
378404
resource "null_resource" "test" {
379405
name = "foo"
380406
}
@@ -401,6 +427,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
401427
type = string
402428
default = "2"
403429
}
430+
data "coder_parameter" "unrelated" {
431+
name = "unrelated"
432+
type = "list(string)"
433+
default = jsonencode(["a", "b"])
434+
}
404435
resource "null_resource" "test" {
405436
name = "foo"
406437
}
@@ -423,6 +454,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
423454
name: "main.tf with workspace tags that attempts to set user scope",
424455
files: map[string]string{
425456
`main.tf`: `
457+
data "coder_parameter" "unrelated" {
458+
name = "unrelated"
459+
type = "list(string)"
460+
default = jsonencode(["a", "b"])
461+
}
426462
resource "null_resource" "test" {}
427463
data "coder_workspace_tags" "tags" {
428464
tags = {
@@ -437,6 +473,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
437473
name: "main.tf with workspace tags that attempt to clobber org ID",
438474
files: map[string]string{
439475
`main.tf`: `
476+
data "coder_parameter" "unrelated" {
477+
name = "unrelated"
478+
type = "list(string)"
479+
default = jsonencode(["a", "b"])
480+
}
440481
resource "null_resource" "test" {}
441482
data "coder_workspace_tags" "tags" {
442483
tags = {
@@ -451,6 +492,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
451492
name: "main.tf with workspace tags that set scope=user",
452493
files: map[string]string{
453494
`main.tf`: `
495+
data "coder_parameter" "unrelated" {
496+
name = "unrelated"
497+
type = "list(string)"
498+
default = jsonencode(["a", "b"])
499+
}
454500
resource "null_resource" "test" {}
455501
data "coder_workspace_tags" "tags" {
456502
tags = {
@@ -460,6 +506,19 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
460506
},
461507
wantTags: map[string]string{"owner": templateAdminUser.ID.String(), "scope": "user"},
462508
},
509+
// Ref: https://github.com/coder/coder/issues/16021
510+
{
511+
name: "main.tf with no workspace_tags and a function call in a parameter default",
512+
files: map[string]string{
513+
`main.tf`: `
514+
data "coder_parameter" "unrelated" {
515+
name = "unrelated"
516+
type = "list(string)"
517+
default = jsonencode(["a", "b"])
518+
}`,
519+
},
520+
wantTags: map[string]string{"owner": "", "scope": "organization"},
521+
},
463522
} {
464523
tt := tt
465524
t.Run(tt.name, func(t *testing.T) {

enterprise/coderd/workspaces_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,11 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
14121412
provider "coder" {}
14131413
data "coder_workspace" "me" {}
14141414
data "coder_workspace_owner" "me" {}
1415+
data "coder_parameter" "unrelated" {
1416+
name = "unrelated"
1417+
type = "list(string)"
1418+
default = jsonencode(["a", "b"])
1419+
}
14151420
%s
14161421
`
14171422

provisioner/terraform/parse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (s *server) Parse(sess *provisionersdk.Session, _ *proto.ParseRequest, _ <-
2626
return provisionersdk.ParseErrorf("load module: %s", formatDiagnostics(sess.WorkDirectory, diags))
2727
}
2828

29-
workspaceTags, err := parser.WorkspaceTags(ctx)
29+
workspaceTags, _, err := parser.WorkspaceTags(ctx)
3030
if err != nil {
3131
return provisionersdk.ParseErrorf("can't load workspace tags: %v", err)
3232
}

0 commit comments

Comments
 (0)