Skip to content

Commit 471368b

Browse files
authored
feat: add coder_env (#174)
Fixes #170
1 parent 39b0b24 commit 471368b

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

Diff for: docs/resources/env.md

+29
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.

Diff for: provider/env.go

+48
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+
func envResource() *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+
return nil
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+
}

Diff for: provider/env_test.go

+129
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+
func TestEnv(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+
for key, expected := range map[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+
return nil
47+
},
48+
}},
49+
})
50+
}
51+
52+
func TestEnvEmptyValue(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+
for key, expected := range map[string]string{
76+
"agent_id": "king",
77+
"name": "MESSAGE",
78+
"value": "",
79+
} {
80+
require.Equal(t, expected, script.Primary.Attributes[key])
81+
}
82+
return nil
83+
},
84+
}},
85+
})
86+
}
87+
88+
func TestEnvBadName(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+
func TestEnvNoName(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+
}

Diff for: provider/provider.go

+1
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)