-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
112 lines (96 loc) · 2.79 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
// User is a spinup user object from the rest interface
type User struct {
First string
Last string
Email string
NetID string
}
// UserFetcher defines an interface for getting user details
type UserFetcher interface {
FetchByID(id string) (*User, error)
Configure(config map[string]string) error
}
// NewUserFetcher creates a new user fetcher and configures it
func NewUserFetcher(config map[string]string) (UserFetcher, error) {
log.Debugf("Creating a new user fetcher with config %+v", config)
switch config["type"] {
case "rest":
u := new(RESTUserFetcher)
err := u.Configure(config)
return u, err
}
return nil, fmt.Errorf("Couldn't find appropriate provider to create for %s", config["type"])
}
// RESTUserFetcher is the configuration detail for getting a user's details from a REST endpoint
type RESTUserFetcher struct {
Endpoint string
Token string
Client HTTPClient
}
// GetUserByID fetches a user by ID
func GetUserByID(f UserFetcher, id string) (*User, error) {
return f.FetchByID(id)
}
// Configure sets up a new RESTful User Fetcher
func (u *RESTUserFetcher) Configure(config map[string]string) error {
if _, ok := config["endpoint"]; !ok {
return fmt.Errorf("Endpoint required and not found in RESTUserFetch configuration")
}
u.Endpoint = config["endpoint"]
if _, ok := config["token"]; !ok {
return fmt.Errorf("Token required and not found in RESTUserFetch configuration")
}
u.Token = config["token"]
client := &http.Client{Timeout: 30 * time.Second}
if timeout, ok := config["timeout"]; ok {
t, err := time.ParseDuration(timeout)
if err != nil {
log.Errorf("Invalid timeout specified for RESTUserFetcher: %s", err)
return err
}
client.Timeout = t
}
u.Client = client
return nil
}
// FetchByID gets a user by ID from a REST endpoint
func (u *RESTUserFetcher) FetchByID(id string) (*User, error) {
url := fmt.Sprintf("%s/%s", u.Endpoint, id)
log.Debugf("Fetching user id %s with from %s", id, url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Forwarded-User", "reaper")
req.Header.Set("X-Auth-Token", u.Token)
req.Header.Set("Content-Type", "application/json")
res, err := u.Client.Do(req)
if err != nil {
return nil, err
}
log.Debug("HTTP response from REST User Fetch by ID:", res)
defer func() {
err := res.Body.Close()
if err != nil {
log.Error(err)
}
}()
if res.StatusCode > 299 {
return nil, fmt.Errorf("Got a non-success http response from http GET to %s, %d", url, res.StatusCode)
}
user := new(User)
err = json.NewDecoder(res.Body).Decode(user)
if err != nil {
log.Error("Failed to unmarshall request for user data:", err)
return nil, err
}
return user, nil
}