forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_info.go
143 lines (127 loc) · 5.47 KB
/
task_info.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
package handler
import (
"net/http"
"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/client"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)
type TaskInfoHandler struct {
DaoRegistry dao.DaoRegistry
TaskClient client.TaskClient
}
func RegisterTaskInfoRoutes(engine *echo.Group, daoReg *dao.DaoRegistry, taskClient *client.TaskClient) {
if engine == nil {
panic("engine is nil")
}
if daoReg == nil {
panic("taskInfoReg is nil")
}
if taskClient == nil {
panic("taskClient is nil")
}
taskInfoHandler := TaskInfoHandler{
DaoRegistry: *daoReg,
TaskClient: *taskClient,
}
addRepoRoute(engine, http.MethodGet, "/tasks/", taskInfoHandler.listTasks, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodGet, "/tasks/:uuid", taskInfoHandler.fetch, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/tasks/:uuid/cancel/", taskInfoHandler.cancel, rbac.RbacVerbWrite)
}
// ListTasks godoc
// @Summary List Tasks
// @ID listTasks
// @Description Get the list of tasks.
// @Tags tasks
// @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 status query string false "A comma separated list of statuses to control response. Statuses can include `running`, `completed`, `failed`."
// @Param type query string false "Filter results based on a specific task types. Helps to narrow down the results to a specific type. Task types can be `snapshot` or `introspect`. "
// @Param repository_uuid query string false "A unique identifier of a repository to filter the results."
// @Param template_uuid query string false "A unique identifier of a template to filter the results."
// @Param exclude_red_hat_org query bool false "A flag to exclude tasks for the red hat org from the query."
// @Accept json
// @Produce json
// @Success 200 {object} api.TaskInfoCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /tasks/ [get]
func (t *TaskInfoHandler) listTasks(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
pageData := ParsePagination(c)
filterData := ParseTaskInfoFilters(c)
tasks, totalTasks, err := t.DaoRegistry.TaskInfo.List(c.Request().Context(), orgID, pageData, filterData)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing tasks", err.Error())
}
return c.JSON(http.StatusOK, setCollectionResponseMetadata(&tasks, c, totalTasks))
}
// Get TaskResponse godoc
// @Summary Get Task
// @ID getTask
// @Description Get information about a specific task.
// @Tags tasks
// @Accept json
// @Produce json
// @Param uuid path string true "Task ID."
// @Success 200 {object} api.TaskInfoResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /tasks/{uuid} [get]
func (t *TaskInfoHandler) fetch(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
id := c.Param("uuid")
response, err := t.DaoRegistry.TaskInfo.Fetch(c.Request().Context(), orgID, id)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error fetching task", err.Error())
}
return c.JSON(http.StatusOK, response)
}
func (t *TaskInfoHandler) cancel(c echo.Context) error {
_, orgID := getAccountIdOrgId(c)
id := c.Param("uuid")
task, err := t.DaoRegistry.TaskInfo.Fetch(c.Request().Context(), orgID, id)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "error canceling task", err.Error())
}
if task.OrgId == config.RedHatOrg {
return ce.NewErrorResponse(http.StatusBadRequest, "error canceling task", "Cannot cancel a Red Hat Task")
}
err = t.TaskClient.Cancel(c.Request().Context(), id)
if err != nil {
if err == queue.ErrNotCancellable {
return ce.NewErrorResponse(http.StatusBadRequest, "error canceling task", err.Error())
} else {
return ce.NewErrorResponse(http.StatusInternalServerError, "error canceling task", err.Error())
}
}
return c.NoContent(http.StatusNoContent)
}
func ParseTaskInfoFilters(c echo.Context) api.TaskInfoFilterData {
filterData := api.TaskInfoFilterData{
Status: "",
Typename: "",
RepoConfigUUID: "",
TemplateUUID: "",
}
err := echo.QueryParamsBinder(c).
String("status", &filterData.Status).
String("type", &filterData.Typename).
String("repository_uuid", &filterData.RepoConfigUUID).
String("template_uuid", &filterData.TemplateUUID).
Bool("exclude_red_hat_org", &filterData.ExcludeRedHatOrg).
BindError()
if err != nil {
log.Ctx(c.Request().Context()).Info().Err(err).Msg("error parsing filters")
}
return filterData
}