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

Commit9f66e78

Browse files
authored
feat: require environment variables (#246)
1 parentdd5f55c commit9f66e78

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

‎provider/helpers/env.go

Lines changed: 6 additions & 0 deletions
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
funcRequireEnv(namestring) (string,error) {
12+
ifos.Getenv("CODER_WORKSPACE_BUILD_ID")=="" {
13+
returnos.Getenv(name),nil
14+
}
15+
1016
val:=os.Getenv(name)
1117
ifval=="" {
1218
return"",fmt.Errorf("%s is required",name)

‎provider/workspace.go

Lines changed: 12 additions & 3 deletions
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+
iferr!=nil {
67+
returndiag.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+
iferr!=nil {
73+
returndiag.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+
iferr!=nil {
79+
returndiag.Errorf("template version is missing: %s",err.Error())
80+
}
7281
_=rd.Set("template_version",templateVersion)
7382

7483
config,valid:=i.(config)

‎provider/workspace_test.go

Lines changed: 32 additions & 0 deletions
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+
funcTestWorkspace_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","owner123@example.com")
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)

[8]ページ先頭

©2009-2025 Movatter.jp