release.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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/{name}/{version}/rollback ->
  341. // release.NewRollbackReleaseHandler
  342. rollbackEndpoint := factory.NewAPIEndpoint(
  343. &types.APIRequestMetadata{
  344. Verb: types.APIVerbUpdate,
  345. Method: types.HTTPVerbPost,
  346. Path: &types.Path{
  347. Parent: basePath,
  348. RelativePath: relPath + "/rollback",
  349. },
  350. Scopes: []types.PermissionScope{
  351. types.UserScope,
  352. types.ProjectScope,
  353. types.ClusterScope,
  354. types.NamespaceScope,
  355. types.ReleaseScope,
  356. },
  357. },
  358. )
  359. rollbackHandler := release.NewRollbackReleaseHandler(
  360. config,
  361. factory.GetDecoderValidator(),
  362. factory.GetResultWriter(),
  363. )
  364. routes = append(routes, &Route{
  365. Endpoint: rollbackEndpoint,
  366. Handler: rollbackHandler,
  367. Router: r,
  368. })
  369. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/upgrade ->
  370. // release.NewUpgradeReleaseHandler
  371. upgradeEndpoint := factory.NewAPIEndpoint(
  372. &types.APIRequestMetadata{
  373. Verb: types.APIVerbUpdate,
  374. Method: types.HTTPVerbPost,
  375. Path: &types.Path{
  376. Parent: basePath,
  377. RelativePath: relPath + "/upgrade",
  378. },
  379. Scopes: []types.PermissionScope{
  380. types.UserScope,
  381. types.ProjectScope,
  382. types.ClusterScope,
  383. types.NamespaceScope,
  384. types.ReleaseScope,
  385. },
  386. },
  387. )
  388. upgradeHandler := release.NewUpgradeReleaseHandler(
  389. config,
  390. factory.GetDecoderValidator(),
  391. factory.GetResultWriter(),
  392. )
  393. routes = append(routes, &Route{
  394. Endpoint: upgradeEndpoint,
  395. Handler: upgradeHandler,
  396. Router: r,
  397. })
  398. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  399. // release.NewDeleteReleaseHandler
  400. deleteEndpoint := factory.NewAPIEndpoint(
  401. &types.APIRequestMetadata{
  402. Verb: types.APIVerbDelete,
  403. Method: types.HTTPVerbDelete,
  404. Path: &types.Path{
  405. Parent: basePath,
  406. RelativePath: relPath,
  407. },
  408. Scopes: []types.PermissionScope{
  409. types.UserScope,
  410. types.ProjectScope,
  411. types.ClusterScope,
  412. types.NamespaceScope,
  413. types.ReleaseScope,
  414. },
  415. },
  416. )
  417. deleteHandler := release.NewDeleteReleaseHandler(
  418. config,
  419. factory.GetDecoderValidator(),
  420. factory.GetResultWriter(),
  421. )
  422. routes = append(routes, &Route{
  423. Endpoint: deleteEndpoint,
  424. Handler: deleteHandler,
  425. Router: r,
  426. })
  427. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/image/batch ->
  428. // release.NewUpdateImageBatchHandler
  429. updateImageBatchEndpoint := factory.NewAPIEndpoint(
  430. &types.APIRequestMetadata{
  431. Verb: types.APIVerbUpdate,
  432. Method: types.HTTPVerbPost,
  433. Path: &types.Path{
  434. Parent: basePath,
  435. RelativePath: "/releases/image/batch",
  436. },
  437. Scopes: []types.PermissionScope{
  438. types.UserScope,
  439. types.ProjectScope,
  440. types.ClusterScope,
  441. types.NamespaceScope,
  442. },
  443. },
  444. )
  445. updateImageBatchHandler := release.NewUpgradeReleaseHandler(
  446. config,
  447. factory.GetDecoderValidator(),
  448. factory.GetResultWriter(),
  449. )
  450. routes = append(routes, &Route{
  451. Endpoint: updateImageBatchEndpoint,
  452. Handler: updateImageBatchHandler,
  453. Router: r,
  454. })
  455. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/jobs ->
  456. // release.NewGetJobsHandler
  457. getJobsEndpoint := factory.NewAPIEndpoint(
  458. &types.APIRequestMetadata{
  459. Verb: types.APIVerbGet,
  460. Method: types.HTTPVerbGet,
  461. Path: &types.Path{
  462. Parent: basePath,
  463. RelativePath: relPath + "/jobs",
  464. },
  465. Scopes: []types.PermissionScope{
  466. types.UserScope,
  467. types.ProjectScope,
  468. types.ClusterScope,
  469. types.NamespaceScope,
  470. types.ReleaseScope,
  471. },
  472. },
  473. )
  474. getJobsHandler := release.NewGetJobsHandler(
  475. config,
  476. factory.GetResultWriter(),
  477. )
  478. routes = append(routes, &Route{
  479. Endpoint: getJobsEndpoint,
  480. Handler: getJobsHandler,
  481. Router: r,
  482. })
  483. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/jobs/status ->
  484. // release.NewGetJobsHandler
  485. getJobsStatusEndpoint := factory.NewAPIEndpoint(
  486. &types.APIRequestMetadata{
  487. Verb: types.APIVerbGet,
  488. Method: types.HTTPVerbGet,
  489. Path: &types.Path{
  490. Parent: basePath,
  491. RelativePath: relPath + "/jobs/status",
  492. },
  493. Scopes: []types.PermissionScope{
  494. types.UserScope,
  495. types.ProjectScope,
  496. types.ClusterScope,
  497. types.NamespaceScope,
  498. types.ReleaseScope,
  499. },
  500. },
  501. )
  502. getJobsStatusHandler := release.NewGetJobsStatusHandler(
  503. config,
  504. factory.GetResultWriter(),
  505. )
  506. routes = append(routes, &Route{
  507. Endpoint: getJobsStatusEndpoint,
  508. Handler: getJobsStatusHandler,
  509. Router: r,
  510. })
  511. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/subdomain -> release.NewCreateSubdomainHandler
  512. createSubdomainEndpoint := factory.NewAPIEndpoint(
  513. &types.APIRequestMetadata{
  514. Verb: types.APIVerbCreate,
  515. Method: types.HTTPVerbPost,
  516. Path: &types.Path{
  517. Parent: basePath,
  518. RelativePath: "/releases/{name}/subdomain",
  519. },
  520. Scopes: []types.PermissionScope{
  521. types.UserScope,
  522. types.ProjectScope,
  523. types.ClusterScope,
  524. types.NamespaceScope,
  525. },
  526. },
  527. )
  528. createSubdomainHandler := release.NewCreateSubdomainHandler(
  529. config,
  530. factory.GetResultWriter(),
  531. )
  532. routes = append(routes, &Route{
  533. Endpoint: createSubdomainEndpoint,
  534. Handler: createSubdomainHandler,
  535. Router: r,
  536. })
  537. return routes, newPath
  538. }