-
Notifications
You must be signed in to change notification settings - Fork 25
/
api.go
244 lines (216 loc) · 7.14 KB
/
api.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
package handler
import (
_ "embed"
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
spec_api "github.com/content-services/content-sources-backend/api"
"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"
"github.com/content-services/content-sources-backend/pkg/db"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
"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"
)
const DefaultOffset = 0
const DefaultLimit = 100
const DefaultSortBy = ""
const DefaultSearch = ""
const DefaultArch = ""
const DefaultVersion = ""
const DefaultAvailableForArch = ""
const DefaultAvailableForVersion = ""
const DefaultStatus = ""
const MaxLimit = 200
const DefaultAdminTaskStatus = ""
const DefaultOrgId = ""
const DefaultAccountId = ""
const DefaultURL = ""
const DefaultUUID = ""
// nolint: lll
// @title ContentSourcesBackend
// DO NOT EDIT version MANUALLY - this variable is modified by generate_docs.sh
// @version v1.0.0
// @description The API for the repositories of the content sources that you can use to create and manage repositories between third-party applications and the [Red Hat Hybrid Cloud Console](https://console.redhat.com). With these repositories, you can build and deploy images using Image Builder for Cloud, on-Premise, and Edge. You can handle tasks, search for required RPMs, fetch a GPGKey from the URL, and list the features within applications.
// @description
// @license.name Apache 2.0
// @license.url https://www.apache.org/licenses/LICENSE-2.0
// @Host api.example.com
// @BasePath /api/content-sources/v1.0/
// @query.collection.format multi
// @securityDefinitions.apikey RhIdentity
// @in header
// @name x-rh-identity
func RegisterRoutes(engine *echo.Echo) {
var (
err error
pgqueue queue.PgQueue
)
paths := []string{api.FullRootPath(), api.MajorRootPath()}
pgqueue, err = queue.NewPgQueue(db.GetUrl())
if err != nil {
panic(err)
}
taskClient := client.NewTaskClient(&pgqueue)
for i := 0; i < len(paths); i++ {
group := engine.Group(paths[i])
group.GET("/openapi.json", openapi)
daoReg := dao.GetDaoRegistry(db.DB)
RegisterRepositoryRoutes(group, daoReg, &taskClient)
RegisterRepositoryParameterRoutes(group, daoReg)
RegisterRpmRoutes(group, daoReg)
RegisterPopularRepositoriesRoutes(group, daoReg)
RegisterTaskInfoRoutes(group, daoReg, &taskClient)
RegisterSnapshotRoutes(group, daoReg)
RegisterAdminTaskRoutes(group, daoReg)
RegisterFeaturesRoutes(group)
RegisterPublicRepositoriesRoutes(group, daoReg)
RegisterPackageGroupRoutes(group, daoReg)
RegisterEnvironmentRoutes(group, daoReg)
RegisterTemplateRoutes(group, daoReg, &taskClient)
}
data, err := json.MarshalIndent(engine.Routes(), "", " ")
if err == nil {
log.Debug().Msg(string(data))
}
}
func RegisterPing(engine *echo.Echo) {
engine.GET("/ping", ping)
engine.GET("/ping/", ping)
}
var PulpConnected bool
func ping(c echo.Context) error {
if config.LoadedConfig.Clients.Pulp.Server != "" && !PulpConnected {
client := pulp_client.GetPulpClientWithDomain(pulp_client.DefaultDomain)
_, err := client.GetRpmRemoteList(c.Request().Context())
if err != nil {
return c.JSON(502, echo.Map{
"message": err.Error(),
})
}
PulpConnected = true
}
return c.JSON(200, echo.Map{
"message": "pong",
})
}
func openapi(c echo.Context) error {
var foo, err = spec_api.Openapi()
if err != nil {
return err
}
return c.JSONBlob(200, foo)
}
func createLink(c echo.Context, offset int) string {
req := c.Request()
q := req.URL.Query()
page := ParsePagination(c)
filters := ParseFilters(c)
q.Set("limit", strconv.Itoa(page.Limit))
q.Set("offset", strconv.Itoa(offset))
if filters.Search != "" {
q.Set("search", filters.Search)
}
if filters.Arch != "" {
q.Set("arch", filters.Arch)
}
if filters.Version != "" {
q.Set("version", filters.Version)
}
if filters.AvailableForArch != "" {
q.Set("available_for_arch", filters.AvailableForArch)
}
if filters.AvailableForVersion != "" {
q.Set("available_for_version", filters.AvailableForVersion)
}
params, _ := url.PathUnescape(q.Encode())
return fmt.Sprintf("%v?%v", req.URL.Path, params)
}
// setCollectionResponseMetadata determines metadata of collection response based on context and collection size.
// Returns collection response with updated metadata.
func setCollectionResponseMetadata(collection api.CollectionMetadataSettable, c echo.Context, totalCount int64) api.CollectionMetadataSettable {
page := ParsePagination(c)
var lastPage int
if int(totalCount) > 0 && (int(totalCount)%page.Limit) == 0 {
lastPage = int(totalCount) - page.Limit
} else {
lastPage = int(totalCount) - int(totalCount)%page.Limit
}
links := api.Links{
First: createLink(c, 0),
// 100/page.Limit - (100%10)
Last: createLink(c, lastPage),
}
if page.Offset+page.Limit < int(totalCount) {
links.Next = createLink(c, page.Offset+page.Limit)
}
if page.Offset-page.Limit >= 0 {
links.Prev = createLink(c, page.Offset-page.Limit)
}
collection.SetMetadata(api.ResponseMetadata{
Count: totalCount,
Limit: page.Limit,
Offset: page.Offset,
}, links)
return collection
}
func ParsePagination(c echo.Context) api.PaginationData {
pageData := api.PaginationData{Limit: DefaultLimit, Offset: DefaultOffset, SortBy: DefaultSortBy}
err := echo.QueryParamsBinder(c).
Int("limit", &pageData.Limit).
Int("offset", &pageData.Offset).
String("sort_by", &pageData.SortBy).
BindError()
if err != nil {
log.Ctx(c.Request().Context()).Info().Err(err).Msg("Failed to bind pagination.")
}
if pageData.SortBy == DefaultSortBy {
err = c.Request().ParseForm()
if err != nil {
log.Ctx(c.Request().Context()).Info().Msg("Failed to bind pagination.")
}
q := c.Request().Form
pageData.SortBy = strings.Join(q["sort_by[]"], ",")
}
if pageData.Limit > MaxLimit {
pageData.Limit = MaxLimit
}
if pageData.Limit == 0 {
pageData.Limit = DefaultLimit
}
return pageData
}
func ParseFilters(c echo.Context) api.FilterData {
filterData := api.FilterData{
Search: DefaultSearch,
Arch: DefaultArch,
Version: DefaultVersion,
AvailableForArch: DefaultAvailableForArch,
AvailableForVersion: DefaultAvailableForVersion,
Status: DefaultStatus,
UUID: DefaultUUID,
URL: DefaultURL,
}
err := echo.QueryParamsBinder(c).
String("uuid", &filterData.UUID).
String("search", &filterData.Search).
String("arch", &filterData.Arch).
String("version", &filterData.Version).
String("available_for_arch", &filterData.AvailableForArch).
String("available_for_version", &filterData.AvailableForVersion).
String("name", &filterData.Name).
String("url", &filterData.URL).
String("status", &filterData.Status).
String("origin", &filterData.Origin).
String("content_type", &filterData.ContentType).
BindError()
if err != nil {
log.Ctx(c.Request().Context()).Info().Msg("Error parsing filters")
}
return filterData
}