Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add owner group to workspace data #204

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}
Comment on lines +40 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't guaranteed to be set :(

_ = 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", "[email protected]")
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, "[email protected]", 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, "[email protected]", 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
Loading