Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd7d2b70

Browse files
authored
feat: add a workspace preset datasource (#332)
* add a workspace preset datasource* add workspace preset type* add validation and tests for coder_workspace_presets* make -B gen* make -B gen* idiomatic tests and review notes* make fmt* make -B gen* add integration tests* make gen fmt
1 parent054e9bc commitd7d2b70

File tree

8 files changed

+311
-19
lines changed

8 files changed

+311
-19
lines changed

‎docs/data-sources/workspace_preset.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title:"coder_workspace_preset Data Source - terraform-provider-coder"
4+
subcategory:""
5+
description:|-
6+
Use this data source to predefine common configurations for workspaces.
7+
---
8+
9+
#coder_workspace_preset (Data Source)
10+
11+
Use this data source to predefine common configurations for workspaces.
12+
13+
##Example Usage
14+
15+
```terraform
16+
provider "coder" {}
17+
18+
# presets can be used to predefine common configurations for workspaces
19+
# Parameters are referenced by their name. Each parameter must be defined in the preset.
20+
# Values defined by the preset must pass validation for the parameter.
21+
# See the coder_parameter data source's documentation for examples of how to define
22+
# parameters like the ones used below.
23+
data "coder_workspace_preset" "example" {
24+
name = "example"
25+
parameters = {
26+
(data.coder_parameter.example.name) = "us-central1-a"
27+
(data.coder_parameter.ami.name) = "ami-xxxxxxxx"
28+
}
29+
}
30+
```
31+
32+
<!-- schema generated by tfplugindocs-->
33+
##Schema
34+
35+
###Required
36+
37+
-`name` (String) Name of the workspace preset.
38+
-`parameters` (Map of String) Parameters of the workspace preset.
39+
40+
###Read-Only
41+
42+
-`id` (String) ID of the workspace preset.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
provider"coder" {}
2+
3+
# presets can be used to predefine common configurations for workspaces
4+
# Parameters are referenced by their name. Each parameter must be defined in the preset.
5+
# Values defined by the preset must pass validation for the parameter.
6+
# See the coder_parameter data source's documentation for examples of how to define
7+
# parameters like the ones used below.
8+
data"coder_workspace_preset""example" {
9+
name="example"
10+
parameters={
11+
(data.coder_parameter.example.name)="us-central1-a"
12+
(data.coder_parameter.ami.name)="ami-xxxxxxxx"
13+
}
14+
}

‎integration/integration_test.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,27 @@ func TestIntegration(t *testing.T) {
7373
name:"test-data-source",
7474
minVersion:"v0.0.0",
7575
expectedOutput:map[string]string{
76-
"provisioner.arch":runtime.GOARCH,
77-
"provisioner.id":`[a-zA-Z0-9-]+`,
78-
"provisioner.os":runtime.GOOS,
79-
"workspace.access_port":`\d+`,
80-
"workspace.access_url":`https?://\D+:\d+`,
81-
"workspace.id":`[a-zA-z0-9-]+`,
82-
"workspace.name":`test-data-source`,
83-
"workspace.start_count":`1`,
84-
"workspace.template_id":`[a-zA-Z0-9-]+`,
85-
"workspace.template_name":`test-data-source`,
86-
"workspace.template_version":`.+`,
87-
"workspace.transition":`start`,
76+
"provisioner.arch":runtime.GOARCH,
77+
"provisioner.id":`[a-zA-Z0-9-]+`,
78+
"provisioner.os":runtime.GOOS,
79+
"workspace.access_port":`\d+`,
80+
"workspace.access_url":`https?://\D+:\d+`,
81+
"workspace.id":`[a-zA-z0-9-]+`,
82+
"workspace.name":`test-data-source`,
83+
"workspace.start_count":`1`,
84+
"workspace.template_id":`[a-zA-Z0-9-]+`,
85+
"workspace.template_name":`test-data-source`,
86+
"workspace.template_version":`.+`,
87+
"workspace.transition":`start`,
88+
"workspace_parameter.name":`param`,
89+
"workspace_parameter.description":`param description`,
90+
// TODO (sasswart): the cli doesn't support presets yet.
91+
// once it does, the value for workspace_parameter.value
92+
// will be the preset value.
93+
"workspace_parameter.value":`param value`,
94+
"workspace_parameter.icon":`param icon`,
95+
"workspace_preset.name":`preset`,
96+
"workspace_preset.parameters.param":`preset param value`,
8897
},
8998
},
9099
{
@@ -179,8 +188,18 @@ func TestIntegration(t *testing.T) {
179188
}
180189
_,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))
181190
require.Equal(t,0,rc)
191+
192+
// Check if parameters.yaml exists
193+
_,rc=execContainer(ctx,t,ctrID,fmt.Sprintf(`stat /src/integration/%s/parameters.yaml 2>/dev/null > /dev/null`,tt.name))
194+
hasParameters:=rc==0
195+
varincludeParametersstring
196+
ifhasParameters {
197+
// If it exists, include it in the create command
198+
includeParameters=fmt.Sprintf(`--rich-parameter-file /src/integration/%s/parameters.yaml`,tt.name)
199+
}
200+
182201
// Create a workspace
183-
_,rc=execContainer(ctx,t,ctrID,fmt.Sprintf(`coder create %s -t %s --yes`,tt.name,tt.name))
202+
_,rc=execContainer(ctx,t,ctrID,fmt.Sprintf(`coder create %s -t %s%s--yes`,tt.name,tt.name,includeParameters))
184203
require.Equal(t,0,rc)
185204
// Fetch the output created by the template
186205
out,rc:=execContainer(ctx,t,ctrID,fmt.Sprintf(`cat /tmp/%s.json`,tt.name))

