From c31e0d013aa73eaeae8a0b848bf9ef1fb2c35572 Mon Sep 17 00:00:00 2001 From: Ethan Dickson Date: Mon, 15 Jul 2024 04:22:51 +0000 Subject: [PATCH] extra fields --- docs/data-sources/user.md | 4 ++ internal/provider/user_data_source.go | 55 ++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/docs/data-sources/user.md b/docs/data-sources/user.md index 74f6e80..88c9a1f 100644 --- a/docs/data-sources/user.md +++ b/docs/data-sources/user.md @@ -22,8 +22,12 @@ An existing user on the coder deployment ### Read-Only +- `created_at` (Number) Unix timestamp of when the user was created. - `email` (String) Email of the user. +- `last_seen_at` (Number) Unix timestamp of when the user was last seen. - `login_type` (String) Type of login for the user. Valid types are 'none', 'password', 'github', and 'oidc'. - `name` (String) Display name of the user. Defaults to username. +- `organization_ids` (Set of String) IDs of organizations the user is associated with. - `roles` (Set of String) Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'. - `suspended` (Boolean) Whether the user is suspended. +- `theme_preference` (String) The user's preferred theme. diff --git a/internal/provider/user_data_source.go b/internal/provider/user_data_source.go index 84dbcb5..0fdf762 100644 --- a/internal/provider/user_data_source.go +++ b/internal/provider/user_data_source.go @@ -32,11 +32,16 @@ type UserDataSourceModel struct { ID types.String `tfsdk:"id"` Username types.String `tfsdk:"username"` - Name types.String `tfsdk:"name"` - Email types.String `tfsdk:"email"` - Roles types.Set `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit) - LoginType types.String `tfsdk:"login_type"` // none, password, github, oidc - Suspended types.Bool `tfsdk:"suspended"` + Name types.String `tfsdk:"name"` + Email types.String `tfsdk:"email"` + Roles types.Set `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit) + LoginType types.String `tfsdk:"login_type"` // none, password, github, oidc + Suspended types.Bool `tfsdk:"suspended"` + AvatarURL types.String `tfsdk:"avatar_url"` + OrganizationIDs types.Set `tfsdk:"organization_ids"` + CreatedAt types.Int64 `tfsdk:"created_at"` // Unix timestamp + LastSeenAt types.Int64 `tfsdk:"last_seen_at"` + ThemePreference types.String `tfsdk:"theme_preference"` } 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 MarkdownDescription: "The username of the user to retrieve. This field will be populated if an ID is supplied.", Optional: true, }, - "email": schema.StringAttribute{ - MarkdownDescription: "Email of the user.", - Computed: true, - }, "name": schema.StringAttribute{ MarkdownDescription: "Display name of the user. Defaults to username.", Computed: true, }, + "email": schema.StringAttribute{ + MarkdownDescription: "Email of the user.", + Computed: true, + }, "roles": schema.SetAttribute{ MarkdownDescription: "Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.", Computed: true, @@ -78,6 +83,27 @@ func (d *UserDataSource) Schema(ctx context.Context, req datasource.SchemaReques MarkdownDescription: "Whether the user is suspended.", Computed: true, }, + "avatar_url": schema.StringAttribute{ + MarkdownDescription: "URL of the user's avatar.", + Computed: true, + }, + "organization_ids": schema.SetAttribute{ + MarkdownDescription: "IDs of organizations the user is associated with.", + Computed: true, + ElementType: types.StringType, + }, + "created_at": schema.Int64Attribute{ + MarkdownDescription: "Unix timestamp of when the user was created.", + Computed: true, + }, + "last_seen_at": schema.Int64Attribute{ + MarkdownDescription: "Unix timestamp of when the user was last seen.", + Computed: true, + }, + "theme_preference": schema.StringAttribute{ + MarkdownDescription: "The user's preferred theme.", + Computed: true, + }, }, } } @@ -131,9 +157,9 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r } data.ID = types.StringValue(user.ID.String()) - data.Email = types.StringValue(user.Email) data.Username = types.StringValue(user.Username) data.Name = types.StringValue(user.Name) + data.Email = types.StringValue(user.Email) roles := make([]attr.Value, 0, len(user.Roles)) for _, role := range user.Roles { roles = append(roles, types.StringValue(role.Name)) @@ -142,6 +168,15 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r data.LoginType = types.StringValue(string(user.LoginType)) data.Suspended = types.BoolValue(user.Status == codersdk.UserStatusSuspended) + orgIDs := make([]attr.Value, 0, len(user.OrganizationIDs)) + for _, orgID := range user.OrganizationIDs { + orgIDs = append(orgIDs, types.StringValue(orgID.String())) + } + data.OrganizationIDs = types.SetValueMust(types.StringType, orgIDs) + data.CreatedAt = types.Int64Value(user.CreatedAt.Unix()) + data.LastSeenAt = types.Int64Value(user.LastSeenAt.Unix()) + data.ThemePreference = types.StringValue(user.ThemePreference) + // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }