-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrpms.go
333 lines (296 loc) · 15.6 KB
/
rpms.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
package handler
import (
"net/http"
"github.com/content-services/content-sources-backend/pkg/api"
"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/utils"
"github.com/content-services/tang/pkg/tangy"
"github.com/labstack/echo/v4"
)
type RpmHandler struct {
Dao dao.DaoRegistry
}
func RegisterRpmRoutes(engine *echo.Group, rDao *dao.DaoRegistry) {
rh := RpmHandler{
Dao: *rDao,
}
addRepoRoute(engine, http.MethodGet, "/repositories/:uuid/rpms", rh.listRepositoriesRpm, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/rpms/names", rh.searchRpmByName, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodGet, "/snapshots/:uuid/rpms", rh.listSnapshotRpm, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodGet, "/snapshots/:uuid/errata", rh.listSnapshotErrata, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/snapshots/rpms/names", rh.searchSnapshotRPMs, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/rpms/presence", rh.detectRpmsPresence, rbac.RbacVerbRead)
addTemplateRoute(engine, http.MethodGet, "/templates/:uuid/rpms", rh.listTemplateRpm, rbac.RbacVerbRead)
addTemplateRoute(engine, http.MethodGet, "/templates/:uuid/errata", rh.listTemplateErrata, rbac.RbacVerbRead)
}
// searchRpmByName godoc
// @Summary Search RPMs
// @ID searchRpm
// @Description This enables users to search for RPMs (Red Hat Package Manager) in a given list of repositories.
// @Tags rpms
// @Accept json
// @Produce json
// @Param body body api.ContentUnitSearchRequest true "request body"
// @Success 200 {object} []api.SearchRpmResponse
// @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 /rpms/names [post]
func (rh *RpmHandler) searchRpmByName(c echo.Context) error {
_, orgId := getAccountIdOrgId(c)
dataInput := api.ContentUnitSearchRequest{}
if err := c.Bind(&dataInput); err != nil {
return ce.NewErrorResponse(http.StatusBadRequest, "Error binding parameters", err.Error())
}
preprocessInput(&dataInput)
apiResponse, err := rh.Dao.Rpm.Search(c.Request().Context(), orgId, dataInput)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error searching RPMs", err.Error())
}
return c.JSON(200, apiResponse)
}
// listRepositoriesRpm godoc
// @Summary List Repositories RPMs
// @ID listRepositoriesRpms
// @Description List RPMs in a repository.
// @Tags rpms
// @Accept json
// @Produce json
// @Param uuid path string true "Repository ID."
// @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 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 search query string false "Term to filter and retrieve items that match the specified search criteria. Search term can include name."
// @Param sort_by query string false "Sort the response based on specific repository parameters. Sort criteria can include `name`, `url`, `status`, and `package_count`."
// @Success 200 {object} api.RepositoryRpmCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /repositories/{uuid}/rpms [get]
func (rh *RpmHandler) listRepositoriesRpm(c echo.Context) error {
// Read input information
rpmInput := api.ContentUnitListRequest{}
if err := c.Bind(&rpmInput); err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error binding parameters", err.Error())
}
_, orgId := getAccountIdOrgId(c)
page := ParsePagination(c)
// Request record from database
apiResponse, total, err := rh.Dao.Rpm.List(c.Request().Context(), orgId, rpmInput.UUID, page.Limit, page.Offset, rpmInput.Search, rpmInput.SortBy)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing RPMs", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&apiResponse, c, total))
}
// searchSnapshotRPMs godoc
// @Summary Search RPMs within snapshots
// @ID searchSnapshotRpms
// @Description This enables users to search for RPMs (Red Hat Package Manager) in a given list of snapshots.
// @Tags rpms
// @Accept json
// @Produce json
// @Param body body api.SnapshotSearchRpmRequest true "request body"
// @Success 200 {object} []api.SearchRpmResponse
// @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 /snapshots/rpms/names [post]
func (rh *RpmHandler) searchSnapshotRPMs(c echo.Context) error {
_, orgId := getAccountIdOrgId(c)
dataInput := api.SnapshotSearchRpmRequest{}
err := CheckSnapshotAccessible(c.Request().Context())
if err != nil {
return err
}
if err = c.Bind(&dataInput); err != nil {
return ce.NewErrorResponse(http.StatusBadRequest, "Error binding parameters", err.Error())
}
if dataInput.Limit == nil || *dataInput.Limit > api.SearchRpmRequestLimitDefault {
dataInput.Limit = utils.Ptr(api.SearchRpmRequestLimitDefault)
}
resp, err := rh.Dao.Rpm.SearchSnapshotRpms(c.Request().Context(), orgId, dataInput)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error searching RPMs", err.Error())
}
return c.JSON(200, resp)
}
// listSnapshotRpm godoc
// @Summary List Snapshot RPMs
// @ID listSnapshotRpms
// @Description List RPMs in a repository snapshot.
// @Tags rpms
// @Accept json
// @Produce json
// @Param uuid path string true "Snapshot ID."
// @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 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 search query string false "Term to filter and retrieve items that match the specified search criteria. Search term can include name."
// @Success 200 {object} api.SnapshotRpmCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /snapshots/{uuid}/rpms [get]
func (rh *RpmHandler) listSnapshotRpm(c echo.Context) error {
// Read input information
rpmInput := api.ContentUnitListRequest{}
if err := c.Bind(&rpmInput); err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error binding parameters", err.Error())
}
_, orgId := getAccountIdOrgId(c)
page := ParsePagination(c)
// Request record from database
data, total, err := rh.Dao.Rpm.ListSnapshotRpms(c.Request().Context(), orgId, []string{rpmInput.UUID}, rpmInput.Search, page)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing RPMs", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&api.SnapshotRpmCollectionResponse{Data: data}, c, int64(total)))
}
// detectRpmsPresence godoc
// @Summary Detect RPMs presence
// @ID detectRpm
// @Description This enables users to detect presence of RPMs (Red Hat Package Manager) in a given list of repositories.
// @Tags rpms
// @Accept json
// @Produce json
// @Param body body api.DetectRpmsRequest true "request body"
// @Success 200 {object} api.DetectRpmsResponse
// @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 /rpms/presence [post]
// @Deprecated
func (rh *RpmHandler) detectRpmsPresence(c echo.Context) error {
_, orgId := getAccountIdOrgId(c)
dataInput := api.DetectRpmsRequest{}
if err := c.Bind(&dataInput); err != nil {
return ce.NewErrorResponse(http.StatusBadRequest, "Error binding parameters", err.Error())
}
apiResponse, err := rh.Dao.Rpm.DetectRpms(c.Request().Context(), orgId, dataInput)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error detecting RPMs", err.Error())
}
return c.JSON(200, apiResponse)
}
// listSnapshotErrata godoc
// @Summary List Snapshot Errata
// @ID listSnapshotErrata
// @Description List errata in a repository snapshot.
// @Tags rpms
// @Accept json
// @Produce json
// @Param uuid path string true "Snapshot ID."
// @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 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 search query string false "Term to filter and retrieve items that match the specified search criteria. Search term can include name."
// @Param type query string false "A comma separated list of types to control api response. Type can include `security`, `enhancement`, `bugfix`, and `other`."
// @Param severity query string false "A comma separated list of severities to control api response. Severity can include `Important`, `Critical`, `Moderate`, `Low`, and `Unknown`."
// @Param sort_by query string false "Sort the response based on specific parameters. Sort criteria can include `issued_date`, `updated_date`, `type`, and `severity`."
// @Success 200 {object} api.SnapshotErrataCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /snapshots/{uuid}/errata [get]
func (rh *RpmHandler) listSnapshotErrata(c echo.Context) error {
// Read input information
snapshotErrataRequest := api.SnapshotErrataListRequest{}
if err := c.Bind(&snapshotErrataRequest); err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error binding parameters", err.Error())
}
_, orgId := getAccountIdOrgId(c)
page := ParsePagination(c)
// Request record from database
data, total, err := rh.Dao.Rpm.ListSnapshotErrata(
c.Request().Context(),
orgId,
[]string{snapshotErrataRequest.UUID},
tangy.ErrataListFilters{Search: snapshotErrataRequest.Search, Type: snapshotErrataRequest.Type, Severity: snapshotErrataRequest.Severity},
page,
)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing Errata", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&api.SnapshotErrataCollectionResponse{Data: data}, c, int64(total)))
}
// listTemplateRpm godoc
// @Summary List Template RPMs
// @ID listTemplateRpms
// @Description List RPMs in a content template.
// @Tags rpms
// @Accept json
// @Produce json
// @Param uuid path string true "Template ID."
// @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 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 search query string false "Term to filter and retrieve items that match the specified search criteria. Search term can include name."
// @Success 200 {object} api.SnapshotRpmCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid}/rpms [get]
func (rh *RpmHandler) listTemplateRpm(c echo.Context) error {
// Read input information
rpmInput := api.ContentUnitListRequest{}
if err := c.Bind(&rpmInput); err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error binding parameters", err.Error())
}
_, orgId := getAccountIdOrgId(c)
page := ParsePagination(c)
// Request record from database
data, total, err := rh.Dao.Rpm.ListTemplateRpms(c.Request().Context(), orgId, rpmInput.UUID, rpmInput.Search, page)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing RPMs", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&api.SnapshotRpmCollectionResponse{Data: data}, c, int64(total)))
}
// listTemplateErrata godoc
// @Summary List Template Errata
// @ID listTemplateErrata
// @Description List errata in a content template.
// @Tags templates
// @Accept json
// @Produce json
// @Param uuid path string true "Template ID."
// @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 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 search query string false "Term to filter and retrieve items that match the specified search criteria. Search term can include name."
// @Param type query string false "A comma separated list of types to control api response. Type can include `security`, `enhancement`, `bugfix`, and `other`."
// @Param severity query string false "A comma separated list of severities to control api response. Severity can include `Important`, `Critical`, `Moderate`, `Low`, and `Unknown`."
// @Param sort_by query string false "Sort the response based on specific parameters. Sort criteria can include `issued_date`, `updated_date`, `type`, and `severity`."
// @Success 200 {object} api.SnapshotErrataCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /templates/{uuid}/errata [get]
func (rh *RpmHandler) listTemplateErrata(c echo.Context) error {
// Read input information
snapshotErrataRequest := api.SnapshotErrataListRequest{}
if err := c.Bind(&snapshotErrataRequest); err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "Error binding parameters", err.Error())
}
_, orgId := getAccountIdOrgId(c)
page := ParsePagination(c)
// Request record from database
data, total, err := rh.Dao.Rpm.ListTemplateErrata(
c.Request().Context(),
orgId,
snapshotErrataRequest.UUID,
tangy.ErrataListFilters{Search: snapshotErrataRequest.Search, Type: snapshotErrataRequest.Type, Severity: snapshotErrataRequest.Severity},
page,
)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing Errata", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&api.SnapshotErrataCollectionResponse{Data: data}, c, int64(total)))
}