registry.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/registry"
  6. v1Registry "github.com/porter-dev/porter/api/server/handlers/v1/registry"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/server/shared/router"
  10. "github.com/porter-dev/porter/api/types"
  11. )
  12. // swagger:parameters getRegistry deleteRegistry createRegistryRepository listRegistryRepositories listRegistryImages
  13. type registryPathParams struct {
  14. // The project id
  15. // in: path
  16. // required: true
  17. // minimum: 1
  18. ProjectID uint `json:"project_id"`
  19. // The registry id
  20. // in: path
  21. // required: true
  22. // minimum: 1
  23. RegistryID uint `json:"registry_id"`
  24. }
  25. func NewV1RegistryScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  26. return &router.Registerer{
  27. GetRoutes: GetV1RegistryScopedRoutes,
  28. Children: children,
  29. }
  30. }
  31. func GetV1RegistryScopedRoutes(
  32. r chi.Router,
  33. config *config.Config,
  34. basePath *types.Path,
  35. factory shared.APIEndpointFactory,
  36. children ...*router.Registerer,
  37. ) []*router.Route {
  38. routes, projPath := getV1RegistryRoutes(r, config, basePath, factory)
  39. if len(children) > 0 {
  40. r.Route(projPath.RelativePath, func(r chi.Router) {
  41. for _, child := range children {
  42. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  43. routes = append(routes, childRoutes...)
  44. }
  45. })
  46. }
  47. return routes
  48. }
  49. func getV1RegistryRoutes(
  50. r chi.Router,
  51. config *config.Config,
  52. basePath *types.Path,
  53. factory shared.APIEndpointFactory,
  54. ) ([]*router.Route, *types.Path) {
  55. relPath := "/registries"
  56. newPath := &types.Path{
  57. Parent: basePath,
  58. RelativePath: relPath,
  59. }
  60. var routes []*router.Route
  61. // POST /api/v1/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
  62. // swagger:operation POST /api/v1/projects/{project_id}/registries createRegistry
  63. //
  64. // Connects a new image registry to the project denoted by `project_id`.
  65. //
  66. // ---
  67. // produces:
  68. // - application/json
  69. // summary: Connect an image registry
  70. // tags:
  71. // - Registries
  72. // parameters:
  73. // - name: project_id
  74. // - in: body
  75. // name: CreateRegistryRequest
  76. // description: The registry to connect
  77. // schema:
  78. // $ref: '#/definitions/CreateRegistryRequest'
  79. // responses:
  80. // '201':
  81. // description: Successfully connected the registry
  82. // schema:
  83. // $ref: '#/definitions/CreateRegistryResponse'
  84. // '400':
  85. // description: A malformed or bad request
  86. // '403':
  87. // description: Forbidden
  88. // '404':
  89. // description: A subresource was not found
  90. createRegistryEndpoint := factory.NewAPIEndpoint(
  91. &types.APIRequestMetadata{
  92. Verb: types.APIVerbCreate,
  93. Method: types.HTTPVerbPost,
  94. Path: &types.Path{
  95. Parent: basePath,
  96. RelativePath: relPath,
  97. },
  98. Scopes: []types.PermissionScope{
  99. types.UserScope,
  100. types.ProjectScope,
  101. },
  102. },
  103. )
  104. createRegistryHandler := registry.NewRegistryCreateHandler(
  105. config,
  106. factory.GetDecoderValidator(),
  107. factory.GetResultWriter(),
  108. )
  109. routes = append(routes, &router.Route{
  110. Endpoint: createRegistryEndpoint,
  111. Handler: createRegistryHandler,
  112. Router: r,
  113. })
  114. // GET /api/v1/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryGetHandler
  115. // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id} getRegistry
  116. //
  117. // Gets an image registry denoted by `registry_id`. The registry should belong to the
  118. // project denoted by `project_id`.
  119. //
  120. // ---
  121. // produces:
  122. // - application/json
  123. // summary: Get an image registry
  124. // tags:
  125. // - Registries
  126. // parameters:
  127. // - name: project_id
  128. // - name: registry_id
  129. // responses:
  130. // '200':
  131. // description: Successfully got the registry
  132. // schema:
  133. // $ref: '#/definitions/GetRegistryResponse'
  134. // '403':
  135. // description: Forbidden
  136. getEndpoint := factory.NewAPIEndpoint(
  137. &types.APIRequestMetadata{
  138. Verb: types.APIVerbGet,
  139. Method: types.HTTPVerbGet,
  140. Path: &types.Path{
  141. Parent: basePath,
  142. RelativePath: relPath + "/{registry_id}",
  143. },
  144. Scopes: []types.PermissionScope{
  145. types.UserScope,
  146. types.ProjectScope,
  147. types.RegistryScope,
  148. },
  149. },
  150. )
  151. getHandler := registry.NewRegistryGetHandler(
  152. config,
  153. factory.GetResultWriter(),
  154. )
  155. routes = append(routes, &router.Route{
  156. Endpoint: getEndpoint,
  157. Handler: getHandler,
  158. Router: r,
  159. })
  160. // GET /api/v1/projects/{project_id}/registries -> registry.NewRegistryListHandler
  161. // swagger:operation GET /api/v1/projects/{project_id}/registries listRegistries
  162. //
  163. // Lists all registries connected to the project denoted by `project_id`.
  164. //
  165. // ---
  166. // produces:
  167. // - application/json
  168. // summary: List image registries
  169. // tags:
  170. // - Registries
  171. // parameters:
  172. // - name: project_id
  173. // - name: registry_id
  174. // responses:
  175. // '200':
  176. // description: Successfully listed registries
  177. // schema:
  178. // $ref: '#/definitions/ListRegistriesResponse'
  179. // '403':
  180. // description: Forbidden
  181. listRegistriesEndpoint := factory.NewAPIEndpoint(
  182. &types.APIRequestMetadata{
  183. Verb: types.APIVerbList,
  184. Method: types.HTTPVerbGet,
  185. Path: &types.Path{
  186. Parent: basePath,
  187. RelativePath: relPath,
  188. },
  189. Scopes: []types.PermissionScope{
  190. types.UserScope,
  191. types.ProjectScope,
  192. },
  193. },
  194. )
  195. listRegistriesHandler := registry.NewRegistryListHandler(
  196. config,
  197. factory.GetResultWriter(),
  198. )
  199. routes = append(routes, &router.Route{
  200. Endpoint: listRegistriesEndpoint,
  201. Handler: listRegistriesHandler,
  202. Router: r,
  203. })
  204. // DELETE /api/v1/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryDeleteHandler
  205. // swagger:operation DELETE /api/v1/projects/{project_id}/registries/{registry_id} deleteRegistry
  206. //
  207. // Deletes a registry denoted by `registry_id`. The registry should belong to
  208. // the project denoted by `project_id`.
  209. //
  210. // ---
  211. // produces:
  212. // - application/json
  213. // summary: Disconnect image registry
  214. // tags:
  215. // - Registries
  216. // parameters:
  217. // - name: project_id
  218. // - name: registry_id
  219. // responses:
  220. // '200':
  221. // description: Successfully disconnected image registry
  222. // '403':
  223. // description: Forbidden
  224. deleteEndpoint := factory.NewAPIEndpoint(
  225. &types.APIRequestMetadata{
  226. Verb: types.APIVerbDelete,
  227. Method: types.HTTPVerbDelete,
  228. Path: &types.Path{
  229. Parent: basePath,
  230. RelativePath: relPath + "/{registry_id}",
  231. },
  232. Scopes: []types.PermissionScope{
  233. types.UserScope,
  234. types.ProjectScope,
  235. types.RegistryScope,
  236. },
  237. },
  238. )
  239. deleteHandler := registry.NewRegistryDeleteHandler(
  240. config,
  241. factory.GetResultWriter(),
  242. )
  243. routes = append(routes, &router.Route{
  244. Endpoint: deleteEndpoint,
  245. Handler: deleteHandler,
  246. Router: r,
  247. })
  248. // POST /api/v1/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryCreateRepositoryHandler
  249. // swagger:operation POST /api/v1/projects/{project_id}/registries/{registry_id}/repositories createRegistryRepository
  250. //
  251. // Creates an image repository inside the registry specified by `registry_id`. This method **only** creates repositories for ECR-integrated
  252. // repositories.
  253. //
  254. // ---
  255. // produces:
  256. // - application/json
  257. // summary: Create image repository
  258. // tags:
  259. // - Registries
  260. // parameters:
  261. // - name: project_id
  262. // - name: registry_id
  263. // - in: body
  264. // name: CreateRepositoryRequest
  265. // description: The repository to create
  266. // schema:
  267. // $ref: '#/definitions/CreateRegistryRepositoryRequest'
  268. // responses:
  269. // '201':
  270. // description: Successfully created the image repository
  271. // '403':
  272. // description: Forbidden
  273. createRepositoryEndpoint := factory.NewAPIEndpoint(
  274. &types.APIRequestMetadata{
  275. Verb: types.APIVerbCreate,
  276. Method: types.HTTPVerbPost,
  277. Path: &types.Path{
  278. Parent: basePath,
  279. RelativePath: relPath + "/{registry_id}/repositories",
  280. },
  281. Scopes: []types.PermissionScope{
  282. types.UserScope,
  283. types.ProjectScope,
  284. types.RegistryScope,
  285. },
  286. },
  287. )
  288. createRepositoryHandler := registry.NewRegistryCreateRepositoryHandler(
  289. config,
  290. factory.GetDecoderValidator(),
  291. factory.GetResultWriter(),
  292. )
  293. routes = append(routes, &router.Route{
  294. Endpoint: createRepositoryEndpoint,
  295. Handler: createRepositoryHandler,
  296. Router: r,
  297. })
  298. // GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryListRepositoriesHandler
  299. // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories listRegistryRepositories
  300. //
  301. // Lists image repositories inside the image registry denoted by `registry_id`. The registry
  302. // should belong to the project denoted by `project_id`.
  303. //
  304. // ---
  305. // produces:
  306. // - application/json
  307. // summary: List image repositories
  308. // tags:
  309. // - Registries
  310. // parameters:
  311. // - name: project_id
  312. // - name: registry_id
  313. // responses:
  314. // '200':
  315. // description: Successfully listed image repositories
  316. // schema:
  317. // $ref: '#/definitions/ListRegistryRepositoriesResponse'
  318. // '403':
  319. // description: Forbidden
  320. listRepositoriesEndpoint := factory.NewAPIEndpoint(
  321. &types.APIRequestMetadata{
  322. Verb: types.APIVerbList,
  323. Method: types.HTTPVerbGet,
  324. Path: &types.Path{
  325. Parent: basePath,
  326. RelativePath: relPath + "/{registry_id}/repositories",
  327. },
  328. Scopes: []types.PermissionScope{
  329. types.UserScope,
  330. types.ProjectScope,
  331. types.RegistryScope,
  332. },
  333. },
  334. )
  335. listRepositoriesHandler := registry.NewRegistryListRepositoriesHandler(
  336. config,
  337. factory.GetResultWriter(),
  338. )
  339. routes = append(routes, &router.Route{
  340. Endpoint: listRepositoriesEndpoint,
  341. Handler: listRepositoriesHandler,
  342. Router: r,
  343. })
  344. // GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories/* -> registry.NewRegistryListImagesHandler
  345. // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories/{repository} listRegistryImages
  346. //
  347. // Lists all images in the image repository denoted by the name `repository`. The repository should belong
  348. // to the registry denoted by `registry_id` which should itself belong to the project denoted by
  349. // `project_id`.
  350. //
  351. // ---
  352. // produces:
  353. // - application/json
  354. // summary: List images
  355. // tags:
  356. // - Registries
  357. // parameters:
  358. // - name: project_id
  359. // - name: registry_id
  360. // - name: repository
  361. // in: path
  362. // description: The image repository name. Should be of the form REPOSITORY/IMAGE when using Google Artifact Registry.
  363. // type: string
  364. // required: true
  365. // - name: num
  366. // in: query
  367. // description: |
  368. // The number of images to list.
  369. // For ECR images, a maximum of 1000 is allowed.
  370. // type: integer
  371. // required: false
  372. // minimum: 1
  373. // - name: next
  374. // in: query
  375. // description: The next page string used for pagination, from a previous request.
  376. // type: string
  377. // - name: page
  378. // in: query
  379. // description: |
  380. // The page number used for pagination, possibly from a previous request.
  381. // (**DigitalOcean only**)
  382. // type: integer
  383. // minimum: 1
  384. // responses:
  385. // '200':
  386. // description: Successfully listed images
  387. // schema:
  388. // $ref: '#/definitions/V1ListImageResponse'
  389. // '400':
  390. // description: A malformed or bad request
  391. // '403':
  392. // description: Forbidden
  393. listImagesEndpoint := factory.NewAPIEndpoint(
  394. &types.APIRequestMetadata{
  395. Verb: types.APIVerbList,
  396. Method: types.HTTPVerbGet,
  397. Path: &types.Path{
  398. Parent: basePath,
  399. RelativePath: fmt.Sprintf(
  400. "%s/{registry_id}/repositories/%s",
  401. relPath,
  402. types.URLParamWildcard,
  403. ),
  404. },
  405. Scopes: []types.PermissionScope{
  406. types.UserScope,
  407. types.ProjectScope,
  408. types.RegistryScope,
  409. },
  410. },
  411. )
  412. listImagesHandler := v1Registry.NewRegistryListImagesHandler(
  413. config,
  414. factory.GetDecoderValidator(),
  415. factory.GetResultWriter(),
  416. )
  417. routes = append(routes, &router.Route{
  418. Endpoint: listImagesEndpoint,
  419. Handler: listImagesHandler,
  420. Router: r,
  421. })
  422. return routes, newPath
  423. }