forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
140 lines (113 loc) · 3.47 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
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
package helpers
import (
"encoding/json"
"time"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
const NonUAAOrigin = "cli-oidc-provider"
type User struct {
GUID string
Username string
Origin string
CreatedAt time.Time
}
// ListUsers returns all the users in the targeted environment.
func GetUsers() []User {
var userPagesResponse struct {
NextURL *string `json:"next_url"`
Resources []struct {
Metadata struct {
GUID string `json:"guid"`
CreatedAt time.Time `json:"created_at"`
} `json:"metadata"`
Entity struct {
Username string `json:"username"`
} `json:"entity"`
} `json:"resources"`
}
var allUsers []User
nextURL := "/v2/users?results-per-page=50"
for {
session := CF("curl", nextURL)
Eventually(session).Should(Exit(0))
err := json.Unmarshal(session.Out.Contents(), &userPagesResponse)
Expect(err).NotTo(HaveOccurred())
for _, resource := range userPagesResponse.Resources {
allUsers = append(allUsers, User{
GUID: resource.Metadata.GUID,
CreatedAt: resource.Metadata.CreatedAt,
Username: resource.Entity.Username,
})
}
if userPagesResponse.NextURL == nil {
break
}
nextURL = *userPagesResponse.NextURL
}
return allUsers
}
func GetUsersV3() []User {
var userPagesResponse struct {
Pagination struct {
NextURL *string `json:"next_url"`
} `json:"pagination"`
Resources []struct {
GUID string `json:"guid"`
Origin string `json:"origin"`
Username string `json:"username"`
} `json:"resources"`
}
var allUsers []User
nextURL := "/v3/users?per_page=50"
for {
session := CF("curl", nextURL)
Eventually(session).Should(Exit(0))
err := json.Unmarshal(session.Out.Contents(), &userPagesResponse)
Expect(err).NotTo(HaveOccurred())
for _, resource := range userPagesResponse.Resources {
allUsers = append(allUsers, User{
Origin: resource.Origin,
Username: resource.Username,
})
}
if userPagesResponse.Pagination.NextURL == nil {
break
}
nextURL = *userPagesResponse.Pagination.NextURL
}
return allUsers
}
// CreateUser creates a user with a random username and password and returns both.
func CreateUser() (string, string) {
username := NewUsername()
password := RandomName()
// env := map[string]string{
// "NEW_USER_PASSWORD": password,
// }
// session := CFWithEnv(env, "create-user", username, "$NEW_USER_PASSWORD")
session := CF("create-user", username, password)
Eventually(session).Should(Exit(0))
return username, password
}
// DeleteUser deletes the user specified by username.
func DeleteUser(username string) {
session := CF("delete-user", username, "-f")
Eventually(session).Should(Exit(0))
}
// CreateUserInOrgRole creates a user with a random username and password and gives them the specified role within
// a specific org. The new user's username and password are returned.
func CreateUserInOrgRole(org, role string) (string, string) {
username, password := CreateUser()
session := CF("set-org-role", username, org, role)
Eventually(session).Should(Exit(0))
return username, password
}
// CreateUserInSpaceRole creates a user with a random username and password and gives them the specified role within
// a specific space. The new user's username and password are returned.
func CreateUserInSpaceRole(org, space, role string) (string, string) {
username, password := CreateUser()
session := CF("set-space-role", username, org, space, role)
Eventually(session).Should(Exit(0))
return username, password
}