-
Notifications
You must be signed in to change notification settings - Fork 81
/
project-registry.go
289 lines (262 loc) · 11.1 KB
/
project-registry.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package registryhandler
import (
"context"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"kubegems.io/kubegems/pkg/i18n"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/service/models"
)
// PostProjectRegistry 创建一个属于 Project 的Registry
// @Tags Project
// @Summary 创建一个属于 Project 的Registry
// @Description 创建一个属于 Project 的Registry
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param param body models.Registry true "表单"
// @Success 200 {object} handlers.ResponseStruct{Data=models.Registry} "models.Registry"
// @Router /v1/project/{project_id}/registry [post]
// @Security JWT
func (h *RegistryHandler) PostProjectRegistry(c *gin.Context) {
var registry models.Registry
if err := c.BindJSON(®istry); err != nil {
handlers.NotOK(c, err)
return
}
if strconv.Itoa(int(registry.ProjectID)) != c.Param("project_id") {
handlers.NotOK(c, i18n.Errorf(c, "URL parameter mismatched with body"))
return
}
// 检查默认仓库
defaultRegistries := []models.Registry{}
ctx := c.Request.Context()
if err := h.GetDB().WithContext(ctx).Where("project_id = ? and id != ? and is_default = ?", registry.ProjectID, registry.ID, true).
Find(&defaultRegistries).Error; err != nil {
handlers.NotOK(c, err)
return
}
if len(defaultRegistries) > 0 && registry.IsDefault {
handlers.NotOK(c, i18n.Errorf(c, "can't add image registry, there must be only one default image registry"))
return
}
err := h.GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Create(®istry).Error; err != nil {
return err
}
return h.onChange(ctx, tx, ®istry)
})
if err != nil {
handlers.NotOK(c, err)
return
}
action := i18n.Sprintf(context.TODO(), "add")
module := i18n.Sprintf(context.TODO(), "image registry")
h.SetAuditData(c, action, module, registry.RegistryName)
h.SetExtraAuditData(c, models.ResProject, registry.ProjectID)
handlers.Created(c, registry)
}
// ListProjectRegistry 获取属于Project的 Registry 列表
// @Tags Project
// @Summary 获取属于 Project 的 Registry 列表
// @Description 获取属于 Project 的 Registry 列表
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param preload query string false "choices Creator,Project"
// @Param page query int false "page"
// @Param size query int false "page"
// @Param search query string false "search in (RegistryName)"
// @Success 200 {object} handlers.ResponseStruct{Data=handlers.PageData{List=[]models.Registry}} "models.Registry"
// @Router /v1/project/{project_id}/registry [get]
// @Security JWT
func (h *RegistryHandler) ListProjectRegistry(c *gin.Context) {
var list []models.Registry
query, err := handlers.GetQuery(c, nil)
if err != nil {
handlers.NotOK(c, err)
return
}
cond := &handlers.PageQueryCond{
Model: "Registry",
SearchFields: []string{"RegistryName"},
PreloadFields: []string{"Creator", "Project"},
Where: []*handlers.QArgs{handlers.Args("project_id = ?", c.Param("project_id"))},
}
total, page, size, err := query.PageList(h.GetDB().WithContext(c.Request.Context()), cond, &list)
if err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, handlers.Page(total, list, page, size))
}
// RetrieveProjectRegistry 获取Project 的一个 Registry详情
// @Tags Project
// @Summary 获取Project 的一个 Registry详情
// @Description 获取Project 的一个 Registry详情
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param registry_id path uint true "registry_id"
// @Success 200 {object} handlers.ResponseStruct{Data=models.Registry} "models.Registry"
// @Router /v1/project/{project_id}/registry/{registry_id} [get]
// @Security JWT
func (h *RegistryHandler) RetrieveProjectRegistry(c *gin.Context) {
var registry models.Registry
if err := h.GetDB().WithContext(c.Request.Context()).First(®istry, "project_id = ? and id = ?", c.Param(ProjectKeyName), c.Param("registry_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, registry)
}
// PutProjectRegistry 修改 Project 的 Registry
// @Tags Project
// @Summary 修改Project 的 Registry
// @Description 修改 Project 的 Registry
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param registry_id path uint true "registry_id"
// @Success 200 {object} handlers.ResponseStruct{Data=models.Registry} "models.Registry"
// @Router /v1/project/{project_id}/registry/{registry_id} [put]
// @Security JWT
func (h *RegistryHandler) PutProjectRegistry(c *gin.Context) {
var registry models.Registry
ctx := c.Request.Context()
if err := h.GetDB().WithContext(ctx).First(®istry, "project_id = ? and id = ?", c.Param(ProjectKeyName), c.Param("registry_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
if err := c.Bind(®istry); err != nil {
handlers.NotOK(c, err)
return
}
if strconv.Itoa(int(registry.ProjectID)) != c.Param(ProjectKeyName) || strconv.Itoa(int(registry.ID)) != c.Param("registry_id") {
handlers.NotOK(c, i18n.Errorf(c, "URL parameter mismatched with body"))
return
}
// 检查默认仓库
defaultRegistries := []models.Registry{}
if err := h.GetDB().WithContext(ctx).Where("project_id = ? and id != ? and is_default = ?", registry.ProjectID, registry.ID, true).
Find(&defaultRegistries).Error; err != nil {
handlers.NotOK(c, err)
return
}
if len(defaultRegistries) > 0 && registry.IsDefault {
handlers.NotOK(c, i18n.Errorf(c, "can't update image registry, the default image registry can only exist one"))
return
}
err := h.GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Save(®istry).Error; err != nil {
return err
}
return h.onChange(ctx, tx, ®istry)
})
if err != nil {
handlers.NotOK(c, err)
return
}
action := i18n.Sprintf(context.TODO(), "update")
module := i18n.Sprintf(context.TODO(), "image registry")
h.SetAuditData(c, action, module, registry.RegistryName)
h.SetExtraAuditData(c, models.ResProject, registry.ProjectID)
handlers.OK(c, registry)
}
// SetDefaultProjectRegistry 设置 Project 的 默认 Registry
// @Tags Project
// @Summary 设置 Project 的 默认 Registry
// @Description 设置 Project 的 默认 Registry
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param registry_id path uint true "registry_id"
// @Param is_default query bool true "是否默认镜像仓库"
// @Success 200 {object} handlers.ResponseStruct{Data=models.Registry} "models.Registry"
// @Router /v1/project/{project_id}/registry/{registry_id} [patch]
// @Security JWT
func (h *RegistryHandler) SetDefaultProjectRegistry(c *gin.Context) {
var registry models.Registry
ctx := c.Request.Context()
if err := h.GetDB().WithContext(ctx).First(®istry, "project_id = ? and id = ?", c.Param(ProjectKeyName), c.Param("registry_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
isDefault, _ := strconv.ParseBool(c.Query("isDefault"))
registry.IsDefault = isDefault
action := ""
module := i18n.Sprintf(context.TODO(), "the project's default image registry ")
if isDefault {
action = i18n.Sprintf(context.TODO(), "set")
} else {
action = i18n.Sprintf(context.TODO(), "unset")
}
h.SetAuditData(c, action, module, registry.RegistryName)
h.SetExtraAuditData(c, models.ResProject, registry.ProjectID)
// 检查默认仓库
defaultRegistries := []models.Registry{}
if err := h.GetDB().WithContext(ctx).Where("project_id = ? and id != ? and is_default = ?", registry.ProjectID, registry.ID, true).
Find(&defaultRegistries).Error; err != nil {
handlers.NotOK(c, err)
return
}
if len(defaultRegistries) > 0 && registry.IsDefault {
handlers.NotOK(c, i18n.Errorf(c, "can't set image registry, the project's default image registry can only exist one"))
return
}
err := h.GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Save(®istry).Error; err != nil {
return err
}
return h.onChange(ctx, tx, ®istry)
})
if err != nil {
handlers.NotOK(c, err)
return
}
h.SetExtraAuditData(c, models.ResProject, registry.ProjectID)
handlers.OK(c, registry)
}
// DeleteProjectRegistry Project 的 Registry
// @Tags Project
// @Summary 删除 Project 的 Registry
// @Description 删除 Project 的 Registry
// @Accept json
// @Produce json
// @Param project_id path uint true "project_id"
// @Param registry_id path uint true "registry_id"
// @Success 200 {object} handlers.ResponseStruct{Data=models.Registry} "models.Registry"
// @Router /v1/project/{project_id}/registry/{registry_id} [delete]
// @Security JWT
func (h *RegistryHandler) DeleteProjectRegistry(c *gin.Context) {
var registry models.Registry
ctx := c.Request.Context()
h.GetDB().WithContext(ctx).First(®istry, c.Param("registry_id"))
err := h.GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Delete(®istry, "project_id = ? and id = ?", c.Param(ProjectKeyName), c.Param("registry_id")).Error; err != nil {
return err
}
return h.onDelete(ctx, tx, ®istry)
})
if err != nil {
handlers.NotOK(c, i18n.Errorf(c, "failed to delete the image registry: %v", err))
}
action := i18n.Sprintf(context.TODO(), "delete")
module := i18n.Sprintf(context.TODO(), "image registry")
h.SetAuditData(c, action, module, registry.RegistryName)
h.SetExtraAuditData(c, models.ResProject, registry.ProjectID)
handlers.NoContent(c, nil)
}