Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 748a714

Browse files
committedMay 24, 2024·
rename to coder_workspace_owner
1 parent 1fc56bb commit 748a714

File tree

7 files changed

+87
-74
lines changed

7 files changed

+87
-74
lines changed
 

‎docs/data-sources/user.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "coder_user Data Source - terraform-provider-coder"
44
subcategory: ""
55
description: |-
6-
Use this data source to fetch information about a user.
6+
Use this data source to fetch information about the workspace owner.
77
---
88

99
# coder_user (Data Source)
1010

11-
Use this data source to fetch information about a user.
11+
Use this data source to fetch information about the workspace owner.
1212

1313

1414

@@ -20,8 +20,9 @@ Use this data source to fetch information about a user.
2020
- `email` (String) The email address of the user.
2121
- `full_name` (String) The full name of the user.
2222
- `groups` (List of String) The groups of which the user is a member.
23-
- `id` (String) The UUID of the user.
23+
- `id` (String) The UUID of the workspace owner.
2424
- `name` (String) The username of the user.
25+
- `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.
2526
- `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.
2627
- `ssh_private_key` (String, Sensitive) The user's generated SSH private key.
2728
- `ssh_public_key` (String) The user's generated SSH public key.

‎docs/data-sources/workspace.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ resource "kubernetes_pod" "dev" {
3030
- `access_url` (String) The access URL of the Coder deployment provisioning this workspace.
3131
- `id` (String) UUID of the workspace.
3232
- `name` (String) Name of the workspace.
33-
- `owner` (String) Username of the workspace owner.
34-
- `owner_email` (String) Email address of the workspace owner.
35-
- `owner_groups` (List of String) List of groups the workspace owner belongs to.
36-
- `owner_id` (String) UUID of the workspace owner.
37-
- `owner_name` (String) Name of the workspace owner.
38-
- `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.
39-
- `owner_session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.
33+
- `owner` (String, Deprecated) Username of the workspace owner.
34+
- `owner_email` (String, Deprecated) Email address of the workspace owner.
35+
- `owner_groups` (List of String, Deprecated) List of groups the workspace owner belongs to.
36+
- `owner_id` (String, Deprecated) UUID of the workspace owner.
37+
- `owner_name` (String, Deprecated) Name of the workspace owner.
38+
- `owner_oidc_access_token` (String, Deprecated) 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.
39+
- `owner_session_token` (String, Deprecated) Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.
4040
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
4141
- `template_id` (String) ID of the workspace's template.
4242
- `template_name` (String) Name of the workspace's template.

‎provider/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func New() *schema.Provider {
7474
"coder_parameter": parameterDataSource(),
7575
"coder_git_auth": gitAuthDataSource(),
7676
"coder_external_auth": externalAuthDataSource(),
77-
"coder_user": userDataSource(),
77+
"coder_user": workspaceOwnerDataSource(),
7878
},
7979
ResourcesMap: map[string]*schema.Resource{
8080
"coder_agent": agentResource(),

‎provider/workspace.go

+7
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,33 @@ func workspaceDataSource() *schema.Resource {
135135
Type: schema.TypeString,
136136
Computed: true,
137137
Description: "Username of the workspace owner.",
138+
Deprecated: "Use `coder_workspace_owner.name` instead.",
138139
},
139140
"owner_email": {
140141
Type: schema.TypeString,
141142
Computed: true,
142143
Description: "Email address of the workspace owner.",
144+
Deprecated: "Use `coder_workspace_owner.email` instead.",
143145
},
144146
"owner_id": {
145147
Type: schema.TypeString,
146148
Computed: true,
147149
Description: "UUID of the workspace owner.",
150+
Deprecated: "Use `coder_workspace_owner.id` instead.",
148151
},
149152
"owner_name": {
150153
Type: schema.TypeString,
151154
Computed: true,
152155
Description: "Name of the workspace owner.",
156+
Deprecated: "Use `coder_workspace_owner.full_name` instead.",
153157
},
154158
"owner_oidc_access_token": {
155159
Type: schema.TypeString,
156160
Computed: true,
157161
Description: "A valid OpenID Connect access token of the workspace owner. " +
158162
"This is only available if the workspace owner authenticated with OpenID Connect. " +
159163
"If a valid token cannot be obtained, this value will be an empty string.",
164+
Deprecated: "Use `coder_workspace_owner.oidc_access_token` instead.",
160165
},
161166
"owner_groups": {
162167
Type: schema.TypeList,
@@ -165,6 +170,7 @@ func workspaceDataSource() *schema.Resource {
165170
},
166171
Computed: true,
167172
Description: "List of groups the workspace owner belongs to.",
173+
Deprecated: "Use `coder_workspace_owner.groups` instead.",
168174
},
169175
"id": {
170176
Type: schema.TypeString,
@@ -180,6 +186,7 @@ func workspaceDataSource() *schema.Resource {
180186
Type: schema.TypeString,
181187
Computed: true,
182188
Description: "Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.",
189+
Deprecated: "Use `coder_workspace_owner.session_token` instead.",
183190
},
184191
"template_id": {
185192
Type: schema.TypeString,

‎provider/user.go renamed to ‎provider/workspace_owner.go

+29-41
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,56 @@ type Role struct {
1616
DisplayName string `json:"display-name"`
1717
}
1818

19-
func userDataSource() *schema.Resource {
19+
func workspaceOwnerDataSource() *schema.Resource {
2020
return &schema.Resource{
21-
Description: "Use this data source to fetch information about a user.",
21+
Description: "Use this data source to fetch information about the workspace owner.",
2222
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
23-
if idStr, ok := os.LookupEnv("CODER_USER_ID"); !ok {
24-
rd.SetId(uuid.NewString())
25-
} else {
23+
if idStr, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_ID"); ok {
2624
rd.SetId(idStr)
25+
} else {
26+
rd.SetId(uuid.NewString())
2727
}
2828

29-
if username, ok := os.LookupEnv("CODER_USER_NAME"); ok {
29+
if username, ok := os.LookupEnv("CODER_WORKSPACE_OWNER"); ok {
3030
_ = rd.Set("name", username)
31-
} else if altUsername, ok := os.LookupEnv("CODER_WORKSPACE_OWNER"); ok {
32-
_ = rd.Set("name", altUsername)
3331
} else {
34-
return diag.Errorf("missing user name")
32+
_ = rd.Set("name", "default")
3533
}
3634

37-
if fullname, ok := os.LookupEnv("CODER_USER_FULL_NAME"); ok {
35+
if fullname, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_NAME"); ok {
3836
_ = rd.Set("full_name", fullname)
39-
} else if altFullname, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_NAME"); ok {
40-
// Compatibility: read from CODER_WORKSPACE_OWNER_NAME
41-
_ = rd.Set("full_name", altFullname)
42-
} else { // fallback
43-
return diag.Errorf("missing user full_name")
37+
} else { // compat: field can be blank, fill in default
38+
_ = rd.Set("full_name", "default")
4439
}
4540

46-
if email, ok := os.LookupEnv("CODER_USER_EMAIL"); ok {
41+
if email, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_EMAIL"); ok {
4742
_ = rd.Set("email", email)
48-
} else if altEmail, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_EMAIL"); ok {
49-
_ = rd.Set("email", altEmail)
5043
} else {
51-
return diag.Errorf("missing user email")
44+
_ = rd.Set("email", "default@example.com")
5245
}
5346

54-
if sshPubKey, ok := os.LookupEnv("CODER_USER_SSH_PUBLIC_KEY"); ok {
47+
if sshPubKey, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY"); ok {
5548
_ = rd.Set("ssh_public_key", sshPubKey)
56-
} else {
57-
// Compat: do not error
58-
_ = rd.Set("ssh_public_key", "missing")
5949
}
6050

61-
if sshPrivKey, ok := os.LookupEnv("CODER_USER_SSH_PRIVATE_KEY"); ok {
51+
if sshPrivKey, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY"); ok {
6252
_ = rd.Set("ssh_private_key", sshPrivKey)
63-
} else {
64-
// Compat: do not error
65-
_ = rd.Set("ssh_private_key", "missing")
6653
}
6754

6855
var groups []string
69-
if groupsRaw, ok := os.LookupEnv("CODER_USER_GROUPS"); ok {
56+
if groupsRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_GROUPS"); ok {
7057
if err := json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups); err != nil {
7158
return diag.Errorf("invalid user groups: %s", err.Error())
7259
}
73-
} else if altGroupsRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_GROUPS"); ok {
74-
if err := json.NewDecoder(strings.NewReader(altGroupsRaw)).Decode(&groups); err != nil {
75-
return diag.Errorf("invalid workspace owner groups: %s", err.Error())
76-
}
77-
} else {
78-
return diag.Errorf("missing user groups")
60+
_ = rd.Set("groups", groups)
7961
}
80-
_ = rd.Set("groups", groups)
8162

82-
if tok, ok := os.LookupEnv("CODER_USER_SESSION_TOKEN"); ok {
63+
if tok, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"); ok {
8364
_ = rd.Set("session_token", tok)
84-
} else if altTok, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"); ok {
85-
_ = rd.Set("session_token", altTok)
86-
} else {
87-
return diag.Errorf("missing user session_token")
65+
}
66+
67+
if tok, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN"); ok {
68+
_ = rd.Set("oidc_access_token", tok)
8869
}
8970

9071
return nil
@@ -93,7 +74,7 @@ func userDataSource() *schema.Resource {
9374
"id": {
9475
Type: schema.TypeString,
9576
Computed: true,
96-
Description: "The UUID of the user.",
77+
Description: "The UUID of the workspace owner.",
9778
},
9879
"name": {
9980
Type: schema.TypeString,
@@ -134,6 +115,13 @@ func userDataSource() *schema.Resource {
134115
Computed: true,
135116
Description: "Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.",
136117
},
118+
"oidc_access_token": {
119+
Type: schema.TypeString,
120+
Computed: true,
121+
Description: "A valid OpenID Connect access token of the workspace owner. " +
122+
"This is only available if the workspace owner authenticated with OpenID Connect. " +
123+
"If a valid token cannot be obtained, this value will be an empty string.",
124+
},
137125
},
138126
}
139127
}

‎provider/user_test.go renamed to ‎provider/workspace_owner_test.go

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/coder/terraform-provider-coder/provider"
@@ -23,16 +24,17 @@ const (
2324
-----END OPENSSH PRIVATE KEY-----`
2425
)
2526

26-
func TestUserDatasource(t *testing.T) {
27+
func TestWorkspaceOwnerDatasource(t *testing.T) {
2728
t.Run("OK", func(t *testing.T) {
28-
t.Setenv("CODER_USER_ID", "11111111-1111-1111-1111-111111111111")
29-
t.Setenv("CODER_USER_NAME", "owner123")
30-
t.Setenv("CODER_USER_FULL_NAME", "Mr Owner")
31-
t.Setenv("CODER_USER_EMAIL", "owner123@example.com")
32-
t.Setenv("CODER_USER_SSH_PUBLIC_KEY", testSSHEd25519PublicKey)
33-
t.Setenv("CODER_USER_SSH_PRIVATE_KEY", testSSHEd25519PrivateKey)
34-
t.Setenv("CODER_USER_GROUPS", `["group1", "group2"]`)
35-
t.Setenv("CODER_USER_SESSION_TOKEN", `supersecret`)
29+
t.Setenv("CODER_WORKSPACE_OWNER_ID", "11111111-1111-1111-1111-111111111111")
30+
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
31+
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
32+
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com")
33+
t.Setenv("CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY", testSSHEd25519PublicKey)
34+
t.Setenv("CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY", testSSHEd25519PrivateKey)
35+
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
36+
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
37+
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`)
3638

3739
resource.Test(t, resource.TestCase{
3840
Providers: map[string]*schema.Provider{
@@ -60,18 +62,28 @@ func TestUserDatasource(t *testing.T) {
6062
assert.Equal(t, `group1`, attrs["groups.0"])
6163
assert.Equal(t, `group2`, attrs["groups.1"])
6264
assert.Equal(t, `supersecret`, attrs["session_token"])
65+
assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"])
6366
return nil
6467
},
6568
}},
6669
})
6770
})
6871

