Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: require environment variables #246

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions provider/helpers/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import (
)

// RequireEnv requires environment variable to be present.
// The constraint can be verified only during execution of the workspace build
// (determined with env `CODER_WORKSPACE_BUILD_ID`).
func RequireEnv(name string) (string, error) {
if os.Getenv("CODER_WORKSPACE_BUILD_ID") == "" {
return os.Getenv(name), nil
}

val := os.Getenv(name)
if val == "" {
return "", fmt.Errorf("%s is required", name)
Expand Down
15 changes: 12 additions & 3 deletions provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ func workspaceDataSource() *schema.Resource {
id := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_ID", uuid.NewString())
rd.SetId(id)

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

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

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

config, valid := i.(config)
Expand Down
32 changes: 32 additions & 0 deletions provider/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -102,3 +103,34 @@ func TestWorkspace_UndefinedOwner(t *testing.T) {
}},
})
}

func TestWorkspace_MissingTemplateName(t *testing.T) {
t.Setenv("CODER_WORKSPACE_BUILD_ID", "1") // Let's pretend this is a workspace build

t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
t.Setenv("CODER_WORKSPACE_OWNER_ID", "11111111-1111-1111-1111-111111111111")
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com")
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123")
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", "supersecret")
t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID")
// CODER_WORKSPACE_TEMPLATE_NAME is missing
t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
url = "https://example.com:8080"
}
data "coder_workspace" "me" {
}`,
ExpectError: regexp.MustCompile("CODER_WORKSPACE_TEMPLATE_NAME is required"),
}},
})
}
Loading