diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index f5b434a9..2ed7c63d 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -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. diff --git a/provider/workspace.go b/provider/workspace.go index be7bf03c..1511d2b5 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -2,6 +2,7 @@ package provider import ( "context" + "encoding/json" "os" "reflect" "strconv" @@ -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) @@ -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, diff --git a/provider/workspace_test.go b/provider/workspace_test.go index 38f9c743..78aeac4b 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -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") @@ -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"]) @@ -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"])