‎integration/test-data-source/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ terraform {
1414
data"coder_provisioner""me" {}
1515
data"coder_workspace""me" {}
1616
data"coder_workspace_owner""me" {}
17+
data"coder_parameter""param" {
18+
name="param"
19+
description="param description"
20+
icon="param icon"
21+
}
22+
data"coder_workspace_preset""preset" {
23+
name="preset"
24+
parameters={
25+
(data.coder_parameter.param.name)="preset param value"
26+
}
27+
}
1728

1829
locals {
1930
# NOTE: these must all be strings in the output
@@ -30,6 +41,12 @@ locals {
3041
"workspace.template_name": data.coder_workspace.me.template_name,
3142
"workspace.template_version": data.coder_workspace.me.template_version,
3243
"workspace.transition": data.coder_workspace.me.transition,
44+
"workspace_parameter.name": data.coder_parameter.param.name,
45+
"workspace_parameter.description": data.coder_parameter.param.description,
46+
"workspace_parameter.value": data.coder_parameter.param.value,
47+
"workspace_parameter.icon": data.coder_parameter.param.icon,
48+
"workspace_preset.name": data.coder_workspace_preset.preset.name,
49+
"workspace_preset.parameters.param": data.coder_workspace_preset.preset.parameters.param,
3350
}
3451
}
3552

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
param:"param value"

‎provider/provider.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ func New() *schema.Provider {
6161
},nil
6262
},
6363
DataSourcesMap:map[string]*schema.Resource{
64-
"coder_workspace":workspaceDataSource(),
65-
"coder_workspace_tags":workspaceTagDataSource(),
66-
"coder_provisioner":provisionerDataSource(),
67-
"coder_parameter":parameterDataSource(),
68-
"coder_external_auth":externalAuthDataSource(),
69-
"coder_workspace_owner":workspaceOwnerDataSource(),
64+
"coder_workspace":workspaceDataSource(),
65+
"coder_workspace_tags":workspaceTagDataSource(),
66+
"coder_provisioner":provisionerDataSource(),
67+
"coder_parameter":parameterDataSource(),
68+
"coder_external_auth":externalAuthDataSource(),
69+
"coder_workspace_owner":workspaceOwnerDataSource(),
70+
"coder_workspace_preset":workspacePresetDataSource(),
7071
},
7172
ResourcesMap:map[string]*schema.Resource{
7273
"coder_agent":agentResource(),

‎provider/workspace_preset.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
9+
"github.com/mitchellh/mapstructure"
10+
)
11+
12+
typeWorkspacePresetstruct {
13+
Namestring`mapstructure:"name"`
14+
Parametersmap[string]string`mapstructure:"parameters"`
15+
}
16+
17+
funcworkspacePresetDataSource()*schema.Resource {
18+
return&schema.Resource{
19+
SchemaVersion:1,
20+
21+
Description:"Use this data source to predefine common configurations for workspaces.",
22+
ReadContext:func(ctx context.Context,rd*schema.ResourceData,iinterface{}) diag.Diagnostics {
23+
varpresetWorkspacePreset
24+
err:=mapstructure.Decode(struct {
25+
Nameinterface{}
26+
Parametersinterface{}
27+
}{
28+
Name:rd.Get("name"),
29+
Parameters:rd.Get("parameters"),
30+
},&preset)
31+
iferr!=nil {
32+
returndiag.Errorf("decode workspace preset: %s",err)
33+
}
34+
35+
// MinItems doesn't work with maps, so we need to check the length
36+
// of the map manually. All other validation is handled by the
37+
// schema.
38+
iflen(preset.Parameters)==0 {
39+
returndiag.Errorf("expected\"parameters\" to not be an empty map")
40+
}
41+
42+
rd.SetId(preset.Name)
43+
44+
returnnil
45+
},
46+
Schema:map[string]*schema.Schema{
47+
"id": {
48+
Type:schema.TypeString,
49+
Description:"ID of the workspace preset.",
50+
Computed:true,
51+
},
52+
"name": {
53+
Type:schema.TypeString,
54+
Description:"Name of the workspace preset.",
55+
Required:true,
56+
ValidateFunc:validation.StringIsNotEmpty,
57+
},
58+
"parameters": {
59+
Type:schema.TypeMap,
60+
Description:"Parameters of the workspace preset.",
61+
Required:true,
62+
Elem:&schema.Schema{
63+
Type:schema.TypeString,
64+
Required:true,
65+
ValidateFunc:validation.StringIsNotEmpty,
66+
},
67+
},
68+
},
69+
}
70+
}

