Skip to content

feat: support ephemeral parameters #138

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
Jul 6, 2023
Merged
Changes from 2 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
1 change: 1 addition & 0 deletions docs/data-sources/parameter.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ Use this data source to configure editable options for workspaces.
- `default` (String) A default value for the parameter.
- `description` (String) Describe what this parameter does.
- `display_name` (String) The displayed name of the parameter as it will appear in the interface.
- `ephemeral` (Boolean) The presence of the ephemeral dictates whether the parameter value will be preserved between consecutive workspace builds.
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/<path>"`.
- `legacy_variable` (String, Deprecated) Reference to the Terraform variable. Coder will use it to lookup the default value.
- `legacy_variable_name` (String, Deprecated) Name of the legacy Terraform variable. Coder will use it to lookup the variable value.
6 changes: 4 additions & 2 deletions examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
@@ -74,8 +74,10 @@ data "coder_parameter" "cat_lives" {
}

data "coder_parameter" "fairy_tale" {
name = "Fairy Tale"
type = "string"
name = "Fairy Tale"
type = "string"
mutable = true
ephemeral = true
}

data "coder_parameter" "users" {
16 changes: 14 additions & 2 deletions provider/parameter.go
Original file line number Diff line number Diff line change
@@ -56,8 +56,8 @@ type Parameter struct {
Option []Option
Validation []Validation
Optional bool

Order int
Order int
Ephemeral bool

LegacyVariableName string `mapstructure:"legacy_variable_name"`
LegacyVariable string `mapstructure:"legacy_variable"`
@@ -93,6 +93,7 @@ func parameterDataSource() *schema.Resource {
Validation interface{}
Optional interface{}
Order interface{}
Ephemeral interface{}

LegacyVariableName interface{}
LegacyVariable interface{}
@@ -126,6 +127,7 @@ func parameterDataSource() *schema.Resource {
return val
}(),
Order: rd.Get("order"),
Ephemeral: rd.Get("ephemeral"),
LegacyVariableName: rd.Get("legacy_variable_name"),
LegacyVariable: rd.Get("legacy_variable"),
}, &parameter)
@@ -146,6 +148,10 @@ func parameterDataSource() *schema.Resource {
}
rd.Set("value", value)

if !parameter.Mutable && parameter.Ephemeral {
return diag.Errorf("parameter can't be immutable and ephemeral")
}

if len(parameter.Validation) == 1 {
validation := &parameter.Validation[0]
err = validation.Valid(parameter.Type, value)
@@ -340,6 +346,12 @@ func parameterDataSource() *schema.Resource {
Optional: true,
Description: "The order determines the position of a template parameter in the UI/CLI presentation. The lowest order is shown first and parameters with equal order are sorted by name (ascending order).",
},
"ephemeral": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: "The presence of the ephemeral dictates whether the parameter value will be preserved between consecutive workspace builds.",
},
"legacy_variable_name": {
Type: schema.TypeString,
Optional: true,
13 changes: 13 additions & 0 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ func TestParameter(t *testing.T) {
description = "Select for east!"
}
order = 5
ephemeral = true
}
`,
Check: func(state *terraform.ResourceState) {
@@ -64,6 +65,7 @@ func TestParameter(t *testing.T) {
"option.1.icon": "/icon/east.svg",
"option.1.description": "Select for east!",
"order": "5",
"ephemeral": "true",
} {
require.Equal(t, value, attrs[key])
}
@@ -602,6 +604,17 @@ data "coder_parameter" "region" {
}
`,
ExpectError: regexp.MustCompile("a min cannot be specified for a bool type"),
}, {
Name: "ImmutableEphemeralError",
Config: `
data "coder_parameter" "region" {
name = "Region"
type = "string"
mutable = false
ephemeral = true
}
`,
ExpectError: regexp.MustCompile("parameter can't be immutable and ephemeral"),
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {