|
| 1 | +# Working with list(string) Parameters |
| 2 | + |
| 3 | +This guide explores the usage of `list(string)` parameters in Coder templates, providing examples and best practices for their implementation. |
| 4 | + |
| 5 | +## Introduction to list(string) Parameters |
| 6 | + |
| 7 | +The `list(string)` parameter type allows users to select multiple string values from a predefined set of options. This is useful for scenarios where multiple selections are needed, such as: |
| 8 | + |
| 9 | +- Selecting multiple security groups |
| 10 | +- Choosing programming languages or tools |
| 11 | +- Applying tags to resources |
| 12 | +- Selecting features to enable |
| 13 | + |
| 14 | +## Basic Usage |
| 15 | + |
| 16 | +To define a basic `list(string)` parameter: |
| 17 | + |
| 18 | +```tf |
| 19 | +data "coder_parameter" "security_groups" { |
| 20 | + name = "security_groups" |
| 21 | + display_name = "Security Groups" |
| 22 | + description = "Select security groups to apply to your workspace" |
| 23 | + type = "list(string)" |
| 24 | + default = jsonencode(["default-sg", "ssh-sg"]) |
| 25 | + mutable = true |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Key points: |
| 30 | +- Use the `jsonencode()` function to specify default values |
| 31 | +- The default values must be a JSON array of strings |
| 32 | +- Users can select zero, one, or multiple values |
| 33 | + |
| 34 | +## UI Rendering Options |
| 35 | + |
| 36 | +### Form Types |
| 37 | + |
| 38 | +Coder supports two main form types for `list(string)` parameters: |
| 39 | + |
| 40 | +#### 1. Multi-select (default) |
| 41 | + |
| 42 | +The standard multi-select dropdown: |
| 43 | + |
| 44 | +```tf |
| 45 | +data "coder_parameter" "languages" { |
| 46 | + name = "languages" |
| 47 | + display_name = "Programming Languages" |
| 48 | + description = "Select languages you'll work with" |
| 49 | + type = "list(string)" |
| 50 | + default = jsonencode(["python", "javascript"]) |
| 51 | + form_type = "multi-select" # This is the default |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +#### 2. Tag-select |
| 56 | + |
| 57 | +A more tag-focused interface, useful for parameters representing tags or labels: |
| 58 | + |
| 59 | +```tf |
| 60 | +data "coder_parameter" "tags" { |
| 61 | + name = "tags" |
| 62 | + display_name = "Resource Tags" |
| 63 | + description = "Tags to apply to your workspace resources" |
| 64 | + type = "list(string)" |
| 65 | + default = jsonencode(["development", "testing"]) |
| 66 | + form_type = "tag-select" |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Adding Icons and Descriptions |
| 71 | + |
| 72 | +You can enhance the UI by adding icons and descriptions to both the parameter and individual options: |
| 73 | + |
| 74 | +```tf |
| 75 | +data "coder_parameter" "environments" { |
| 76 | + name = "environments" |
| 77 | + display_name = "Deployment Environments" |
| 78 | + description = "Select which environments this workspace can deploy to" |
| 79 | + type = "list(string)" |
| 80 | + default = jsonencode(["dev"]) |
| 81 | + icon = "/emojis/1f30e.png" |
| 82 | + |
| 83 | + option { |
| 84 | + name = "Development" |
| 85 | + value = "dev" |
| 86 | + icon = "/emojis/1f3ed.png" |
| 87 | + description = "Development environment with debugging tools" |
| 88 | + } |
| 89 | + |
| 90 | + option { |
| 91 | + name = "Production" |
| 92 | + value = "prod" |
| 93 | + icon = "/emojis/1f6eb.png" |
| 94 | + description = "Production-grade environment" |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Working with Selected Values |
| 100 | + |
| 101 | +To use the selected values in your template: |
| 102 | + |
| 103 | +```tf |
| 104 | +resource "aws_instance" "workspace" { |
| 105 | + // Other configuration... |
| 106 | + |
| 107 | + // Use jsondecode() to convert the JSON string back to a list |
| 108 | + security_groups = jsondecode(data.coder_parameter.security_groups.value) |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +For displaying in metadata: |
| 113 | + |
| 114 | +```tf |
| 115 | +resource "coder_metadata" "workspace_info" { |
| 116 | + resource_id = aws_instance.workspace.id |
| 117 | + |
| 118 | + item { |
| 119 | + key = "Environments" |
| 120 | + value = join(", ", jsondecode(data.coder_parameter.environments.value)) |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Dynamic Options |
| 126 | + |
| 127 | +You can dynamically generate options for a `list(string)` parameter: |
| 128 | + |
| 129 | +```tf |
| 130 | +locals { |
| 131 | + available_regions = { |
| 132 | + "us-east-1" = { |
| 133 | + name = "US East (N. Virginia)", |
| 134 | + icon = "/emojis/1f1fa-1f1f8.png" |
| 135 | + }, |
| 136 | + "eu-west-1" = { |
| 137 | + name = "EU West (Ireland)", |
| 138 | + icon = "/emojis/1f1ea-1f1fa.png" |
| 139 | + } |
| 140 | + // Additional regions... |
| 141 | + } |
| 142 | +} |
| 143 | +
|
| 144 | +data "coder_parameter" "regions" { |
| 145 | + name = "regions" |
| 146 | + display_name = "AWS Regions" |
| 147 | + description = "Select AWS regions to deploy to" |
| 148 | + type = "list(string)" |
| 149 | + default = jsonencode(["us-east-1"]) |
| 150 | + |
| 151 | + dynamic "option" { |
| 152 | + for_each = local.available_regions |
| 153 | + content { |
| 154 | + name = option.value.name |
| 155 | + value = option.key |
| 156 | + icon = option.value.icon |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Empty Defaults |
| 163 | + |
| 164 | +To create a parameter with no default selections: |
| 165 | + |
| 166 | +```tf |
| 167 | +data "coder_parameter" "features" { |
| 168 | + name = "features" |
| 169 | + display_name = "Optional Features" |
| 170 | + description = "Select optional features to enable" |
| 171 | + type = "list(string)" |
| 172 | + default = jsonencode([]) // Empty array means no default selection |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## Ephemeral List Parameters |
| 177 | + |
| 178 | +For operations that should only apply during workspace creation or updates: |
| 179 | + |
| 180 | +```tf |
| 181 | +data "coder_parameter" "startup_tasks" { |
| 182 | + name = "startup_tasks" |
| 183 | + display_name = "Startup Tasks" |
| 184 | + description = "Tasks to run during workspace startup" |
| 185 | + type = "list(string)" |
| 186 | + default = jsonencode(["update_packages"]) |
| 187 | + ephemeral = true |
| 188 | + |
| 189 | + option { |
| 190 | + name = "Update Packages" |
| 191 | + value = "update_packages" |
| 192 | + } |
| 193 | + option { |
| 194 | + name = "Run Migrations" |
| 195 | + value = "run_migrations" |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +## Command Line Usage |
| 201 | + |
| 202 | +Using `list(string)` parameters on the command line requires careful quoting: |
| 203 | + |
| 204 | +```bash |
| 205 | +coder create --parameter "\"security_groups=[\"\"web-sg\"\",\"\"db-sg\"\"]\"" |
| 206 | +``` |
| 207 | + |
| 208 | +Alternatively, use a parameter file for a cleaner approach: |
| 209 | + |
| 210 | +```yaml |
| 211 | +# params.yaml |
| 212 | +security_groups: |
| 213 | + - web-sg |
| 214 | + - db-sg |
| 215 | +``` |
| 216 | +
|
| 217 | +```bash |
| 218 | +coder create --rich-parameter-file params.yaml |
| 219 | +``` |
| 220 | + |
| 221 | +## Best Practices |
| 222 | + |
| 223 | +1. **Provide Clear Descriptions**: Ensure each option has a clear description |
| 224 | +2. **Use Appropriate Icons**: Visual cues help users quickly identify options |
| 225 | +3. **Group Related Parameters**: Keep related parameters together using the `order` attribute |
| 226 | +4. **Consider Default Selections**: Choose sensible defaults to minimize user effort |
| 227 | +5. **Use Form Types Appropriately**: Use `tag-select` for tags, `multi-select` for other selections |
| 228 | +6. **Limit Option Count**: Avoid overwhelming users with too many options |
| 229 | +7. **Use Dynamic Options**: Generate options programmatically when the list might change |
| 230 | + |
| 231 | +## Full Example |
| 232 | + |
| 233 | +See the [list-string-parameters example template](https://github.com/coder/coder/tree/main/examples/parameters-list-string) for a complete implementation showcasing various styling and configuration options. |
| 234 | + |
| 235 | +```tf |
| 236 | +data "coder_parameter" "environments" { |
| 237 | + name = "environments" |
| 238 | + display_name = "Deployment Environments" |
| 239 | + description = "Select which environments this workspace can deploy to" |
| 240 | + type = "list(string)" |
| 241 | + default = jsonencode(["dev"]) |
| 242 | + mutable = true |
| 243 | + icon = "/emojis/1f30e.png" |
| 244 | + form_type = "multi-select" |
| 245 | + |
| 246 | + option { |
| 247 | + name = "Development" |
| 248 | + value = "dev" |
| 249 | + icon = "/emojis/1f3ed.png" |
| 250 | + description = "Development environment with debugging tools" |
| 251 | + } |
| 252 | + |
| 253 | + option { |
| 254 | + name = "Staging" |
| 255 | + value = "staging" |
| 256 | + icon = "/emojis/1f6a7.png" |
| 257 | + description = "Pre-production environment for testing" |
| 258 | + } |
| 259 | + |
| 260 | + option { |
| 261 | + name = "Production" |
| 262 | + value = "prod" |
| 263 | + icon = "/emojis/1f6eb.png" |
| 264 | + description = "Production-grade environment" |
| 265 | + } |
| 266 | +} |
| 267 | +``` |
0 commit comments