From 37908cf4a0bf393cc20f6617b85e1281be9a1bf9 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Mon, 3 Nov 2025 21:30:26 +0000 Subject: [PATCH 1/6] feat: add coder_ai_task_prompt data source --- docs/data-sources/ai_task_prompt.md | 22 ++++++++ integration/coder-ai-task/main.tf | 6 +++ integration/integration_test.go | 14 ++--- provider/ai_task.go | 41 +++++++++++++++ provider/ai_task_test.go | 81 +++++++++++++++++++++++++++++ provider/provider.go | 1 + provider/provider_test.go | 3 +- 7 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 docs/data-sources/ai_task_prompt.md diff --git a/docs/data-sources/ai_task_prompt.md b/docs/data-sources/ai_task_prompt.md new file mode 100644 index 00000000..5bd10f5a --- /dev/null +++ b/docs/data-sources/ai_task_prompt.md @@ -0,0 +1,22 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "coder_ai_task_prompt Data Source - terraform-provider-coder" +subcategory: "" +description: |- + Use this data source to read information about Coder tasks. +--- + +# coder_ai_task_prompt (Data Source) + +Use this data source to read information about Coder tasks. + + + + +## Schema + +### Read-Only + +- `enabled` (Boolean) True when executing in a Coder Task context, false when in a Coder Workspace context +- `id` (String) The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context. +- `value` (String) The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context. diff --git a/integration/coder-ai-task/main.tf b/integration/coder-ai-task/main.tf index aed9a796..f567ad70 100644 --- a/integration/coder-ai-task/main.tf +++ b/integration/coder-ai-task/main.tf @@ -32,6 +32,8 @@ data "coder_parameter" "ai_prompt" { mutable = true } +data "coder_ai_task_prompt" "me" {} + resource "coder_ai_task" "task" { sidebar_app { id = coder_app.ai_interface.id @@ -46,6 +48,10 @@ locals { "ai_task.prompt" = coder_ai_task.task.prompt "ai_task.enabled" = tostring(coder_ai_task.task.enabled) "app.id" = coder_app.ai_interface.id + + "ai_task_prompt.id" = data.coder_ai_task_prompt.me.id + "ai_task_prompt.value" = data.coder_ai_task_prompt.me.value + "ai_task_prompt.enabled" = tostring(data.coder_ai_task_prompt.me.enabled) } } diff --git a/integration/integration_test.go b/integration/integration_test.go index c86397da..7846222e 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -215,15 +215,17 @@ func TestIntegration(t *testing.T) { name: "coder-ai-task", minVersion: "v2.26.0", expectedOutput: map[string]string{ - "ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task.prompt": "", - "ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task.enabled": "false", - "app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task.prompt": "", + "ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task.enabled": "false", + "app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task_prompt.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task_prompt.value": "", + "ai_task_prompt.enabled": "false", }, }, } { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() if coderVersion != "latest" && semver.Compare(coderVersion, tt.minVersion) < 0 { diff --git a/provider/ai_task.go b/provider/ai_task.go index 01baf9ea..0eb5b8a7 100644 --- a/provider/ai_task.go +++ b/provider/ai_task.go @@ -115,3 +115,44 @@ func aiTaskResource() *schema.Resource { }, } } + +func aiTaskPromptDatasource() *schema.Resource { + return &schema.Resource{ + Description: "Use this data source to read information about Coder tasks.", + ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + diags := diag.Diagnostics{} + + idStr := os.Getenv("CODER_TASK_ID") + if idStr == "" || idStr == uuid.Nil.String() { + rd.SetId(uuid.NewString()) + _ = rd.Set("enabled", false) + } else if _, err := uuid.Parse(idStr); err == nil { + rd.SetId(idStr) + _ = rd.Set("enabled", true) + } else { // invalid UUID + diags = append(diags, errorAsDiagnostics(err)...) + } + + prompt := os.Getenv("CODER_TASK_PROMPT") + _ = rd.Set("value", prompt) + return diags + }, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + Description: "The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context.", + }, + "value": { + Type: schema.TypeString, + Computed: true, + Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.", + }, + "enabled": { + Type: schema.TypeBool, + Computed: true, + Description: "True when executing in a Coder Task context, false when in a Coder Workspace context", + }, + }, + } +} diff --git a/provider/ai_task_test.go b/provider/ai_task_test.go index 3108fb9d..751f69e1 100644 --- a/provider/ai_task_test.go +++ b/provider/ai_task_test.go @@ -4,6 +4,7 @@ import ( "regexp" "testing" + "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/require" @@ -214,3 +215,83 @@ func TestAITask(t *testing.T) { }) }) } + +func TestAITaskPromptDatasource(t *testing.T) { + t.Run("Exists", func(t *testing.T) { + t.Setenv("CODER_TASK_ID", "7d8d4c2e-fb57-44f9-a183-22509819c2e7") + t.Setenv("CODER_TASK_PROMPT", "some task prompt") + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + provider "coder" {} + data "coder_ai_task_prompt" "me" {} + `, + Check: func(s *terraform.State) error { + require.Len(t, s.Modules, 1) + require.Len(t, s.Modules[0].Resources, 1) + resource := s.Modules[0].Resources["data.coder_ai_task_prompt.me"] + require.NotNil(t, resource) + + taskID := resource.Primary.Attributes["id"] + require.Equal(t, "7d8d4c2e-fb57-44f9-a183-22509819c2e7", taskID) + + taskPromptValue := resource.Primary.Attributes["value"] + require.Equal(t, "some task prompt", taskPromptValue) + + enabledValue := resource.Primary.Attributes["enabled"] + require.Equal(t, "true", enabledValue) + return nil + }, + }}, + }) + }) + + t.Run("NotExists", func(t *testing.T) { + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + provider "coder" {} + data "coder_ai_task_prompt" "me" {} + `, + Check: func(s *terraform.State) error { + require.Len(t, s.Modules, 1) + require.Len(t, s.Modules[0].Resources, 1) + resource := s.Modules[0].Resources["data.coder_ai_task_prompt.me"] + require.NotNil(t, resource) + + taskID := resource.Primary.Attributes["id"] + require.NotEmpty(t, taskID) + require.NotEqual(t, uuid.Nil.String(), taskID) + _, err := uuid.Parse(taskID) + require.NoError(t, err) + + taskPromptValue := resource.Primary.Attributes["value"] + require.Empty(t, taskPromptValue) + + enabledValue := resource.Primary.Attributes["enabled"] + require.Equal(t, "false", enabledValue) + return nil + }, + }}, + }) + }) + + t.Run("InvalidTaskID", func(t *testing.T) { + t.Setenv("CODER_TASK_ID", "not a valid UUID") + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + provider "coder" {} + data "coder_ai_task_prompt" "me" {} + `, + ExpectError: regexp.MustCompile(`invalid UUID`), + }}, + }) + }) +} diff --git a/provider/provider.go b/provider/provider.go index 2b6409ba..3507c1b2 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -64,6 +64,7 @@ func New() *schema.Provider { "coder_external_auth": externalAuthDataSource(), "coder_workspace_owner": workspaceOwnerDataSource(), "coder_workspace_preset": workspacePresetDataSource(), + "coder_ai_task_prompt": aiTaskPromptDatasource(), }, ResourcesMap: map[string]*schema.Resource{ "coder_agent": agentResource(), diff --git a/provider/provider_test.go b/provider/provider_test.go index 4bf98b32..9cc02659 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -37,7 +37,8 @@ func TestProviderEmpty(t *testing.T) { } data "coder_parameter" "param" { name = "hey" - }`, + } + data "coder_ai_task_prompt" "me" {}`, Check: func(state *terraform.State) error { return nil }, From 51684f4ae16b88b007ef4c3c68cf97d4b7461307 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 4 Nov 2025 09:12:15 +0000 Subject: [PATCH 2/6] add version note --- docs/data-sources/ai_task_prompt.md | 6 +++++- provider/ai_task.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/data-sources/ai_task_prompt.md b/docs/data-sources/ai_task_prompt.md index 5bd10f5a..55c6c131 100644 --- a/docs/data-sources/ai_task_prompt.md +++ b/docs/data-sources/ai_task_prompt.md @@ -17,6 +17,10 @@ Use this data source to read information about Coder tasks. ### Read-Only -- `enabled` (Boolean) True when executing in a Coder Task context, false when in a Coder Workspace context +- `enabled` (Boolean) True when executing in a Coder Task context, false when in a Coder Workspace context. + +->This field is only populated in Coder v2.28 and later. - `id` (String) The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context. - `value` (String) The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context. + +->This field is only populated in Coder v2.28 and later. diff --git a/provider/ai_task.go b/provider/ai_task.go index 0eb5b8a7..a6ffcbe3 100644 --- a/provider/ai_task.go +++ b/provider/ai_task.go @@ -146,12 +146,12 @@ func aiTaskPromptDatasource() *schema.Resource { "value": { Type: schema.TypeString, Computed: true, - Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.", + Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.\n\n->This field is only populated in Coder v2.28 and later.", }, "enabled": { Type: schema.TypeBool, Computed: true, - Description: "True when executing in a Coder Task context, false when in a Coder Workspace context", + Description: "True when executing in a Coder Task context, false when in a Coder Workspace context.\n\n->This field is only populated in Coder v2.28 and later.", }, }, } From a27420c24f715be9e7a792a1e47f8d519163d00e Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 4 Nov 2025 09:47:44 +0000 Subject: [PATCH 3/6] rename datasource coder_ai_task_prompt -> coder_task --- docs/data-sources/{ai_task_prompt.md => task.md} | 6 +++--- integration/coder-ai-task/main.tf | 8 ++++---- integration/integration_test.go | 16 ++++++++-------- provider/ai_task.go | 7 +++---- provider/ai_task_test.go | 14 +++++++------- provider/helpers/validation.go | 2 +- provider/provider.go | 2 +- provider/provider_test.go | 2 +- provider/script_test.go | 8 ++++---- 9 files changed, 32 insertions(+), 33 deletions(-) rename docs/data-sources/{ai_task_prompt.md => task.md} (71%) diff --git a/docs/data-sources/ai_task_prompt.md b/docs/data-sources/task.md similarity index 71% rename from docs/data-sources/ai_task_prompt.md rename to docs/data-sources/task.md index 55c6c131..8a9c3848 100644 --- a/docs/data-sources/ai_task_prompt.md +++ b/docs/data-sources/task.md @@ -1,12 +1,12 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "coder_ai_task_prompt Data Source - terraform-provider-coder" +page_title: "coder_task Data Source - terraform-provider-coder" subcategory: "" description: |- Use this data source to read information about Coder tasks. --- -# coder_ai_task_prompt (Data Source) +# coder_task (Data Source) Use this data source to read information about Coder tasks. @@ -21,6 +21,6 @@ Use this data source to read information about Coder tasks. ->This field is only populated in Coder v2.28 and later. - `id` (String) The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context. -- `value` (String) The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context. +- `prompt` (String) The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context. ->This field is only populated in Coder v2.28 and later. diff --git a/integration/coder-ai-task/main.tf b/integration/coder-ai-task/main.tf index f567ad70..50e5289d 100644 --- a/integration/coder-ai-task/main.tf +++ b/integration/coder-ai-task/main.tf @@ -32,7 +32,7 @@ data "coder_parameter" "ai_prompt" { mutable = true } -data "coder_ai_task_prompt" "me" {} +data "coder_task" "me" {} resource "coder_ai_task" "task" { sidebar_app { @@ -49,9 +49,9 @@ locals { "ai_task.enabled" = tostring(coder_ai_task.task.enabled) "app.id" = coder_app.ai_interface.id - "ai_task_prompt.id" = data.coder_ai_task_prompt.me.id - "ai_task_prompt.value" = data.coder_ai_task_prompt.me.value - "ai_task_prompt.enabled" = tostring(data.coder_ai_task_prompt.me.enabled) + "task.id" = data.coder_task.me.id + "task.prompt" = data.coder_task.me.prompt + "task.enabled" = tostring(data.coder_task.me.enabled) } } diff --git a/integration/integration_test.go b/integration/integration_test.go index 7846222e..bc6fac96 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -215,14 +215,14 @@ func TestIntegration(t *testing.T) { name: "coder-ai-task", minVersion: "v2.26.0", expectedOutput: map[string]string{ - "ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task.prompt": "", - "ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task.enabled": "false", - "app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task_prompt.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, - "ai_task_prompt.value": "", - "ai_task_prompt.enabled": "false", + "ai_task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task.prompt": "", + "ai_task.app_id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "ai_task.enabled": "false", + "app.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "task.id": `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, + "task.prompt": "", + "task.enabled": "false", }, }, } { diff --git a/provider/ai_task.go b/provider/ai_task.go index a6ffcbe3..b11f243d 100644 --- a/provider/ai_task.go +++ b/provider/ai_task.go @@ -116,7 +116,7 @@ func aiTaskResource() *schema.Resource { } } -func aiTaskPromptDatasource() *schema.Resource { +func aiTaskDatasource() *schema.Resource { return &schema.Resource{ Description: "Use this data source to read information about Coder tasks.", ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { @@ -133,8 +133,7 @@ func aiTaskPromptDatasource() *schema.Resource { diags = append(diags, errorAsDiagnostics(err)...) } - prompt := os.Getenv("CODER_TASK_PROMPT") - _ = rd.Set("value", prompt) + _ = rd.Set("prompt", os.Getenv("CODER_TASK_PROMPT")) return diags }, Schema: map[string]*schema.Schema{ @@ -143,7 +142,7 @@ func aiTaskPromptDatasource() *schema.Resource { Computed: true, Description: "The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context.", }, - "value": { + "prompt": { Type: schema.TypeString, Computed: true, Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.\n\n->This field is only populated in Coder v2.28 and later.", diff --git a/provider/ai_task_test.go b/provider/ai_task_test.go index 751f69e1..9a27c82c 100644 --- a/provider/ai_task_test.go +++ b/provider/ai_task_test.go @@ -226,18 +226,18 @@ func TestAITaskPromptDatasource(t *testing.T) { Steps: []resource.TestStep{{ Config: ` provider "coder" {} - data "coder_ai_task_prompt" "me" {} + data "coder_task" "me" {} `, Check: func(s *terraform.State) error { require.Len(t, s.Modules, 1) require.Len(t, s.Modules[0].Resources, 1) - resource := s.Modules[0].Resources["data.coder_ai_task_prompt.me"] + resource := s.Modules[0].Resources["data.coder_task.me"] require.NotNil(t, resource) taskID := resource.Primary.Attributes["id"] require.Equal(t, "7d8d4c2e-fb57-44f9-a183-22509819c2e7", taskID) - taskPromptValue := resource.Primary.Attributes["value"] + taskPromptValue := resource.Primary.Attributes["prompt"] require.Equal(t, "some task prompt", taskPromptValue) enabledValue := resource.Primary.Attributes["enabled"] @@ -255,12 +255,12 @@ func TestAITaskPromptDatasource(t *testing.T) { Steps: []resource.TestStep{{ Config: ` provider "coder" {} - data "coder_ai_task_prompt" "me" {} + data "coder_task" "me" {} `, Check: func(s *terraform.State) error { require.Len(t, s.Modules, 1) require.Len(t, s.Modules[0].Resources, 1) - resource := s.Modules[0].Resources["data.coder_ai_task_prompt.me"] + resource := s.Modules[0].Resources["data.coder_task.me"] require.NotNil(t, resource) taskID := resource.Primary.Attributes["id"] @@ -269,7 +269,7 @@ func TestAITaskPromptDatasource(t *testing.T) { _, err := uuid.Parse(taskID) require.NoError(t, err) - taskPromptValue := resource.Primary.Attributes["value"] + taskPromptValue := resource.Primary.Attributes["prompt"] require.Empty(t, taskPromptValue) enabledValue := resource.Primary.Attributes["enabled"] @@ -288,7 +288,7 @@ func TestAITaskPromptDatasource(t *testing.T) { Steps: []resource.TestStep{{ Config: ` provider "coder" {} - data "coder_ai_task_prompt" "me" {} + data "coder_task" "me" {} `, ExpectError: regexp.MustCompile(`invalid UUID`), }}, diff --git a/provider/helpers/validation.go b/provider/helpers/validation.go index 9cc21b89..e58a3b03 100644 --- a/provider/helpers/validation.go +++ b/provider/helpers/validation.go @@ -17,6 +17,6 @@ func ValidateURL(value any, label string) ([]string, []error) { if _, err := url.Parse(val); err != nil { return nil, []error{err} } - + return nil, nil } diff --git a/provider/provider.go b/provider/provider.go index 3507c1b2..4b3e767a 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -64,7 +64,7 @@ func New() *schema.Provider { "coder_external_auth": externalAuthDataSource(), "coder_workspace_owner": workspaceOwnerDataSource(), "coder_workspace_preset": workspacePresetDataSource(), - "coder_ai_task_prompt": aiTaskPromptDatasource(), + "coder_task": aiTaskDatasource(), }, ResourcesMap: map[string]*schema.Resource{ "coder_agent": agentResource(), diff --git a/provider/provider_test.go b/provider/provider_test.go index 9cc02659..606ae72b 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -38,7 +38,7 @@ func TestProviderEmpty(t *testing.T) { data "coder_parameter" "param" { name = "hey" } - data "coder_ai_task_prompt" "me" {}`, + data "coder_task" "me" {}`, Check: func(state *terraform.State) error { return nil }, diff --git a/provider/script_test.go b/provider/script_test.go index 64808372..8e6221f1 100644 --- a/provider/script_test.go +++ b/provider/script_test.go @@ -131,10 +131,10 @@ func TestValidateCronExpression(t *testing.T) { t.Parallel() tests := []struct { - name string - cronExpr string - expectWarnings bool - expectErrors bool + name string + cronExpr string + expectWarnings bool + expectErrors bool warningContains string }{ { From 1983ed0235863285857f296dc65d625341f1fb0a Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 4 Nov 2025 09:52:53 +0000 Subject: [PATCH 4/6] add example --- docs/data-sources/task.md | 17 +++++++++++++++++ examples/data-sources/coder_task/data-source.tf | 14 ++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 examples/data-sources/coder_task/data-source.tf diff --git a/docs/data-sources/task.md b/docs/data-sources/task.md index 8a9c3848..3c56d45c 100644 --- a/docs/data-sources/task.md +++ b/docs/data-sources/task.md @@ -10,7 +10,24 @@ description: |- Use this data source to read information about Coder tasks. +## Example Usage +```terraform +provider "coder" {} + +data "coder_workspace" "me" {} +data "coder_task" "me" {} + +resource "coder_ai_task" "task" { + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + app_id = module.example-agent.task_app_id +} + +module "example-agent" { + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + prompt = data.coder_ai_task.me.prompt +} +``` ## Schema diff --git a/examples/data-sources/coder_task/data-source.tf b/examples/data-sources/coder_task/data-source.tf new file mode 100644 index 00000000..af2098e1 --- /dev/null +++ b/examples/data-sources/coder_task/data-source.tf @@ -0,0 +1,14 @@ +provider "coder" {} + +data "coder_workspace" "me" {} +data "coder_task" "me" {} + +resource "coder_ai_task" "task" { + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + app_id = module.example-agent.task_app_id +} + +module "example-agent" { + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + prompt = data.coder_ai_task.me.prompt +} From 11bc99489016f29efcf97dbdb322d0879973a1dc Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 4 Nov 2025 10:02:31 +0000 Subject: [PATCH 5/6] address PR comments --- docs/data-sources/task.md | 12 ++++++------ provider/ai_task.go | 4 ++-- provider/ai_task_test.go | 2 +- provider/provider.go | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/data-sources/task.md b/docs/data-sources/task.md index 3c56d45c..11c911d0 100644 --- a/docs/data-sources/task.md +++ b/docs/data-sources/task.md @@ -3,12 +3,12 @@ page_title: "coder_task Data Source - terraform-provider-coder" subcategory: "" description: |- - Use this data source to read information about Coder tasks. + Use this data source to read information about Coder Tasks. --- # coder_task (Data Source) -Use this data source to read information about Coder tasks. +Use this data source to read information about Coder Tasks. ## Example Usage @@ -19,13 +19,13 @@ data "coder_workspace" "me" {} data "coder_task" "me" {} resource "coder_ai_task" "task" { - count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 - app_id = module.example-agent.task_app_id + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + app_id = module.example-agent.task_app_id } module "example-agent" { - count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 - prompt = data.coder_ai_task.me.prompt + count = data.coder_task.me.enabled ? data.coder_workspace.me.start_count : 0 + prompt = data.coder_ai_task.me.prompt } ``` diff --git a/provider/ai_task.go b/provider/ai_task.go index b11f243d..ce4fb9cf 100644 --- a/provider/ai_task.go +++ b/provider/ai_task.go @@ -116,9 +116,9 @@ func aiTaskResource() *schema.Resource { } } -func aiTaskDatasource() *schema.Resource { +func taskDatasource() *schema.Resource { return &schema.Resource{ - Description: "Use this data source to read information about Coder tasks.", + Description: "Use this data source to read information about Coder Tasks.", ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { diags := diag.Diagnostics{} diff --git a/provider/ai_task_test.go b/provider/ai_task_test.go index 9a27c82c..a67ba060 100644 --- a/provider/ai_task_test.go +++ b/provider/ai_task_test.go @@ -216,7 +216,7 @@ func TestAITask(t *testing.T) { }) } -func TestAITaskPromptDatasource(t *testing.T) { +func TestTaskDatasource(t *testing.T) { t.Run("Exists", func(t *testing.T) { t.Setenv("CODER_TASK_ID", "7d8d4c2e-fb57-44f9-a183-22509819c2e7") t.Setenv("CODER_TASK_PROMPT", "some task prompt") diff --git a/provider/provider.go b/provider/provider.go index 4b3e767a..7e4451b8 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -64,7 +64,7 @@ func New() *schema.Provider { "coder_external_auth": externalAuthDataSource(), "coder_workspace_owner": workspaceOwnerDataSource(), "coder_workspace_preset": workspacePresetDataSource(), - "coder_task": aiTaskDatasource(), + "coder_task": taskDatasource(), }, ResourcesMap: map[string]*schema.Resource{ "coder_agent": agentResource(), From 9ef5524db240b745b71dc3041e82f4cfc38f58f2 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 4 Nov 2025 10:15:13 +0000 Subject: [PATCH 6/6] adjust docs --- docs/data-sources/task.md | 4 ++-- provider/ai_task.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/data-sources/task.md b/docs/data-sources/task.md index 11c911d0..43396eae 100644 --- a/docs/data-sources/task.md +++ b/docs/data-sources/task.md @@ -36,8 +36,8 @@ module "example-agent" { - `enabled` (Boolean) True when executing in a Coder Task context, false when in a Coder Workspace context. -->This field is only populated in Coder v2.28 and later. + -> The `enabled` field is only populated in Coder v2.28 and later. - `id` (String) The UUID of the task, if executing in a Coder Task context. Empty in a Coder Workspace context. - `prompt` (String) The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context. -->This field is only populated in Coder v2.28 and later. + -> The `prompt` field is only populated in Coder v2.28 and later. diff --git a/provider/ai_task.go b/provider/ai_task.go index ce4fb9cf..281299b3 100644 --- a/provider/ai_task.go +++ b/provider/ai_task.go @@ -145,12 +145,12 @@ func taskDatasource() *schema.Resource { "prompt": { Type: schema.TypeString, Computed: true, - Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.\n\n->This field is only populated in Coder v2.28 and later.", + Description: "The prompt text provided to the task by Coder, if executing in a Coder Task context. Empty in a Coder Workspace context.\n\n -> The `prompt` field is only populated in Coder v2.28 and later.", }, "enabled": { Type: schema.TypeBool, Computed: true, - Description: "True when executing in a Coder Task context, false when in a Coder Workspace context.\n\n->This field is only populated in Coder v2.28 and later.", + Description: "True when executing in a Coder Task context, false when in a Coder Workspace context.\n\n -> The `enabled` field is only populated in Coder v2.28 and later.", }, }, }