-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.go
216 lines (188 loc) · 6.1 KB
/
robot.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"gorm.io/gorm"
)
// https://registry-dub.tools.3stripes.net/api/v2.0/robots?q=Level%3Dproject%2CProjectID%3D35&page_size=15&page=1
func queryByProyect(project ProjectResponse) string {
return fmt.Sprint("&q=Level%3Dproject%2CProjectID%3D", project.ProjectId)
}
func _getRobots(harborServer, user, pass, apiVersion, query string) ([]RobotResponse, error) {
var robots []RobotResponse
cnt := 1
for {
url := fmt.Sprintf("%v/api/%vrobots?page=%v%s",
harborServer,
apiVersion,
cnt,
query)
res, body := client(
ClientPrt{
Url: url,
Method: "GET",
ContentType: "application/json",
User: user,
Password: pass,
},
)
if res.StatusCode < 399 && res.StatusCode > 100 {
var aux []RobotResponse
json.Unmarshal([]byte(body), &aux)
robots = append(robots, aux...)
} else {
log.Fatal("Error getting robots")
}
l := res.Header.Get("Link")
if strings.Contains(l, "next") {
cnt++
} else {
break
}
}
if len(robots) > 0 {
return robots, nil
}
return robots, errors.New("Error getting robots")
}
func getRobots(harborServer, user, pass, apiVersion string) []RobotResponse {
// getPublicRobots
robots, _ := _getRobots(harborServer, user, pass, apiVersion, "")
// getProyects
projects := listProjects(harborServer, user, pass, apiVersion)
// var proyectsRobots []RobotResponse
for _, project := range projects {
aux, _ := _getRobots(harborServer, user, pass, apiVersion, queryByProyect(project))
robots = append(robots, aux...)
}
return robots
}
func updateRobotTarget(harborTarget, userTarget, passTarget, apiVersionTarget string, robotSource, robotTarget RobotResponse) {
url := fmt.Sprintf("%v/api/%vrobots/%d",
harborTarget,
apiVersionTarget,
robotTarget.ID,
)
id := robotSource.ID
robotSource.ID = robotTarget.ID
res, _ := client(
ClientPrt{
Url: url,
Method: "PUT",
ContentType: "application/json",
User: userTarget,
Password: passTarget,
Body: robotSource,
},
)
robotSource.ID = id
if res.StatusCode < 100 || (res.StatusCode > 399 && res.StatusCode != 409) {
// print status code because some groups might not be valid anymore and import should continue
fmt.Printf("Status Code: %d while trying to update robot, robotSource: %s, robotTarget: %s\n", res.StatusCode, robotSource, robotTarget)
} else {
fmt.Printf("Robot updated: %s\n", robotTarget)
}
}
func updateRobotDb(dbSource, dbTarget *gorm.DB, robotSourceId, robotTargetId int) {
var dbRobotSource, dbRobotTarget DbRobot
dbSource.First(&dbRobotSource, robotSourceId)
dbTarget.First(&dbRobotTarget, robotTargetId)
if dbRobotTarget.Secret != dbRobotSource.Secret || dbRobotTarget.Salt != dbRobotSource.Salt {
dbRobotTarget.Secret = dbRobotSource.Secret
dbRobotTarget.Salt = dbRobotSource.Salt
dbTarget.Save(&dbRobotTarget)
fmt.Printf("UpdateRobot secret, robot_source: %s robot_target: %s\n", dbRobotSource, dbRobotTarget)
}
}
func createRobotTarget(harborTarget, userTarget, passTarget, apiVersionTarget string, robot RobotResponse) RobotCreatedReponse {
url := fmt.Sprintf("%v/api/%vrobots",
harborTarget,
apiVersionTarget,
)
name := robot.Name
if robot.Level == "system" {
robot.Name = strings.Split(robot.Name, "$")[1]
} else if robot.Level == "project" {
robot.Name = strings.Split(robot.Name, "+")[1]
} else {
fmt.Printf("Error when try identified robot type. Robot: %s\n", robot)
}
id := robot.ID
robot.ID = 0
res, body := client(
ClientPrt{
Url: url,
Method: "POST",
ContentType: "application/json",
User: userTarget,
Password: passTarget,
Body: robot,
},
)
robot.ID = id
robot.Name = name
var robotResponse RobotCreatedReponse
if res.StatusCode < 100 || (res.StatusCode > 399 && res.StatusCode != 409) {
// print status code because some groups might not be valid anymore and import should continue
fmt.Printf("Status Code: %d while trying to create robot: %s\n", res.StatusCode, robot)
} else {
fmt.Printf("Robot created: %s\n", robot)
json.Unmarshal([]byte(body), &robotResponse)
}
return robotResponse
}
func deleteRobot(harborServer, user, pass, apiVersion string, robot RobotResponse) {
url := fmt.Sprintf("%v/api/%vrobots/%d",
harborServer,
apiVersion,
robot.ID)
res, _ := client(
ClientPrt{
Url: url,
Method: "DELETE",
ContentType: "application/json",
User: user,
Password: pass,
},
)
if res.StatusCode < 100 || (res.StatusCode > 399 && res.StatusCode != 409) {
// print status code because some groups might not be valid anymore and import should continue
fmt.Printf("Status Code: %d while trying to delete robot: %s", res.StatusCode, robot)
} else {
fmt.Printf("Robot %s deleted: ", robot)
}
}
func syncRobots(harborSource, userSource, passSource, apiVersionSource, harborTarget, userTarget, passTarget, apiVersionTarget string, dbSource, dbTarget *gorm.DB) {
robotsSource := getRobots(harborSource, userSource, passSource, apiVersionSource)
robotsTarget := getRobots(harborTarget, userTarget, passTarget, apiVersionSource)
mRobotsTarget := make(map[string]RobotResponse)
mRobotsSource := make(map[string]RobotResponse)
for _, robot := range robotsTarget {
mRobotsTarget[robot.Name] = robot
}
for _, robot := range robotsSource {
mRobotsSource[robot.Name] = robot
}
for _, robotSource := range robotsSource {
// robot account is not legacy
if robotSource.Editable {
if robotTarget, ok := mRobotsTarget[robotSource.Name]; ok {
updateRobotTarget(harborTarget, userTarget, passTarget, apiVersionTarget, robotSource, robotTarget)
updateRobotDb(dbSource, dbTarget, robotSource.ID, robotTarget.ID)
} else {
robotCreated := createRobotTarget(harborTarget, userTarget, passTarget, apiVersionTarget, robotSource)
if robotCreated.ID != 0 {
updateRobotDb(dbSource, dbTarget, robotSource.ID, robotCreated.ID)
}
}
}
}
for _, robot := range robotsTarget {
if _, ok := mRobotsSource[robot.Name]; !ok {
deleteRobot(harborTarget, userTarget, passTarget, apiVersionTarget, robot)
}
}
}