Skip to content

Commit cbec77b

Browse files
committed
added more examples
1 parent d0769d8 commit cbec77b

File tree

1 file changed

+144
-102
lines changed

1 file changed

+144
-102
lines changed

docs/admin/templates/extending-templates/dynamic-parameters.md

Lines changed: 144 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ data "coder_parameter" "cpu_cores" {
284284
}
285285
```
286286

287-
<!-- ### Secrets
287+
### Secrets
288288

289289
Sliders can be used for configuration on a linear scale, like resource allocation. The `validation` block is used to clamp the minimum and maximum values for the parameter.
290290

291291
[Try secret parameters on the Parameters Playground](https://playground.coder.app/parameters/wmiP7FM3Za).
292292

293+
Note: this text may not be properly hidden in the Playground. The `mask_input` styling attribute is supported in v2.24.0 and onward.
294+
293295
```terraform
294296
data "coder_parameter" "private_api_key" {
295297
name = "private_api_key"
@@ -302,10 +304,10 @@ data "coder_parameter" "private_api_key" {
302304
default = "privatekey"
303305
304306
styling = jsonencode({
305-
maskInput = true
307+
mask_input = true
306308
})
307309
}
308-
``` -->
310+
```
309311

310312

311313
</div>
@@ -549,35 +551,159 @@ data "coder_parameter" "region" {
549551

550552
</div>
551553

554+
## Identity-aware parameters (Premium)
555+
556+
Premium users can leverage our roles and groups to conditionally expose or change parameters based on user identity. This is helpful for establishing governance policy directly in the workspace creation form, rather than creating multiple templates to manage RBAC.
557+
558+
User identity is referenced in Terraform by reading the [`coder_workspace_owner`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace_owner) data source.
559+
552560
<div class="tabs">
553561

554562
## Admin Options
555563

564+
Template administrators often want to expose certain experimental or unstable options only to those with elevated roles. You can now do this by setting `count` based on a user's group or role, referencing the [`coder_workspace_owner`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace_owner) data source.
565+
566+
[Try out admin-only options in the Playground](https://playground.coder.app/parameters/5Gn9W3hYs7).
567+
568+
```terraform
569+
570+
locals {
571+
roles = [for r in data.coder_workspace_owner.me.rbac_roles: r.name]
572+
is_admin = contains(data.coder_workspace_owner.me.groups, "admin")
573+
has_admin_role = contains(local.roles, "owner")
574+
}
575+
576+
data "coder_workspace_owner" "me" {}
577+
578+
data "coder_parameter" "advanced_settings" {
579+
# This parameter is only visible when the user is an administrator
580+
count = local.is_admin ? 1 : 0
581+
582+
name = "advanced_settings"
583+
display_name = "Add an arbitrary script"
584+
description = "An advanced configuration option only available to admins."
585+
type = "string"
586+
form_type = "textarea"
587+
mutable = true
588+
order = 5
589+
590+
styling = jsonencode({
591+
placeholder = <<-EOT
592+
#!/usr/bin/env bash
593+
while true; do
594+
echo "hello world"
595+
sleep 1
596+
done
597+
EOT
598+
})
599+
}
600+
556601
```
557-
data "coder_parameter" "advanced_setting" {
602+
603+
## Role-specific options
604+
605+
Similarly to the above example, you can show certain options to specific roles on the platform. This allows you to restrict resources for those who may not need them.
606+
607+
```terraform
608+
locals {
609+
roles = [for r in data.coder_workspace_owner.me.rbac_roles: r.name]
610+
is_admin = contains(data.coder_workspace_owner.me.groups, "admin")
611+
has_admin_role = contains(roles, "owner")
612+
613+
614+
615+
non_admin_desc = "YOU ARE NOT AN ADMIN! Set user to 'Administrator' in the top right."
616+
admin_desc = "You are an administrator! You now have access to the secret advanced option."
617+
}
618+
619+
data "coder_workspace_owner" "me" {}
620+
621+
data "coder_parameter" "advanced_settings" {
558622
# This parameter is only visible when show_advanced is true
559-
count = contains(data.workspace_owner.groups) ? 1 : 0
560-
name = "advanced_setting"
561-
display_name = "Advanced Setting"
562-
description = "An advanced configuration option"
623+
count = local.is_admin ? 1 : 0
624+
name = "advanced_settings"
625+
display_name = "Add an arbitrary script"
626+
description = "An advanced configuration option only available to admins."
563627
type = "string"
564-
default = "default_value"
628+
form_type = "textarea"
629+
630+
styling = jsonencode({
631+
placeholder = <<-EOT
632+
#!/usr/bin/env bash
633+
while true; do
634+
echo "hello world"
635+
sleep 1
636+
done
637+
EOT
638+
})
639+
565640
mutable = true
566-
order = 1
641+
order = 5
642+
}
643+
```
644+
645+
### User-aware Regions
646+
647+
You can expose regions depending on which group a user belongs to. This way developers can't incidentally induce low-latency with world-spanning connections.
648+
649+
[Try user-aware regions in the parameter playground](https://playground.coder.app/parameters/tBD-mbZRGm)
650+
651+
```terraform
652+
653+
locals {
654+
eu_regions = [
655+
"eu-west-1 (Ireland)",
656+
"eu-central-1 (Frankfurt)",
657+
"eu-north-1 (Stockholm)",
658+
"eu-west-3 (Paris)",
659+
"eu-south-1 (Milan)"
660+
]
661+
662+
us_regions = [
663+
"us-east-1 (N. Virginia)",
664+
"us-west-1 (California)",
665+
"us-west-2 (Oregon)",
666+
"us-east-2 (Ohio)",
667+
"us-central-1 (Iowa)"
668+
]
669+
670+
eu_group_name = "eu-helsinki"
671+
is_eu_dev = contains(data.coder_workspace_owner.me.groups, local.eu_group_name)
672+
region_desc_tag = local.is_eu_dev ? "european" : "american"
673+
}
674+
675+
data "coder_parameter" "region" {
676+
name = "region"
677+
display_name = "Select a Region"
678+
description = "Select from ${local.region_desc_tag} region options."
679+
type = "string"
680+
form_type = "dropdown"
681+
order = 5
682+
default = local.is_eu_dev ? local.eu_regions[0] : local.us_regions[0]
683+
684+
dynamic "option" {
685+
for_each = local.is_eu_dev ? local.eu_regions : local.us_regions
686+
content {
687+
name = option.value
688+
value = option.value
689+
description = "Use ${option.value}"
690+
}
691+
}
567692
}
568693
```
569694

570-
## Role-specific options
571695

572-
## Groups as namespaces
696+
### Groups as namespaces
573697

574-
</div>
698+
A slightly unorthodox way to leverage this is filling the selections of a parameter from the user's groups. Some users associate groups with namespaces (E.G. Kubernetes), then allow users to target that namespace with a parameter like so.
575699

576-
## Advanced use cases
577700

578-
## Dynamic Parameter Use Case Examples
701+
```terraform
702+
579703
580-
<details><summary>Conditional Parameters: Region and Instance Types</summary>
704+
```
705+
706+
</div>
581707

582708
This example shows instance types based on the selected region:
583709

@@ -626,39 +752,6 @@ data "coder_parameter" "instance_type" {
626752
}
627753
```
628754

629-
</details>
630-
631-
<details><summary>Advanced Options Toggle</summary>
632-
633-
This example shows how to create an advanced options section:
634-
635-
```tf
636-
data "coder_parameter" "show_advanced" {
637-
name = "show_advanced"
638-
display_name = "Show Advanced Options"
639-
description = "Enable to show advanced configuration options"
640-
type = "bool"
641-
default = false
642-
order = 0
643-
}
644-
645-
data "coder_parameter" "advanced_setting" {
646-
# This parameter is only visible when show_advanced is true
647-
count = data.coder_parameter.show_advanced.value ? 1 : 0
648-
name = "advanced_setting"
649-
display_name = "Advanced Setting"
650-
description = "An advanced configuration option"
651-
type = "string"
652-
default = "default_value"
653-
mutable = true
654-
order = 1
655-
}
656-
```
657-
658-
</details>
659-
660-
<details><summary>Team-specific Resources</summary>
661-
662755
This example filters resources based on user group membership:
663756

664757
```tf
@@ -685,9 +778,7 @@ data "coder_parameter" "instance_type" {
685778
}
686779
```
687780

688-
### Advanced Usage Patterns
689-
690-
<details><summary>Creating Branching Paths</summary>
781+
###
691782

692783
For templates serving multiple teams or use cases, you can create comprehensive branching paths:
693784

@@ -761,54 +852,5 @@ data "coder_parameter" "vm_image" {
761852
}
762853
```
763854

764-
</details>
765-
766-
<details><summary>Conditional Validation</summary>
767-
768-
Adjust validation rules dynamically based on parameter values:
769-
770-
```tf
771-
data "coder_parameter" "team" {
772-
name = "team"
773-
display_name = "Team"
774-
type = "string"
775-
default = "engineering"
776-
777-
option {
778-
name = "Engineering"
779-
value = "engineering"
780-
}
781-
782-
option {
783-
name = "Data Science"
784-
value = "data-science"
785-
}
786-
}
787-
788-
data "coder_parameter" "cpu_count" {
789-
name = "cpu_count"
790-
display_name = "CPU Count"
791-
type = "number"
792-
default = 2
793-
794-
# Engineering team has lower limits
795-
dynamic "validation" {
796-
for_each = data.coder_parameter.team.value == "engineering" ? [1] : []
797-
content {
798-
min = 1
799-
max = 4
800-
}
801-
}
802-
803-
# Data Science team has higher limits
804-
dynamic "validation" {
805-
for_each = data.coder_parameter.team.value == "data-science" ? [1] : []
806-
content {
807-
min = 2
808-
max = 8
809-
}
810-
}
811-
}
812-
```
855+
## Troubleshooting
813856

814-
</details>

0 commit comments

Comments
 (0)