69-
t.Run("Compat", func(t *testing.T) {
70-
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
71-
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
72-
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com")
73-
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
74-
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
72+
t.Run("Defaults", func(t *testing.T) {
73+
for _, v := range []string{
74+
"CODER_WORKSPACE_OWNER",
75+
"CODER_WORKSPACE_OWNER_ID",
76+
"CODER_WORKSPACE_OWNER_EMAIL",
77+
"CODER_WORKSPACE_OWNER_NAME",
78+
"CODER_WORKSPACE_OWNER_SESSION_TOKEN",
79+
"CODER_WORKSPACE_OWNER_GROUPS",
80+
"CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN",
81+
"CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY",
82+
"CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY",
83+
} { // https://github.com/golang/go/issues/52817
84+
t.Setenv(v, "")
85+
os.Unsetenv(v)
86+
}
7587

7688
resource.Test(t, resource.TestCase{
7789
Providers: map[string]*schema.Provider{
@@ -91,13 +103,14 @@ func TestUserDatasource(t *testing.T) {
91103

92104
attrs := resource.Primary.Attributes
93105
assert.NotEmpty(t, attrs["id"])
94-
assert.Equal(t, "owner123", attrs["name"])
95-
assert.Equal(t, "Mr Owner", attrs["full_name"])
96-
assert.Equal(t, "owner123@example.com", attrs["email"])
97-
assert.Equal(t, "missing", attrs["ssh_public_key"])
98-
assert.Equal(t, "missing", attrs["ssh_private_key"])
99-
assert.Equal(t, `group1`, attrs["groups.0"])
100-
assert.Equal(t, `group2`, attrs["groups.1"])
106+
assert.Equal(t, "default", attrs["name"])
107+
assert.Equal(t, "default", attrs["full_name"])
108+
assert.Equal(t, "default@example.com", attrs["email"])
109+
assert.Empty(t, attrs["ssh_public_key"])
110+
assert.Empty(t, attrs["ssh_private_key"])
111+
assert.Empty(t, attrs["groups.0"])
112+
assert.Empty(t, attrs["session_token"])
113+
assert.Empty(t, attrs["oidc_access_token"])
101114
return nil
102115
},
103116
}},

‎provider/workspace_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414

1515
func TestWorkspace(t *testing.T) {
1616
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
17+
t.Setenv("CODER_WORKSPACE_OWNER_ID", "11111111-1111-1111-1111-111111111111")
1718
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner")
1819
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com")
1920
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123")
2021
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
22+
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", "supersecret")
2123
t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID")
2224
t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123")
2325
t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")
@@ -47,13 +49,15 @@ func TestWorkspace(t *testing.T) {
4749
assert.Equal(t, "https://example.com:8080", attribs["access_url"])
4850
assert.Equal(t, "8080", attribs["access_port"])
4951
assert.Equal(t, "owner123", attribs["owner"])
52+
assert.Equal(t, "11111111-1111-1111-1111-111111111111", attribs["owner_id"])
5053
assert.Equal(t, "Mr Owner", attribs["owner_name"])
5154
assert.Equal(t, "owner123@example.com", attribs["owner_email"])
5255
assert.Equal(t, "group1", attribs["owner_groups.0"])
5356
assert.Equal(t, "group2", attribs["owner_groups.1"])
5457
assert.Equal(t, "templateID", attribs["template_id"])
5558
assert.Equal(t, "template123", attribs["template_name"])
5659
assert.Equal(t, "v1.2.3", attribs["template_version"])
60+
assert.Equal(t, "supersecret", attribs["owner_oidc_access_token"])
5761
return nil
5862
},
5963
}},

0 commit comments

Comments
 (0)
Please sign in to comment.