Skip to content

Commit 578c648

Browse files
authored
feat: Remove agent_script data source to simplify resources (#8)
This adds a new resource for associating instance IDs, which will only need to be used in edge-cases. We'll auto-detect instance identifiers for the most popular cases, and perform zero-trust authentication. It seemed weird to separate the agent script and agent itself. This also led to difficulties previewing the operating system prior to start.
1 parent 549826b commit 578c648

File tree

12 files changed

+254
-247
lines changed

12 files changed

+254
-247
lines changed

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
default: testacc
22

3+
fmt:
4+
terraform fmt -recursive
5+
6+
gen:
7+
# go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest
8+
tfplugindocs
9+
310
# Run acceptance tests
411
.PHONY: testacc
512
testacc:

docs/data-sources/agent_script.md

-47
This file was deleted.

docs/data-sources/workspace.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ resource "kubernetes_pod" "dev" {
2626

2727
### Optional
2828

29-
- **id** (String) The ID of this resource.
29+
- `id` (String) The ID of this resource.
3030

3131
### Read-Only
3232

33-
- **name** (String) Name of the workspace.
34-
- **owner** (String) Username of the workspace owner.
35-
- **transition** (String) Either "start" or "stop". Use this to start/stop resources with "count".
33+
- `name` (String) Name of the workspace.
34+
- `owner` (String) Username of the workspace owner.
35+
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
36+
- `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count".
3637

3738

docs/index.md

+10-25
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ description: |-
1313
## Example
1414

1515
```terraform
16-
variable "gcp_credentials" {
17-
sensitive = true
18-
}
19-
2016
terraform {
2117
required_providers {
2218
coder = {
@@ -26,26 +22,23 @@ terraform {
2622
}
2723
2824
provider "google" {
29-
region = "us-central1"
30-
credentials = var.gcp_credentials
25+
region = "us-central1"
3126
}
3227
3328
data "coder_workspace" "me" {}
34-
data "google_compute_default_service_account" "default" {}
35-
data "coder_agent_script" "dev" {
29+
30+
resource "coder_agent" "dev" {
3631
arch = "amd64"
3732
os = "linux"
38-
}
39-
resource "random_string" "random" {
40-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
41-
length = 8
42-
special = false
33+
auth = "google-instance-identity"
4334
}
4435
36+
data "google_compute_default_service_account" "default" {}
37+
4538
resource "google_compute_instance" "dev" {
4639
zone = "us-central1-a"
47-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
48-
name = "coder-${lower(random_string.random[0].result)}"
40+
count = data.coder_workspace.me.start_count
41+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
4942
machine_type = "e2-medium"
5043
network_interface {
5144
network = "default"
@@ -62,15 +55,7 @@ resource "google_compute_instance" "dev" {
6255
email = data.google_compute_default_service_account.default.email
6356
scopes = ["cloud-platform"]
6457
}
65-
metadata_startup_script = data.coder_agent_script.dev.value
66-
}
67-
68-
resource "coder_agent" "dev" {
69-
count = length(google_compute_instance.dev)
70-
auth {
71-
type = "google-instance-identity"
72-
instance_id = google_compute_instance.dev[0].instance_id
73-
}
58+
metadata_startup_script = coder_agent.dev.init_script
7459
}
7560
```
7661

@@ -79,4 +64,4 @@ resource "coder_agent" "dev" {
7964

8065
### Optional
8166

82-
- **url** (String) The URL to access Coder.
67+
- `url` (String) The URL to access Coder.

docs/resources/agent.md

+17-11
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ Use this resource to associate an agent.
1313
## Example Usage
1414

1515
```terraform
16-
data "coder_agent_script" "dev" {
17-
os = "linux"
18-
arch = "amd64"
16+
data "coder_workspace" "me" {
1917
}
2018
2119
resource "coder_agent" "dev" {
22-
startup_script = "code-server"
20+
os = "linux"
21+
arch = "amd64"
2322
}
2423
25-
resource "google_compute_instance" "dev" {
24+
resource "kubernetes_pod" "dev" {
25+
count = data.coder_workspace.me.start_count
2626
spec {
2727
container {
28-
command = ["sh", "-c", data.coder_agent_script.dev.value]
28+
command = ["sh", "-c", coder_agent.dev.init_script]
2929
env {
3030
name = "CODER_TOKEN"
3131
value = coder_agent.dev.token
@@ -38,15 +38,21 @@ resource "google_compute_instance" "dev" {
3838
<!-- schema generated by tfplugindocs -->
3939
## Schema
4040

41+
### Required
42+
43+
- `arch` (String) The architecture the agent will run on. Must be one of: "amd64", "arm64".
44+
- `os` (String) The operating system the agent will run on. Must be one of: "linux", "darwin", or "windows".
45+
4146
### Optional
4247

43-
- **env** (Map of String) A mapping of environment variables to set inside the workspace.
44-
- **id** (String) The ID of this resource.
45-
- **instance_id** (String) An instance ID from a provisioned instance to enable zero-trust agent authentication.
46-
- **startup_script** (String) A script to run after the agent starts.
48+
- `auth` (String) The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity".
49+
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
50+
- `id` (String) The ID of this resource.
51+
- `startup_script` (String) A script to run after the agent starts.
4752

4853
### Read-Only
4954

50-
- **token** (String) Set the environment variable "CODER_TOKEN" with this token to authenticate an agent.
55+
- `init_script` (String) Run this script on startup of an instance to initialize the agent.
56+
- `token` (String) Set the environment variable "CODER_TOKEN" with this token to authenticate an agent.
5157

5258

docs/resources/agent_instance.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_agent_instance Resource - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Use this resource to associate an instance ID with an agent for zero-trust authentication. This association is done automatically for "googlecomputeinstance", "awsinstance", "azurermlinuxvirtualmachine", and "azurermwindowsvirtual_machine" resources.
7+
---
8+
9+
# coder_agent_instance (Resource)
10+
11+
Use this resource to associate an instance ID with an agent for zero-trust authentication. This association is done automatically for "google_compute_instance", "aws_instance", "azurerm_linux_virtual_machine", and "azurerm_windows_virtual_machine" resources.
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "coder_agent" "dev" {
17+
os = "linux"
18+
arch = "amd64"
19+
auth = "google-instance-identity"
20+
}
21+
22+
resource "google_compute_instance" "dev" {
23+
zone = "us-central1-a"
24+
}
25+
26+
resource "coder_agent_instance" "dev" {
27+
agent_id = coder_agent.dev.id
28+
instance_id = google_compute_instance.dev.instance_id
29+
}
30+
```
31+
32+
<!-- schema generated by tfplugindocs -->
33+
## Schema
34+
35+
### Required
36+
37+
- `agent_id` (String) The "id" property of a "coder_agent" resource to associate with.
38+
- `instance_id` (String) The instance identifier of a provisioned resource.
39+
40+
### Optional
41+
42+
- `id` (String) The ID of this resource.
43+
44+

examples/data-sources/coder_agent_script/data-source.tf

-12
This file was deleted.

examples/provider/provider.tf

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
variable "gcp_credentials" {
2-
sensitive = true
3-
}
4-
51
terraform {
62
required_providers {
73
coder = {
@@ -11,26 +7,23 @@ terraform {
117
}
128

139
provider "google" {
14-
region = "us-central1"
15-
credentials = var.gcp_credentials
10+
region = "us-central1"
1611
}
1712

1813
data "coder_workspace" "me" {}
19-
data "google_compute_default_service_account" "default" {}
20-
data "coder_agent_script" "dev" {
14+
15+
resource "coder_agent" "dev" {
2116
arch = "amd64"
2217
os = "linux"
23-
}
24-
resource "random_string" "random" {
25-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
26-
length = 8
27-
special = false
18+
auth = "google-instance-identity"
2819
}
2920

21+
data "google_compute_default_service_account" "default" {}
22+
3023
resource "google_compute_instance" "dev" {
3124
zone = "us-central1-a"
32-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
33-
name = "coder-${lower(random_string.random[0].result)}"
25+
count = data.coder_workspace.me.start_count
26+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
3427
machine_type = "e2-medium"
3528
network_interface {
3629
network = "default"
@@ -47,13 +40,5 @@ resource "google_compute_instance" "dev" {
4740
email = data.google_compute_default_service_account.default.email
4841
scopes = ["cloud-platform"]
4942
}
50-
metadata_startup_script = data.coder_agent_script.dev.value
51-
}
52-
53-
resource "coder_agent" "dev" {
54-
count = length(google_compute_instance.dev)
55-
auth {
56-
type = "google-instance-identity"
57-
instance_id = google_compute_instance.dev[0].instance_id
58-
}
43+
metadata_startup_script = coder_agent.dev.init_script
5944
}

examples/resources/coder_agent/resource.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
data "coder_agent_script" "dev" {
2-
os = "linux"
3-
arch = "amd64"
1+
data "coder_workspace" "me" {
42
}
53

64
resource "coder_agent" "dev" {
7-
startup_script = "code-server"
5+
os = "linux"
6+
arch = "amd64"
87
}
98

10-
resource "google_compute_instance" "dev" {
9+
resource "kubernetes_pod" "dev" {
10+
count = data.coder_workspace.me.start_count
1111
spec {
1212
container {
13-
command = ["sh", "-c", data.coder_agent_script.dev.value]
13+
command = ["sh", "-c", coder_agent.dev.init_script]
1414
env {
1515
name = "CODER_TOKEN"
1616
value = coder_agent.dev.token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "coder_agent" "dev" {
2+
os = "linux"
3+
arch = "amd64"
4+
auth = "google-instance-identity"
5+
}
6+
7+
resource "google_compute_instance" "dev" {
8+
zone = "us-central1-a"
9+
}
10+
11+
resource "coder_agent_instance" "dev" {
12+
agent_id = coder_agent.dev.id
13+
instance_id = google_compute_instance.dev.instance_id
14+
}

0 commit comments

Comments
 (0)