Skip to content

feat: add coder_ai_task resource #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
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
31 changes: 31 additions & 0 deletions docs/resources/ai_task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coder_ai_task Resource - terraform-provider-coder"
subcategory: ""
description: |-
Use this resource to define Coder tasks.
---

# coder_ai_task (Resource)

Use this resource to define Coder tasks.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `sidebar_app` (Block Set, Min: 1, Max: 1) The coder_app to display in the sidebar. Usually a chat interface with the AI agent running in the workspace, like https://github.com/coder/agentapi. (see [below for nested schema](#nestedblock--sidebar_app))

### Read-Only

- `id` (String) A unique identifier for this resource.

<a id="nestedblock--sidebar_app"></a>
### Nested Schema for `sidebar_app`

Required:

- `id` (String) A reference to an existing `coder_app` resource in your template.
61 changes: 61 additions & 0 deletions provider/ai_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package provider

import (
"context"

"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"
)

type AITask struct {
ID string `mapstructure:"id"`
SidebarApp []AITaskSidebarApp `mapstructure:"sidebar_app"`
}

type AITaskSidebarApp struct {
ID string `mapstructure:"id"`
}

// TaskPromptParameterName is the name of the parameter which is *required* to be defined when a coder_ai_task is used.
const TaskPromptParameterName = "AI Prompt"

func aiTask() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,

Description: "Use this resource to define Coder tasks.",
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i any) diag.Diagnostics {
resourceData.SetId(uuid.NewString())
return nil
},
ReadContext: schema.NoopContext,
DeleteContext: schema.NoopContext,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "A unique identifier for this resource.",
Computed: true,
},
"sidebar_app": {
Type: schema.TypeSet,
Description: "The coder_app to display in the sidebar. Usually a chat interface with the AI agent running in the workspace, like https://github.com/coder/agentapi.",
ForceNew: true,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "A reference to an existing `coder_app` resource in your template.",
Required: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
},
},
},
},
}
}
82 changes: 82 additions & 0 deletions provider/ai_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package provider_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"
)

func TestAITask(t *testing.T) {
t.Parallel()

t.Run("OK", func(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
ProviderFactories: coderFactory(),
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
slug = "code-server"
display_name = "code-server"
icon = "builtin:vim"
url = "http://localhost:13337"
open_in = "slim-window"
}
resource "coder_ai_task" "test" {
sidebar_app {
id = coder_app.code-server.id
}
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
resource := state.Modules[0].Resources["coder_ai_task.test"]
require.NotNil(t, resource)
for _, key := range []string{
"id",
"sidebar_app.#",
} {
value := resource.Primary.Attributes[key]
require.NotNil(t, value)
require.Greater(t, len(value), 0)
}
require.Equal(t, "1", resource.Primary.Attributes["sidebar_app.#"])
return nil
},
}},
})
})

t.Run("InvalidSidebarAppID", func(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
ProviderFactories: coderFactory(),
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_ai_task" "test" {
sidebar_app {
id = "not-a-uuid"
}
}
`,
ExpectError: regexp.MustCompile(`expected "sidebar_app.0.id" to be a valid UUID`),
}},
})
})
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func New() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"coder_agent": agentResource(),
"coder_agent_instance": agentInstanceResource(),
"coder_ai_task": aiTask(),
"coder_app": appResource(),
"coder_metadata": metadataResource(),
"coder_script": scriptResource(),
Expand Down
Loading