- Notifications
You must be signed in to change notification settings - Fork24
feat: add a workspace preset datasource#332
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
8881ae0 add a workspace preset datasource
SasSwartaf7a1ed add workspace preset type
SasSwart2f00546 add validation and tests for coder_workspace_presets
SasSwart9d69ead make -B gen
SasSwart8f03598 make -B gen
SasSwart4a02d81 idiomatic tests and review notes
SasSwartb508049 make fmt
SasSwart1a717dd make -B gen
SasSwarte6f35c6 add integration tests
SasSwart8032d6e make gen fmt
SasSwartFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletionsdocs/data-sources/workspace_preset.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| # 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. | ||
| ## 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 generated by tfplugindocs --> | ||
| ## 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. |
14 changes: 14 additions & 0 deletionsexamples/data-sources/coder_workspace_preset/data-source.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
45 changes: 32 additions & 13 deletionsintegration/integration_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletionsintegration/test-data-source/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsintegration/test-data-source/parameters.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| param: "param value" |
13 changes: 7 additions & 6 deletionsprovider/provider.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletionsprovider/workspace_preset.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package provider | ||
| import ( | ||
| "context" | ||
| "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" | ||
| ) | ||
| type WorkspacePreset struct { | ||
| Name string `mapstructure:"name"` | ||
| Parameters map[string]string `mapstructure:"parameters"` | ||
| } | ||
| func workspacePresetDataSource() *schema.Resource { | ||
| return &schema.Resource{ | ||
| SchemaVersion: 1, | ||
| Description: "Use this data source to predefine common configurations for workspaces.", | ||
| ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
| 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) | ||
| } | ||
| // 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("expected \"parameters\" to not be an empty map") | ||
| } | ||
| rd.SetId(preset.Name) | ||
| return nil | ||
| }, | ||
| Schema: map[string]*schema.Schema{ | ||
SasSwart marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| "id": { | ||
| Type: schema.TypeString, | ||
| Description: "ID of the workspace preset.", | ||
| Computed: true, | ||
| }, | ||
| "name": { | ||
| 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, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
128 changes: 128 additions & 0 deletionsprovider/workspace_preset_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| 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 TestWorkspacePreset(t *testing.T) { | ||
| 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" | ||
| 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 | ||
| }, | ||
| }, | ||
| { | ||
| Name: "Name field is not provided", | ||
| Config: ` | ||
| data "coder_workspace_preset" "preset_1" { | ||
| parameters = { | ||
| "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("The argument \"name\" is required, but no definition was found"), | ||
| }, | ||
| { | ||
| Name: "Name field is empty", | ||
| Config: ` | ||
| data "coder_workspace_preset" "preset_1" { | ||
| name = "" | ||
| parameters = { | ||
| "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("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] | ||
| parameters = { | ||
| "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"), | ||
| }, | ||
| { | ||
| 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"), | ||
| }, | ||
| { | ||
| Name: "Parameters field is empty", | ||
| Config: ` | ||
| data "coder_workspace_preset" "preset_1" { | ||
| name = "preset_1" | ||
| parameters = {} | ||
| }`, | ||
| // 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 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, | ||
| }}, | ||
| }) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.