forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
85 lines (72 loc) · 1.67 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
package uaa
import (
"bytes"
"encoding/json"
"net/http"
"code.cloudfoundry.org/cli/api/uaa/internal"
)
// User represents an UAA user account.
type User struct {
ID string
}
// newUserRequestBody represents the body of the request.
type newUserRequestBody struct {
Username string `json:"userName"`
Password string `json:"password"`
Origin string `json:"origin"`
Name userName `json:"name"`
Emails []email `json:"emails"`
}
type userName struct {
FamilyName string `json:"familyName"`
GivenName string `json:"givenName"`
}
type email struct {
Value string `json:"value"`
Primary bool `json:"primary"`
}
// newUserResponse represents the HTTP JSON response.
type newUserResponse struct {
ID string `json:"id"`
}
// CreateUser creates a new UAA user account with the provided password.
func (client *Client) CreateUser(user string, password string, origin string) (User, error) {
userRequest := newUserRequestBody{
Username: user,
Password: password,
Origin: origin,
Name: userName{
FamilyName: user,
GivenName: user,
},
Emails: []email{
{
Value: user,
Primary: true,
},
},
}
bodyBytes, err := json.Marshal(userRequest)
if err != nil {
return User{}, err
}
request, err := client.newRequest(requestOptions{
RequestName: internal.PostUserRequest,
Header: http.Header{
"Content-Type": {"application/json"},
},
Body: bytes.NewBuffer(bodyBytes),
})
if err != nil {
return User{}, err
}
var userResponse newUserResponse
response := Response{
Result: &userResponse,
}
err = client.connection.Make(request, &response)
if err != nil {
return User{}, err
}
return User{ID: userResponse.ID}, nil
}