-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathenvironments.go
134 lines (117 loc) · 5.75 KB
/
environments.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
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/labstack/echo/v4"
)
type RepositoryEnvironmentHandler struct {
Dao dao.DaoRegistry
}
func RegisterEnvironmentRoutes(engine *echo.Group, rDao *dao.DaoRegistry) {
rh := RepositoryEnvironmentHandler{
Dao: *rDao,
}
addRepoRoute(engine, http.MethodGet, "/repositories/:uuid/environments", rh.listRepositoriesEnvironments, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/environments/names", rh.searchEnvironmentByName, rbac.RbacVerbRead)
addRepoRoute(engine, http.MethodPost, "/snapshots/environments/names", rh.searchSnapshotEnvironments, rbac.RbacVerbRead)
}
// searchEnvironmentByName godoc
// @Summary Search environments
// @ID searchEnvironments
// @Description This enables users to search for environments in a given list of repositories.
// @Tags environments
// @Accept json
// @Produce json
// @Param body body api.ContentUnitSearchRequest true "request body"
// @Success 200 {object} []api.SearchEnvironmentResponse
// @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 /environments/names [post]
func (rh *RepositoryEnvironmentHandler) searchEnvironmentByName(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.Environment.Search(c.Request().Context(), orgId, dataInput)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error searching environments", err.Error())
}
return c.JSON(200, apiResponse)
}
// listRepositoriesEnvironments godoc
// @Summary List Repositories Environments
// @ID listRepositoriesEnvironments
// @Description List environments in a repository.
// @Tags environments
// @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 `id`, `name`, and `description`."
// @Success 200 {object} api.RepositoryEnvironmentCollectionResponse
// @Failure 400 {object} ce.ErrorResponse
// @Failure 401 {object} ce.ErrorResponse
// @Failure 404 {object} ce.ErrorResponse
// @Failure 500 {object} ce.ErrorResponse
// @Router /repositories/{uuid}/environments [get]
func (rh *RepositoryEnvironmentHandler) listRepositoriesEnvironments(c echo.Context) error {
// Read input information
environmentInput := api.ContentUnitListRequest{}
if err := c.Bind(&environmentInput); 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.Environment.List(c.Request().Context(), orgId, environmentInput.UUID, page.Limit, page.Offset, environmentInput.Search, environmentInput.SortBy)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error listing environments", err.Error())
}
return c.JSON(200, setCollectionResponseMetadata(&apiResponse, c, total))
}
// searchSnapshotEnvironments godoc
// @Summary Search environments within snapshots
// @ID searchSnapshotEnvironments
// @Description This enables users to search for environments in a given list of snapshots.
// @Tags environments
// @Accept json
// @Produce json
// @Param body body api.SnapshotSearchRpmRequest true "request body"
// @Success 200 {object} []api.SearchEnvironmentResponse
// @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/environments/names [post]
func (rh *RepositoryEnvironmentHandler) searchSnapshotEnvironments(c echo.Context) error {
_, orgId := getAccountIdOrgId(c)
dataInput := api.SnapshotSearchRpmRequest{}
var err error
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.Environment.SearchSnapshotEnvironments(c.Request().Context(), orgId, dataInput)
if err != nil {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error searching environments", err.Error())
}
return c.JSON(200, resp)
}