Skip to content

Commit

Permalink
extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Jul 15, 2024
1 parent c2c2e07 commit c31e0d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
4 changes: 4 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
55 changes: 45 additions & 10 deletions internal/provider/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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))
Expand All @@ -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)...)
}
Expand Down

0 comments on commit c31e0d0

Please sign in to comment.