cluster.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/cluster"
  6. "github.com/porter-dev/porter/api/server/handlers/database"
  7. "github.com/porter-dev/porter/api/server/handlers/environment"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/server/shared/router"
  11. "github.com/porter-dev/porter/api/types"
  12. )
  13. func NewClusterScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  14. return &router.Registerer{
  15. GetRoutes: GetClusterScopedRoutes,
  16. Children: children,
  17. }
  18. }
  19. func GetClusterScopedRoutes(
  20. r chi.Router,
  21. config *config.Config,
  22. basePath *types.Path,
  23. factory shared.APIEndpointFactory,
  24. children ...*router.Registerer,
  25. ) []*router.Route {
  26. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  27. if len(children) > 0 {
  28. r.Route(projPath.RelativePath, func(r chi.Router) {
  29. for _, child := range children {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. }
  33. })
  34. }
  35. return routes
  36. }
  37. func getClusterRoutes(
  38. r chi.Router,
  39. config *config.Config,
  40. basePath *types.Path,
  41. factory shared.APIEndpointFactory,
  42. ) ([]*router.Route, *types.Path) {
  43. relPath := "/clusters/{cluster_id}"
  44. newPath := &types.Path{
  45. Parent: basePath,
  46. RelativePath: relPath,
  47. }
  48. routes := make([]*router.Route, 0)
  49. // POST /api/projects/{project_id}/clusters -> project.NewCreateClusterManualHandler
  50. createEndpoint := factory.NewAPIEndpoint(
  51. &types.APIRequestMetadata{
  52. Verb: types.APIVerbCreate,
  53. Method: types.HTTPVerbPost,
  54. Path: &types.Path{
  55. Parent: basePath,
  56. RelativePath: "/clusters",
  57. },
  58. Scopes: []types.PermissionScope{
  59. types.UserScope,
  60. types.ProjectScope,
  61. },
  62. },
  63. )
  64. createHandler := cluster.NewCreateClusterManualHandler(
  65. config,
  66. factory.GetDecoderValidator(),
  67. factory.GetResultWriter(),
  68. )
  69. routes = append(routes, &router.Route{
  70. Endpoint: createEndpoint,
  71. Handler: createHandler,
  72. Router: r,
  73. })
  74. // POST /api/projects/{project_id}/clusters/candidates -> project.NewCreateClusterCandidateHandler
  75. createCandidateEndpoint := factory.NewAPIEndpoint(
  76. &types.APIRequestMetadata{
  77. Verb: types.APIVerbCreate,
  78. Method: types.HTTPVerbPost,
  79. Path: &types.Path{
  80. Parent: basePath,
  81. RelativePath: "/clusters/candidates",
  82. },
  83. Scopes: []types.PermissionScope{
  84. types.UserScope,
  85. types.ProjectScope,
  86. },
  87. CheckUsage: true,
  88. UsageMetric: types.Clusters,
  89. },
  90. )
  91. createCandidateHandler := cluster.NewCreateClusterCandidateHandler(
  92. config,
  93. factory.GetDecoderValidator(),
  94. factory.GetResultWriter(),
  95. )
  96. routes = append(routes, &router.Route{
  97. Endpoint: createCandidateEndpoint,
  98. Handler: createCandidateHandler,
  99. Router: r,
  100. })
  101. // GET /api/projects/{project_id}/clusters/candidates -> project.NewListClusterCandidatesHandler
  102. listCandidatesEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbList,
  105. Method: types.HTTPVerbGet,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: "/clusters/candidates",
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. },
  114. },
  115. )
  116. listCandidatesHandler := cluster.NewListClusterCandidatesHandler(
  117. config,
  118. factory.GetResultWriter(),
  119. )
  120. routes = append(routes, &router.Route{
  121. Endpoint: listCandidatesEndpoint,
  122. Handler: listCandidatesHandler,
  123. Router: r,
  124. })
  125. // POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve -> project.NewResolveClusterCandidateHandler
  126. resolveCandidateEndpoint := factory.NewAPIEndpoint(
  127. &types.APIRequestMetadata{
  128. Verb: types.APIVerbCreate,
  129. Method: types.HTTPVerbPost,
  130. Path: &types.Path{
  131. Parent: basePath,
  132. RelativePath: fmt.Sprintf(
  133. "/clusters/candidates/{%s}/resolve",
  134. types.URLParamCandidateID,
  135. ),
  136. },
  137. Scopes: []types.PermissionScope{
  138. types.UserScope,
  139. types.ProjectScope,
  140. },
  141. CheckUsage: true,
  142. UsageMetric: types.Clusters,
  143. },
  144. )
  145. resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
  146. config,
  147. factory.GetDecoderValidator(),
  148. factory.GetResultWriter(),
  149. )
  150. routes = append(routes, &router.Route{
  151. Endpoint: resolveCandidateEndpoint,
  152. Handler: resolveCandidateHandler,
  153. Router: r,
  154. })
  155. // POST /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterUpdateHandler
  156. updateClusterEndpoint := factory.NewAPIEndpoint(
  157. &types.APIRequestMetadata{
  158. Verb: types.APIVerbUpdate,
  159. Method: types.HTTPVerbPost,
  160. Path: &types.Path{
  161. Parent: basePath,
  162. RelativePath: relPath,
  163. },
  164. Scopes: []types.PermissionScope{
  165. types.UserScope,
  166. types.ProjectScope,
  167. types.ClusterScope,
  168. },
  169. },
  170. )
  171. updateClusterHandler := cluster.NewClusterUpdateHandler(
  172. config,
  173. factory.GetDecoderValidator(),
  174. factory.GetResultWriter(),
  175. )
  176. routes = append(routes, &router.Route{
  177. Endpoint: updateClusterEndpoint,
  178. Handler: updateClusterHandler,
  179. Router: r,
  180. })
  181. // DELETE /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterDeleteHandler
  182. deleteClusterEndpoint := factory.NewAPIEndpoint(
  183. &types.APIRequestMetadata{
  184. Verb: types.APIVerbDelete,
  185. Method: types.HTTPVerbDelete,
  186. Path: &types.Path{
  187. Parent: basePath,
  188. RelativePath: relPath,
  189. },
  190. Scopes: []types.PermissionScope{
  191. types.UserScope,
  192. types.ProjectScope,
  193. types.ClusterScope,
  194. },
  195. },
  196. )
  197. deleteClusterHandler := cluster.NewClusterDeleteHandler(
  198. config,
  199. factory.GetResultWriter(),
  200. )
  201. routes = append(routes, &router.Route{
  202. Endpoint: deleteClusterEndpoint,
  203. Handler: deleteClusterHandler,
  204. Router: r,
  205. })
  206. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  207. getEndpoint := factory.NewAPIEndpoint(
  208. &types.APIRequestMetadata{
  209. Verb: types.APIVerbGet,
  210. Method: types.HTTPVerbGet,
  211. Path: &types.Path{
  212. Parent: basePath,
  213. RelativePath: relPath,
  214. },
  215. Scopes: []types.PermissionScope{
  216. types.UserScope,
  217. types.ProjectScope,
  218. types.ClusterScope,
  219. },
  220. },
  221. )
  222. getHandler := cluster.NewClusterGetHandler(
  223. config,
  224. factory.GetResultWriter(),
  225. )
  226. routes = append(routes, &router.Route{
  227. Endpoint: getEndpoint,
  228. Handler: getHandler,
  229. Router: r,
  230. })
  231. // GET /api/projects/{project_id}/clusters/{cluster_id}/databases -> database.NewDatabaseListHandler
  232. listDatabaseEndpoint := factory.NewAPIEndpoint(
  233. &types.APIRequestMetadata{
  234. Verb: types.APIVerbList,
  235. Method: types.HTTPVerbGet,
  236. Path: &types.Path{
  237. Parent: basePath,
  238. RelativePath: relPath + "/databases",
  239. },
  240. Scopes: []types.PermissionScope{
  241. types.UserScope,
  242. types.ProjectScope,
  243. types.ClusterScope,
  244. },
  245. },
  246. )
  247. listDatabaseHandler := database.NewDatabaseListHandler(
  248. config,
  249. factory.GetResultWriter(),
  250. )
  251. routes = append(routes, &router.Route{
  252. Endpoint: listDatabaseEndpoint,
  253. Handler: listDatabaseHandler,
  254. Router: r,
  255. })
  256. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  257. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments -> environment.NewListEnvironmentHandler
  258. listEnvEndpoint := factory.NewAPIEndpoint(
  259. &types.APIRequestMetadata{
  260. Verb: types.APIVerbGet,
  261. Method: types.HTTPVerbGet,
  262. Path: &types.Path{
  263. Parent: basePath,
  264. RelativePath: relPath + "/environments",
  265. },
  266. Scopes: []types.PermissionScope{
  267. types.UserScope,
  268. types.ProjectScope,
  269. types.ClusterScope,
  270. },
  271. },
  272. )
  273. listEnvHandler := environment.NewListEnvironmentHandler(
  274. config,
  275. factory.GetResultWriter(),
  276. )
  277. routes = append(routes, &router.Route{
  278. Endpoint: listEnvEndpoint,
  279. Handler: listEnvHandler,
  280. Router: r,
  281. })
  282. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id} -> environment.NewGetEnvironmentHandler
  283. getEnvEndpoint := factory.NewAPIEndpoint(
  284. &types.APIRequestMetadata{
  285. Verb: types.APIVerbGet,
  286. Method: types.HTTPVerbGet,
  287. Path: &types.Path{
  288. Parent: basePath,
  289. RelativePath: relPath + "/environments/{environment_id}",
  290. },
  291. Scopes: []types.PermissionScope{
  292. types.UserScope,
  293. types.ProjectScope,
  294. types.ClusterScope,
  295. },
  296. },
  297. )
  298. getEnvHandler := environment.NewGetEnvironmentHandler(
  299. config,
  300. factory.GetResultWriter(),
  301. )
  302. routes = append(routes, &router.Route{
  303. Endpoint: getEnvEndpoint,
  304. Handler: getEnvHandler,
  305. Router: r,
  306. })
  307. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/environment/{environment_id}/toggle_new_comment -> environment.NewToggleNewCommentHandler
  308. toggleNewCommentEndpoint := factory.NewAPIEndpoint(
  309. &types.APIRequestMetadata{
  310. Verb: types.APIVerbUpdate,
  311. Method: types.HTTPVerbPatch,
  312. Path: &types.Path{
  313. Parent: basePath,
  314. RelativePath: relPath + "/environments/{environment_id}/toggle_new_comment",
  315. },
  316. Scopes: []types.PermissionScope{
  317. types.UserScope,
  318. types.ProjectScope,
  319. types.ClusterScope,
  320. },
  321. },
  322. )
  323. toggleNewCommentHandler := environment.NewToggleNewCommentHandler(
  324. config,
  325. factory.GetDecoderValidator(),
  326. factory.GetResultWriter(),
  327. )
  328. routes = append(routes, &router.Route{
  329. Endpoint: toggleNewCommentEndpoint,
  330. Handler: toggleNewCommentHandler,
  331. Router: r,
  332. })
  333. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployments -> environment.NewListDeploymentsByClusterHandler
  334. listDeploymentsEndpoint := factory.NewAPIEndpoint(
  335. &types.APIRequestMetadata{
  336. Verb: types.APIVerbGet,
  337. Method: types.HTTPVerbGet,
  338. Path: &types.Path{
  339. Parent: basePath,
  340. RelativePath: relPath + "/deployments",
  341. },
  342. Scopes: []types.PermissionScope{
  343. types.UserScope,
  344. types.ProjectScope,
  345. types.ClusterScope,
  346. },
  347. },
  348. )
  349. listDeploymentsHandler := environment.NewListDeploymentsByClusterHandler(
  350. config,
  351. factory.GetDecoderValidator(),
  352. factory.GetResultWriter(),
  353. )
  354. routes = append(routes, &router.Route{
  355. Endpoint: listDeploymentsEndpoint,
  356. Handler: listDeploymentsHandler,
  357. Router: r,
  358. })
  359. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/deployment -> environment.NewGetDeploymentByClusterHandler
  360. getDeploymentEndpoint := factory.NewAPIEndpoint(
  361. &types.APIRequestMetadata{
  362. Verb: types.APIVerbGet,
  363. Method: types.HTTPVerbGet,
  364. Path: &types.Path{
  365. Parent: basePath,
  366. RelativePath: relPath + "/environments/{environment_id}/deployment",
  367. },
  368. Scopes: []types.PermissionScope{
  369. types.UserScope,
  370. types.ProjectScope,
  371. types.ClusterScope,
  372. },
  373. },
  374. )
  375. getDeploymentHandler := environment.NewGetDeploymentByEnvironmentHandler(
  376. config,
  377. factory.GetDecoderValidator(),
  378. factory.GetResultWriter(),
  379. )
  380. routes = append(routes, &router.Route{
  381. Endpoint: getDeploymentEndpoint,
  382. Handler: getDeploymentHandler,
  383. Router: r,
  384. })
  385. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/reenable -> environment.NewReenableDeploymentHandler
  386. reenableDeploymentEndpoint := factory.NewAPIEndpoint(
  387. &types.APIRequestMetadata{
  388. Verb: types.APIVerbUpdate,
  389. Method: types.HTTPVerbPatch,
  390. Path: &types.Path{
  391. Parent: basePath,
  392. RelativePath: relPath + "/deployments/{deployment_id}/reenable",
  393. },
  394. Scopes: []types.PermissionScope{
  395. types.UserScope,
  396. types.ProjectScope,
  397. types.ClusterScope,
  398. },
  399. },
  400. )
  401. reenableDeploymentHandler := environment.NewReenableDeploymentHandler(
  402. config,
  403. factory.GetDecoderValidator(),
  404. factory.GetResultWriter(),
  405. )
  406. routes = append(routes, &router.Route{
  407. Endpoint: reenableDeploymentEndpoint,
  408. Handler: reenableDeploymentHandler,
  409. Router: r,
  410. })
  411. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/trigger_workflow -> environment.NewTriggerDeploymentWorkflowHandler
  412. triggerDeploymentWorkflowEndpoint := factory.NewAPIEndpoint(
  413. &types.APIRequestMetadata{
  414. Verb: types.APIVerbCreate,
  415. Method: types.HTTPVerbPost,
  416. Path: &types.Path{
  417. Parent: basePath,
  418. RelativePath: relPath + "/deployments/{deployment_id}/trigger_workflow",
  419. },
  420. Scopes: []types.PermissionScope{
  421. types.UserScope,
  422. types.ProjectScope,
  423. types.ClusterScope,
  424. },
  425. },
  426. )
  427. triggerDeploymentWorkflowHandler := environment.NewTriggerDeploymentWorkflowHandler(
  428. config,
  429. factory.GetDecoderValidator(),
  430. factory.GetResultWriter(),
  431. )
  432. routes = append(routes, &router.Route{
  433. Endpoint: triggerDeploymentWorkflowEndpoint,
  434. Handler: triggerDeploymentWorkflowHandler,
  435. Router: r,
  436. })
  437. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/pull_request -> environment.NewEnablePullRequestHandler
  438. enablePullRequestEndpoint := factory.NewAPIEndpoint(
  439. &types.APIRequestMetadata{
  440. Verb: types.APIVerbCreate,
  441. Method: types.HTTPVerbPost,
  442. Path: &types.Path{
  443. Parent: basePath,
  444. RelativePath: relPath + "/deployments/pull_request",
  445. },
  446. Scopes: []types.PermissionScope{
  447. types.UserScope,
  448. types.ProjectScope,
  449. types.ClusterScope,
  450. },
  451. },
  452. )
  453. enablePullRequestHandler := environment.NewEnablePullRequestHandler(
  454. config,
  455. factory.GetDecoderValidator(),
  456. factory.GetResultWriter(),
  457. )
  458. routes = append(routes, &router.Route{
  459. Endpoint: enablePullRequestEndpoint,
  460. Handler: enablePullRequestHandler,
  461. Router: r,
  462. })
  463. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id} ->
  464. // environment.NewDeleteDeploymentHandler
  465. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  466. &types.APIRequestMetadata{
  467. Verb: types.APIVerbDelete,
  468. Method: types.HTTPVerbDelete,
  469. Path: &types.Path{
  470. Parent: basePath,
  471. RelativePath: relPath + "/deployments/{deployment_id}",
  472. },
  473. Scopes: []types.PermissionScope{
  474. types.UserScope,
  475. types.ProjectScope,
  476. types.ClusterScope,
  477. },
  478. },
  479. )
  480. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  481. config,
  482. factory.GetDecoderValidator(),
  483. factory.GetResultWriter(),
  484. )
  485. routes = append(routes, &router.Route{
  486. Endpoint: deleteDeploymentEndpoint,
  487. Handler: deleteDeploymentHandler,
  488. Router: r,
  489. })
  490. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/settings ->
  491. // environment.NewUpdateEnvironmentSettingsHandler
  492. updateEnvironmentSettingsEndpoint := factory.NewAPIEndpoint(
  493. &types.APIRequestMetadata{
  494. Verb: types.APIVerbUpdate,
  495. Method: types.HTTPVerbPatch,
  496. Path: &types.Path{
  497. Parent: basePath,
  498. RelativePath: relPath + "/environments/{environment_id}/settings",
  499. },
  500. Scopes: []types.PermissionScope{
  501. types.UserScope,
  502. types.ProjectScope,
  503. types.ClusterScope,
  504. },
  505. },
  506. )
  507. updateEnvironmentSettingsHandler := environment.NewUpdateEnvironmentSettingsHandler(
  508. config,
  509. factory.GetDecoderValidator(),
  510. factory.GetResultWriter(),
  511. )
  512. routes = append(routes, &router.Route{
  513. Endpoint: updateEnvironmentSettingsEndpoint,
  514. Handler: updateEnvironmentSettingsHandler,
  515. Router: r,
  516. })
  517. }
  518. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  519. listNamespacesEndpoint := factory.NewAPIEndpoint(
  520. &types.APIRequestMetadata{
  521. Verb: types.APIVerbGet,
  522. Method: types.HTTPVerbGet,
  523. Path: &types.Path{
  524. Parent: basePath,
  525. RelativePath: relPath + "/namespaces",
  526. },
  527. Scopes: []types.PermissionScope{
  528. types.UserScope,
  529. types.ProjectScope,
  530. types.ClusterScope,
  531. },
  532. },
  533. )
  534. listNamespacesHandler := cluster.NewListNamespacesHandler(
  535. config,
  536. factory.GetResultWriter(),
  537. )
  538. routes = append(routes, &router.Route{
  539. Endpoint: listNamespacesEndpoint,
  540. Handler: listNamespacesHandler,
  541. Router: r,
  542. })
  543. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  544. listNodesEndpoint := factory.NewAPIEndpoint(
  545. &types.APIRequestMetadata{
  546. Verb: types.APIVerbGet,
  547. Method: types.HTTPVerbGet,
  548. Path: &types.Path{
  549. Parent: basePath,
  550. RelativePath: relPath + "/nodes",
  551. },
  552. Scopes: []types.PermissionScope{
  553. types.UserScope,
  554. types.ProjectScope,
  555. types.ClusterScope,
  556. },
  557. },
  558. )
  559. listNodesHandler := cluster.NewListNodesHandler(
  560. config,
  561. factory.GetResultWriter(),
  562. )
  563. routes = append(routes, &router.Route{
  564. Endpoint: listNodesEndpoint,
  565. Handler: listNodesHandler,
  566. Router: r,
  567. })
  568. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  569. getNodeEndpoint := factory.NewAPIEndpoint(
  570. &types.APIRequestMetadata{
  571. Verb: types.APIVerbGet,
  572. Method: types.HTTPVerbGet,
  573. Path: &types.Path{
  574. Parent: basePath,
  575. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  576. },
  577. Scopes: []types.PermissionScope{
  578. types.UserScope,
  579. types.ProjectScope,
  580. types.ClusterScope,
  581. },
  582. },
  583. )
  584. getNodeHandler := cluster.NewGetNodeHandler(
  585. config,
  586. factory.GetResultWriter(),
  587. )
  588. routes = append(routes, &router.Route{
  589. Endpoint: getNodeEndpoint,
  590. Handler: getNodeHandler,
  591. Router: r,
  592. })
  593. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  594. createNamespaceEndpoint := factory.NewAPIEndpoint(
  595. &types.APIRequestMetadata{
  596. Verb: types.APIVerbCreate,
  597. Method: types.HTTPVerbPost,
  598. Path: &types.Path{
  599. Parent: basePath,
  600. RelativePath: relPath + "/namespaces/create",
  601. },
  602. Scopes: []types.PermissionScope{
  603. types.UserScope,
  604. types.ProjectScope,
  605. types.ClusterScope,
  606. },
  607. },
  608. )
  609. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  610. config,
  611. factory.GetDecoderValidator(),
  612. factory.GetResultWriter(),
  613. )
  614. routes = append(routes, &router.Route{
  615. Endpoint: createNamespaceEndpoint,
  616. Handler: createNamespaceHandler,
  617. Router: r,
  618. })
  619. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  620. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  621. &types.APIRequestMetadata{
  622. Verb: types.APIVerbDelete,
  623. Method: types.HTTPVerbDelete,
  624. Path: &types.Path{
  625. Parent: basePath,
  626. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  627. },
  628. Scopes: []types.PermissionScope{
  629. types.UserScope,
  630. types.ProjectScope,
  631. types.ClusterScope,
  632. },
  633. },
  634. )
  635. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  636. config,
  637. factory.GetDecoderValidator(),
  638. )
  639. routes = append(routes, &router.Route{
  640. Endpoint: deleteNamespaceEndpoint,
  641. Handler: deleteNamespaceHandler,
  642. Router: r,
  643. })
  644. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  645. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  646. &types.APIRequestMetadata{
  647. Verb: types.APIVerbUpdate, // we do not want users with no-write access to be able to use this
  648. Method: types.HTTPVerbGet,
  649. Path: &types.Path{
  650. Parent: basePath,
  651. RelativePath: relPath + "/kubeconfig",
  652. },
  653. Scopes: []types.PermissionScope{
  654. types.UserScope,
  655. types.ProjectScope,
  656. types.ClusterScope,
  657. },
  658. },
  659. )
  660. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  661. config,
  662. factory.GetResultWriter(),
  663. )
  664. routes = append(routes, &router.Route{
  665. Endpoint: getTemporaryKubeconfigEndpoint,
  666. Handler: getTemporaryKubeconfigHandler,
  667. Router: r,
  668. })
  669. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  670. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  671. &types.APIRequestMetadata{
  672. Verb: types.APIVerbGet,
  673. Method: types.HTTPVerbGet,
  674. Path: &types.Path{
  675. Parent: basePath,
  676. RelativePath: relPath + "/prometheus/detect",
  677. },
  678. Scopes: []types.PermissionScope{
  679. types.UserScope,
  680. types.ProjectScope,
  681. types.ClusterScope,
  682. },
  683. },
  684. )
  685. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  686. routes = append(routes, &router.Route{
  687. Endpoint: detectPrometheusInstalledEndpoint,
  688. Handler: detectPrometheusInstalledHandler,
  689. Router: r,
  690. })
  691. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  692. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  693. &types.APIRequestMetadata{
  694. Verb: types.APIVerbGet,
  695. Method: types.HTTPVerbGet,
  696. Path: &types.Path{
  697. Parent: basePath,
  698. RelativePath: relPath + "/agent/detect",
  699. },
  700. Scopes: []types.PermissionScope{
  701. types.UserScope,
  702. types.ProjectScope,
  703. types.ClusterScope,
  704. },
  705. },
  706. )
  707. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  708. routes = append(routes, &router.Route{
  709. Endpoint: detectAgentInstalledEndpoint,
  710. Handler: detectAgentInstalledHandler,
  711. Router: r,
  712. })
  713. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  714. installAgentEndpoint := factory.NewAPIEndpoint(
  715. &types.APIRequestMetadata{
  716. Verb: types.APIVerbCreate,
  717. Method: types.HTTPVerbPost,
  718. Path: &types.Path{
  719. Parent: basePath,
  720. RelativePath: relPath + "/agent/install",
  721. },
  722. Scopes: []types.PermissionScope{
  723. types.UserScope,
  724. types.ProjectScope,
  725. types.ClusterScope,
  726. },
  727. },
  728. )
  729. installAgentHandler := cluster.NewInstallAgentHandler(
  730. config,
  731. factory.GetDecoderValidator(),
  732. factory.GetResultWriter(),
  733. )
  734. routes = append(routes, &router.Route{
  735. Endpoint: installAgentEndpoint,
  736. Handler: installAgentHandler,
  737. Router: r,
  738. })
  739. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/status -> cluster.NewGetAgentStatusHandler
  740. getAgentStatusEndpoint := factory.NewAPIEndpoint(
  741. &types.APIRequestMetadata{
  742. Verb: types.APIVerbGet,
  743. Method: types.HTTPVerbGet,
  744. Path: &types.Path{
  745. Parent: basePath,
  746. RelativePath: relPath + "/agent/status",
  747. },
  748. Scopes: []types.PermissionScope{
  749. types.UserScope,
  750. types.ProjectScope,
  751. types.ClusterScope,
  752. },
  753. },
  754. )
  755. getAgentStatusHandler := cluster.NewGetAgentStatusHandler(config, factory.GetResultWriter())
  756. routes = append(routes, &router.Route{
  757. Endpoint: getAgentStatusEndpoint,
  758. Handler: getAgentStatusHandler,
  759. Router: r,
  760. })
  761. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  762. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  763. &types.APIRequestMetadata{
  764. Verb: types.APIVerbCreate,
  765. Method: types.HTTPVerbPost,
  766. Path: &types.Path{
  767. Parent: basePath,
  768. RelativePath: relPath + "/agent/upgrade",
  769. },
  770. Scopes: []types.PermissionScope{
  771. types.UserScope,
  772. types.ProjectScope,
  773. types.ClusterScope,
  774. },
  775. },
  776. )
  777. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  778. config,
  779. factory.GetDecoderValidator(),
  780. factory.GetResultWriter(),
  781. )
  782. routes = append(routes, &router.Route{
  783. Endpoint: upgradeAgentEndpoint,
  784. Handler: upgradeAgentHandler,
  785. Router: r,
  786. })
  787. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  788. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  789. &types.APIRequestMetadata{
  790. Verb: types.APIVerbGet,
  791. Method: types.HTTPVerbGet,
  792. Path: &types.Path{
  793. Parent: basePath,
  794. RelativePath: relPath + "/prometheus/ingresses",
  795. },
  796. Scopes: []types.PermissionScope{
  797. types.UserScope,
  798. types.ProjectScope,
  799. types.ClusterScope,
  800. },
  801. },
  802. )
  803. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  804. config,
  805. factory.GetResultWriter(),
  806. )
  807. routes = append(routes, &router.Route{
  808. Endpoint: listNGINXIngressesEndpoint,
  809. Handler: listNGINXIngressesHandler,
  810. Router: r,
  811. })
  812. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  813. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  814. &types.APIRequestMetadata{
  815. Verb: types.APIVerbGet,
  816. Method: types.HTTPVerbGet,
  817. Path: &types.Path{
  818. Parent: basePath,
  819. RelativePath: relPath + "/metrics",
  820. },
  821. Scopes: []types.PermissionScope{
  822. types.UserScope,
  823. types.ProjectScope,
  824. types.ClusterScope,
  825. },
  826. },
  827. )
  828. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  829. config,
  830. factory.GetDecoderValidator(),
  831. factory.GetResultWriter(),
  832. )
  833. routes = append(routes, &router.Route{
  834. Endpoint: getPodMetricsEndpoint,
  835. Handler: getPodMetricsHandler,
  836. Router: r,
  837. })
  838. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  839. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  840. &types.APIRequestMetadata{
  841. Verb: types.APIVerbGet,
  842. Method: types.HTTPVerbGet,
  843. Path: &types.Path{
  844. Parent: basePath,
  845. RelativePath: relPath + "/helm_release",
  846. },
  847. Scopes: []types.PermissionScope{
  848. types.UserScope,
  849. types.ProjectScope,
  850. types.ClusterScope,
  851. },
  852. IsWebsocket: true,
  853. },
  854. )
  855. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  856. config,
  857. factory.GetDecoderValidator(),
  858. factory.GetResultWriter(),
  859. )
  860. routes = append(routes, &router.Route{
  861. Endpoint: streamHelmReleaseEndpoint,
  862. Handler: streamHelmReleaseHandler,
  863. Router: r,
  864. })
  865. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  866. streamStatusEndpoint := factory.NewAPIEndpoint(
  867. &types.APIRequestMetadata{
  868. Verb: types.APIVerbGet,
  869. Method: types.HTTPVerbGet,
  870. Path: &types.Path{
  871. Parent: basePath,
  872. RelativePath: fmt.Sprintf(
  873. "%s/{%s}/status",
  874. relPath,
  875. types.URLParamKind,
  876. ),
  877. },
  878. Scopes: []types.PermissionScope{
  879. types.UserScope,
  880. types.ProjectScope,
  881. types.ClusterScope,
  882. },
  883. IsWebsocket: true,
  884. },
  885. )
  886. streamStatusHandler := cluster.NewStreamStatusHandler(
  887. config,
  888. factory.GetDecoderValidator(),
  889. factory.GetResultWriter(),
  890. )
  891. routes = append(routes, &router.Route{
  892. Endpoint: streamStatusEndpoint,
  893. Handler: streamStatusHandler,
  894. Router: r,
  895. })
  896. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  897. getPodsEndpoint := factory.NewAPIEndpoint(
  898. &types.APIRequestMetadata{
  899. Verb: types.APIVerbGet,
  900. Method: types.HTTPVerbGet,
  901. Path: &types.Path{
  902. Parent: basePath,
  903. RelativePath: relPath + "/pods",
  904. },
  905. Scopes: []types.PermissionScope{
  906. types.UserScope,
  907. types.ProjectScope,
  908. types.ClusterScope,
  909. },
  910. },
  911. )
  912. getPodsHandler := cluster.NewGetPodsHandler(
  913. config,
  914. factory.GetDecoderValidator(),
  915. factory.GetResultWriter(),
  916. )
  917. routes = append(routes, &router.Route{
  918. Endpoint: getPodsEndpoint,
  919. Handler: getPodsHandler,
  920. Router: r,
  921. })
  922. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewListIncidentsHandler
  923. listIncidentsEndpoint := factory.NewAPIEndpoint(
  924. &types.APIRequestMetadata{
  925. Verb: types.APIVerbGet,
  926. Method: types.HTTPVerbGet,
  927. Path: &types.Path{
  928. Parent: basePath,
  929. RelativePath: relPath + "/incidents",
  930. },
  931. Scopes: []types.PermissionScope{
  932. types.UserScope,
  933. types.ProjectScope,
  934. types.ClusterScope,
  935. },
  936. },
  937. )
  938. listIncidentsHandler := cluster.NewListIncidentsHandler(
  939. config,
  940. factory.GetDecoderValidator(),
  941. factory.GetResultWriter(),
  942. )
  943. routes = append(routes, &router.Route{
  944. Endpoint: listIncidentsEndpoint,
  945. Handler: listIncidentsHandler,
  946. Router: r,
  947. })
  948. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/{incident_id} -> cluster.NewGetIncidentHandler
  949. getIncidentEndpoint := factory.NewAPIEndpoint(
  950. &types.APIRequestMetadata{
  951. Verb: types.APIVerbGet,
  952. Method: types.HTTPVerbGet,
  953. Path: &types.Path{
  954. Parent: basePath,
  955. RelativePath: fmt.Sprintf("%s/incidents/{%s}", relPath, types.URLParamIncidentID),
  956. },
  957. Scopes: []types.PermissionScope{
  958. types.UserScope,
  959. types.ProjectScope,
  960. types.ClusterScope,
  961. },
  962. },
  963. )
  964. getIncidentHandler := cluster.NewGetIncidentHandler(
  965. config,
  966. factory.GetDecoderValidator(),
  967. factory.GetResultWriter(),
  968. )
  969. routes = append(routes, &router.Route{
  970. Endpoint: getIncidentEndpoint,
  971. Handler: getIncidentHandler,
  972. Router: r,
  973. })
  974. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/{incident_id}/events -> cluster.NewListIncidentEventsHandler
  975. listIncidentEventsEndpoint := factory.NewAPIEndpoint(
  976. &types.APIRequestMetadata{
  977. Verb: types.APIVerbGet,
  978. Method: types.HTTPVerbGet,
  979. Path: &types.Path{
  980. Parent: basePath,
  981. RelativePath: fmt.Sprintf("%s/incidents/{%s}/events", relPath, types.URLParamIncidentID),
  982. },
  983. Scopes: []types.PermissionScope{
  984. types.UserScope,
  985. types.ProjectScope,
  986. types.ClusterScope,
  987. },
  988. },
  989. )
  990. listIncidentEventsHandler := cluster.NewListIncidentEventsHandler(
  991. config,
  992. factory.GetDecoderValidator(),
  993. factory.GetResultWriter(),
  994. )
  995. routes = append(routes, &router.Route{
  996. Endpoint: listIncidentEventsEndpoint,
  997. Handler: listIncidentEventsHandler,
  998. Router: r,
  999. })
  1000. // GET /api/projects/{project_id}/clusters/{cluster_id}/logs -> cluster.NewGetLogsHandler
  1001. getLogsEndpoint := factory.NewAPIEndpoint(
  1002. &types.APIRequestMetadata{
  1003. Verb: types.APIVerbGet,
  1004. Method: types.HTTPVerbGet,
  1005. Path: &types.Path{
  1006. Parent: basePath,
  1007. RelativePath: fmt.Sprintf("%s/logs", relPath),
  1008. },
  1009. Scopes: []types.PermissionScope{
  1010. types.UserScope,
  1011. types.ProjectScope,
  1012. types.ClusterScope,
  1013. },
  1014. },
  1015. )
  1016. getLogsHandler := cluster.NewGetLogsHandler(
  1017. config,
  1018. factory.GetDecoderValidator(),
  1019. factory.GetResultWriter(),
  1020. )
  1021. routes = append(routes, &router.Route{
  1022. Endpoint: getLogsEndpoint,
  1023. Handler: getLogsHandler,
  1024. Router: r,
  1025. })
  1026. // GET /api/projects/{project_id}/clusters/{cluster_id}/logs/pod_values -> cluster.NewGetLogPodValuesHandler
  1027. getLogPodValuesEndpoint := factory.NewAPIEndpoint(
  1028. &types.APIRequestMetadata{
  1029. Verb: types.APIVerbGet,
  1030. Method: types.HTTPVerbGet,
  1031. Path: &types.Path{
  1032. Parent: basePath,
  1033. RelativePath: fmt.Sprintf("%s/logs/pod_values", relPath),
  1034. },
  1035. Scopes: []types.PermissionScope{
  1036. types.UserScope,
  1037. types.ProjectScope,
  1038. types.ClusterScope,
  1039. },
  1040. },
  1041. )
  1042. getLogPodValuesHandler := cluster.NewGetLogPodValuesHandler(
  1043. config,
  1044. factory.GetDecoderValidator(),
  1045. factory.GetResultWriter(),
  1046. )
  1047. routes = append(routes, &router.Route{
  1048. Endpoint: getLogPodValuesEndpoint,
  1049. Handler: getLogPodValuesHandler,
  1050. Router: r,
  1051. })
  1052. // GET /api/projects/{project_id}/clusters/{cluster_id}/events -> cluster.NewGetEventsHandler
  1053. getEventsEndpoint := factory.NewAPIEndpoint(
  1054. &types.APIRequestMetadata{
  1055. Verb: types.APIVerbGet,
  1056. Method: types.HTTPVerbGet,
  1057. Path: &types.Path{
  1058. Parent: basePath,
  1059. RelativePath: fmt.Sprintf("%s/events", relPath),
  1060. },
  1061. Scopes: []types.PermissionScope{
  1062. types.UserScope,
  1063. types.ProjectScope,
  1064. types.ClusterScope,
  1065. },
  1066. },
  1067. )
  1068. getEventsHandler := cluster.NewGetEventsHandler(
  1069. config,
  1070. factory.GetDecoderValidator(),
  1071. factory.GetResultWriter(),
  1072. )
  1073. routes = append(routes, &router.Route{
  1074. Endpoint: getEventsEndpoint,
  1075. Handler: getEventsHandler,
  1076. Router: r,
  1077. })
  1078. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1079. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1080. &types.APIRequestMetadata{
  1081. Verb: types.APIVerbCreate,
  1082. Method: types.HTTPVerbPost,
  1083. Path: &types.Path{
  1084. Parent: basePath,
  1085. RelativePath: relPath + "/incidents/notify_new",
  1086. },
  1087. Scopes: []types.PermissionScope{
  1088. types.UserScope,
  1089. types.ProjectScope,
  1090. types.ClusterScope,
  1091. },
  1092. },
  1093. )
  1094. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1095. config,
  1096. factory.GetDecoderValidator(),
  1097. factory.GetResultWriter(),
  1098. )
  1099. routes = append(routes, &router.Route{
  1100. Endpoint: notifyNewIncidentEndpoint,
  1101. Handler: notifyNewIncidentHandler,
  1102. Router: r,
  1103. })
  1104. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1105. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1106. &types.APIRequestMetadata{
  1107. Verb: types.APIVerbCreate,
  1108. Method: types.HTTPVerbPost,
  1109. Path: &types.Path{
  1110. Parent: basePath,
  1111. RelativePath: relPath + "/incidents/notify_resolved",
  1112. },
  1113. Scopes: []types.PermissionScope{
  1114. types.UserScope,
  1115. types.ProjectScope,
  1116. types.ClusterScope,
  1117. },
  1118. },
  1119. )
  1120. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1121. config,
  1122. factory.GetDecoderValidator(),
  1123. factory.GetResultWriter(),
  1124. )
  1125. routes = append(routes, &router.Route{
  1126. Endpoint: notifyResolvedIncidentEndpoint,
  1127. Handler: notifyResolvedIncidentHandler,
  1128. Router: r,
  1129. })
  1130. return routes, newPath
  1131. }