-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
119 lines (102 loc) · 2.42 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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
)
func getUserFromUsername(harborServer, user, password, apiVersion, userName string) (UserResponse, error) {
url := fmt.Sprintf("%v/api/%vusers?username=%v",
harborServer,
apiVersion,
userName,
)
return getUser(url, user, password)
}
func getUserFromEmail(harborServer, user, password, apiVersion, email string) (UserResponse, error) {
url := fmt.Sprintf("%v/api/%vusers?email=%v",
harborServer,
apiVersion,
email,
)
return getUser(url, user, password)
}
func getUser(url, user, pass string) (UserResponse, error) {
response := []UserResponse{}
res, body := client(
ClientPrt{
Url: url,
Method: "GET",
ContentType: "application/json",
User: user,
Password: pass,
},
)
if res.StatusCode < 399 && res.StatusCode > 100 {
json.Unmarshal([]byte(body), &response)
} else {
log.Fatal("Error getting Ldap information")
}
if len(response) > 0 {
return response[0], nil
}
return UserResponse{}, errors.New("User not found")
}
func getSourceUsers(harborServer, user, pass, apiVersion string) (UserListResponse, error) {
var users UserListResponse
cnt := 1
for {
url := fmt.Sprintf("%v/api/%vusers?page=%v",
harborServer,
apiVersion,
cnt)
res, body := client(
ClientPrt{
Url: url,
Method: "GET",
ContentType: "application/json",
User: user,
Password: pass,
},
)
if res.StatusCode < 399 && res.StatusCode > 100 {
var u UserListResponse
json.Unmarshal([]byte(body), &u)
users = append(users, u...)
} else {
log.Fatal("Error getting users")
}
l := res.Header.Get("Link")
if strings.Contains(l, "next") {
cnt++
} else {
break
}
}
if len(users) > 0 {
return users, nil
}
return users, errors.New("Error getting users")
}
func importLdapUser(harborServer, user, pass, apiVersion string, userImport UserImport) {
url := fmt.Sprintf("%v/api/%vldap/users/import",
harborServer,
apiVersion,
)
res, _ := client(
ClientPrt{
Url: url,
Method: "POST",
ContentType: "application/json",
User: user,
Password: pass,
Body: userImport,
},
)
if res.StatusCode < 100 || (res.StatusCode > 399 && res.StatusCode != 409) {
//print status code as if user not found to import it returns 404
log.Println(res.StatusCode)
log.Fatal("Error importing ldap user")
}
}