Skip to content

Commit

Permalink
data_source_user.go: allow either email or userid to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
bosc0 committed Jan 2, 2025
1 parent 74b1ab3 commit 70f2635
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
8 changes: 4 additions & 4 deletions docs/data-sources/user_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
page_title: "slack_user_data Data Source - slack"
subcategory: ""
description: |-
Retrieve Slack user information.
Retrieve Slack user information. Either user_id or email must be specified, but not both.
---

# slack_user_data (Data Source)

Retrieve Slack user information.
Retrieve Slack user information. Either `user_id` or `email` must be specified, but not both.



<!-- schema generated by tfplugindocs -->
## Schema

### Required
### Optional

- `email` (String) Email of the user to look up.
- `user_id` (String) Slack user ID to look up.

### Read-Only

- `display_name` (String) User's display name.
- `email` (String) Email of the user.
- `id` (String) Unique identifier for Terraform state.
- `real_name` (String) User's real name.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.4

require (
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/hashicorp/terraform-json v0.23.0 h1:sniCkExU4iKtTADReHzACkk8fnpQXrdD2
github.com/hashicorp/terraform-json v0.23.0/go.mod h1:MHdXbBAbSg0GvzuWazEGKAn/cyNfIB7mN6y7KJN6y2c=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 h1:O9QqGoYDzQT7lwTXUsZEtgabeWW96zUBh47Smn2lkFA=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
29 changes: 24 additions & 5 deletions internal/provider/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/slack-go/slack"
Expand Down Expand Up @@ -38,15 +41,21 @@ func (d *UserDataSource) Metadata(ctx context.Context, req datasource.MetadataRe

func (d *UserDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Retrieve Slack user information.",
MarkdownDescription: "Retrieve Slack user information. Either `user_id` or `email` must be specified, but not both.",
Attributes: map[string]schema.Attribute{
"user_id": schema.StringAttribute{
MarkdownDescription: "Slack user ID to look up.",
Required: true,
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRelative().AtParent().AtName("email")),
},
},
"email": schema.StringAttribute{
MarkdownDescription: "Email of the user.",
Computed: true,
MarkdownDescription: "Email of the user to look up.",
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRelative().AtParent().AtName("user_id")),
},
},
"real_name": schema.StringAttribute{
MarkdownDescription: "User's real name.",
Expand Down Expand Up @@ -90,7 +99,17 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
return
}

user, err := d.client.GetUserInfo(data.UserID.ValueString())
var (
user *slack.User
err error
)

if !data.UserID.IsNull() {
user, err = d.client.GetUserInfo(data.UserID.ValueString())
} else {
user, err = d.client.GetUserByEmail(data.Email.ValueString())
}

if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand Down

0 comments on commit 70f2635

Please sign in to comment.