Skip to content

Commit d784267

Browse files
committed
feat: Add rbac_roles to coder_workspace_owner data source
1 parent 054e9bc commit d784267

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

docs/data-sources/workspace_owner.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ resource "coder_env" "git_author_email" {
4949
- `email` (String) The email address of the user.
5050
- `full_name` (String) The full name of the user.
5151
- `groups` (List of String) The groups of which the user is a member.
52+
- `rbac_roles` (List of String) The rbac roles of which the user is assigned.
5253
- `id` (String) The UUID of the workspace owner.
5354
- `login_type` (String) The type of login the user has.
5455
- `name` (String) The username of the user.

integration/integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func TestIntegration(t *testing.T) {
113113
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
114114
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
115115
"workspace_owner.login_type": ``,
116+
"workspace_owner.rbac_roles": `\[(\"member\")?\]`,
116117
},
117118
},
118119
{

integration/workspace-owner/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ locals {
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
4242
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
43+
"workspace_owner.rbac_roles" : data.coder_workspace_owner.me.rbac_roles,
4344
}
4445
}
4546

provider/workspace_owner.go

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ func workspaceOwnerDataSource() *schema.Resource {
5959
_ = rd.Set("login_type", loginType)
6060
}
6161

62+
var rbacRoles []string
63+
if rolesRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_RBAC_ROLES"); ok {
64+
if err := json.NewDecoder(strings.NewReader(rolesRaw)).Decode(&rbacRoles); err != nil {
65+
return diag.Errorf("invalid user rbac roles: %s", err.Error())
66+
}
67+
}
68+
_ = rd.Set("rbac_roles", rbacRoles)
69+
6270
return diags
6371
},
6472
Schema: map[string]*schema.Schema{
@@ -118,6 +126,14 @@ func workspaceOwnerDataSource() *schema.Resource {
118126
Computed: true,
119127
Description: "The type of login the user has.",
120128
},
129+
"rbac_roles": {
130+
Type: schema.TypeList,
131+
Elem: &schema.Schema{
132+
Type: schema.TypeString,
133+
},
134+
Computed: true,
135+
Description: "The RBAC roles of which the user is assigned.",
136+
},
121137
},
122138
}
123139
}

provider/workspace_owner_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
3434
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
3535
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`)
3636
t.Setenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE", `github`)
37+
t.Setenv("CODER_WORKSPACE_OWNER_RBAC_ROLES", `["member", "auditor"]`)
3738

3839
resource.Test(t, resource.TestCase{
3940
ProviderFactories: coderFactory(),
@@ -61,6 +62,8 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
6162
assert.Equal(t, `supersecret`, attrs["session_token"])
6263
assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"])
6364
assert.Equal(t, `github`, attrs["login_type"])
65+
assert.Equal(t, `member`, attrs["rbac_roles.0"])
66+
assert.Equal(t, `auditor`, attrs["rbac_roles.1"])
6467

6568
return nil
6669
},
@@ -80,6 +83,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
8083
"CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY",
8184
"CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY",
8285
"CODER_WORKSPACE_OWNER_LOGIN_TYPE",
86+
"CODER_WORKSPACE_OWNER_RBAC_ROLES",
8387
} { // https://github.com/golang/go/issues/52817
8488
t.Setenv(v, "")
8589
os.Unsetenv(v)
@@ -110,6 +114,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
110114
assert.Empty(t, attrs["session_token"])
111115
assert.Empty(t, attrs["oidc_access_token"])
112116
assert.Empty(t, attrs["login_type"])
117+
assert.Empty(t, attrs["rbac_roles.0"])
113118
return nil
114119
},
115120
}},

0 commit comments

Comments
 (0)