Skip to content

Commit 9b25602

Browse files
feat: add enabled computed field to coder_ai_task
As discussed, we want to enable a way for consumers to know if their template is being provisioned as a task or not. We know that `CODER_TASK_ID` is set by the provisioner, it is being provisioned as a task, and not as a usual workspace. We use this knowledge to set a computed field `enabled` to reflect this. fix: tests fix: maybe fix? does this fix it?
1 parent fc9724b commit 9b25602

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

docs/resources/ai_task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Use this resource to define Coder tasks.
2222

2323
### Read-Only
2424

25+
- `enabled` (Boolean) True when executing in a Coder Task context, false when in a Coder Workspace context
2526
- `id` (String) A unique identifier for this resource.
2627
- `prompt` (String) The prompt text provided to the task by Coder.
2728

integration/coder-ai-task/main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ resource "coder_ai_task" "task" {
4141
locals {
4242
# NOTE: these must all be strings in the output
4343
output = {
44-
"ai_task.id" = coder_ai_task.task.id
45-
"ai_task.app_id" = coder_ai_task.task.app_id
46-
"ai_task.prompt" = coder_ai_task.task.prompt
47-
"app.id" = coder_app.ai_interface.id
44+
"ai_task.id" = coder_ai_task.task.id
45+
"ai_task.app_id" = coder_ai_task.task.app_id
46+
"ai_task.prompt" = coder_ai_task.task.prompt
47+
"ai_task.enabled" = tostring(coder_ai_task.task.enabled)
48+
"app.id" = coder_app.ai_interface.id
4849
}
4950
}
5051

integration/integration_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@ func TestIntegration(t *testing.T) {
215215
name: "coder-ai-task",
216216
minVersion: "v2.26.0",
217217
expectedOutput: map[string]string{
218-
"ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
219-
"ai_task.prompt": "",
220-
"ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
221-
"app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
218+
"ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
219+
"ai_task.prompt": "",
220+
"ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
221+
"ai_task.enabled": "false",
222+
"app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
222223
},
223224
},
224225
} {

provider/ai_task.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ func aiTaskResource() *schema.Resource {
3232
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i any) diag.Diagnostics {
3333
var diags diag.Diagnostics
3434

35-
if idStr := os.Getenv("CODER_TASK_ID"); idStr != "" {
36-
resourceData.SetId(idStr)
35+
if id, err := uuid.Parse(os.Getenv("CODER_TASK_ID")); err == nil && id != uuid.Nil {
36+
resourceData.SetId(id.String())
37+
resourceData.Set("enabled", true)
3738
} else {
3839
resourceData.SetId(uuid.NewString())
39-
40-
diags = append(diags, diag.Diagnostic{
41-
Severity: diag.Warning,
42-
Summary: "`CODER_TASK_ID` should be set. If you are seeing this message, the version of the Coder Terraform provider you are using is likely too new for your current Coder version.",
43-
})
40+
resourceData.Set("enabled", false)
4441
}
4542

4643
if prompt := os.Getenv("CODER_TASK_PROMPT"); prompt != "" {
@@ -110,6 +107,11 @@ func aiTaskResource() *schema.Resource {
110107
ValidateFunc: validation.IsUUID,
111108
ConflictsWith: []string{"sidebar_app"},
112109
},
110+
"enabled": {
111+
Type: schema.TypeBool,
112+
Description: "True when executing in a Coder Task context, false when in a Coder Workspace context",
113+
Computed: true,
114+
},
113115
},
114116
}
115117
}

provider/ai_task_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,62 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func TestAITask_Enabled(t *testing.T) {
13+
t.Run("EnabledWhenTask", func(t *testing.T) {
14+
t.Setenv("CODER_TASK_ID", "7d8d4c2e-fb57-44f9-a183-22509819c2e7")
15+
16+
resource.Test(t, resource.TestCase{
17+
ProviderFactories: coderFactory(),
18+
IsUnitTest: true,
19+
Steps: []resource.TestStep{{
20+
Config: `
21+
provider "coder" {
22+
}
23+
resource "coder_ai_task" "test" {
24+
app_id = "9a3ff7b4-4b3f-48c6-8d3a-a8118ac921fc"
25+
}
26+
`,
27+
Check: func(state *terraform.State) error {
28+
require.Len(t, state.Modules, 1)
29+
resource := state.Modules[0].Resources["coder_ai_task.test"]
30+
require.NotNil(t, resource)
31+
32+
require.Equal(t, "true", resource.Primary.Attributes["enabled"])
33+
34+
return nil
35+
},
36+
}},
37+
})
38+
})
39+
40+
t.Run("DisabledWhenWorkspace", func(t *testing.T) {
41+
t.Setenv("CODER_TASK_ID", "")
42+
43+
resource.Test(t, resource.TestCase{
44+
ProviderFactories: coderFactory(),
45+
IsUnitTest: true,
46+
Steps: []resource.TestStep{{
47+
Config: `
48+
provider "coder" {
49+
}
50+
resource "coder_ai_task" "test" {
51+
app_id = "9a3ff7b4-4b3f-48c6-8d3a-a8118ac921fc"
52+
}
53+
`,
54+
Check: func(state *terraform.State) error {
55+
require.Len(t, state.Modules, 1)
56+
resource := state.Modules[0].Resources["coder_ai_task.test"]
57+
require.NotNil(t, resource)
58+
59+
require.Equal(t, "false", resource.Primary.Attributes["enabled"])
60+
61+
return nil
62+
},
63+
}},
64+
})
65+
})
66+
}
67+
1268
func TestAITask(t *testing.T) {
1369
t.Setenv("CODER_TASK_ID", "7d8d4c2e-fb57-44f9-a183-22509819c2e7")
1470
t.Setenv("CODER_TASK_PROMPT", "some task prompt")
@@ -35,6 +91,7 @@ func TestAITask(t *testing.T) {
3591
"id",
3692
"prompt",
3793
"app_id",
94+
"enabled",
3895
} {
3996
value := resource.Primary.Attributes[key]
4097
require.NotNil(t, value)
@@ -97,6 +154,7 @@ func TestAITask(t *testing.T) {
97154
"id",
98155
"prompt",
99156
"app_id",
157+
"enabled",
100158
} {
101159
value := resource.Primary.Attributes[key]
102160
require.NotNil(t, value)

0 commit comments

Comments
 (0)