Skip to content

Commit c31e0d0

Browse files
committed
extra fields
1 parent c2c2e07 commit c31e0d0

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

docs/data-sources/user.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ An existing user on the coder deployment
2222

2323
### Read-Only
2424

25+
- `created_at` (Number) Unix timestamp of when the user was created.
2526
- `email` (String) Email of the user.
27+
- `last_seen_at` (Number) Unix timestamp of when the user was last seen.
2628
- `login_type` (String) Type of login for the user. Valid types are 'none', 'password', 'github', and 'oidc'.
2729
- `name` (String) Display name of the user. Defaults to username.
30+
- `organization_ids` (Set of String) IDs of organizations the user is associated with.
2831
- `roles` (Set of String) Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.
2932
- `suspended` (Boolean) Whether the user is suspended.
33+
- `theme_preference` (String) The user's preferred theme.

internal/provider/user_data_source.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ type UserDataSourceModel struct {
3232
ID types.String `tfsdk:"id"`
3333
Username types.String `tfsdk:"username"`
3434

35-
Name types.String `tfsdk:"name"`
36-
Email types.String `tfsdk:"email"`
37-
Roles types.Set `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit)
38-
LoginType types.String `tfsdk:"login_type"` // none, password, github, oidc
39-
Suspended types.Bool `tfsdk:"suspended"`
35+
Name types.String `tfsdk:"name"`
36+
Email types.String `tfsdk:"email"`
37+
Roles types.Set `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit)
38+
LoginType types.String `tfsdk:"login_type"` // none, password, github, oidc
39+
Suspended types.Bool `tfsdk:"suspended"`
40+
AvatarURL types.String `tfsdk:"avatar_url"`
41+
OrganizationIDs types.Set `tfsdk:"organization_ids"`
42+
CreatedAt types.Int64 `tfsdk:"created_at"` // Unix timestamp
43+
LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
44+
ThemePreference types.String `tfsdk:"theme_preference"`
4045
}
4146

4247
func (d *UserDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
@@ -57,14 +62,14 @@ func (d *UserDataSource) Schema(ctx context.Context, req datasource.SchemaReques
5762
MarkdownDescription: "The username of the user to retrieve. This field will be populated if an ID is supplied.",
5863
Optional: true,
5964
},
60-
"email": schema.StringAttribute{
61-
MarkdownDescription: "Email of the user.",
62-
Computed: true,
63-
},
6465
"name": schema.StringAttribute{
6566
MarkdownDescription: "Display name of the user. Defaults to username.",
6667
Computed: true,
6768
},
69+
"email": schema.StringAttribute{
70+
MarkdownDescription: "Email of the user.",
71+
Computed: true,
72+
},
6873
"roles": schema.SetAttribute{
6974
MarkdownDescription: "Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.",
7075
Computed: true,
@@ -78,6 +83,27 @@ func (d *UserDataSource) Schema(ctx context.Context, req datasource.SchemaReques
7883
MarkdownDescription: "Whether the user is suspended.",
7984
Computed: true,
8085
},
86+
"avatar_url": schema.StringAttribute{
87+
MarkdownDescription: "URL of the user's avatar.",
88+
Computed: true,
89+
},
90+
"organization_ids": schema.SetAttribute{
91+
MarkdownDescription: "IDs of organizations the user is associated with.",
92+
Computed: true,
93+
ElementType: types.StringType,
94+
},
95+
"created_at": schema.Int64Attribute{
96+
MarkdownDescription: "Unix timestamp of when the user was created.",
97+
Computed: true,
98+
},
99+
"last_seen_at": schema.Int64Attribute{
100+
MarkdownDescription: "Unix timestamp of when the user was last seen.",
101+
Computed: true,
102+
},
103+
"theme_preference": schema.StringAttribute{
104+
MarkdownDescription: "The user's preferred theme.",
105+
Computed: true,
106+
},
81107
},
82108
}
83109
}
@@ -131,9 +157,9 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
131157
}
132158

133159
data.ID = types.StringValue(user.ID.String())
134-
data.Email = types.StringValue(user.Email)
135160
data.Username = types.StringValue(user.Username)
136161
data.Name = types.StringValue(user.Name)
162+
data.Email = types.StringValue(user.Email)
137163
roles := make([]attr.Value, 0, len(user.Roles))
138164
for _, role := range user.Roles {
139165
roles = append(roles, types.StringValue(role.Name))
@@ -142,6 +168,15 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
142168
data.LoginType = types.StringValue(string(user.LoginType))
143169
data.Suspended = types.BoolValue(user.Status == codersdk.UserStatusSuspended)
144170

171+
orgIDs := make([]attr.Value, 0, len(user.OrganizationIDs))
172+
for _, orgID := range user.OrganizationIDs {
173+
orgIDs = append(orgIDs, types.StringValue(orgID.String()))
174+
}
175+
data.OrganizationIDs = types.SetValueMust(types.StringType, orgIDs)
176+
data.CreatedAt = types.Int64Value(user.CreatedAt.Unix())
177+
data.LastSeenAt = types.Int64Value(user.LastSeenAt.Unix())
178+
data.ThemePreference = types.StringValue(user.ThemePreference)
179+
145180
// Save data into Terraform state
146181
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
147182
}

0 commit comments

Comments
 (0)