-
Notifications
You must be signed in to change notification settings - Fork 20
/
datasource_user.go
62 lines (54 loc) · 1.26 KB
/
datasource_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/slack-go/slack"
)
func dataSourceSlackUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceSlackUserRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"real_name": {
Type: schema.TypeString,
Optional: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func dataSourceSlackUserRead(d *schema.ResourceData, meta interface{}) error {
api := slack.New(meta.(*Config).APIToken)
email := d.Get("email").(string)
log.Printf("[INFO] Reading Slack user '%s'", email)
// user, err := api.GetUserByEmail(email)
// if err != nil {
// return fmt.Errorf("Invalid user with email '%s'", email)
// }
users, err := api.GetUsers()
if err != nil {
return err
}
for _, user := range users {
if user.Profile.Email == email {
log.Printf("[DEBUG] Slack user: %v", user)
d.SetId(user.ID)
d.Set("name", user.Name)
d.Set("real_name", user.RealName)
d.Set("email", user.Profile.Email)
return nil
}
}
return fmt.Errorf("Invalid user with email '%s'", email)
}