forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
394 lines (352 loc) · 15.1 KB
/
templates.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package handler
import (
"net/http"
"strings"
"time"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/dao"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/rbac"
"github.com/content-services/content-sources-backend/pkg/tasks"
"github.com/content-services/content-sources-backend/pkg/tasks/client"
"github.com/content-services/content-sources-backend/pkg/tasks/payloads"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/content-services/content-sources-backend/pkg/utils"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
"github.com/rs/zerolog/log"
)
type TemplateHandler struct {
DaoRegistry dao.DaoRegistry
TaskClient client.TaskClient
}
func RegisterTemplateRoutes(engine *echo.Group, daoReg *dao.DaoRegistry, taskClient *client.TaskClient) {
if engine == nil {
panic("engine is nil")
}
if daoReg == nil {
panic("daoReg is nil")
}
if taskClient == nil {
panic("taskClient is nil")
}
h := TemplateHandler{
DaoRegistry: *daoReg,
TaskClient: *taskClient,
}
addTemplateRoute(engine, http.MethodGet, "/templates/", h.listTemplates, rbac.RbacVerbRead)
addTemplateRoute(engine, http.MethodGet, "/templates/:uuid", h.fetch, rbac.RbacVerbRead)
addTemplateRoute(engine, http.MethodPost, "/templates/", h.createTemplate, rbac.RbacVerbWrite)
addTemplateRoute(engine, http.MethodDelete, "/templates/:uuid", h.deleteTemplate, rbac.RbacVerbWrite)
addTemplateRoute(engine, http.MethodPut, "/templates/:uuid", h.fullUpdate, rbac.RbacVerbWrite)
addTemplateRoute(engine, http.MethodPatch, "/templates/:uuid", h.partialUpdate, rbac.RbacVerbWrite)
addTemplateRoute(engine, http.MethodGet, "/templates/:template_uuid/config.repo", h.getTemplateRepoConfigurationFile, rbac.RbacVerbRead)
}
// CreateRepository godoc
// @Summary Create Template
// @ID createTemplate
// @Description This operation enables creating templates based on user preferences.
// @Tags templates
// @Accept json
// @Produce json
// @Param body body api.TemplateRequest true "request body"
// @Success 201 {object} api.TemplateResponse
// @Header 201 {string} Location "resource URL"
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 415 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/ [post]
func (th *TemplateHandler) createTemplate(c echo.Context) error {
var newTemplate api.TemplateRequest
if err := c.Bind(&newTemplate); err != nil {
return ce.NewErrorResponse(http.StatusBadRequest, "Error binding params", err.Error())
}
_, orgID := getAccountIdOrgId(c)
newTemplate.OrgID = &orgID
user := getUser(c)
newTemplate.User = &user
respTemplate, err := th.DaoRegistry.Template.Create(c.Request().Context(), newTemplate)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error creating template", err.Error())
}
if config.Get().Clients.Candlepin.Server != "" {
th.enqueueUpdateTemplateContentEvent(c, respTemplate)
}
return c.JSON(http.StatusCreated, respTemplate)
}
// Get RepositoryResponse godoc
// @Summary Get Template
// @ID getTemplate
// @Description Get template information.
// @Tags templates
// @Accept json
// @Produce json
// @Param uuid path string true "Template ID."
// @Success 200 {object} api.TemplateResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid} [get]
func (th *TemplateHandler) fetch(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
uuid := c.Param("uuid")
resp, err := th.DaoRegistry.Template.Fetch(c.Request().Context(), orgID, uuid, false)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error fetching template", err.Error())
}
return c.JSON(http.StatusOK, resp)
}
// ListTemplates godoc
// @Summary List Templates
// @ID listTemplates
// @Description This operation enables users to retrieve a list of templates.
// @Tags templates
// @Param offset query int false "Starting point for retrieving a subset of results. Determines how many items to skip from the beginning of the result set. Default value:`0`."
// @Param limit query int false "Number of items to include in response. Use it to control the number of items, particularly when dealing with large datasets. Default value: `100`."
// @Param version query string false "Filter templates by version."
// @Param arch query string false "Filter templates by architecture."
// @Param name query string false "Filter templates by name."
// @Param repository_uuids query string false "Filter templates by associated repositories using a comma separated list of repository UUIDs"
// @Param snapshot_uuids query string false "Filter templates by associated snapshots using a comma separated list of snapshot UUIDs"
// @Param sort_by query string false "Sort the response data based on specific parameters. Sort criteria can include `name`, `arch`, and `version`."
// @Accept json
// @Produce json
// @Success 200 {object} api.TemplateCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/ [get]
func (th *TemplateHandler) listTemplates(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
pageData := ParsePagination(c)
filterData := ParseTemplateFilters(c)
templates, total, err := th.DaoRegistry.Template.List(c.Request().Context(), orgID, false, pageData, filterData)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing templates", err.Error())
}
return c.JSON(http.StatusOK, setCollectionResponseMetadata(&templates, c, total))
}
// FullUpdateTemplate godoc
// @Summary Fully update all attributes of a Template
// @ID fullUpdateTemplate
// @Description This operation enables updating all attributes of a template
// @Tags templates
// @Accept json
// @Produce json
// @Param uuid path string true "Template ID."
// @Param body body api.TemplateUpdateRequest true "request body"
// @Success 201 {object} api.TemplateResponse
// @Header 201 {string} Location "resource URL"
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 415 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid} [put]
func (th *TemplateHandler) fullUpdate(c echo.Context) error {
return th.update(c, true)
}
// PartiallyUpdateTemplate godoc
// @Summary Update some attributes of a Template
// @ID partialUpdateTemplate
// @Description This operation enables updating some subset of attributes of a template
// @Tags templates
// @Accept json
// @Produce json
// @Param uuid path string true "Template ID."
// @Param body body api.TemplateUpdateRequest true "request body"
// @Success 201 {object} api.TemplateResponse
// @Header 201 {string} Location "resource URL"
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 415 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid} [patch]
func (th *TemplateHandler) partialUpdate(c echo.Context) error {
return th.update(c, false)
}
func (th *TemplateHandler) update(c echo.Context, fillDefaults bool) error {
uuid := c.Param("uuid")
tempParams := api.TemplateUpdateRequest{}
_, orgID := getAccountIdOrgId(c)
user := getUser(c)
tempParams.User = &user
if err := c.Bind(&tempParams); err != nil {
return ce.NewErrorResponse(http.StatusBadRequest, "Error binding parameters", err.Error())
}
if fillDefaults {
tempParams.FillDefaults()
} else {
if tempParams.IsUsingLatest() && tempParams.Date == nil {
tempParams.Date = utils.Ptr(api.EmptiableDate(time.Time{}))
}
}
respTemplate, err := th.DaoRegistry.Template.Update(c.Request().Context(), orgID, uuid, tempParams)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error updating template", err.Error())
}
if config.Get().Clients.Candlepin.Server != "" {
th.enqueueUpdateTemplateContentEvent(c, respTemplate)
}
return c.JSON(http.StatusOK, respTemplate)
}
func ParseTemplateFilters(c echo.Context) api.TemplateFilterData {
filterData := api.TemplateFilterData{
Name: "",
Version: "",
Arch: "",
Search: "",
}
repositoryUUIDs := ""
snapshotUUIDs := ""
err := echo.QueryParamsBinder(c).
String("name", &filterData.Name).
String("version", &filterData.Version).
String("arch", &filterData.Arch).
String("search", &filterData.Search).
String("repository_uuids", &repositoryUUIDs).
String("snapshot_uuids", &snapshotUUIDs).
Bool("use_latest", &filterData.UseLatest).
BindError()
if err != nil {
log.Ctx(c.Request().Context()).Info().Err(err).Msg("error parsing filters")
}
if repositoryUUIDs != "" {
filterData.RepositoryUUIDs = strings.Split(repositoryUUIDs, ",")
}
if snapshotUUIDs != "" {
filterData.SnapshotUUIDs = strings.Split(snapshotUUIDs, ",")
}
return filterData
}
// DeleteTemplate godoc
// @summary Delete a template
// @ID deleteTemplate
// @Description This enables deleting a specific template.
// @Tags templates
// @Param uuid path string true "Template ID."
// @Success 204 "Template was successfully deleted"
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid} [delete]
func (th *TemplateHandler) deleteTemplate(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
uuid := c.Param("uuid")
template, err := th.DaoRegistry.Template.Fetch(c.Request().Context(), orgID, uuid, false)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error fetching template", err.Error())
}
err = th.cancelUpdateTemplateContent(c, orgID, template)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error cancelling template update", err.Error())
}
if err := th.DaoRegistry.Template.SoftDelete(c.Request().Context(), orgID, uuid); err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error deleting template", err.Error())
}
enqueueErr := th.enqueueTemplateDeleteEvent(c, orgID, template)
if enqueueErr != nil {
if err = th.DaoRegistry.Template.ClearDeletedAt(c.Request().Context(), orgID, uuid); err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error clearing deleted_at field", err.Error())
}
return ce.NewErrorResponse(ce.HttpCodeForDaoError(enqueueErr), "Error enqueueing task", enqueueErr.Error())
}
return c.NoContent(http.StatusNoContent)
}
// GetTemplateRepoConfigurationFiles godoc
// @Summary Get configuration file for all repositories in a template
// @ID getTemplateRepoConfigurationFile
// @Tags templates
// @Accept json
// @Produce text/plain
// @Param template_uuid path string true "Identifier of the template"
// @Success 200 {string} string
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{template_uuid}/config.repo [get]
func (th *TemplateHandler) getTemplateRepoConfigurationFile(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
templateUUID := c.Param("template_uuid")
templateRepoConfigFiles, err := th.DaoRegistry.Template.GetRepositoryConfigurationFile(c.Request().Context(), orgID, templateUUID)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error getting template repository configuration files", err.Error())
}
return c.String(http.StatusOK, templateRepoConfigFiles)
}
func (th *TemplateHandler) enqueueTemplateDeleteEvent(c echo.Context, orgID string, template api.TemplateResponse) error {
accountID, _ := getAccountIdOrgId(c)
payload := tasks.DeleteTemplatesPayload{TemplateUUID: template.UUID, RepoConfigUUIDs: template.RepositoryUUIDS}
task := queue.Task{
Typename: config.DeleteTemplatesTask,
Payload: payload,
ObjectUUID: &template.UUID,
ObjectType: utils.Ptr(config.ObjectTypeTemplate),
OrgId: orgID,
AccountId: accountID,
RequestID: c.Response().Header().Get(config.HeaderRequestId),
}
taskID, err := th.TaskClient.Enqueue(task)
if err != nil {
logger := tasks.LogForTask(taskID.String(), task.Typename, task.RequestID)
logger.Error().Msg("error enqueuing task")
return err
}
return nil
}
func (th *TemplateHandler) enqueueUpdateTemplateContentEvent(c echo.Context, template api.TemplateResponse) uuid.UUID {
accountID, orgID := getAccountIdOrgId(c)
payload := payloads.UpdateTemplateContentPayload{TemplateUUID: template.UUID, RepoConfigUUIDs: template.RepositoryUUIDS}
task := queue.Task{
Typename: config.UpdateTemplateContentTask,
Payload: payload,
ObjectUUID: &template.UUID,
ObjectType: utils.Ptr(config.ObjectTypeTemplate),
OrgId: orgID,
AccountId: accountID,
RequestID: c.Response().Header().Get(config.HeaderRequestId),
Priority: 1,
}
taskID, err := th.TaskClient.Enqueue(task)
logger := tasks.LogForTask(taskID.String(), task.Typename, task.RequestID)
if err != nil {
logger.Error().Msg("error enqueuing task")
}
if err == nil {
if err = th.DaoRegistry.Template.UpdateLastUpdateTask(c.Request().Context(), taskID.String(), orgID, template.UUID); err != nil {
logger.Error().Msg("error updating LastUpdateTask task")
} else {
template.LastUpdateTaskUUID = taskID.String()
}
}
return taskID
}
func (th *TemplateHandler) cancelUpdateTemplateContent(c echo.Context, orgID string, template api.TemplateResponse) error {
taskIDs, err := th.DaoRegistry.TaskInfo.FetchActiveTasks(c.Request().Context(), orgID, template.UUID, config.UpdateTemplateContentTask)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error checking if update template content is in progress", err.Error())
}
for _, taskID := range taskIDs {
err = th.TaskClient.Cancel(c.Request().Context(), taskID)
if err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error canceling update-template-content", err.Error())
}
}
return nil
}
func getUser(c echo.Context) string {
id := identity.Get(c.Request().Context())
if id.Identity.User != nil {
return id.Identity.User.Username
}
return ""
}