Skip to content

Commit 4128618

Browse files
Update coder-login module to use coder_env resources
- Replaced coder_script resource with coder_env resources for CODER_SESSION_TOKEN and CODER_URL - Removed run.sh script file in favor of native Terraform provider resources - Added comprehensive Terraform tests with mocked data validation - Uses correct environment variable names as per coder CLI documentation - Cleaner approach using native Terraform provider resources Co-authored-by: Atif Ali <atif@coder.com>
1 parent 97b036e commit 4128618

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

registry/coder/modules/coder-login/main.tf

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ variable "agent_id" {
1717
data "coder_workspace" "me" {}
1818
data "coder_workspace_owner" "me" {}
1919

20-
resource "coder_script" "coder-login" {
20+
resource "coder_env" "coder_session_token" {
2121
agent_id = var.agent_id
22-
script = templatefile("${path.module}/run.sh", {
23-
CODER_USER_TOKEN : data.coder_workspace_owner.me.session_token,
24-
CODER_DEPLOYMENT_URL : data.coder_workspace.me.access_url
25-
})
26-
display_name = "Coder Login"
27-
icon = "/icon/coder.svg"
28-
run_on_start = true
29-
start_blocks_login = true
22+
name = "CODER_SESSION_TOKEN"
23+
value = data.coder_workspace_owner.me.session_token
3024
}
3125

26+
resource "coder_env" "coder_url" {
27+
agent_id = var.agent_id
28+
name = "CODER_URL"
29+
value = data.coder_workspace.me.access_url
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Test for coder-login module
2+
3+
run "test_coder_login_module" {
4+
command = plan
5+
6+
variables {
7+
agent_id = "test-agent-id"
8+
}
9+
10+
# Test that the coder_env resources are created with correct configuration
11+
assert {
12+
condition = coder_env.coder_session_token.agent_id == "test-agent-id"
13+
error_message = "CODER_SESSION_TOKEN agent ID should match the input variable"
14+
}
15+
16+
assert {
17+
condition = coder_env.coder_session_token.name == "CODER_SESSION_TOKEN"
18+
error_message = "Environment variable name should be 'CODER_SESSION_TOKEN'"
19+
}
20+
21+
assert {
22+
condition = coder_env.coder_url.agent_id == "test-agent-id"
23+
error_message = "CODER_URL agent ID should match the input variable"
24+
}
25+
26+
assert {
27+
condition = coder_env.coder_url.name == "CODER_URL"
28+
error_message = "Environment variable name should be 'CODER_URL'"
29+
}
30+
}
31+
32+
# Test with mock data sources
33+
run "test_with_mock_data" {
34+
command = plan
35+
36+
variables {
37+
agent_id = "mock-agent"
38+
}
39+
40+
# Mock the data sources for testing
41+
override_data {
42+
target = data.coder_workspace.me
43+
values = {
44+
access_url = "https://coder.example.com"
45+
}
46+
}
47+
48+
override_data {
49+
target = data.coder_workspace_owner.me
50+
values = {
51+
session_token = "mock-session-token"
52+
}
53+
}
54+
55+
# Verify environment variables get the mocked values
56+
assert {
57+
condition = coder_env.coder_url.value == "https://coder.example.com"
58+
error_message = "CODER_URL should match workspace access_url"
59+
}
60+
61+
assert {
62+
condition = coder_env.coder_session_token.value == "mock-session-token"
63+
error_message = "CODER_SESSION_TOKEN should match workspace owner session_token"
64+
}
65+
}

registry/coder/modules/coder-login/run.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)