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

Commit471368b

Browse files
authored
feat: addcoder_env (#174)
Fixes#170
1 parent39b0b24 commit471368b

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

‎docs/resources/env.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title:"coder_env Resource - terraform-provider-coder"
4+
subcategory:""
5+
description:|-
6+
Use this resource to set an environment variable in a workspace.
7+
---
8+
9+
#coder_env (Resource)
10+
11+
Use this resource to set an environment variable in a workspace.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs-->
16+
##Schema
17+
18+
###Required
19+
20+
-`agent_id` (String) The "id" property of a "coder_agent" resource to associate with.
21+
-`name` (String) The name of the environment variable.
22+
23+
###Optional
24+
25+
-`value` (String) The value of the environment variable.
26+
27+
###Read-Only
28+
29+
-`id` (String) The ID of this resource.

‎provider/env.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"regexp"
6+
7+
"github.com/google/uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
)
12+
13+
funcenvResource()*schema.Resource {
14+
return&schema.Resource{
15+
Description:"Use this resource to set an environment variable in a workspace.",
16+
CreateContext:func(_ context.Context,rd*schema.ResourceData,_interface{}) diag.Diagnostics {
17+
rd.SetId(uuid.NewString())
18+
19+
returnnil
20+
},
21+
ReadContext:schema.NoopContext,
22+
DeleteContext:schema.NoopContext,
23+
Schema:map[string]*schema.Schema{
24+
"agent_id": {
25+
Type:schema.TypeString,
26+
Description:`The "id" property of a "coder_agent" resource to associate with.`,
27+
ForceNew:true,
28+
Required:true,
29+
},
30+
"name": {
31+
Type:schema.TypeString,
32+
Description:"The name of the environment variable.",
33+
ForceNew:true,
34+
Required:true,
35+
ValidateFunc:validation.StringMatch(
36+
regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`),
37+
"must be a valid environment variable name",
38+
),
39+
},
40+
"value": {
41+
Type:schema.TypeString,
42+
Description:"The value of the environment variable.",
43+
ForceNew:true,
44+
Optional:true,
45+
},
46+
},
47+
}
48+
}

‎provider/env_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package provider_test
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/coder/terraform-provider-coder/provider"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13+
)
14+
15+
funcTestEnv(t*testing.T) {
16+
t.Parallel()
17+
18+
resource.Test(t, resource.TestCase{
19+
Providers:map[string]*schema.Provider{
20+
"coder":provider.New(),
21+
},
22+
IsUnitTest:true,
23+
Steps: []resource.TestStep{{
24+
Config:`
25+
provider "coder" {
26+
}
27+
resource "coder_env" "example" {
28+
agent_id = "king"
29+
name = "MESSAGE"
30+
value = "Believe in yourself and there will come a day when others will have no choice but to believe with you."
31+
}
32+
`,
33+
Check:func(state*terraform.State)error {
34+
require.Len(t,state.Modules,1)
35+
require.Len(t,state.Modules[0].Resources,1)
36+
script:=state.Modules[0].Resources["coder_env.example"]
37+
require.NotNil(t,script)
38+
t.Logf("script attributes: %#v",script.Primary.Attributes)
39+
forkey,expected:=rangemap[string]string{
40+
"agent_id":"king",
41+
"name":"MESSAGE",
42+
"value":"Believe in yourself and there will come a day when others will have no choice but to believe with you.",
43+
} {
44+
require.Equal(t,expected,script.Primary.Attributes[key])
45+
}
46+
returnnil
47+
},
48+
}},
49+
})
50+
}
51+
52+
funcTestEnvEmptyValue(t*testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
Providers:map[string]*schema.Provider{
57+
"coder":provider.New(),
58+
},
59+
IsUnitTest:true,
60+
Steps: []resource.TestStep{{
61+
Config:`
62+
provider "coder" {
63+
}
64+
resource "coder_env" "example" {
65+
agent_id = "king"
66+
name = "MESSAGE"
67+
}
68+
`,
69+
Check:func(state*terraform.State)error {
70+
require.Len(t,state.Modules,1)
71+
require.Len(t,state.Modules[0].Resources,1)
72+
script:=state.Modules[0].Resources["coder_env.example"]
73+
require.NotNil(t,script)
74+
t.Logf("script attributes: %#v",script.Primary.Attributes)
75+
forkey,expected:=rangemap[string]string{
76+
"agent_id":"king",
77+
"name":"MESSAGE",
78+
"value":"",
79+
} {
80+
require.Equal(t,expected,script.Primary.Attributes[key])
81+
}
82+
returnnil
83+
},
84+
}},
85+
})
86+
}
87+
88+
funcTestEnvBadName(t*testing.T) {
89+
t.Parallel()
90+
91+
resource.Test(t, resource.TestCase{
92+
Providers:map[string]*schema.Provider{
93+
"coder":provider.New(),
94+
},
95+
IsUnitTest:true,
96+
Steps: []resource.TestStep{{
97+
Config:`
98+
provider "coder" {
99+
}
100+
resource "coder_env" "example" {
101+
agent_id = ""
102+
name = "bad-name"
103+
}
104+
`,
105+
ExpectError:regexp.MustCompile(`must be a valid environment variable name`),
106+
}},
107+
})
108+
}
109+
110+
funcTestEnvNoName(t*testing.T) {
111+
t.Parallel()
112+
113+
resource.Test(t, resource.TestCase{
114+
Providers:map[string]*schema.Provider{
115+
"coder":provider.New(),
116+
},
117+
IsUnitTest:true,
118+
Steps: []resource.TestStep{{
119+
Config:`
120+
provider "coder" {
121+
}
122+
resource "coder_env" "example" {
123+
agent_id = ""
124+
}
125+
`,
126+
ExpectError:regexp.MustCompile(`The argument "name" is required, but no definition was found.`),
127+
}},
128+
})
129+
}

‎provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func New() *schema.Provider {
8080
"coder_app":appResource(),
8181
"coder_metadata":metadataResource(),
8282
"coder_script":scriptResource(),
83+
"coder_env":envResource(),
8384
},
8485
}
8586
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp