Skip to content

Commit 98c5c44

Browse files
committed
chore: add in demo template
not all functionality is present, the demo template serves as a complex example.
1 parent 645c6a5 commit 98c5c44

File tree

9 files changed

+191
-68
lines changed

9 files changed

+191
-68
lines changed

preview_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ func Test_Extract(t *testing.T) {
231231
f(),
232232
},
233233
},
234+
{
235+
name: "ambigious",
236+
dir: "ambigious",
237+
expTags: map[string]string{},
238+
input: preview.Input{
239+
PlanJSONPath: "",
240+
ParameterValues: map[string]string{},
241+
Owner: types.WorkspaceOwner{
242+
Groups: []string{},
243+
},
244+
},
245+
expUnknowns: []string{},
246+
params: map[string]func(t *testing.T, parameter types.Parameter){},
247+
},
234248
} {
235249
t.Run(tc.name, func(t *testing.T) {
236250
t.Parallel()

testdata/ambigious/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ module "decisions" {
1212
source = "./modules/decisions"
1313
}
1414

15+
data "coder_parameter" "zero" {
16+
count = module.decisions.zero
17+
name = "Zero"
18+
type = "bool"
19+
default = false
20+
}
21+
1522
data "coder_parameter" "example" {
1623
count = module.decisions.isAdmin ? 1 : 0
1724
name = "IsAdmin"
1825
type = "bool"
1926
default = module.decisions.isAdmin
2027
}
2128

29+
data "coder_parameter" "never-show" {
30+
count = module.decisions.staticFalse ? 1 : 0
31+
name = "NeverShow"
32+
type = "bool"
33+
default = module.decisions.staticFalse
34+
}
35+
2236
data "coder_parameter" "example_root" {
2337
count = contains(data.coder_workspace_owner.me.groups, "admin") ? 1 : 0
2438
name = "IsAdmin_Root"

testdata/ambigious/modules/decisions/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ terraform {
88

99
data coder_workspace_owner "me" {}
1010

11+
output "staticFalse" {
12+
value = false
13+
}
14+
1115
output "isAdmin" {
1216
value = contains(data.coder_workspace_owner.me.groups, "admin")
1317
}
1418

1519
output "groups" {
1620
value = data.coder_workspace_owner.me.groups
21+
}
22+
23+
output "zero" {
24+
value = 0
1725
}

testdata/demo/locals.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
locals {
2+
isAdmin = contains(data.coder_workspace_owner.me.groups, "admin")
3+
4+
fe_codes = ["PS", "WS"]
5+
be_codes = ["CL", "GO", "IU", "PY"]
6+
teams = {
7+
"frontend" = {
8+
"display_name" = "Frontend",
9+
"codes" = local.fe_codes,
10+
"description" = "The team that works on the frontend.",
11+
"icon" = "/icon/desktop.svg"
12+
},
13+
"backend" = {
14+
"display_name" = "Backend",
15+
"codes" = local.be_codes,
16+
"description" = "The team that works on the backend.",
17+
"icon" = "/emojis/2699.png",
18+
},
19+
"fullstack" = {
20+
"display_name" = "Fullstack",
21+
"codes" = concat(local.be_codes, local.fe_codes),
22+
"description" = "The team that works on both the frontend and backend.",
23+
"icon" = "/emojis/1f916.png",
24+
}
25+
}
26+
27+
regions = [
28+
{
29+
icon = "/emojis/1f1fa-1f1f8.png"
30+
name = "Pittsburgh"
31+
value = "us-pittsburgh"
32+
},
33+
{
34+
icon = "/emojis/1f1eb-1f1ee.png"
35+
name = "Helsinki"
36+
value = "eu-helsinki"
37+
},
38+
{
39+
icon = "/emojis/1f1e6-1f1fa.png"
40+
name = "Sydney"
41+
value = "ap-sydney"
42+
},
43+
{
44+
icon = "/emojis/1f1e7-1f1f7.png"
45+
name = "São Paulo"
46+
value = "sa-saopaulo"
47+
},
48+
{
49+
icon = "/emojis/1f1ff-1f1e6.png"
50+
name = "Johannesburg"
51+
value = "za-jnb"
52+
}
53+
]
54+
55+
region_values = [for region in local.regions : region.value]
56+
default_regions = tolist(setintersection(data.coder_workspace_owner.me.groups, local.region_values))
57+
default_region = length(local.default_regions) > 0 ? local.default_regions[0] : local.region_values[0]
58+
}
59+
60+
output "test" {
61+
value = local.default_region
62+
}

testdata/demo/main.tf

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,23 @@
11
// Demo terraform has a complex configuration.
2+
// CODER_WORKSPACE_OWNER_GROUPS='["admin","developer"]' terraform apply
3+
//
4+
// Some run options
5+
// preview -v Team=backend -g admin
6+
// preview -v Team=backend -g admin -g sa-saopaulo
27
terraform {
38
required_providers {
49
coder = {
510
source = "coder/coder"
611
}
7-
}
8-
}
9-
10-
11-
locals {
12-
fe_codes = ["PS", "WS"]
13-
be_codes = ["CL", "GO", "IU", "PY"]
14-
teams = {
15-
"frontend" = {
16-
"display_name" = "Frontend",
17-
"codes" = local.fe_codes,
18-
"description" = "The team that works on the frontend.",
19-
"icon" = "/icon/desktop.svg"
20-
},
21-
"backend" = {
22-
"display_name" = "Backend",
23-
"codes" = local.be_codes,
24-
"description" = "The team that works on the backend.",
25-
"icon" = "/emojis/2699.png",
26-
},
27-
"fullstack" = {
28-
"display_name" = "Fullstack",
29-
"codes" = concat(local.be_codes, local.fe_codes),
30-
"description" = "The team that works on both the frontend and backend.",
31-
"icon" = "/emojis/1f916.png",
12+
docker = {
13+
source = "kreuzwerker/docker"
14+
version = "3.0.2"
3215
}
3316
}
3417
}
3518

36-
data "coder_parameter" "team" {
37-
name = "Team"
38-
description = "Which team are you on?"
39-
type = "string"
40-
default = "fullstack"
41-
order = 1
4219

43-
dynamic "option" {
44-
for_each = local.teams
45-
content {
46-
name = option.value.display_name
47-
value = option.key
48-
description = option.value.description
49-
icon = option.value.icon
50-
}
51-
}
52-
53-
validation {
54-
regex = "^frontend|backend|fullstack$"
55-
error = "You must select either frontend, backend, or fullstack."
56-
}
57-
}
20+
data coder_workspace_owner "me" {}
5821

5922
module "jetbrains_gateway" {
6023
count = 1

testdata/demo/modules/base/base.tf

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ data "coder_parameter" "security_level" {
4242
# }
4343
}
4444

45-
// TODO: REMOVE THIS
46-
data "coder_parameter" "test" {
47-
name = "Test"
48-
description = "Test"
49-
type = "string"
50-
default = tostring(local.choose_security ? 1 : 0)
51-
}
5245

46+
output "security_level" {
47+
value = local.secutity_level
48+
}

testdata/demo/modules/deploys/main.tf

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,3 @@ data "coder_parameter" "direct_ssh" {
3232
type = "bool"
3333
default = false
3434
}
35-
36-
data "coder_parameter" "my_groups" {
37-
# count = 1
38-
name = "Groups"
39-
type = "string"
40-
41-
dynamic "option" {
42-
for_each = data.coder_workspace_owner.me.groups
43-
content {
44-
name = option.value
45-
value = option.value
46-
}
47-
}
48-
}

testdata/demo/parameters.tf

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
data "coder_parameter" "team" {
2+
name = "Team"
3+
description = "Which team are you on?"
4+
type = "string"
5+
default = "fullstack"
6+
order = 1
7+
8+
dynamic "option" {
9+
for_each = local.teams
10+
content {
11+
name = option.value.display_name
12+
value = option.key
13+
description = option.value.description
14+
icon = option.value.icon
15+
}
16+
}
17+
18+
validation {
19+
regex = "^frontend|backend|fullstack$"
20+
error = "You must select either frontend, backend, or fullstack."
21+
}
22+
}
23+
24+
data "coder_parameter" "cpu" {
25+
name = "cpu"
26+
display_name = "CPU"
27+
description = "The number of CPU cores"
28+
type ="number"
29+
default = "2"
30+
icon = "/icon/memory.svg"
31+
mutable = true
32+
33+
validation {
34+
min = 1
35+
// Confidential instances are more expensive, or some justification like
36+
// that
37+
max = module.base.security_level == "high" ? 4: 8
38+
error = "CPU range must be between {min} and {max}."
39+
}
40+
}
41+
42+
// Advanced admin parameter
43+
data "coder_parameter" "image_hash" {
44+
count = local.isAdmin ? 1 : 0
45+
name = "Image Hash"
46+
description = "Override the hash of the image to use. Only available to admins."
47+
// Value can get stale
48+
default = trimprefix(data.docker_registry_image.coder.sha256_digest, "sha256:")
49+
50+
validation {
51+
regex = "^[a-f0-9A-F]{64}$"
52+
error = "The image hash must be a 64-character hexadecimal string."
53+
}
54+
}
55+
56+
data "docker_registry_image" "coder" {
57+
name = "ghcr.io/coder/coder:latest"
58+
}
59+
60+
data "coder_parameter" "region" {
61+
name = "Region"
62+
display_name = "Region"
63+
description = "What region are you in?"
64+
default = local.default_region
65+
icon = "/icon/memory.svg"
66+
mutable = false
67+
68+
dynamic "option" {
69+
for_each = local.regions
70+
content {
71+
name = option.value.name
72+
value = option.value.value
73+
icon = option.value.icon
74+
}
75+
}
76+
}

todo.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@
4343

4444
- How will the hooks work if they cannot be merged upstream? Alternative?
4545
- Load in plan state
46-
- Semantics for parameter coder blocks
46+
- Semantics for parameter coder blocks
47+
48+
## Backward compatibility
49+
50+
- Omitting `type` behavior, is there a default?

0 commit comments

Comments
 (0)