Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions integration/coder-ai-task/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
}
local = {
source = "hashicorp/local"
}
}
}

data "coder_workspace" "me" {}

resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
dir = "/workspace"
}

resource "coder_app" "ai_interface" {
agent_id = coder_agent.dev.id
slug = "ai-chat"
share = "owner"
url = "http://localhost:8080"
}

data "coder_parameter" "ai_prompt" {
type = "string"
name = "AI Prompt"
default = ""
description = "Write a prompt for Claude Code"
mutable = true
}

resource "coder_ai_task" "task" {
sidebar_app {
id = coder_app.ai_interface.id
}
}

locals {
# NOTE: these must all be strings in the output
output = {
"ai_task.id" = coder_ai_task.task.id
"ai_task.app_id" = coder_ai_task.task.app_id
"ai_task.prompt" = coder_ai_task.task.prompt
"app.id" = coder_app.ai_interface.id
}
}

variable "output_path" {
type = string
}

resource "local_file" "output" {
filename = var.output_path
content = jsonencode(local.output)
}

output "output" {
value = local.output
sensitive = true
}
1 change: 1 addition & 0 deletions integration/coder-ai-task/parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AI Prompt: "some prompt"
11 changes: 11 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,24 @@ func TestIntegration(t *testing.T) {
"coder_app.defaulted.hidden": "false",
},
},
{
name: "coder-ai-task",
minVersion: "v2.26.0",
expectedOutput: map[string]string{
"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}$`,
"ai_task.prompt": "default",
"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}$`,
"app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if coderVersion != "latest" && semver.Compare(coderVersion, tt.minVersion) < 0 {
t.Skipf("skipping due to CODER_VERSION %q < minVersion %q", coderVersion, tt.minVersion)
}

// Given: we have an existing Coder deployment running locally
// Import named template

Expand Down
12 changes: 10 additions & 2 deletions provider/ai_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -27,10 +28,17 @@ func aiTaskResource() *schema.Resource {

Description: "Use this resource to define Coder tasks.",
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i any) diag.Diagnostics {
var diags diag.Diagnostics

if idStr := os.Getenv("CODER_TASK_ID"); idStr != "" {
resourceData.SetId(idStr)
} else {
return diag.Errorf("CODER_TASK_ID must be set")
resourceData.SetId(uuid.NewString())

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
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.",
})
}

if prompt := os.Getenv("CODER_TASK_PROMPT"); prompt != "" {
Expand Down Expand Up @@ -58,7 +66,7 @@ func aiTaskResource() *schema.Resource {
return diag.Errorf("'app_id' must be set")
}

return nil
return diags
},
ReadContext: schema.NoopContext,
DeleteContext: schema.NoopContext,
Expand Down