Skip to content

Commit 41bfccc

Browse files
authored
chore: cherry-pick coder#16183 and coder#16303 for 2.18 (coder#16313)
1 parent 8fffdce commit 41bfccc

File tree

11 files changed

+327
-47
lines changed

11 files changed

+327
-47
lines changed

coderd/templateversions_test.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,8 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
429429
}
430430
}`,
431431
},
432-
reqTags: map[string]string{"a": "b"},
433-
// wantTags: map[string]string{"owner": "", "scope": "organization", "a": "b"},
434-
expectError: `provisioner tag "a" evaluated to an empty value`,
432+
reqTags: map[string]string{"a": "b"},
433+
wantTags: map[string]string{"owner": "", "scope": "organization", "a": "b"},
435434
},
436435
{
437436
name: "main.tf with disallowed workspace tag value",
@@ -489,11 +488,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
489488
"foo": "bar",
490489
"a": var.a,
491490
"b": data.coder_parameter.b.value,
492-
"test": try(null_resource.test.name, "whatever"),
491+
"test": pathexpand("~/file.txt"),
493492
}
494493
}`,
495494
},
496-
expectError: `Function calls not allowed; Functions may not be called here.`,
495+
expectError: `function "pathexpand" may not be used here`,
497496
},
498497
// We will allow coder_workspace_tags to set the scope on a template version import job
499498
// BUT the user ID will be ultimately determined by the API key in the scope.
@@ -568,6 +567,42 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
568567
},
569568
wantTags: map[string]string{"owner": "", "scope": "organization"},
570569
},
570+
{
571+
name: "main.tf with tags from parameter with default value from variable no default",
572+
files: map[string]string{
573+
`main.tf`: `
574+
variable "provisioner" {
575+
type = string
576+
}
577+
variable "default_provisioner" {
578+
type = string
579+
default = "" # intentionally blank, set on template creation
580+
}
581+
data "coder_parameter" "provisioner" {
582+
name = "provisioner"
583+
mutable = false
584+
default = var.default_provisioner
585+
dynamic "option" {
586+
for_each = toset(split(",", var.provisioner))
587+
content {
588+
name = option.value
589+
value = option.value
590+
}
591+
}
592+
}
593+
data "coder_workspace_tags" "tags" {
594+
tags = {
595+
"provisioner" : data.coder_parameter.provisioner.value
596+
}
597+
}`,
598+
},
599+
reqTags: map[string]string{
600+
"provisioner": "alpha",
601+
},
602+
wantTags: map[string]string{
603+
"provisioner": "alpha", "owner": "", "scope": "organization",
604+
},
605+
},
571606
} {
572607
tt := tt
573608
t.Run(tt.name, func(t *testing.T) {

docs/admin/templates/extending-templates/workspace-tags.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ variables and parameters. This is illustrated in the table below:
6262

6363
## Constraints
6464

65-
### Default Values
66-
67-
All template variables and `coder_parameter` data sources **must** provide a
68-
default value. Failure to do so will result in an error.
69-
7065
### Tagged provisioners
7166

7267
It is possible to choose tag combinations that no provisioner can handle. This
@@ -127,6 +122,6 @@ variables, and references to other resources.
127122

128123
**Not supported**
129124

130-
- Function calls: `try(var.foo, "default")`
125+
- Function calls that reference files on disk: `abspath`, `file*`, `pathexpand`
131126
- Resources: `compute_instance.dev.name`
132127
- Data sources other than `coder_parameter`: `data.local_file.hostname.content`

enterprise/coderd/coderdenttest/coderdenttest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func newExternalProvisionerDaemon(t testing.TB, client *codersdk.Client, org uui
389389
daemon := provisionerd.New(func(ctx context.Context) (provisionerdproto.DRPCProvisionerDaemonClient, error) {
390390
return client.ServeProvisionerDaemon(ctx, codersdk.ServeProvisionerDaemonRequest{
391391
ID: uuid.New(),
392-
Name: t.Name(),
392+
Name: testutil.GetRandomName(t),
393393
Organization: org,
394394
Provisioners: []codersdk.ProvisionerType{provisionerType},
395395
Tags: tags,

enterprise/coderd/provisionerdaemons_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func TestProvisionerDaemonServe(t *testing.T) {
285285
daemons, err := client.ProvisionerDaemons(context.Background())
286286
assert.NoError(t, err, "failed to get provisioner daemons")
287287
return len(daemons) > 0 &&
288-
assert.Equal(t, t.Name(), daemons[0].Name) &&
288+
assert.NotEmpty(t, daemons[0].Name) &&
289289
assert.Equal(t, provisionersdk.ScopeUser, daemons[0].Tags[provisionersdk.TagScope]) &&
290290
assert.Equal(t, user.UserID.String(), daemons[0].Tags[provisionersdk.TagOwner])
291291
}, testutil.WaitShort, testutil.IntervalMedium)

enterprise/coderd/workspaces_test.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,11 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
12171217
createTemplateVersionRequestTags map[string]string
12181218
// the coder_workspace_tags bit of main.tf.
12191219
// you can add more stuff here if you need
1220-
tfWorkspaceTags string
1221-
skipCreateWorkspace bool
1220+
tfWorkspaceTags string
1221+
templateImportUserVariableValues []codersdk.VariableValue
1222+
// if we need to set parameters on workspace build
1223+
workspaceBuildParameters []codersdk.WorkspaceBuildParameter
1224+
skipCreateWorkspace bool
12221225
}{
12231226
{
12241227
name: "no tags",
@@ -1318,6 +1321,38 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
13181321
// matching tag foo=bar.
13191322
skipCreateWorkspace: true,
13201323
},
1324+
{
1325+
name: "overrides with dynamic option from var",
1326+
provisionerTags: map[string]string{"foo": "bar"},
1327+
createTemplateVersionRequestTags: map[string]string{"foo": "bar"},
1328+
templateImportUserVariableValues: []codersdk.VariableValue{{Name: "default_foo", Value: "baz"}, {Name: "foo", Value: "bar,baz"}},
1329+
workspaceBuildParameters: []codersdk.WorkspaceBuildParameter{{Name: "foo", Value: "bar"}},
1330+
tfWorkspaceTags: `
1331+
variable "default_foo" {
1332+
type = string
1333+
}
1334+
variable "foo" {
1335+
type = string
1336+
}
1337+
data "coder_parameter" "foo" {
1338+
name = "foo"
1339+
type = "string"
1340+
default = var.default_foo
1341+
mutable = false
1342+
dynamic "option" {
1343+
for_each = toset(split(",", var.foo))
1344+
content {
1345+
name = option.value
1346+
value = option.value
1347+
}
1348+
}
1349+
}
1350+
data "coder_workspace_tags" "tags" {
1351+
tags = {
1352+
"foo" = data.coder_parameter.foo.value
1353+
}
1354+
}`,
1355+
},
13211356
} {
13221357
tc := tc
13231358
t.Run(tc.name, func(t *testing.T) {
@@ -1346,11 +1381,12 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
13461381
fi, err := templateAdmin.Upload(ctx, "application/x-tar", bytes.NewReader(tarBytes))
13471382
require.NoError(t, err, "failed to upload file")
13481383
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{
1349-
Name: testutil.GetRandomName(t),
1350-
FileID: fi.ID,
1351-
StorageMethod: codersdk.ProvisionerStorageMethodFile,
1352-
Provisioner: codersdk.ProvisionerTypeTerraform,
1353-
ProvisionerTags: tc.createTemplateVersionRequestTags,
1384+
Name: testutil.GetRandomName(t),
1385+
FileID: fi.ID,
1386+
StorageMethod: codersdk.ProvisionerStorageMethodFile,
1387+
Provisioner: codersdk.ProvisionerTypeTerraform,
1388+
ProvisionerTags: tc.createTemplateVersionRequestTags,
1389+
UserVariableValues: tc.templateImportUserVariableValues,
13541390
})
13551391
require.NoError(t, err, "failed to create template version")
13561392
coderdtest.AwaitTemplateVersionJobCompleted(t, templateAdmin, tv.ID)
@@ -1359,8 +1395,9 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
13591395
if !tc.skipCreateWorkspace {
13601396
// Creating a workspace as a non-privileged user must succeed
13611397
ws, err := member.CreateUserWorkspace(ctx, memberUser.Username, codersdk.CreateWorkspaceRequest{
1362-
TemplateID: tpl.ID,
1363-
Name: coderdtest.RandomUsername(t),
1398+
TemplateID: tpl.ID,
1399+
Name: coderdtest.RandomUsername(t),
1400+
RichParameterValues: tc.workspaceBuildParameters,
13641401
})
13651402
require.NoError(t, err, "failed to create workspace")
13661403
coderdtest.AwaitWorkspaceBuildJobCompleted(t, member, ws.LatestBuild.ID)

examples/workspace-tags/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ icon: /icon/docker.png
77

88
# Overview
99

10-
This Coder template presents use of [Workspace Tags](https://coder.com/docs/templates/workspace-tags) [Coder Parameters](https://coder.com/docs/templates/parameters).
10+
This Coder template presents use of [Workspace Tags](https://coder.com/docs/admin/templates/extending-templates/workspace-tags) and [Coder Parameters](https://coder.com/docs/templates/parameters).
1111

1212
# Use case
1313

@@ -18,10 +18,7 @@ By using `coder_workspace_tags` and `coder_parameter`s, template administrators
1818
# Notes
1919

2020
- You will need to have an [external provisioner](https://coder.com/docs/admin/provisioners#external-provisioners) with the correct tagset running in order to import this template.
21-
- When specifying values for the `coder_workspace_tags` data source, you are restricted to using a subset of Terraform's capabilities.
22-
- You must specify default values for all data sources and variables referenced by the `coder_workspace_tags` data source.
23-
24-
See [Workspace Tags](https://coder.com/docs/templates/workspace-tags) for more information.
21+
- When specifying values for the `coder_workspace_tags` data source, you are restricted to using a subset of Terraform's capabilities. See [here](https://coder.com/docs/admin/templates/extending-templates/workspace-tags) for more details.
2522

2623
## Development
2724

go.mod

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ require (
256256
filippo.io/edwards25519 v1.1.0 // indirect
257257
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
258258
github.com/DataDog/appsec-internal-go v1.8.0 // indirect
259-
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 // indirect
260-
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.57.0 // indirect
261-
github.com/DataDog/datadog-go/v5 v5.3.0 // indirect
259+
github.com/DataDog/datadog-agent/pkg/obfuscate v0.58.0 // indirect
260+
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.58.0 // indirect
261+
github.com/DataDog/datadog-go/v5 v5.5.0 // indirect
262262
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
263263
github.com/DataDog/gostackparse v0.7.0 // indirect
264264
github.com/DataDog/sketches-go v1.4.5 // indirect
@@ -444,3 +444,14 @@ require (
444444
inet.af/peercred v0.0.0-20210906144145-0893ea02156a // indirect
445445
sigs.k8s.io/yaml v1.4.0 // indirect
446446
)
447+
448+
require (
449+
github.com/aquasecurity/trivy-iac v0.8.0
450+
github.com/zclconf/go-cty-yaml v1.0.3
451+
)
452+
453+
require (
454+
github.com/DataDog/go-sqllexer v0.0.14 // indirect
455+
github.com/apparentlymart/go-cidr v1.1.0 // indirect
456+
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
457+
)

go.sum

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
2626
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
2727
github.com/DataDog/appsec-internal-go v1.8.0 h1:1Tfn3LEogntRqZtf88twSApOCAAO3V+NILYhuQIo4J4=
2828
github.com/DataDog/appsec-internal-go v1.8.0/go.mod h1:wW0cRfWBo4C044jHGwYiyh5moQV2x0AhnwqMuiX7O/g=
29-
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 h1:bUMSNsw1iofWiju9yc1f+kBd33E3hMJtq9GuU602Iy8=
30-
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0/go.mod h1:HzySONXnAgSmIQfL6gOv9hWprKJkx8CicuXuUbmgWfo=
31-
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.57.0 h1:LplNAmMgZvGU7kKA0+4c1xWOjz828xweW5TCi8Mw9Q0=
32-
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.57.0/go.mod h1:4Vo3SJ24uzfKHUHLoFa8t8o+LH+7TCQ7sPcZDtOpSP4=
33-
github.com/DataDog/datadog-go/v5 v5.3.0 h1:2q2qjFOb3RwAZNU+ez27ZVDwErJv5/VpbBPprz7Z+s8=
34-
github.com/DataDog/datadog-go/v5 v5.3.0/go.mod h1:XRDJk1pTc00gm+ZDiBKsjh7oOOtJfYfglVCmFb8C2+Q=
29+
github.com/DataDog/datadog-agent/pkg/obfuscate v0.58.0 h1:nOrRNCHyriM/EjptMrttFOQhRSmvfagESdpyknb5VPg=
30+
github.com/DataDog/datadog-agent/pkg/obfuscate v0.58.0/go.mod h1:MfDvphBMmEMwE3a30h27AtPO7OzmvdoVTiGY1alEmo4=
31+
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.58.0 h1:5hGO0Z8ih0bRojuq+1ZwLFtdgsfO3TqIjbwJAH12sOQ=
32+
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.58.0/go.mod h1:jN5BsZI+VilHJV1Wac/efGxS4TPtXa1Lh9SiUyv93F4=
33+
github.com/DataDog/datadog-go/v5 v5.5.0 h1:G5KHeB8pWBNXT4Jtw0zAkhdxEAWSpWH00geHI6LDrKU=
34+
github.com/DataDog/datadog-go/v5 v5.5.0/go.mod h1:K9kcYBlxkcPP8tvvjZZKs/m1edNAUFzBbdpTUKfCsuw=
3535
github.com/DataDog/go-libddwaf/v3 v3.4.0 h1:NJ2W2vhYaOm1OWr1LJCbdgp7ezG/XLJcQKBmjFwhSuM=
3636
github.com/DataDog/go-libddwaf/v3 v3.4.0/go.mod h1:n98d9nZ1gzenRSk53wz8l6d34ikxS+hs62A31Fqmyi4=
37+
github.com/DataDog/go-sqllexer v0.0.14 h1:xUQh2tLr/95LGxDzLmttLgTo/1gzFeOyuwrQa/Iig4Q=
38+
github.com/DataDog/go-sqllexer v0.0.14/go.mod h1:KwkYhpFEVIq+BfobkTC1vfqm4gTi65skV/DpDBXtexc=
3739
github.com/DataDog/go-tuf v1.1.0-0.5.2 h1:4CagiIekonLSfL8GMHRHcHudo1fQnxELS9g4tiAupQ4=
3840
github.com/DataDog/go-tuf v1.1.0-0.5.2/go.mod h1:zBcq6f654iVqmkk8n2Cx81E1JnNTMOAx1UEO/wZR+P0=
3941
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
@@ -76,11 +78,15 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
7678
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
7779
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
7880
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
81+
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
82+
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
7983
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
8084
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
8185
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
8286
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
8387
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
88+
github.com/aquasecurity/trivy-iac v0.8.0 h1:NKFhk/BTwQ0jIh4t74V8+6UIGUvPlaxO9HPlSMQi3fo=
89+
github.com/aquasecurity/trivy-iac v0.8.0/go.mod h1:ARiMeNqcaVWOXJmp8hmtMnNm/Jd836IOmDBUW5r4KEk=
8490
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
8591
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
8692
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 h1:7Ip0wMmLHLRJdrloDxZfhMm0xrLXZS8+COSu2bXmEQs=
@@ -157,6 +163,8 @@ github.com/bep/tmc v0.5.1/go.mod h1:tGYHN8fS85aJPhDLgXETVKp+PR382OvFi2+q2GkGsq0=
157163
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
158164
github.com/bgentry/speakeasy v0.2.0 h1:tgObeVOf8WAvtuAX6DhJ4xks4CFNwPDZiqzGqIHE51E=
159165
github.com/bgentry/speakeasy v0.2.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
166+
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
167+
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
160168
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
161169
github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs=
162170
github.com/bramvdbogaerde/go-scp v1.5.0 h1:a9BinAjTfQh273eh7vd3qUgmBC+bx+3TRDtkZWmIpzM=
@@ -953,6 +961,8 @@ github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ
953961
github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
954962
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
955963
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
964+
github.com/zclconf/go-cty-yaml v1.0.3 h1:og/eOQ7lvA/WWhHGFETVWNduJM7Rjsv2RRpx1sdFMLc=
965+
github.com/zclconf/go-cty-yaml v1.0.3/go.mod h1:9YLUH4g7lOhVWqUbctnVlZ5KLpg7JAprQNgxSZ1Gyxs=
956966
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
957967
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
958968
github.com/zeebo/errs v1.3.0 h1:hmiaKqgYZzcVgRL1Vkc1Mn2914BbzB0IBxs+ebeutGs=

0 commit comments

Comments
 (0)