Skip to content

Commit

Permalink
feat: add owner group to workspace data (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
f0ssel authored Apr 2, 2024
1 parent ec5b604 commit 176fb6a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ resource "kubernetes_pod" "dev" {
- `name` (String) Name of the workspace.
- `owner` (String) Username of the workspace owner.
- `owner_email` (String) Email address of the workspace owner.
- `owner_groups` (List of String) List of groups the workspace owner belongs to.
- `owner_id` (String) UUID of the workspace owner.
- `owner_name` (String) Name of the workspace owner.
- `owner_oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
Expand Down
17 changes: 17 additions & 0 deletions provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"encoding/json"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -36,6 +37,14 @@ func workspaceDataSource() *schema.Resource {
ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL")
_ = rd.Set("owner_email", ownerEmail)

ownerGroupsText := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS")
var ownerGroups []string
err := json.Unmarshal([]byte(ownerGroupsText), &ownerGroups)
if err != nil {
return diag.Errorf("couldn't parse owner groups %q", ownerGroupsText)
}
_ = rd.Set("owner_groups", ownerGroups)

ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME")
_ = rd.Set("owner_name", ownerName)

Expand Down Expand Up @@ -141,6 +150,14 @@ func workspaceDataSource() *schema.Resource {
"This is only available if the workspace owner authenticated with OpenID Connect. " +
"If a valid token cannot be obtained, this value will be an empty string.",
},
"owner_groups": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "List of groups the workspace owner belongs to.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Expand Down
5 changes: 5 additions & 0 deletions provider/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestWorkspace(t *testing.T) {
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_TEMPLATE_ID", "templateID")
t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123")
t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")
Expand Down Expand Up @@ -47,6 +48,8 @@ func TestWorkspace(t *testing.T) {
require.Equal(t, "Mr Owner", attribs["owner_name"])
require.Equal(t, "owner123@example.com", attribs["owner_email"])
require.Equal(t, "abc123", attribs["owner_session_token"])
require.Equal(t, "group1", attribs["owner_groups.0"])
require.Equal(t, "group2", attribs["owner_groups.1"])
require.Equal(t, "templateID", attribs["template_id"])
require.Equal(t, "template123", attribs["template_name"])
require.Equal(t, "v1.2.3", attribs["template_version"])
Expand Down Expand Up @@ -80,6 +83,8 @@ func TestWorkspace(t *testing.T) {
require.Equal(t, "owner123", attribs["owner"])
require.Equal(t, "Mr Owner", attribs["owner_name"])
require.Equal(t, "owner123@example.com", attribs["owner_email"])
require.Equal(t, "group1", attribs["owner_groups.0"])
require.Equal(t, "group2", attribs["owner_groups.1"])
require.Equal(t, "templateID", attribs["template_id"])
require.Equal(t, "template123", attribs["template_name"])
require.Equal(t, "v1.2.3", attribs["template_version"])
Expand Down

0 comments on commit 176fb6a

Please sign in to comment.