Skip to content

Commit 652b242

Browse files
committed
handle unknown values in dockerdata
1 parent 0175889 commit 652b242

File tree

5 files changed

+63
-25
lines changed

5 files changed

+63
-25
lines changed

preview_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,35 +130,34 @@ func Test_Extract(t *testing.T) {
130130
},
131131
},
132132
{
133-
name: "external docker resource",
134-
dir: "dockerdata",
135-
expTags: map[string]string{"qux": "quux"},
136-
unknownTags: []string{
137-
"foo", "bar",
133+
name: "external docker resource without plan data",
134+
dir: "dockerdata",
135+
expTags: map[string]string{
136+
"qux": "quux",
137+
"ubuntu": "0000000000000000000000000000000000000000000000000000000000000000",
138+
"centos": "0000000000000000000000000000000000000000000000000000000000000000",
138139
},
139-
140-
input: preview.Input{},
140+
unknownTags: []string{},
141+
input: preview.Input{},
141142
params: map[string]assertParam{
142-
"Example": ap().
143-
unknown().
144-
// Value is unknown, but this is the safe string
145-
value("data.coder_parameter.example.value"),
143+
"os": ap().
144+
value("0000000000000000000000000000000000000000000000000000000000000000"),
146145
},
147146
},
148147
{
149148
name: "external docker resource with plan data",
150149
dir: "dockerdata",
151150
expTags: map[string]string{
152-
"qux": "quux",
153-
"foo": "sha256:18305429afa14ea462f810146ba44d4363ae76e4c8dfc38288cf73aa07485005",
154-
"bar": "sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177",
151+
"qux": "quux",
152+
"ubuntu": "18305429afa14ea462f810146ba44d4363ae76e4c8dfc38288cf73aa07485005",
153+
"centos": "a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177",
155154
},
156155
unknownTags: []string{},
157156
input: preview.Input{
158157
PlanJSONPath: "plan.json",
159158
},
160159
params: map[string]assertParam{
161-
"Example": ap().
160+
"os": ap().
162161
value("18305429afa14ea462f810146ba44d4363ae76e4c8dfc38288cf73aa07485005"),
163162
},
164163
},

testdata/conditional/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,23 @@ data "coder_parameter" "compute" {
7171
}
7272
}
7373
}
74+
75+
data coder_workspace_owner "me" {}
76+
locals {
77+
isAdmin = contains(data.coder_workspace_owner.me.groups, "admin")
78+
}
79+
80+
data "coder_parameter" "image_hash" {
81+
count = local.isAdmin ? 1 : 0
82+
name = "hash"
83+
display_name = "Image Hash"
84+
description = "Override the hash of the image to use. Only available to admins."
85+
// Value can get stale
86+
default = "e64c69d84d5f910b5cd4fc7bc01a67a6436865787b429e7e60ebaeb4e7dd1b44"
87+
order = 3
88+
89+
validation {
90+
regex = "^[a-f0-9A-F]{64}$"
91+
error = "The image hash must be a 64-character hexadecimal string."
92+
}
93+
}

testdata/conditional/users.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"developer": {
3+
"groups": ["developer"]
4+
},
5+
"administrator": {
6+
"groups": ["admin"]
7+
}
8+
}

testdata/dockerdata/main.tf

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,36 @@ terraform {
1212
}
1313
}
1414

15-
data "coder_parameter" "example" {
16-
name = "Example"
15+
locals {
16+
empty_hash = "0000000000000000000000000000000000000000000000000000000000000000"
17+
ubuntu_hash = try(trimprefix(data.docker_registry_image.ubuntu.sha256_digest, "sha256:"), local.empty_hash)
18+
centos_hash = try(trimprefix(data.docker_registry_image.centos.sha256_digest, "sha256:"), local.empty_hash)
19+
}
20+
21+
data "coder_parameter" "os" {
22+
name = "os"
23+
display_name = "Choose your operating system"
1724
description = "An example parameter that has no purpose."
1825
type = "string"
19-
default = trimprefix(data.docker_registry_image.ubuntu.sha256_digest, "sha256:")
26+
default = local.ubuntu_hash
2027

2128
option {
22-
name = "Ubuntu"
29+
name = "Ubuntu (${substr(local.ubuntu_hash, 0, 6)}...${substr(local.ubuntu_hash, 58, 64)})"
2330
description = data.docker_registry_image.ubuntu.name
24-
value = trimprefix(data.docker_registry_image.ubuntu.sha256_digest, "sha256:")
31+
value = local.ubuntu_hash
2532
}
2633

2734
option {
28-
name = "Centos"
35+
name = "Centos (${substr(local.centos_hash, 0, 6)}...${substr(local.centos_hash, 58, 64)})"
2936
description = data.docker_registry_image.centos.name
30-
value = trimprefix(data.docker_registry_image.centos.sha256_digest, "sha256:")
37+
value = local.centos_hash
3138
}
3239
}
3340

3441
data "coder_workspace_tags" "custom_workspace_tags" {
3542
tags = {
36-
"foo" = data.docker_registry_image.ubuntu.sha256_digest
37-
"bar" = data.docker_registry_image.centos.sha256_digest
43+
"ubuntu" = local.ubuntu_hash
44+
"centos" = local.centos_hash
3845
"qux" = "quux"
3946
}
4047
}

workspacetags.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ func NewTag(srcRange *hcl.Range, files map[string]*hcl.File, key, val cty.Value)
121121
}
122122

123123
if val.IsKnown() && val.Type() != cty.String {
124+
fr := "<nil>"
125+
if !val.Type().Equals(cty.NilType) {
126+
fr = val.Type().FriendlyName()
127+
}
124128
//r := expr.ValueExpr.Range()
125129
return types.Tag{}, &hcl.Diagnostic{
126130
Severity: hcl.DiagError,
127131
Summary: "Invalid value type for tag",
128-
Detail: fmt.Sprintf("Value must be a string, but got %s", val.Type().FriendlyName()),
132+
Detail: fmt.Sprintf("Value must be a string, but got %s", fr),
129133
//Subject: &r,
130134
Context: srcRange,
131135
//Expression: expr.ValueExpr,

0 commit comments

Comments
 (0)