Skip to content

Commit eb191d3

Browse files
fix: make ai_task.id change backwards compatible (#447)
Updates the create logic to warn instead of error when no CODER_TASK_ID is set so that we are backwards compatible.
1 parent 7ecb65d commit eb191d3

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

integration/coder-ai-task/main.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_workspace" "me" {}
13+
14+
resource "coder_agent" "dev" {
15+
os = "linux"
16+
arch = "amd64"
17+
dir = "/workspace"
18+
}
19+
20+
resource "coder_app" "ai_interface" {
21+
agent_id = coder_agent.dev.id
22+
slug = "ai-chat"
23+
share = "owner"
24+
url = "http://localhost:8080"
25+
}
26+
27+
data "coder_parameter" "ai_prompt" {
28+
type = "string"
29+
name = "AI Prompt"
30+
default = ""
31+
description = "Write a prompt for Claude Code"
32+
mutable = true
33+
}
34+
35+
resource "coder_ai_task" "task" {
36+
sidebar_app {
37+
id = coder_app.ai_interface.id
38+
}
39+
}
40+
41+
locals {
42+
# NOTE: these must all be strings in the output
43+
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
48+
}
49+
}
50+
51+
variable "output_path" {
52+
type = string
53+
}
54+
55+
resource "local_file" "output" {
56+
filename = var.output_path
57+
content = jsonencode(local.output)
58+
}
59+
60+
output "output" {
61+
value = local.output
62+
sensitive = true
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AI Prompt: "some prompt"

integration/integration_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,24 @@ func TestIntegration(t *testing.T) {
211211
"coder_app.defaulted.hidden": "false",
212212
},
213213
},
214+
{
215+
name: "coder-ai-task",
216+
minVersion: "v2.26.0",
217+
expectedOutput: map[string]string{
218+
"ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
219+
"ai_task.prompt": "default",
220+
"ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
221+
"app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
222+
},
223+
},
214224
} {
215225
tt := tt
216226
t.Run(tt.name, func(t *testing.T) {
217227
t.Parallel()
218228
if coderVersion != "latest" && semver.Compare(coderVersion, tt.minVersion) < 0 {
219229
t.Skipf("skipping due to CODER_VERSION %q < minVersion %q", coderVersion, tt.minVersion)
220230
}
231+
221232
// Given: we have an existing Coder deployment running locally
222233
// Import named template
223234

provider/ai_task.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66

7+
"github.com/google/uuid"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -29,10 +30,17 @@ func aiTaskResource() *schema.Resource {
2930

3031
Description: "Use this resource to define Coder tasks.",
3132
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i any) diag.Diagnostics {
33+
var diags diag.Diagnostics
34+
3235
if idStr := os.Getenv("CODER_TASK_ID"); idStr != "" {
3336
resourceData.SetId(idStr)
3437
} else {
35-
return diag.Errorf("CODER_TASK_ID must be set")
38+
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+
})
3644
}
3745

3846
if prompt := os.Getenv("CODER_TASK_PROMPT"); prompt != "" {
@@ -60,7 +68,7 @@ func aiTaskResource() *schema.Resource {
6068
return diag.Errorf("'app_id' must be set")
6169
}
6270

63-
return nil
71+
return diags
6472
},
6573
ReadContext: schema.NoopContext,
6674
DeleteContext: schema.NoopContext,

0 commit comments

Comments
 (0)