forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
63 lines (52 loc) · 1.33 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
package ccv2
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// User represents a Cloud Controller User.
type User struct {
GUID string
}
// userRequestBody represents the body of the request.
type userRequestBody struct {
GUID string `json:"guid"`
}
// UnmarshalJSON helps unmarshal a Cloud Controller User response.
func (user *User) UnmarshalJSON(data []byte) error {
var ccUser struct {
Metadata internal.Metadata `json:"metadata"`
}
if err := json.Unmarshal(data, &ccUser); err != nil {
return err
}
user.GUID = ccUser.Metadata.GUID
return nil
}
// CreateUser creates a new Cloud Controller User from the provided UAA user
// ID.
func (client *Client) CreateUser(uaaUserID string) (User, Warnings, error) {
bodyBytes, err := json.Marshal(userRequestBody{
GUID: uaaUserID,
})
if err != nil {
return User{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostUserRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return User{}, nil, err
}
var user User
response := cloudcontroller.Response{
Result: &user,
}
err = client.connection.Make(request, &response)
if err != nil {
return User{}, response.Warnings, err
}
return user, response.Warnings, nil
}