Skip to content

Commit 9f66e78

Browse files
authored
feat: require environment variables (#246)
1 parent dd5f55c commit 9f66e78

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

Diff for: provider/helpers/env.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import (
66
)
77

88
// RequireEnv requires environment variable to be present.
9+
// The constraint can be verified only during execution of the workspace build
10+
// (determined with env `CODER_WORKSPACE_BUILD_ID`).
911
func RequireEnv(name string) (string, error) {
12+
if os.Getenv("CODER_WORKSPACE_BUILD_ID") == "" {
13+
return os.Getenv(name), nil
14+
}
15+
1016
val := os.Getenv(name)
1117
if val == "" {
1218
return "", fmt.Errorf("%s is required", name)

Diff for: provider/workspace.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,22 @@ func workspaceDataSource() *schema.Resource {
6262
id := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_ID", uuid.NewString())
6363
rd.SetId(id)
6464

65-
templateID := helpers.OptionalEnv("CODER_WORKSPACE_TEMPLATE_ID") // FIXME switch to `helpers.RequireEnv(...)`
65+
templateID, err := helpers.RequireEnv("CODER_WORKSPACE_TEMPLATE_ID")
66+
if err != nil {
67+
return diag.Errorf("template ID is missing: %s", err.Error())
68+
}
6669
_ = rd.Set("template_id", templateID)
6770

68-
templateName := helpers.OptionalEnv("CODER_WORKSPACE_TEMPLATE_NAME") // FIXME switch to `helpers.RequireEnv(...)`
71+
templateName, err := helpers.RequireEnv("CODER_WORKSPACE_TEMPLATE_NAME")
72+
if err != nil {
73+
return diag.Errorf("template name is missing: %s", err.Error())
74+
}
6975
_ = rd.Set("template_name", templateName)
7076

71-
templateVersion := helpers.OptionalEnv("CODER_WORKSPACE_TEMPLATE_VERSION") // FIXME switch to `helpers.RequireEnv(...)`
77+
templateVersion, err := helpers.RequireEnv("CODER_WORKSPACE_TEMPLATE_VERSION")
78+
if err != nil {
79+
return diag.Errorf("template version is missing: %s", err.Error())
80+
}
7281
_ = rd.Set("template_version", templateVersion)
7382

7483
config, valid := i.(config)

Diff for: provider/workspace_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider_test
22

33
import (
4+
"regexp"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -102,3 +103,34 @@ func TestWorkspace_UndefinedOwner(t *testing.T) {
102103
}},
103104
})
104105
}
106+
107+
func TestWorkspace_MissingTemplateName(t *testing.T) {
108+
t.Setenv("CODER_WORKSPACE_BUILD_ID", "1") // Let's pretend this is a workspace build
109+
110+
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
111+
t.Setenv("CODER_WORKSPACE_OWNER_ID", "11111111-1111-1111-1111-111111111111")
112+
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
113+
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "[email protected]")
114+
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123")
115+
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
116+
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", "supersecret")
117+
t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID")
118+
// CODER_WORKSPACE_TEMPLATE_NAME is missing
119+
t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")
120+
121+
resource.Test(t, resource.TestCase{
122+
Providers: map[string]*schema.Provider{
123+
"coder": provider.New(),
124+
},
125+
IsUnitTest: true,
126+
Steps: []resource.TestStep{{
127+
Config: `
128+
provider "coder" {
129+
url = "https://example.com:8080"
130+
}
131+
data "coder_workspace" "me" {
132+
}`,
133+
ExpectError: regexp.MustCompile("CODER_WORKSPACE_TEMPLATE_NAME is required"),
134+
}},
135+
})
136+
}

0 commit comments

Comments
 (0)