forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
169 lines (149 loc) · 4.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type UserRequest struct {
Guid string `json:"guid"`
DefaultSpaceGuid string `json:"default_space_guid,omitempty"`
}
type Users []User
type User struct {
Guid string `json:"guid"`
Admin bool `json:"admin"`
Active bool `json:"active"`
DefaultSpaceGUID string `json:"default_space_guid"`
Username string `json:"username"`
SpacesURL string `json:"spaces_url"`
OrgsURL string `json:"organizations_url"`
ManagedOrgsURL string `json:"managed_organizations_url"`
BillingManagedOrgsURL string `json:"billing_managed_organizations_url"`
AuditedOrgsURL string `json:"audited_organizations_url"`
ManagedSpacesURL string `json:"managed_spaces_url"`
AuditedSpacesURL string `json:"audited_spaces_url"`
c *Client
}
type UserResource struct {
Meta Meta `json:"metadata"`
Entity User `json:"entity"`
}
type UserResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []UserResource `json:"resources"`
}
func (c *Client) ListUsersByQuery(query url.Values) (Users, error) {
var users []User
requestUrl := "/v2/users?" + query.Encode()
for {
userResp, err := c.getUserResponse(requestUrl)
if err != nil {
return []User{}, err
}
for _, user := range userResp.Resources {
user.Entity.Guid = user.Meta.Guid
user.Entity.c = c
users = append(users, user.Entity)
}
requestUrl = userResp.NextUrl
if requestUrl == "" {
break
}
}
return users, nil
}
func (c *Client) ListUsers() (Users, error) {
return c.ListUsersByQuery(nil)
}
func (c *Client) ListUserSpaces(userGuid string) ([]Space, error) {
return c.fetchSpaces(fmt.Sprintf("/v2/users/%s/spaces", userGuid))
}
func (c *Client) ListUserAuditedSpaces(userGuid string) ([]Space, error) {
return c.fetchSpaces(fmt.Sprintf("/v2/users/%s/audited_spaces", userGuid))
}
func (c *Client) ListUserManagedSpaces(userGuid string) ([]Space, error) {
return c.fetchSpaces(fmt.Sprintf("/v2/users/%s/managed_spaces", userGuid))
}
func (c *Client) ListUserOrgs(userGuid string) ([]Org, error) {
return c.fetchOrgs(fmt.Sprintf("/v2/users/%s/organizations", userGuid))
}
func (c *Client) ListUserManagedOrgs(userGuid string) ([]Org, error) {
return c.fetchOrgs(fmt.Sprintf("/v2/users/%s/managed_organizations", userGuid))
}
func (c *Client) ListUserAuditedOrgs(userGuid string) ([]Org, error) {
return c.fetchOrgs(fmt.Sprintf("/v2/users/%s/audited_organizations", userGuid))
}
func (c *Client) ListUserBillingManagedOrgs(userGuid string) ([]Org, error) {
return c.fetchOrgs(fmt.Sprintf("/v2/users/%s/billing_managed_organizations", userGuid))
}
func (c *Client) CreateUser(req UserRequest) (User, error) {
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
return User{}, err
}
r := c.NewRequestWithBody("POST", "/v2/users", buf)
resp, err := c.DoRequest(r)
if err != nil {
return User{}, err
}
if resp.StatusCode != http.StatusCreated {
return User{}, errors.Wrapf(err, "Error creating user, response code: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return User{}, err
}
var userResource UserResource
err = json.Unmarshal(body, &userResource)
if err != nil {
return User{}, err
}
user := userResource.Entity
user.Guid = userResource.Meta.Guid
user.c = c
return user, nil
}
func (c *Client) DeleteUser(userGuid string) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/users/%s", userGuid)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting user %s, response code: %d", userGuid, resp.StatusCode)
}
return nil
}
func (u Users) GetUserByUsername(username string) User {
for _, user := range u {
if user.Username == username {
return user
}
}
return User{}
}
func (c *Client) getUserResponse(requestUrl string) (UserResponse, error) {
var userResp UserResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return UserResponse{}, errors.Wrap(err, "Error requesting users")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return UserResponse{}, errors.Wrap(err, "Error reading user request")
}
err = json.Unmarshal(resBody, &userResp)
if err != nil {
return UserResponse{}, errors.Wrap(err, "Error unmarshalling user")
}
return userResp, nil
}