‎provider/workspace_preset_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package provider_test
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
funcTestWorkspacePreset(t*testing.T) {
13+
t.Parallel()
14+
typetestcasestruct {
15+
Namestring
16+
Configstring
17+
ExpectError*regexp.Regexp
18+
Checkfunc(state*terraform.State)error
19+
}
20+
testcases:= []testcase{
21+
{
22+
Name:"Happy Path",
23+
Config:`
24+
data "coder_workspace_preset" "preset_1" {
25+
name = "preset_1"
26+
parameters = {
27+
"region" = "us-east1-a"
28+
}
29+
}`,
30+
Check:func(state*terraform.State)error {
31+
require.Len(t,state.Modules,1)
32+
require.Len(t,state.Modules[0].Resources,1)
33+
resource:=state.Modules[0].Resources["data.coder_workspace_preset.preset_1"]
34+
require.NotNil(t,resource)
35+
attrs:=resource.Primary.Attributes
36+
require.Equal(t,attrs["name"],"preset_1")
37+
require.Equal(t,attrs["parameters.region"],"us-east1-a")
38+
returnnil
39+
},
40+
},
41+
{
42+
Name:"Name field is not provided",
43+
Config:`
44+
data "coder_workspace_preset" "preset_1" {
45+
parameters = {
46+
"region" = "us-east1-a"
47+
}
48+
}`,
49+
// This validation is done by Terraform, but it could still break if we misconfigure the schema.
50+
// So we test it here to make sure we don't regress.
51+
ExpectError:regexp.MustCompile("The argument\"name\" is required, but no definition was found"),
52+
},
53+
{
54+
Name:"Name field is empty",
55+
Config:`
56+
data "coder_workspace_preset" "preset_1" {
57+
name = ""
58+
parameters = {
59+
"region" = "us-east1-a"
60+
}
61+
}`,
62+
// This validation is done by Terraform, but it could still break if we misconfigure the schema.
63+
// So we test it here to make sure we don't regress.
64+
ExpectError:regexp.MustCompile("expected\"name\" to not be an empty string"),
65+
},
66+
{
67+
Name:"Name field is not a string",
68+
Config:`
69+
data "coder_workspace_preset" "preset_1" {
70+
name = [1, 2, 3]
71+
parameters = {
72+
"region" = "us-east1-a"
73+
}
74+
}`,
75+
// This validation is done by Terraform, but it could still break if we misconfigure the schema.
76+
// So we test it here to make sure we don't regress.
77+
ExpectError:regexp.MustCompile("Incorrect attribute value type"),
78+
},
79+
{
80+
Name:"Parameters field is not provided",
81+
Config:`
82+
data "coder_workspace_preset" "preset_1" {
83+
name = "preset_1"
84+
}`,
85+
// This validation is done by Terraform, but it could still break if we misconfigure the schema.
86+
// So we test it here to make sure we don't regress.
87+
ExpectError:regexp.MustCompile("The argument\"parameters\" is required, but no definition was found"),
88+
},
89+
{
90+
Name:"Parameters field is empty",
91+
Config:`
92+
data "coder_workspace_preset" "preset_1" {
93+
name = "preset_1"
94+
parameters = {}
95+
}`,
96+
// This validation is *not* done by Terraform, because MinItems doesn't work with maps.
97+
// We've implemented the validation in ReadContext, so we test it here to make sure we don't regress.
98+
ExpectError:regexp.MustCompile("expected\"parameters\" to not be an empty map"),
99+
},
100+
{
101+
Name:"Parameters field is not a map",
102+
Config:`
103+
data "coder_workspace_preset" "preset_1" {
104+
name = "preset_1"
105+
parameters = "not a map"
106+
}`,
107+
// This validation is done by Terraform, but it could still break if we misconfigure the schema.
108+
// So we test it here to make sure we don't regress.
109+
ExpectError:regexp.MustCompile("Inappropriate value for attribute\"parameters\": map of string required"),
110+
},
111+
}
112+
113+
for_,testcase:=rangetestcases {
114+
t.Run(testcase.Name,func(t*testing.T) {
115+
t.Parallel()
116+
117+
resource.Test(t, resource.TestCase{
118+
ProviderFactories:coderFactory(),
119+
IsUnitTest:true,
120+
Steps: []resource.TestStep{{
121+
Config:testcase.Config,
122+
ExpectError:testcase.ExpectError,
123+
Check:testcase.Check,
124+
}},
125+
})
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp