-
Notifications
You must be signed in to change notification settings - Fork 22
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 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
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8881ae0
add a workspace preset datasource
SasSwart af7a1ed
add workspace preset type
SasSwart 2f00546
add validation and tests for coder_workspace_presets
SasSwart 9d69ead
make -B gen
SasSwart 8f03598
make -B gen
SasSwart 4a02d81
idiomatic tests and review notes
SasSwart b508049
make fmt
SasSwart 1a717dd
make -B gen
SasSwart e6f35c6
add integration tests
SasSwart 8032d6e
make gen fmt
SasSwart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 deletions
14
examples/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" | ||
} | ||
} |
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
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,64 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"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) | ||
} | ||
|
||
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{ | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"id": { | ||
Type: schema.TypeString, | ||
Description: "ID of the workspace preset.", | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "Name of the workspace preset.", | ||
Required: true, | ||
}, | ||
"parameters": { | ||
Type: schema.TypeMap, | ||
Description: "Parameters of the workspace preset.", | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} |
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,127 @@ | ||
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) { | ||
// Happy Path: | ||
resource.Test(t, resource.TestCase{ | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
}, | ||
}}, | ||
}) | ||
|
||
// 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"), | ||
}}, | ||
}) | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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"), | ||
}}, | ||
}) | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.