Skip to content

Commit

Permalink
fix: provider: coder_workspace_owner: avoid setting null values (#232)
Browse files Browse the repository at this point in the history
* chore: add coder_workspace_owner to TestProviderEmpty, rm unused struct

* fix: do not set null values
  • Loading branch information
johnstcn authored May 27, 2024
1 parent eb43b9f commit 96993eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
1 change: 1 addition & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestProviderEmpty(t *testing.T) {
provider "coder" {}
data "coder_provisioner" "me" {}
data "coder_workspace" "me" {}
data "coder_workspace_owner" "me" {}
data "coder_external_auth" "git" {
id = "git"
}
Expand Down
33 changes: 9 additions & 24 deletions provider/workspace_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,47 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type Role struct {
Name string `json:"name"`
DisplayName string `json:"display-name"`
}

func workspaceOwnerDataSource() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to fetch information about the workspace owner.",
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
if idStr, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_ID"); ok {
if idStr := os.Getenv("CODER_WORKSPACE_OWNER_ID"); idStr != "" {
rd.SetId(idStr)
} else {
rd.SetId(uuid.NewString())
}

if username, ok := os.LookupEnv("CODER_WORKSPACE_OWNER"); ok {
if username := os.Getenv("CODER_WORKSPACE_OWNER"); username != "" {
_ = rd.Set("name", username)
} else {
_ = rd.Set("name", "default")
}

if fullname, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_NAME"); ok {
if fullname := os.Getenv("CODER_WORKSPACE_OWNER_NAME"); fullname != "" {
_ = rd.Set("full_name", fullname)
} else { // compat: field can be blank, fill in default
_ = rd.Set("full_name", "default")
}

if email, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_EMAIL"); ok {
if email := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL"); email != "" {
_ = rd.Set("email", email)
} else {
_ = rd.Set("email", "default@example.com")
}

if sshPubKey, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY"); ok {
_ = rd.Set("ssh_public_key", sshPubKey)
}

if sshPrivKey, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY"); ok {
_ = rd.Set("ssh_private_key", sshPrivKey)
}
_ = rd.Set("ssh_public_key", os.Getenv("CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY"))
_ = rd.Set("ssh_private_key", os.Getenv("CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY"))

var groups []string
if groupsRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_GROUPS"); ok {
if err := json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups); err != nil {
return diag.Errorf("invalid user groups: %s", err.Error())
}
_ = rd.Set("groups", groups)
}

if tok, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"); ok {
_ = rd.Set("session_token", tok)
}
_ = rd.Set("groups", groups)

if tok, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN"); ok {
_ = rd.Set("oidc_access_token", tok)
}
_ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"))
_ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN"))

return nil
},
Expand Down

0 comments on commit 96993eb

Please sign in to comment.