From 8881ae0416a32be3411ee17533419813b4fc206e Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 28 Jan 2025 12:26:38 +0000 Subject: [PATCH 01/10] add a workspace preset datasource --- provider/provider.go | 13 ++++++------ provider/workspace_preset.go | 34 ++++++++++++++++++++++++++++++ provider/workspace_preset_test.go | 35 +++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 provider/workspace_preset.go create mode 100644 provider/workspace_preset_test.go diff --git a/provider/provider.go b/provider/provider.go index 1d78f2dd..d9780d76 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -61,12 +61,13 @@ func New() *schema.Provider { }, nil }, DataSourcesMap: map[string]*schema.Resource{ - "coder_workspace": workspaceDataSource(), - "coder_workspace_tags": workspaceTagDataSource(), - "coder_provisioner": provisionerDataSource(), - "coder_parameter": parameterDataSource(), - "coder_external_auth": externalAuthDataSource(), - "coder_workspace_owner": workspaceOwnerDataSource(), + "coder_workspace": workspaceDataSource(), + "coder_workspace_tags": workspaceTagDataSource(), + "coder_provisioner": provisionerDataSource(), + "coder_parameter": parameterDataSource(), + "coder_external_auth": externalAuthDataSource(), + "coder_workspace_owner": workspaceOwnerDataSource(), + "coder_workspace_preset": workspacePresetDataSource(), }, ResourcesMap: map[string]*schema.Resource{ "coder_agent": agentResource(), diff --git a/provider/workspace_preset.go b/provider/workspace_preset.go new file mode 100644 index 00000000..60e94cf6 --- /dev/null +++ b/provider/workspace_preset.go @@ -0,0 +1,34 @@ +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" +) + +func workspacePresetDataSource() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Description: "", + ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + rd.SetId(uuid.NewString()) + + return nil + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Description: "Name of the workspace preset.", + Required: true, + }, + "parameters": { + Type: schema.TypeMap, + Description: "Parameters of the workspace preset.", + Required: true, + }, + }, + } +} diff --git a/provider/workspace_preset_test.go b/provider/workspace_preset_test.go new file mode 100644 index 00000000..c4fba55b --- /dev/null +++ b/provider/workspace_preset_test.go @@ -0,0 +1,35 @@ +package provider_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/stretchr/testify/require" +) + +func TestWorkspacePreset(t *testing.T) { + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = "preset_1" + parameters = { + "region" = "us-east1-a" + } + }`, + Check: func(state *terraform.State) error { + require.Len(t, state.Modules, 1) + require.Len(t, state.Modules[0].Resources, 1) + resource := state.Modules[0].Resources["data.coder_workspace_preset.preset_1"] + require.NotNil(t, resource) + attrs := resource.Primary.Attributes + require.Equal(t, attrs["name"], "preset_1") + require.Equal(t, attrs["parameters.region"], "us-east1-a") + return nil + }, + }}, + }) +} From af7a1ed4e4ddc67ee2f753b5c79852563739dcd7 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Wed, 29 Jan 2025 08:45:17 +0000 Subject: [PATCH 02/10] add workspace preset type --- provider/workspace_preset.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/provider/workspace_preset.go b/provider/workspace_preset.go index 60e94cf6..04f4d759 100644 --- a/provider/workspace_preset.go +++ b/provider/workspace_preset.go @@ -8,6 +8,11 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) +type WorkspacePreset struct { + Name string `mapstructure:"name"` + Parameters map[string]string `mapstructure:"parameters"` +} + func workspacePresetDataSource() *schema.Resource { return &schema.Resource{ SchemaVersion: 1, From 2f0054689a2a0e6e684ce0f30f6dbf0adeb58ec3 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 31 Jan 2025 14:18:17 +0000 Subject: [PATCH 03/10] add validation and tests for coder_workspace_presets --- provider/workspace_preset.go | 31 ++++++++++- provider/workspace_preset_test.go | 92 +++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 3 deletions(-) diff --git a/provider/workspace_preset.go b/provider/workspace_preset.go index 04f4d759..dc14aab5 100644 --- a/provider/workspace_preset.go +++ b/provider/workspace_preset.go @@ -3,9 +3,9 @@ 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/mitchellh/mapstructure" ) type WorkspacePreset struct { @@ -17,13 +17,38 @@ func workspacePresetDataSource() *schema.Resource { return &schema.Resource{ SchemaVersion: 1, - Description: "", + Description: "Use this data source to predefine common configurations for workspaces.", ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { - rd.SetId(uuid.NewString()) + var preset WorkspacePreset + err := mapstructure.Decode(struct { + Name interface{} + Parameters interface{} + }{ + Name: rd.Get("name"), + Parameters: rd.Get("parameters"), + }, &preset) + if err != nil { + return diag.Errorf("decode workspace preset: %s", err) + } + + if preset.Name == "" { + return diag.Errorf("workspace preset name must be set") + } + + if len(preset.Parameters) == 0 { + return diag.Errorf("workspace preset must define a value for at least one parameter") + } + + rd.SetId(preset.Name) return nil }, Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Description: "ID of the workspace preset.", + Computed: true, + }, "name": { Type: schema.TypeString, Description: "Name of the workspace preset.", diff --git a/provider/workspace_preset_test.go b/provider/workspace_preset_test.go index c4fba55b..a24c615c 100644 --- a/provider/workspace_preset_test.go +++ b/provider/workspace_preset_test.go @@ -1,6 +1,7 @@ package provider_test import ( + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -9,6 +10,7 @@ import ( ) func TestWorkspacePreset(t *testing.T) { + // Happy Path: resource.Test(t, resource.TestCase{ ProviderFactories: coderFactory(), IsUnitTest: true, @@ -32,4 +34,94 @@ func TestWorkspacePreset(t *testing.T) { }, }}, }) + + // Given the Name field is not provided + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + parameters = { + "region" = "us-east1-a" + } + }`, + // This is from terraform's validation based on our schema, not based on our validation in ReadContext: + ExpectError: regexp.MustCompile("The argument \"name\" is required, but no definition was found"), + }}, + }) + + // Given the Name field is empty + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = "" + parameters = { + "region" = "us-east1-a" + } + }`, + ExpectError: regexp.MustCompile("workspace preset name must be set"), + }}, + }) + + // Given the Name field is not a string + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = [1, 2, 3] + parameters = { + "region" = "us-east1-a" + } + }`, + ExpectError: regexp.MustCompile("Incorrect attribute value type"), + }}, + }) + + // Given the Parameters field is not provided + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = "preset_1" + }`, + ExpectError: regexp.MustCompile("The argument \"parameters\" is required, but no definition was found"), + }}, + }) + + // Given the Parameters field is empty + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = "preset_1" + parameters = {} + }`, + ExpectError: regexp.MustCompile("workspace preset must define a value for at least one parameter"), + }}, + }) + + // Given the Parameters field is not a map + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: ` + data "coder_workspace_preset" "preset_1" { + name = "preset_1" + parameters = "not a map" + }`, + // This is from terraform's validation based on our schema, not based on our validation in ReadContext: + ExpectError: regexp.MustCompile("Inappropriate value for attribute \"parameters\": map of string required"), + }}, + }) } From 9d69ead7601a2407bf2cf02084f8f9cededb18f8 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 31 Jan 2025 14:38:00 +0000 Subject: [PATCH 04/10] make -B gen --- docs/data-sources/workspace_preset.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 docs/data-sources/workspace_preset.md diff --git a/docs/data-sources/workspace_preset.md b/docs/data-sources/workspace_preset.md new file mode 100644 index 00000000..1d876db4 --- /dev/null +++ b/docs/data-sources/workspace_preset.md @@ -0,0 +1,25 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "coder_workspace_preset Data Source - terraform-provider-coder" +subcategory: "" +description: |- + Use this data source to predefine common configurations for workspaces. +--- + +# coder_workspace_preset (Data Source) + +Use this data source to predefine common configurations for workspaces. + + + + +## Schema + +### Required + +- `name` (String) Name of the workspace preset. +- `parameters` (Map of String) Parameters of the workspace preset. + +### Read-Only + +- `id` (String) ID of the workspace preset. From 8f0359836c3bdaa237a9d052272c0d8e6e4a1eff Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Fri, 31 Jan 2025 14:49:32 +0000 Subject: [PATCH 05/10] make -B gen --- docs/data-sources/workspace_preset.md | 19 ++++++++++++++++++- .../coder_workspace_preset/data-source.tf | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 examples/data-sources/coder_workspace_preset/data-source.tf diff --git a/docs/data-sources/workspace_preset.md b/docs/data-sources/workspace_preset.md index 1d876db4..1fa3164f 100644 --- a/docs/data-sources/workspace_preset.md +++ b/docs/data-sources/workspace_preset.md @@ -10,7 +10,24 @@ description: |- Use this data source to predefine common configurations for workspaces. - +## Example Usage + +```terraform +provider "coder" {} + +# presets can be used to predefine common configurations for workspaces +# Parameters are referenced by their name. Each parameter must be defined in the preset. +# Values defined by the preset must pass validation for the parameter. +# See the coder_parameter data source's documentation for examples of how to define +# parameters like the ones used below. +data "coder_workspace_preset" "example" { + name = "example" + parameters = { + (data.coder_parameter.example.name) = "us-central1-a" + (data.coder_parameter.ami.name) = "ami-xxxxxxxx" + } +} +``` ## Schema diff --git a/examples/data-sources/coder_workspace_preset/data-source.tf b/examples/data-sources/coder_workspace_preset/data-source.tf new file mode 100644 index 00000000..738841ea --- /dev/null +++ b/examples/data-sources/coder_workspace_preset/data-source.tf @@ -0,0 +1,14 @@ +provider "coder" {} + +# presets can be used to predefine common configurations for workspaces +# Parameters are referenced by their name. Each parameter must be defined in the preset. +# Values defined by the preset must pass validation for the parameter. +# See the coder_parameter data source's documentation for examples of how to define +# parameters like the ones used below. +data "coder_workspace_preset" "example" { + name = "example" + parameters = { + (data.coder_parameter.example.name) = "us-central1-a" + (data.coder_parameter.ami.name) = "ami-xxxxxxxx" + } +} From 4a02d819981196eea6ae867b379de00605a09bb2 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 4 Feb 2025 06:45:18 +0000 Subject: [PATCH 06/10] idiomatic tests and review notes --- provider/workspace_preset.go | 22 ++++-- provider/workspace_preset_test.go | 119 +++++++++++++++--------------- 2 files changed, 74 insertions(+), 67 deletions(-) diff --git a/provider/workspace_preset.go b/provider/workspace_preset.go index dc14aab5..cd56c980 100644 --- a/provider/workspace_preset.go +++ b/provider/workspace_preset.go @@ -5,6 +5,7 @@ import ( "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" "github.com/mitchellh/mapstructure" ) @@ -31,12 +32,11 @@ func workspacePresetDataSource() *schema.Resource { return diag.Errorf("decode workspace preset: %s", err) } - if preset.Name == "" { - return diag.Errorf("workspace preset name must be set") - } - + // MinItems doesn't work with maps, so we need to check the length + // of the map manually. All other validation is handled by the + // schema. if len(preset.Parameters) == 0 { - return diag.Errorf("workspace preset must define a value for at least one parameter") + return diag.Errorf("expected \"parameters\" to not be an empty map") } rd.SetId(preset.Name) @@ -50,14 +50,20 @@ func workspacePresetDataSource() *schema.Resource { Computed: true, }, "name": { - Type: schema.TypeString, - Description: "Name of the workspace preset.", - Required: true, + Type: schema.TypeString, + Description: "Name of the workspace preset.", + Required: true, + ValidateFunc: validation.StringIsNotEmpty, }, "parameters": { Type: schema.TypeMap, Description: "Parameters of the workspace preset.", Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, }, }, } diff --git a/provider/workspace_preset_test.go b/provider/workspace_preset_test.go index a24c615c..876e2044 100644 --- a/provider/workspace_preset_test.go +++ b/provider/workspace_preset_test.go @@ -10,11 +10,16 @@ import ( ) func TestWorkspacePreset(t *testing.T) { - // Happy Path: - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + t.Parallel() + type testcase struct { + Name string + Config string + ExpectError *regexp.Regexp + Check func(state *terraform.State) error + } + testcases := []testcase{ + { + Name: "Happy Path", Config: ` data "coder_workspace_preset" "preset_1" { name = "preset_1" @@ -32,30 +37,21 @@ func TestWorkspacePreset(t *testing.T) { require.Equal(t, attrs["parameters.region"], "us-east1-a") return nil }, - }}, - }) - - // Given the Name field is not provided - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + }, + { + Name: "Name field is not provided", Config: ` data "coder_workspace_preset" "preset_1" { parameters = { "region" = "us-east1-a" } }`, - // This is from terraform's validation based on our schema, not based on our validation in ReadContext: + // This validation is done by Terraform, but it could still break if we misconfigure the schema. + // So we test it here to make sure we don't regress. ExpectError: regexp.MustCompile("The argument \"name\" is required, but no definition was found"), - }}, - }) - - // Given the Name field is empty - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + }, + { + Name: "Name field is empty", Config: ` data "coder_workspace_preset" "preset_1" { name = "" @@ -63,15 +59,12 @@ func TestWorkspacePreset(t *testing.T) { "region" = "us-east1-a" } }`, - ExpectError: regexp.MustCompile("workspace preset name must be set"), - }}, - }) - - // Given the Name field is not a string - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + // This validation is done by Terraform, but it could still break if we misconfigure the schema. + // So we test it here to make sure we don't regress. + ExpectError: regexp.MustCompile("expected \"name\" to not be an empty string"), + }, + { + Name: "Name field is not a string", Config: ` data "coder_workspace_preset" "preset_1" { name = [1, 2, 3] @@ -79,49 +72,57 @@ func TestWorkspacePreset(t *testing.T) { "region" = "us-east1-a" } }`, + // This validation is done by Terraform, but it could still break if we misconfigure the schema. + // So we test it here to make sure we don't regress. ExpectError: regexp.MustCompile("Incorrect attribute value type"), - }}, - }) - - // Given the Parameters field is not provided - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + }, + { + Name: "Parameters field is not provided", Config: ` data "coder_workspace_preset" "preset_1" { name = "preset_1" }`, + // This validation is done by Terraform, but it could still break if we misconfigure the schema. + // So we test it here to make sure we don't regress. ExpectError: regexp.MustCompile("The argument \"parameters\" is required, but no definition was found"), - }}, - }) - - // Given the Parameters field is empty - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + }, + { + Name: "Parameters field is empty", Config: ` data "coder_workspace_preset" "preset_1" { name = "preset_1" parameters = {} }`, - ExpectError: regexp.MustCompile("workspace preset must define a value for at least one parameter"), - }}, - }) - - // Given the Parameters field is not a map - resource.Test(t, resource.TestCase{ - ProviderFactories: coderFactory(), - IsUnitTest: true, - Steps: []resource.TestStep{{ + // This validation is *not* done by Terraform, because MinItems doesn't work with maps. + // We've implemented the validation in ReadContext, so we test it here to make sure we don't regress. + ExpectError: regexp.MustCompile("expected \"parameters\" to not be an empty map"), + }, + { + Name: "Parameters field is not a map", Config: ` data "coder_workspace_preset" "preset_1" { name = "preset_1" parameters = "not a map" }`, - // This is from terraform's validation based on our schema, not based on our validation in ReadContext: + // This validation is done by Terraform, but it could still break if we misconfigure the schema. + // So we test it here to make sure we don't regress. ExpectError: regexp.MustCompile("Inappropriate value for attribute \"parameters\": map of string required"), - }}, - }) + }, + } + + for _, testcase := range testcases { + t.Run(testcase.Name, func(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + ProviderFactories: coderFactory(), + IsUnitTest: true, + Steps: []resource.TestStep{{ + Config: testcase.Config, + ExpectError: testcase.ExpectError, + Check: testcase.Check, + }}, + }) + }) + } } From b508049bd8bbfb9ebd254ff05c5d0d9e52f8219c Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 4 Feb 2025 07:03:07 +0000 Subject: [PATCH 07/10] make fmt --- examples/data-sources/coder_workspace_preset/data-source.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/data-sources/coder_workspace_preset/data-source.tf b/examples/data-sources/coder_workspace_preset/data-source.tf index 738841ea..4f29a199 100644 --- a/examples/data-sources/coder_workspace_preset/data-source.tf +++ b/examples/data-sources/coder_workspace_preset/data-source.tf @@ -9,6 +9,6 @@ data "coder_workspace_preset" "example" { name = "example" parameters = { (data.coder_parameter.example.name) = "us-central1-a" - (data.coder_parameter.ami.name) = "ami-xxxxxxxx" + (data.coder_parameter.ami.name) = "ami-xxxxxxxx" } } From 1a717ddbf97769a3d97ec981b5fa54b0f9172551 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 4 Feb 2025 07:05:47 +0000 Subject: [PATCH 08/10] make -B gen --- docs/data-sources/workspace_preset.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/workspace_preset.md b/docs/data-sources/workspace_preset.md index 1fa3164f..28f90faa 100644 --- a/docs/data-sources/workspace_preset.md +++ b/docs/data-sources/workspace_preset.md @@ -24,7 +24,7 @@ data "coder_workspace_preset" "example" { name = "example" parameters = { (data.coder_parameter.example.name) = "us-central1-a" - (data.coder_parameter.ami.name) = "ami-xxxxxxxx" + (data.coder_parameter.ami.name) = "ami-xxxxxxxx" } } ``` From e6f35c650991cac89aa3cfad0701cb8d5bf4c69c Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 4 Feb 2025 08:04:55 +0000 Subject: [PATCH 09/10] add integration tests --- integration/integration_test.go | 45 ++++++++++++++------ integration/test-data-source/main.tf | 17 ++++++++ integration/test-data-source/parameters.yaml | 1 + 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 integration/test-data-source/parameters.yaml diff --git a/integration/integration_test.go b/integration/integration_test.go index 8ac68f28..bbbd5587 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -73,18 +73,27 @@ func TestIntegration(t *testing.T) { name: "test-data-source", minVersion: "v0.0.0", expectedOutput: map[string]string{ - "provisioner.arch": runtime.GOARCH, - "provisioner.id": `[a-zA-Z0-9-]+`, - "provisioner.os": runtime.GOOS, - "workspace.access_port": `\d+`, - "workspace.access_url": `https?://\D+:\d+`, - "workspace.id": `[a-zA-z0-9-]+`, - "workspace.name": `test-data-source`, - "workspace.start_count": `1`, - "workspace.template_id": `[a-zA-Z0-9-]+`, - "workspace.template_name": `test-data-source`, - "workspace.template_version": `.+`, - "workspace.transition": `start`, + "provisioner.arch": runtime.GOARCH, + "provisioner.id": `[a-zA-Z0-9-]+`, + "provisioner.os": runtime.GOOS, + "workspace.access_port": `\d+`, + "workspace.access_url": `https?://\D+:\d+`, + "workspace.id": `[a-zA-z0-9-]+`, + "workspace.name": `test-data-source`, + "workspace.start_count": `1`, + "workspace.template_id": `[a-zA-Z0-9-]+`, + "workspace.template_name": `test-data-source`, + "workspace.template_version": `.+`, + "workspace.transition": `start`, + "workspace_parameter.name": `param`, + "workspace_parameter.description": `param description`, + // TODO (sasswart): the cli doesn't support presets yet. + // once it does, the value for workspace_parameter.value + // will be the preset value. + "workspace_parameter.value": `param value`, + "workspace_parameter.icon": `param icon`, + "workspace_preset.name": `preset`, + "workspace_preset.parameters.param": `preset param value`, }, }, { @@ -179,8 +188,18 @@ func TestIntegration(t *testing.T) { } _, rc := execContainer(ctx, t, ctrID, fmt.Sprintf(`coder templates %s %s --directory /src/integration/%s --var output_path=/tmp/%s.json --yes`, templateCreateCmd, tt.name, tt.name, tt.name)) require.Equal(t, 0, rc) + + // Check if parameters.yaml exists + _, rc = execContainer(ctx, t, ctrID, fmt.Sprintf(`stat /src/integration/%s/parameters.yaml 2>/dev/null > /dev/null`, tt.name)) + hasParameters := rc == 0 + var includeParameters string + if hasParameters { + // If it exists, include it in the create command + includeParameters = fmt.Sprintf(`--rich-parameter-file /src/integration/%s/parameters.yaml`, tt.name) + } + // Create a workspace - _, rc = execContainer(ctx, t, ctrID, fmt.Sprintf(`coder create %s -t %s --yes`, tt.name, tt.name)) + _, rc = execContainer(ctx, t, ctrID, fmt.Sprintf(`coder create %s -t %s %s --yes`, tt.name, tt.name, includeParameters)) require.Equal(t, 0, rc) // Fetch the output created by the template out, rc := execContainer(ctx, t, ctrID, fmt.Sprintf(`cat /tmp/%s.json`, tt.name)) diff --git a/integration/test-data-source/main.tf b/integration/test-data-source/main.tf index 6d4b85cd..4fc55253 100644 --- a/integration/test-data-source/main.tf +++ b/integration/test-data-source/main.tf @@ -14,6 +14,17 @@ terraform { data "coder_provisioner" "me" {} data "coder_workspace" "me" {} data "coder_workspace_owner" "me" {} +data "coder_parameter" "param" { + name = "param" + description = "param description" + icon = "param icon" +} +data "coder_workspace_preset" "preset" { + name = "preset" + parameters = { + (data.coder_parameter.param.name) = "preset param value" + } +} locals { # NOTE: these must all be strings in the output @@ -30,6 +41,12 @@ locals { "workspace.template_name" : data.coder_workspace.me.template_name, "workspace.template_version" : data.coder_workspace.me.template_version, "workspace.transition" : data.coder_workspace.me.transition, + "workspace_parameter.name" : data.coder_parameter.param.name, + "workspace_parameter.description" : data.coder_parameter.param.description, + "workspace_parameter.value" : data.coder_parameter.param.value, + "workspace_parameter.icon" : data.coder_parameter.param.icon, + "workspace_preset.name" : data.coder_workspace_preset.preset.name, + "workspace_preset.parameters.param" : data.coder_workspace_preset.preset.parameters.param, } } diff --git a/integration/test-data-source/parameters.yaml b/integration/test-data-source/parameters.yaml new file mode 100644 index 00000000..0e75c133 --- /dev/null +++ b/integration/test-data-source/parameters.yaml @@ -0,0 +1 @@ +param: "param value" \ No newline at end of file From 8032d6e04f1057e265055f24678b73d595c51742 Mon Sep 17 00:00:00 2001 From: Sas Swart Date: Tue, 4 Feb 2025 08:06:44 +0000 Subject: [PATCH 10/10] make gen fmt --- integration/test-data-source/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/test-data-source/main.tf b/integration/test-data-source/main.tf index 4fc55253..5fb2e0e6 100644 --- a/integration/test-data-source/main.tf +++ b/integration/test-data-source/main.tf @@ -15,9 +15,9 @@ data "coder_provisioner" "me" {} data "coder_workspace" "me" {} data "coder_workspace_owner" "me" {} data "coder_parameter" "param" { - name = "param" + name = "param" description = "param description" - icon = "param icon" + icon = "param icon" } data "coder_workspace_preset" "preset" { name = "preset"