Skip to content

Commit 176fb6a

Browse files
authored
feat: add owner group to workspace data (#204)
1 parent ec5b604 commit 176fb6a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

docs/data-sources/workspace.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ resource "kubernetes_pod" "dev" {
3232
- `name` (String) Name of the workspace.
3333
- `owner` (String) Username of the workspace owner.
3434
- `owner_email` (String) Email address of the workspace owner.
35+
- `owner_groups` (List of String) List of groups the workspace owner belongs to.
3536
- `owner_id` (String) UUID of the workspace owner.
3637
- `owner_name` (String) Name of the workspace owner.
3738
- `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.

provider/workspace.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"encoding/json"
56
"os"
67
"reflect"
78
"strconv"
@@ -36,6 +37,14 @@ func workspaceDataSource() *schema.Resource {
3637
ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL")
3738
_ = rd.Set("owner_email", ownerEmail)
3839

40+
ownerGroupsText := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS")
41+
var ownerGroups []string
42+
err := json.Unmarshal([]byte(ownerGroupsText), &ownerGroups)
43+
if err != nil {
44+
return diag.Errorf("couldn't parse owner groups %q", ownerGroupsText)
45+
}
46+
_ = rd.Set("owner_groups", ownerGroups)
47+
3948
ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME")
4049
_ = rd.Set("owner_name", ownerName)
4150

@@ -141,6 +150,14 @@ func workspaceDataSource() *schema.Resource {
141150
"This is only available if the workspace owner authenticated with OpenID Connect. " +
142151
"If a valid token cannot be obtained, this value will be an empty string.",
143152
},
153+
"owner_groups": {
154+
Type: schema.TypeList,
155+
Elem: &schema.Schema{
156+
Type: schema.TypeString,
157+
},
158+
Computed: true,
159+
Description: "List of groups the workspace owner belongs to.",
160+
},
144161
"id": {
145162
Type: schema.TypeString,
146163
Computed: true,

provider/workspace_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestWorkspace(t *testing.T) {
1616
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
1717
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "[email protected]")
1818
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123")
19+
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
1920
t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID")
2021
t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123")
2122
t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")
@@ -47,6 +48,8 @@ func TestWorkspace(t *testing.T) {
4748
require.Equal(t, "Mr Owner", attribs["owner_name"])
4849
require.Equal(t, "[email protected]", attribs["owner_email"])
4950
require.Equal(t, "abc123", attribs["owner_session_token"])
51+
require.Equal(t, "group1", attribs["owner_groups.0"])
52+
require.Equal(t, "group2", attribs["owner_groups.1"])
5053
require.Equal(t, "templateID", attribs["template_id"])
5154
require.Equal(t, "template123", attribs["template_name"])
5255
require.Equal(t, "v1.2.3", attribs["template_version"])
@@ -80,6 +83,8 @@ func TestWorkspace(t *testing.T) {
8083
require.Equal(t, "owner123", attribs["owner"])
8184
require.Equal(t, "Mr Owner", attribs["owner_name"])
8285
require.Equal(t, "[email protected]", attribs["owner_email"])
86+
require.Equal(t, "group1", attribs["owner_groups.0"])
87+
require.Equal(t, "group2", attribs["owner_groups.1"])
8388
require.Equal(t, "templateID", attribs["template_id"])
8489
require.Equal(t, "template123", attribs["template_name"])
8590
require.Equal(t, "v1.2.3", attribs["template_version"])

0 commit comments

Comments
 (0)