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

feat: require environment variables#246

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 2 commits intomainfrom13680-build-id
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsprovider/helpers/env.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsprovider/workspace.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsprovider/workspace_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package provider_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp