-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
user.go
196 lines (172 loc) · 4.42 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/Pallinder/go-randomdata"
"github.com/jenkins-zh/jenkins-cli/util"
)
// UserClient for connect the user
type UserClient struct {
JenkinsCore
}
// Token is the token of user
type Token struct {
Status string
Data TokenData
}
// TokenData represents the token
type TokenData struct {
TokenName string
TokenUUID string
TokenValue string
}
// Get returns a user's detail
func (q *UserClient) Get() (status *User, err error) {
api := fmt.Sprintf("%s/user/%s/api/json", q.URL, q.UserName)
var (
req *http.Request
response *http.Response
)
req, err = http.NewRequest("GET", api, nil)
if err == nil {
q.AuthHandle(req)
} else {
return
}
client := q.GetClient()
if response, err = client.Do(req); err == nil {
code := response.StatusCode
var data []byte
data, err = ioutil.ReadAll(response.Body)
if code == 200 {
if err == nil {
status = &User{}
err = json.Unmarshal(data, status)
}
} else {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
return
}
// EditDesc update the description of a user
func (q *UserClient) EditDesc(description string) (err error) {
formData := url.Values{}
formData.Add("description", description)
payload := strings.NewReader(formData.Encode())
_, err = q.RequestWithoutData("POST", fmt.Sprintf("/user/%s/submitDescription", q.UserName), map[string]string{util.ContentType: util.ApplicationForm}, payload, 200)
return
}
// Delete will remove a user from Jenkins
func (q *UserClient) Delete(username string) (err error) {
_, err = q.RequestWithoutData("POST", fmt.Sprintf("/securityRealm/user/%s/doDelete", username), map[string]string{util.ContentType: util.ApplicationForm}, nil, 200)
return
}
// Create will create a user in Jenkins
func (q *UserClient) Create(username string) (user *UserForCreate, err error) {
api := fmt.Sprintf("%s/securityRealm/createAccountByAdmin", q.URL)
var (
req *http.Request
response *http.Response
)
passwd := util.GeneratePassword(8)
user = &UserForCreate{
User: User{FullName: username},
Username: username,
Password1: passwd,
Password2: passwd,
Email: fmt.Sprintf("%s@%s.com", username, username),
}
userData, _ := json.Marshal(user)
formData := url.Values{
"json": {string(userData)},
"username": {username},
"password1": {passwd},
"password2": {passwd},
"fullname": {username},
"email": {user.Email},
}
payload := strings.NewReader(formData.Encode())
req, err = http.NewRequest("POST", api, payload)
if err == nil {
q.AuthHandle(req)
} else {
return
}
req.Header.Set(util.ContentType, util.ApplicationForm)
client := q.GetClient()
if response, err = client.Do(req); err == nil {
code := response.StatusCode
var data []byte
data, err = ioutil.ReadAll(response.Body)
if code != 200 && code != 302 {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
return
}
// CreateToken create a token in Jenkins
func (q *UserClient) CreateToken(newTokenName string) (status *Token, err error) {
if newTokenName == "" {
newTokenName = fmt.Sprintf("jcli-%s", randomdata.SillyName())
}
api := fmt.Sprintf("%s/user/%s/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken", q.URL, q.UserName)
var (
req *http.Request
response *http.Response
)
formData := url.Values{}
formData.Add("newTokenName", newTokenName)
payload := strings.NewReader(formData.Encode())
req, err = http.NewRequest("POST", api, payload)
if err == nil {
q.AuthHandle(req)
} else {
return
}
req.Header.Set(util.ContentType, util.ApplicationForm)
if err = q.CrumbHandle(req); err != nil {
log.Fatal(err)
}
client := q.GetClient()
if response, err = client.Do(req); err == nil {
code := response.StatusCode
var data []byte
data, err = ioutil.ReadAll(response.Body)
if code == 200 {
if err == nil {
status = &Token{}
err = json.Unmarshal(data, status)
}
} else {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
return
}
// User for Jenkins
type User struct {
AbsoluteURL string `json:"absoluteUrl"`
Description string
FullName string `json:"fullname"`
ID string
}
// UserForCreate is the data for creatig a user
type UserForCreate struct {
User `json:",inline"`
Username string `json:"username"`
Password1 string `json:"password1"`
Password2 string `json:"password2"`
Email string `json:"email"`
}