This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
124 lines (106 loc) · 3.54 KB
/
users.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
113
114
115
116
117
118
119
120
121
122
123
124
package resolver
import (
"github.com/opsee/basic/schema"
opsee "github.com/opsee/basic/service"
log "github.com/opsee/logrus"
"golang.org/x/net/context"
)
func (c *Client) ListCustomers(ctx context.Context, req *opsee.ListUsersRequest) (*opsee.ListCustomersResponse, error) {
log.Debug("list users request")
resp, err := c.Cats.ListUsers(ctx, req)
if err != nil {
log.WithError(err).Error("error listing users from cats")
return nil, err
}
// as a shim until we have a cats endpoint for listing customers, unique the customers with a map
customerIdMap := make(map[string][]*schema.User)
for _, user := range resp.Users {
_, ok := customerIdMap[user.CustomerId]
if !ok {
customerIdMap[user.CustomerId] = make([]*schema.User, 0)
}
customerIdMap[user.CustomerId] = append(customerIdMap[user.CustomerId], user)
}
customerIds := make([]string, 0, len(customerIdMap))
for customerId, _ := range customerIdMap {
customerIds = append(customerIds, customerId)
}
stateResp, err := c.Keelhaul.ListBastionStates(ctx, &opsee.ListBastionStatesRequest{CustomerIds: customerIds})
if err != nil {
log.WithError(err).Error("error listing users from keelhaul")
return nil, err
}
bastionStates := make(map[string][]*schema.BastionState)
for _, bastionState := range stateResp.BastionStates {
_, ok := bastionStates[bastionState.CustomerId]
if !ok {
bastionStates[bastionState.CustomerId] = make([]*schema.BastionState, 0)
}
bastionStates[bastionState.CustomerId] = append(bastionStates[bastionState.CustomerId], bastionState)
}
// this stuff is also a crummy shim for no customer listing endpoint
customers := make([]*schema.Customer, 0, len(customerIds))
for cid, userlist := range customerIdMap {
customers = append(customers, &schema.Customer{
Id: cid,
Name: userlist[0].Name,
CreatedAt: userlist[0].CreatedAt,
UpdatedAt: userlist[0].UpdatedAt,
Users: userlist[:1],
BastionStates: bastionStates[cid],
})
}
return &opsee.ListCustomersResponse{
Customers: customers,
Page: resp.Page,
PerPage: resp.PerPage,
Total: resp.Total,
}, nil
}
func (c *Client) GetUser(ctx context.Context, req *opsee.GetUserRequest) (*opsee.GetUserResponse, error) {
log.WithFields(log.Fields{
"customer_id": req.CustomerId,
"id": req.Id,
"email": req.Email,
}).Debug("get user request")
resp, err := c.Cats.GetUser(ctx, req)
if err != nil {
log.WithError(err).Error("error getting user from cats")
return nil, err
}
return resp, nil
}
func (c *Client) PutUser(ctx context.Context, req *opsee.UpdateUserRequest) (*schema.User, error) {
log.WithFields(log.Fields{
"customer_id": req.User.CustomerId,
"id": req.User.Id,
"email": req.User.Email,
}).Debug("update user request")
resp, err := c.Cats.UpdateUser(ctx, req)
if err != nil {
log.WithError(err).Error("error updating user")
return nil, err
}
return resp.User, nil
}
func (c *Client) InviteUser(ctx context.Context, req *opsee.InviteUserRequest) (*schema.User, error) {
log.WithFields(log.Fields{
"customer_id": req.Requestor.CustomerId,
"email": req.Email,
"perms": req.Perms.HighFlags(),
}).Debug("invite user request")
resp, err := c.Cats.InviteUser(ctx, req)
if err != nil {
log.WithError(err).Error("error inviting user")
return nil, err
}
if resp.Invite == nil {
return &schema.User{}, nil
}
return &schema.User{
Id: resp.Invite.Id,
Email: resp.Invite.Email,
CustomerId: resp.Invite.CustomerId,
Perms: resp.Invite.Perms,
}, nil
}