Skip to content

Commit 240cb18

Browse files
committed
Merge branch 'main' of github.com:/coder/terraform-provider-coder into dk/prebuilds
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
2 parents 12b724d + 53a68cd commit 240cb18

23 files changed

+1038
-80
lines changed

docs/data-sources/parameter.md

+2
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,12 @@ data "coder_parameter" "home_volume_size" {
145145
- `description` (String) Describe what this parameter does.
146146
- `display_name` (String) The displayed name of the parameter as it will appear in the interface.
147147
- `ephemeral` (Boolean) The value of an ephemeral parameter will not be preserved between consecutive workspace builds.
148+
- `form_type` (String) The type of this parameter. Must be one of: [radio, slider, input, dropdown, checkbox, switch, multi-select, tag-select, textarea, error].
148149
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a built-in icon with `"${data.coder_workspace.me.access_url}/icon/<path>"`.
149150
- `mutable` (Boolean) Whether this value can be changed after workspace creation. This can be destructive for values like region, so use with caution!
150151
- `option` (Block List) Each `option` block defines a value for a user to select from. (see [below for nested schema](#nestedblock--option))
151152
- `order` (Number) The order determines the position of a template parameter in the UI/CLI presentation. The lowest order is shown first and parameters with equal order are sorted by name (ascending order).
153+
- `styling` (String) JSON encoded string containing the metadata for controlling the appearance of this parameter in the UI. This option is purely cosmetic and does not affect the function of the parameter in terraform.
152154
- `type` (String) The type of this parameter. Must be one of: `"number"`, `"string"`, `"bool"`, or `"list(string)"`.
153155
- `validation` (Block List, Max: 1) Validate the input of a parameter. (see [below for nested schema](#nestedblock--validation))
154156

docs/data-sources/workspace.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ resource "docker_container" "workspace" {
6969
- `access_port` (Number) The access port of the Coder deployment provisioning this workspace.
7070
- `access_url` (String) The access URL of the Coder deployment provisioning this workspace.
7171
- `id` (String) UUID of the workspace.
72+
- `is_prebuild` (Boolean) Similar to `prebuild_count`, but a boolean value instead of a count. This is set to true if the workspace is a currently unassigned prebuild. Once the workspace is assigned, this value will be false.
7273
- `name` (String) Name of the workspace.
74+
- `prebuild_count` (Number) A computed count, equal to 1 if the workspace is a currently unassigned prebuild. Use this to conditionally act on the status of a prebuild. Actions that do not require user identity can be taken when this value is set to 1. Actions that should only be taken once the workspace has been assigned to a user may be taken when this value is set to 0.
7375
- `start_count` (Number) A computed count based on `transition` state. If `start`, count will equal 1.
7476
- `template_id` (String) ID of the workspace's template.
7577
- `template_name` (String) Name of the workspace's template.

docs/data-sources/workspace_owner.md

+9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ resource "coder_env" "git_author_email" {
5353
- `login_type` (String) The type of login the user has.
5454
- `name` (String) The username of the user.
5555
- `oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
56+
- `rbac_roles` (List of Object) The RBAC roles of which the user is assigned. (see [below for nested schema](#nestedatt--rbac_roles))
5657
- `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.
5758
- `ssh_private_key` (String, Sensitive) The user's generated SSH private key.
5859
- `ssh_public_key` (String) The user's generated SSH public key.
60+
61+
<a id="nestedatt--rbac_roles"></a>
62+
### Nested Schema for `rbac_roles`
63+
64+
Read-Only:
65+
66+
- `name` (String)
67+
- `org_id` (String)

docs/data-sources/workspace_preset.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "coder_workspace_preset Data Source - terraform-provider-coder"
44
subcategory: ""
55
description: |-
6-
Use this data source to predefine common configurations for workspaces.
6+
Use this data source to predefine common configurations for coder workspaces. Users will have the option to select a defined preset, which will automatically apply the selected configuration. Any parameters defined in the preset will be applied to the workspace. Parameters that are not defined by the preset will still be configurable when creating a workspace.
77
---
88

99
# coder_workspace_preset (Data Source)
1010

11-
Use this data source to predefine common configurations for workspaces.
11+
Use this data source to predefine common configurations for coder workspaces. Users will have the option to select a defined preset, which will automatically apply the selected configuration. Any parameters defined in the preset will be applied to the workspace. Parameters that are not defined by the preset will still be configurable when creating a workspace.
1212

1313
## Example Usage
1414

@@ -34,9 +34,20 @@ data "coder_workspace_preset" "example" {
3434

3535
### Required
3636

37-
- `name` (String) Name of the workspace preset.
38-
- `parameters` (Map of String) Parameters of the workspace preset.
37+
- `name` (String) The name of the workspace preset.
38+
39+
### Optional
40+
41+
- `parameters` (Map of String) Workspace parameters that will be set by the workspace preset. For simple templates that only need prebuilds, you may define a preset with zero parameters. Because workspace parameters may change between Coder template versions, preset parameters are allowed to define values for parameters that do not exist in the current template version.
42+
- `prebuilds` (Block Set, Max: 1) Prebuilt workspace configuration related to this workspace preset. Coder will build and maintain workspaces in reserve based on this configuration. When a user creates a new workspace using a preset, they will be assigned a prebuilt workspace, instead of waiting for a new workspace to build. (see [below for nested schema](#nestedblock--prebuilds))
3943

4044
### Read-Only
4145

42-
- `id` (String) ID of the workspace preset.
46+
- `id` (String) The preset ID is automatically generated and may change between runs. It is recommended to use the `name` attribute to identify the preset.
47+
48+
<a id="nestedblock--prebuilds"></a>
49+
### Nested Schema for `prebuilds`
50+
51+
Required:
52+
53+
- `instances` (Number) The number of workspaces to keep in reserve for this preset.

docs/resources/devcontainer.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_devcontainer Resource - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Define a Dev Container the agent should know of and attempt to autostart (minimum Coder version: v2.21).
7+
---
8+
9+
# coder_devcontainer (Resource)
10+
11+
Define a Dev Container the agent should know of and attempt to autostart (minimum Coder version: v2.21).
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `agent_id` (String) The `id` property of a `coder_agent` resource to associate with.
21+
- `workspace_folder` (String) The workspace folder to for the Dev Container.
22+
23+
### Optional
24+
25+
- `config_path` (String) The path to the Dev Container configuration file (devcontainer.json).
26+
27+
### Read-Only
28+
29+
- `id` (String) The ID of this resource.

integration/integration_test.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ func TestIntegration(t *testing.T) {
9090
// TODO (sasswart): the cli doesn't support presets yet.
9191
// once it does, the value for workspace_parameter.value
9292
// will be the preset value.
93-
"workspace_parameter.value": `param value`,
94-
"workspace_parameter.icon": `param icon`,
95-
"workspace_preset.name": `preset`,
96-
"workspace_preset.parameters.param": `preset param value`,
93+
"workspace_parameter.value": `param value`,
94+
"workspace_parameter.icon": `param icon`,
95+
"workspace_preset.name": `preset`,
96+
"workspace_preset.parameters.param": `preset param value`,
97+
"workspace_preset.prebuilds.instances": `1`,
9798
},
9899
},
99100
{
@@ -122,6 +123,7 @@ func TestIntegration(t *testing.T) {
122123
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
123124
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
124125
"workspace_owner.login_type": ``,
126+
"workspace_owner.rbac_roles": ``,
125127
},
126128
},
127129
{
@@ -150,6 +152,36 @@ func TestIntegration(t *testing.T) {
150152
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
151153
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
152154
"workspace_owner.login_type": `password`,
155+
"workspace_owner.rbac_roles": ``,
156+
},
157+
},
158+
{
159+
name: "workspace-owner-rbac-roles",
160+
minVersion: "v2.21.0", // anticipated version, update as required
161+
expectedOutput: map[string]string{
162+
"provisioner.arch": runtime.GOARCH,
163+
"provisioner.id": `[a-zA-Z0-9-]+`,
164+
"provisioner.os": runtime.GOOS,
165+
"workspace.access_port": `\d+`,
166+
"workspace.access_url": `https?://\D+:\d+`,
167+
"workspace.id": `[a-zA-z0-9-]+`,
168+
"workspace.name": ``,
169+
"workspace.start_count": `1`,
170+
"workspace.template_id": `[a-zA-Z0-9-]+`,
171+
"workspace.template_name": `workspace-owner`,
172+
"workspace.template_version": `.+`,
173+
"workspace.transition": `start`,
174+
"workspace_owner.email": `testing@coder\.com`,
175+
"workspace_owner.full_name": `default`,
176+
"workspace_owner.groups": `\[(\"Everyone\")?\]`,
177+
"workspace_owner.id": `[a-zA-Z0-9-]+`,
178+
"workspace_owner.name": `testing`,
179+
"workspace_owner.oidc_access_token": `^$`, // TODO: test OIDC integration
180+
"workspace_owner.session_token": `.+`,
181+
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
182+
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
183+
"workspace_owner.login_type": `password`,
184+
"workspace_owner.rbac_roles": `(?is)\[(\{"name":"[a-z0-9-:]+","org_id":"[a-f0-9-]+"\},?)+\]`,
153185
},
154186
},
155187
{

integration/test-data-source/main.tf

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ data "coder_workspace_preset" "preset" {
2424
parameters = {
2525
(data.coder_parameter.param.name) = "preset param value"
2626
}
27+
28+
prebuilds {
29+
instances = 1
30+
}
2731
}
2832

2933
locals {
@@ -47,6 +51,7 @@ locals {
4751
"workspace_parameter.icon" : data.coder_parameter.param.icon,
4852
"workspace_preset.name" : data.coder_workspace_preset.preset.name,
4953
"workspace_preset.parameters.param" : data.coder_workspace_preset.preset.parameters.param,
54+
"workspace_preset.prebuilds.instances" : tostring(one(data.coder_workspace_preset.preset.prebuilds).instances),
5055
}
5156
}
5257

integration/workspace-owner-filled/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ locals {
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
4242
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
43+
"workspace_owner.rbac_roles" : jsonencode(data.coder_workspace_owner.me.rbac_roles),
4344
}
4445
}
4546

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
local = {
7+
source = "hashicorp/local"
8+
}
9+
}
10+
}
11+
12+
data "coder_provisioner" "me" {}
13+
data "coder_workspace" "me" {}
14+
data "coder_workspace_owner" "me" {}
15+
16+
locals {
17+
# NOTE: these must all be strings in the output
18+
output = {
19+
"provisioner.arch" : data.coder_provisioner.me.arch,
20+
"provisioner.id" : data.coder_provisioner.me.id,
21+
"provisioner.os" : data.coder_provisioner.me.os,
22+
"workspace.access_port" : tostring(data.coder_workspace.me.access_port),
23+
"workspace.access_url" : data.coder_workspace.me.access_url,
24+
"workspace.id" : data.coder_workspace.me.id,
25+
"workspace.name" : data.coder_workspace.me.name,
26+
"workspace.start_count" : tostring(data.coder_workspace.me.start_count),
27+
"workspace.template_id" : data.coder_workspace.me.template_id,
28+
"workspace.template_name" : data.coder_workspace.me.template_name,
29+
"workspace.template_version" : data.coder_workspace.me.template_version,
30+
"workspace.transition" : data.coder_workspace.me.transition,
31+
"workspace_owner.email" : data.coder_workspace_owner.me.email,
32+
"workspace_owner.full_name" : data.coder_workspace_owner.me.full_name,
33+
"workspace_owner.groups" : jsonencode(data.coder_workspace_owner.me.groups),
34+
"workspace_owner.id" : data.coder_workspace_owner.me.id,
35+
"workspace_owner.name" : data.coder_workspace_owner.me.name,
36+
"workspace_owner.oidc_access_token" : data.coder_workspace_owner.me.oidc_access_token,
37+
"workspace_owner.session_token" : data.coder_workspace_owner.me.session_token,
38+
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
39+
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
40+
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
41+
"workspace_owner.rbac_roles" : jsonencode(data.coder_workspace_owner.me.rbac_roles),
42+
}
43+
}
44+
45+
variable "output_path" {
46+
type = string
47+
}
48+
49+
resource "local_file" "output" {
50+
filename = var.output_path
51+
content = jsonencode(local.output)
52+
}
53+
54+
output "output" {
55+
value = local.output
56+
sensitive = true
57+
}

integration/workspace-owner/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ locals {
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
4242
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
43+
"workspace_owner.rbac_roles" : jsonencode(data.coder_workspace_owner.me.rbac_roles),
4344
}
4445
}
4546

main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"flag"
5+
46
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
57

68
"github.com/coder/terraform-provider-coder/v2/provider"
@@ -11,8 +13,15 @@ import (
1113
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
1214

1315
func main() {
14-
servePprof()
15-
plugin.Serve(&plugin.ServeOpts{
16+
debug := flag.Bool("debug", false, "Enable debug mode for the provider")
17+
flag.Parse()
18+
19+
opts := &plugin.ServeOpts{
20+
Debug: *debug,
21+
ProviderAddr: "registry.terraform.io/coder/coder",
1622
ProviderFunc: provider.New,
17-
})
23+
}
24+
25+
servePprof()
26+
plugin.Serve(opts)
1827
}

provider/devcontainer.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/uuid"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
10+
)
11+
12+
func devcontainerResource() *schema.Resource {
13+
return &schema.Resource{
14+
SchemaVersion: 1,
15+
16+
Description: "Define a Dev Container the agent should know of and attempt to autostart (minimum Coder version: v2.21).",
17+
CreateContext: func(_ context.Context, rd *schema.ResourceData, _ interface{}) diag.Diagnostics {
18+
rd.SetId(uuid.NewString())
19+
20+
return nil
21+
},
22+
ReadContext: schema.NoopContext,
23+
DeleteContext: schema.NoopContext,
24+
Schema: map[string]*schema.Schema{
25+
"agent_id": {
26+
Type: schema.TypeString,
27+
Description: "The `id` property of a `coder_agent` resource to associate with.",
28+
ForceNew: true,
29+
Required: true,
30+
},
31+
"workspace_folder": {
32+
Type: schema.TypeString,
33+
Description: "The workspace folder to for the Dev Container.",
34+
ForceNew: true,
35+
Required: true,
36+
ValidateFunc: validation.StringIsNotEmpty,
37+
},
38+
"config_path": {
39+
Type: schema.TypeString,
40+
Description: "The path to the Dev Container configuration file (devcontainer.json).",
41+
ForceNew: true,
42+
Optional: true,
43+
},
44+
},
45+
}
46+
}

0 commit comments

Comments
 (0)