release.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/release"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewReleaseScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetReleaseScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetReleaseScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getReleaseRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getReleaseRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/releases/{name}/{version}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} -> release.NewReleaseGetHandler
  46. getEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath,
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. types.ClusterScope,
  58. types.NamespaceScope,
  59. types.ReleaseScope,
  60. },
  61. },
  62. )
  63. getHandler := release.NewReleaseGetHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &Route{
  68. Endpoint: getEndpoint,
  69. Handler: getHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/controllers -> release.NewGetControllersHandler
  73. getControllersEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/controllers",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. types.NamespaceScope,
  86. types.ReleaseScope,
  87. },
  88. },
  89. )
  90. getControllersHandler := release.NewGetControllersHandler(
  91. config,
  92. factory.GetResultWriter(),
  93. )
  94. routes = append(routes, &Route{
  95. Endpoint: getControllersEndpoint,
  96. Handler: getControllersHandler,
  97. Router: r,
  98. })
  99. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/components -> release.NewGetComponentsHandler
  100. getComponentsEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbGet,
  103. Method: types.HTTPVerbGet,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: relPath + "/components",
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. types.ClusterScope,
  112. types.NamespaceScope,
  113. types.ReleaseScope,
  114. },
  115. },
  116. )
  117. getComponentsHandler := release.NewGetComponentsHandler(
  118. config,
  119. factory.GetResultWriter(),
  120. )
  121. routes = append(routes, &Route{
  122. Endpoint: getComponentsEndpoint,
  123. Handler: getComponentsHandler,
  124. Router: r,
  125. })
  126. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/history -> release.NewGetHistoryHandler
  127. getHistoryEndpoint := factory.NewAPIEndpoint(
  128. &types.APIRequestMetadata{
  129. Verb: types.APIVerbGet,
  130. Method: types.HTTPVerbGet,
  131. Path: &types.Path{
  132. Parent: basePath,
  133. RelativePath: "/releases/{name}/history",
  134. },
  135. Scopes: []types.PermissionScope{
  136. types.UserScope,
  137. types.ProjectScope,
  138. types.ClusterScope,
  139. types.NamespaceScope,
  140. },
  141. },
  142. )
  143. getHistoryHandler := release.NewGetReleaseHistoryHandler(
  144. config,
  145. factory.GetDecoderValidator(),
  146. factory.GetResultWriter(),
  147. )
  148. routes = append(routes, &Route{
  149. Endpoint: getHistoryEndpoint,
  150. Handler: getHistoryHandler,
  151. Router: r,
  152. })
  153. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/pods/all -> release.NewGetAllPodsHandler
  154. getAllPodsEndpoint := factory.NewAPIEndpoint(
  155. &types.APIRequestMetadata{
  156. Verb: types.APIVerbGet,
  157. Method: types.HTTPVerbGet,
  158. Path: &types.Path{
  159. Parent: basePath,
  160. RelativePath: relPath + "/pods/all",
  161. },
  162. Scopes: []types.PermissionScope{
  163. types.UserScope,
  164. types.ProjectScope,
  165. types.ClusterScope,
  166. types.NamespaceScope,
  167. types.ReleaseScope,
  168. },
  169. },
  170. )
  171. getAllPodsHandler := release.NewGetAllPodsHandler(
  172. config,
  173. factory.GetResultWriter(),
  174. )
  175. routes = append(routes, &Route{
  176. Endpoint: getAllPodsEndpoint,
  177. Handler: getAllPodsHandler,
  178. Router: r,
  179. })
  180. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/notifications -> release.NewUpdateNotificationHandler
  181. updateNotifsEndpoint := factory.NewAPIEndpoint(
  182. &types.APIRequestMetadata{
  183. Verb: types.APIVerbUpdate,
  184. Method: types.HTTPVerbPost,
  185. Path: &types.Path{
  186. Parent: basePath,
  187. RelativePath: "/releases/{name}/notifications",
  188. },
  189. Scopes: []types.PermissionScope{
  190. types.UserScope,
  191. types.ProjectScope,
  192. types.ClusterScope,
  193. types.NamespaceScope,
  194. },
  195. },
  196. )
  197. updateNotifsHandler := release.NewUpdateNotificationHandler(
  198. config,
  199. factory.GetDecoderValidator(),
  200. factory.GetResultWriter(),
  201. )
  202. routes = append(routes, &Route{
  203. Endpoint: updateNotifsEndpoint,
  204. Handler: updateNotifsHandler,
  205. Router: r,
  206. })
  207. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/notifications -> release.NewGetNotificationHandler
  208. getNotifsEndpoint := factory.NewAPIEndpoint(
  209. &types.APIRequestMetadata{
  210. Verb: types.APIVerbGet,
  211. Method: types.HTTPVerbGet,
  212. Path: &types.Path{
  213. Parent: basePath,
  214. RelativePath: "/releases/{name}/notifications",
  215. },
  216. Scopes: []types.PermissionScope{
  217. types.UserScope,
  218. types.ProjectScope,
  219. types.ClusterScope,
  220. types.NamespaceScope,
  221. },
  222. },
  223. )
  224. getNotifsHandler := release.NewGetNotificationHandler(
  225. config,
  226. factory.GetResultWriter(),
  227. )
  228. routes = append(routes, &Route{
  229. Endpoint: getNotifsEndpoint,
  230. Handler: getNotifsHandler,
  231. Router: r,
  232. })
  233. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/webhook -> release.NewGetWebhookHandler
  234. getWebhookEndpoint := factory.NewAPIEndpoint(
  235. &types.APIRequestMetadata{
  236. Verb: types.APIVerbGet,
  237. Method: types.HTTPVerbGet,
  238. Path: &types.Path{
  239. Parent: basePath,
  240. RelativePath: "/releases/{name}/webhook",
  241. },
  242. Scopes: []types.PermissionScope{
  243. types.UserScope,
  244. types.ProjectScope,
  245. types.ClusterScope,
  246. types.NamespaceScope,
  247. },
  248. },
  249. )
  250. getWebhookHandler := release.NewGetWebhookHandler(
  251. config,
  252. factory.GetResultWriter(),
  253. )
  254. routes = append(routes, &Route{
  255. Endpoint: getWebhookEndpoint,
  256. Handler: getWebhookHandler,
  257. Router: r,
  258. })
  259. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/webhook -> release.NewCreateWebhookHandler
  260. createWebhookEndpoint := factory.NewAPIEndpoint(
  261. &types.APIRequestMetadata{
  262. Verb: types.APIVerbCreate,
  263. Method: types.HTTPVerbPost,
  264. Path: &types.Path{
  265. Parent: basePath,
  266. RelativePath: relPath + "/webhook",
  267. },
  268. Scopes: []types.PermissionScope{
  269. types.UserScope,
  270. types.ProjectScope,
  271. types.ClusterScope,
  272. types.NamespaceScope,
  273. types.ReleaseScope,
  274. },
  275. },
  276. )
  277. createWebhookHandler := release.NewCreateWebhookHandler(
  278. config,
  279. factory.GetResultWriter(),
  280. )
  281. routes = append(routes, &Route{
  282. Endpoint: createWebhookEndpoint,
  283. Handler: createWebhookHandler,
  284. Router: r,
  285. })
  286. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> release.NewCreateReleaseHandler
  287. createReleaseEndpoint := factory.NewAPIEndpoint(
  288. &types.APIRequestMetadata{
  289. Verb: types.APIVerbCreate,
  290. Method: types.HTTPVerbPost,
  291. Path: &types.Path{
  292. Parent: basePath,
  293. RelativePath: "/releases",
  294. },
  295. Scopes: []types.PermissionScope{
  296. types.UserScope,
  297. types.ProjectScope,
  298. types.ClusterScope,
  299. types.NamespaceScope,
  300. },
  301. },
  302. )
  303. createReleaseHandler := release.NewCreateReleaseHandler(
  304. config,
  305. factory.GetDecoderValidator(),
  306. factory.GetResultWriter(),
  307. )
  308. routes = append(routes, &Route{
  309. Endpoint: createReleaseEndpoint,
  310. Handler: createReleaseHandler,
  311. Router: r,
  312. })
  313. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/addons -> release.NewCreateAddonHandler
  314. createAddonEndpoint := factory.NewAPIEndpoint(
  315. &types.APIRequestMetadata{
  316. Verb: types.APIVerbCreate,
  317. Method: types.HTTPVerbPost,
  318. Path: &types.Path{
  319. Parent: basePath,
  320. RelativePath: "/addons",
  321. },
  322. Scopes: []types.PermissionScope{
  323. types.UserScope,
  324. types.ProjectScope,
  325. types.ClusterScope,
  326. types.NamespaceScope,
  327. },
  328. },
  329. )
  330. createAddonHandler := release.NewCreateAddonHandler(
  331. config,
  332. factory.GetDecoderValidator(),
  333. factory.GetResultWriter(),
  334. )
  335. routes = append(routes, &Route{
  336. Endpoint: createAddonEndpoint,
  337. Handler: createAddonHandler,
  338. Router: r,
  339. })
  340. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/gha_template -> release.NewGetGHATemplateHandler
  341. getGHATemplateEndpoint := factory.NewAPIEndpoint(
  342. &types.APIRequestMetadata{
  343. Verb: types.APIVerbGet,
  344. Method: types.HTTPVerbPost,
  345. Path: &types.Path{
  346. Parent: basePath,
  347. RelativePath: "/releases/gha_template",
  348. },
  349. Scopes: []types.PermissionScope{
  350. types.UserScope,
  351. types.ProjectScope,
  352. types.ClusterScope,
  353. types.NamespaceScope,
  354. },
  355. },
  356. )
  357. getGHATemplateHandler := release.NewGetGHATemplateHandler(
  358. config,
  359. factory.GetDecoderValidator(),
  360. factory.GetResultWriter(),
  361. )
  362. routes = append(routes, &Route{
  363. Endpoint: getGHATemplateEndpoint,
  364. Handler: getGHATemplateHandler,
  365. Router: r,
  366. })
  367. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/rollback ->
  368. // release.NewRollbackReleaseHandler
  369. rollbackEndpoint := factory.NewAPIEndpoint(
  370. &types.APIRequestMetadata{
  371. Verb: types.APIVerbUpdate,
  372. Method: types.HTTPVerbPost,
  373. Path: &types.Path{
  374. Parent: basePath,
  375. RelativePath: relPath + "/rollback",
  376. },
  377. Scopes: []types.PermissionScope{
  378. types.UserScope,
  379. types.ProjectScope,
  380. types.ClusterScope,
  381. types.NamespaceScope,
  382. types.ReleaseScope,
  383. },
  384. },
  385. )
  386. rollbackHandler := release.NewRollbackReleaseHandler(
  387. config,
  388. factory.GetDecoderValidator(),
  389. factory.GetResultWriter(),
  390. )
  391. routes = append(routes, &Route{
  392. Endpoint: rollbackEndpoint,
  393. Handler: rollbackHandler,
  394. Router: r,
  395. })
  396. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/upgrade ->
  397. // release.NewUpgradeReleaseHandler
  398. upgradeEndpoint := factory.NewAPIEndpoint(
  399. &types.APIRequestMetadata{
  400. Verb: types.APIVerbUpdate,
  401. Method: types.HTTPVerbPost,
  402. Path: &types.Path{
  403. Parent: basePath,
  404. RelativePath: relPath + "/upgrade",
  405. },
  406. Scopes: []types.PermissionScope{
  407. types.UserScope,
  408. types.ProjectScope,
  409. types.ClusterScope,
  410. types.NamespaceScope,
  411. types.ReleaseScope,
  412. },
  413. },
  414. )
  415. upgradeHandler := release.NewUpgradeReleaseHandler(
  416. config,
  417. factory.GetDecoderValidator(),
  418. factory.GetResultWriter(),
  419. )
  420. routes = append(routes, &Route{
  421. Endpoint: upgradeEndpoint,
  422. Handler: upgradeHandler,
  423. Router: r,
  424. })
  425. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  426. // release.NewDeleteReleaseHandler
  427. deleteEndpoint := factory.NewAPIEndpoint(
  428. &types.APIRequestMetadata{
  429. Verb: types.APIVerbDelete,
  430. Method: types.HTTPVerbDelete,
  431. Path: &types.Path{
  432. Parent: basePath,
  433. RelativePath: relPath,
  434. },
  435. Scopes: []types.PermissionScope{
  436. types.UserScope,
  437. types.ProjectScope,
  438. types.ClusterScope,
  439. types.NamespaceScope,
  440. types.ReleaseScope,
  441. },
  442. },
  443. )
  444. deleteHandler := release.NewDeleteReleaseHandler(
  445. config,
  446. factory.GetDecoderValidator(),
  447. factory.GetResultWriter(),
  448. )
  449. routes = append(routes, &Route{
  450. Endpoint: deleteEndpoint,
  451. Handler: deleteHandler,
  452. Router: r,
  453. })
  454. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/image/batch ->
  455. // release.NewUpdateImageBatchHandler
  456. updateImageBatchEndpoint := factory.NewAPIEndpoint(
  457. &types.APIRequestMetadata{
  458. Verb: types.APIVerbUpdate,
  459. Method: types.HTTPVerbPost,
  460. Path: &types.Path{
  461. Parent: basePath,
  462. RelativePath: "/releases/image/batch",
  463. },
  464. Scopes: []types.PermissionScope{
  465. types.UserScope,
  466. types.ProjectScope,
  467. types.ClusterScope,
  468. types.NamespaceScope,
  469. },
  470. },
  471. )
  472. updateImageBatchHandler := release.NewUpgradeReleaseHandler(
  473. config,
  474. factory.GetDecoderValidator(),
  475. factory.GetResultWriter(),
  476. )
  477. routes = append(routes, &Route{
  478. Endpoint: updateImageBatchEndpoint,
  479. Handler: updateImageBatchHandler,
  480. Router: r,
  481. })
  482. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/jobs ->
  483. // release.NewGetJobsHandler
  484. getJobsEndpoint := factory.NewAPIEndpoint(
  485. &types.APIRequestMetadata{
  486. Verb: types.APIVerbGet,
  487. Method: types.HTTPVerbGet,
  488. Path: &types.Path{
  489. Parent: basePath,
  490. RelativePath: relPath + "/jobs",
  491. },
  492. Scopes: []types.PermissionScope{
  493. types.UserScope,
  494. types.ProjectScope,
  495. types.ClusterScope,
  496. types.NamespaceScope,
  497. types.ReleaseScope,
  498. },
  499. },
  500. )
  501. getJobsHandler := release.NewGetJobsHandler(
  502. config,
  503. factory.GetResultWriter(),
  504. )
  505. routes = append(routes, &Route{
  506. Endpoint: getJobsEndpoint,
  507. Handler: getJobsHandler,
  508. Router: r,
  509. })
  510. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/jobs/status ->
  511. // release.NewGetJobsHandler
  512. getJobsStatusEndpoint := factory.NewAPIEndpoint(
  513. &types.APIRequestMetadata{
  514. Verb: types.APIVerbGet,
  515. Method: types.HTTPVerbGet,
  516. Path: &types.Path{
  517. Parent: basePath,
  518. RelativePath: relPath + "/jobs/status",
  519. },
  520. Scopes: []types.PermissionScope{
  521. types.UserScope,
  522. types.ProjectScope,
  523. types.ClusterScope,
  524. types.NamespaceScope,
  525. types.ReleaseScope,
  526. },
  527. },
  528. )
  529. getJobsStatusHandler := release.NewGetJobsStatusHandler(
  530. config,
  531. factory.GetResultWriter(),
  532. )
  533. routes = append(routes, &Route{
  534. Endpoint: getJobsStatusEndpoint,
  535. Handler: getJobsStatusHandler,
  536. Router: r,
  537. })
  538. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/subdomain -> release.NewCreateSubdomainHandler
  539. createSubdomainEndpoint := factory.NewAPIEndpoint(
  540. &types.APIRequestMetadata{
  541. Verb: types.APIVerbCreate,
  542. Method: types.HTTPVerbPost,
  543. Path: &types.Path{
  544. Parent: basePath,
  545. RelativePath: "/releases/{name}/subdomain",
  546. },
  547. Scopes: []types.PermissionScope{
  548. types.UserScope,
  549. types.ProjectScope,
  550. types.ClusterScope,
  551. types.NamespaceScope,
  552. },
  553. },
  554. )
  555. createSubdomainHandler := release.NewCreateSubdomainHandler(
  556. config,
  557. factory.GetResultWriter(),
  558. )
  559. routes = append(routes, &Route{
  560. Endpoint: createSubdomainEndpoint,
  561. Handler: createSubdomainHandler,
  562. Router: r,
  563. })
  564. return routes, newPath
  565. }