Skip to content

Commit

Permalink
Merge pull request #32 from billcchung/master
Browse files Browse the repository at this point in the history
Using GetUserByEmailContext to query user by email
  • Loading branch information
jmatsu authored Sep 5, 2020
2 parents d097860 + 081e17e commit 97ec4a6
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions slack/data_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"github.com/slack-go/slack"
)

const userListCacheFileName = "users.json"
const (
userListCacheFileName = "users.json"
userQueryTypeID = "id"
userQueryTypeName = "name"
userQueryTypeEmail = "email"
)

func dataSourceSlackUser() *schema.Resource {
return &schema.Resource{
Expand All @@ -19,7 +24,7 @@ func dataSourceSlackUser() *schema.Resource {
"query_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateEnums([]string{"id", "name", "email"}),
ValidateFunc: validateEnums([]string{userQueryTypeID, userQueryTypeName, userQueryTypeEmail}),
},
"query_value": {
Type: schema.TypeString,
Expand Down Expand Up @@ -72,7 +77,7 @@ func dataSourceSlackUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Team).client
ctx := context.WithValue(context.Background(), ctxId, queryValue)

if queryType == "id" {
if queryType == userQueryTypeID {
// https://api.slack.com/docs/rate-limits#tier_t4
user, err := client.GetUserInfoContext(ctx, queryValue)

Expand All @@ -84,6 +89,19 @@ func dataSourceSlackUserRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

if queryType == userQueryTypeEmail {
// https://api.slack.com/methods/users.lookupByEmail
// https://api.slack.com/docs/rate-limits#tier_t3
user, err := client.GetUserByEmailContext(ctx, queryValue)

if err != nil {
return err
}

configureUserFunc(d, *user)
return nil
}

// Use a cache for users api call because the limitation is more strict than user.info
var users *[]slack.User

Expand Down Expand Up @@ -115,9 +133,9 @@ func dataSourceSlackUserRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceSlackUserMatch(user *slack.User, queryType string, queryValue string) bool {
switch queryType {
case "name":
case userQueryTypeName:
return user.Name == queryValue || user.RealName == queryValue || user.Profile.DisplayName == queryValue
case "email":
case userQueryTypeEmail:
return user.Profile.Email == queryValue
}
return false
Expand Down

0 comments on commit 97ec4a6

Please sign in to comment.