cluster.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. }
  491. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  492. listNamespacesEndpoint := factory.NewAPIEndpoint(
  493. &types.APIRequestMetadata{
  494. Verb: types.APIVerbGet,
  495. Method: types.HTTPVerbGet,
  496. Path: &types.Path{
  497. Parent: basePath,
  498. RelativePath: relPath + "/namespaces",
  499. },
  500. Scopes: []types.PermissionScope{
  501. types.UserScope,
  502. types.ProjectScope,
  503. types.ClusterScope,
  504. },
  505. },
  506. )
  507. listNamespacesHandler := cluster.NewListNamespacesHandler(
  508. config,
  509. factory.GetResultWriter(),
  510. )
  511. routes = append(routes, &router.Route{
  512. Endpoint: listNamespacesEndpoint,
  513. Handler: listNamespacesHandler,
  514. Router: r,
  515. })
  516. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  517. listNodesEndpoint := factory.NewAPIEndpoint(
  518. &types.APIRequestMetadata{
  519. Verb: types.APIVerbGet,
  520. Method: types.HTTPVerbGet,
  521. Path: &types.Path{
  522. Parent: basePath,
  523. RelativePath: relPath + "/nodes",
  524. },
  525. Scopes: []types.PermissionScope{
  526. types.UserScope,
  527. types.ProjectScope,
  528. types.ClusterScope,
  529. },
  530. },
  531. )
  532. listNodesHandler := cluster.NewListNodesHandler(
  533. config,
  534. factory.GetResultWriter(),
  535. )
  536. routes = append(routes, &router.Route{
  537. Endpoint: listNodesEndpoint,
  538. Handler: listNodesHandler,
  539. Router: r,
  540. })
  541. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  542. getNodeEndpoint := factory.NewAPIEndpoint(
  543. &types.APIRequestMetadata{
  544. Verb: types.APIVerbGet,
  545. Method: types.HTTPVerbGet,
  546. Path: &types.Path{
  547. Parent: basePath,
  548. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  549. },
  550. Scopes: []types.PermissionScope{
  551. types.UserScope,
  552. types.ProjectScope,
  553. types.ClusterScope,
  554. },
  555. },
  556. )
  557. getNodeHandler := cluster.NewGetNodeHandler(
  558. config,
  559. factory.GetResultWriter(),
  560. )
  561. routes = append(routes, &router.Route{
  562. Endpoint: getNodeEndpoint,
  563. Handler: getNodeHandler,
  564. Router: r,
  565. })
  566. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  567. createNamespaceEndpoint := factory.NewAPIEndpoint(
  568. &types.APIRequestMetadata{
  569. Verb: types.APIVerbCreate,
  570. Method: types.HTTPVerbPost,
  571. Path: &types.Path{
  572. Parent: basePath,
  573. RelativePath: relPath + "/namespaces/create",
  574. },
  575. Scopes: []types.PermissionScope{
  576. types.UserScope,
  577. types.ProjectScope,
  578. types.ClusterScope,
  579. },
  580. },
  581. )
  582. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  583. config,
  584. factory.GetDecoderValidator(),
  585. factory.GetResultWriter(),
  586. )
  587. routes = append(routes, &router.Route{
  588. Endpoint: createNamespaceEndpoint,
  589. Handler: createNamespaceHandler,
  590. Router: r,
  591. })
  592. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  593. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  594. &types.APIRequestMetadata{
  595. Verb: types.APIVerbDelete,
  596. Method: types.HTTPVerbDelete,
  597. Path: &types.Path{
  598. Parent: basePath,
  599. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  600. },
  601. Scopes: []types.PermissionScope{
  602. types.UserScope,
  603. types.ProjectScope,
  604. types.ClusterScope,
  605. },
  606. },
  607. )
  608. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  609. config,
  610. factory.GetDecoderValidator(),
  611. )
  612. routes = append(routes, &router.Route{
  613. Endpoint: deleteNamespaceEndpoint,
  614. Handler: deleteNamespaceHandler,
  615. Router: r,
  616. })
  617. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  618. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  619. &types.APIRequestMetadata{
  620. Verb: types.APIVerbUpdate, // we do not want users with no-write access to be able to use this
  621. Method: types.HTTPVerbGet,
  622. Path: &types.Path{
  623. Parent: basePath,
  624. RelativePath: relPath + "/kubeconfig",
  625. },
  626. Scopes: []types.PermissionScope{
  627. types.UserScope,
  628. types.ProjectScope,
  629. types.ClusterScope,
  630. },
  631. },
  632. )
  633. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  634. config,
  635. factory.GetResultWriter(),
  636. )
  637. routes = append(routes, &router.Route{
  638. Endpoint: getTemporaryKubeconfigEndpoint,
  639. Handler: getTemporaryKubeconfigHandler,
  640. Router: r,
  641. })
  642. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  643. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  644. &types.APIRequestMetadata{
  645. Verb: types.APIVerbGet,
  646. Method: types.HTTPVerbGet,
  647. Path: &types.Path{
  648. Parent: basePath,
  649. RelativePath: relPath + "/prometheus/detect",
  650. },
  651. Scopes: []types.PermissionScope{
  652. types.UserScope,
  653. types.ProjectScope,
  654. types.ClusterScope,
  655. },
  656. },
  657. )
  658. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  659. routes = append(routes, &router.Route{
  660. Endpoint: detectPrometheusInstalledEndpoint,
  661. Handler: detectPrometheusInstalledHandler,
  662. Router: r,
  663. })
  664. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  665. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  666. &types.APIRequestMetadata{
  667. Verb: types.APIVerbGet,
  668. Method: types.HTTPVerbGet,
  669. Path: &types.Path{
  670. Parent: basePath,
  671. RelativePath: relPath + "/agent/detect",
  672. },
  673. Scopes: []types.PermissionScope{
  674. types.UserScope,
  675. types.ProjectScope,
  676. types.ClusterScope,
  677. },
  678. },
  679. )
  680. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  681. routes = append(routes, &router.Route{
  682. Endpoint: detectAgentInstalledEndpoint,
  683. Handler: detectAgentInstalledHandler,
  684. Router: r,
  685. })
  686. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  687. installAgentEndpoint := factory.NewAPIEndpoint(
  688. &types.APIRequestMetadata{
  689. Verb: types.APIVerbCreate,
  690. Method: types.HTTPVerbPost,
  691. Path: &types.Path{
  692. Parent: basePath,
  693. RelativePath: relPath + "/agent/install",
  694. },
  695. Scopes: []types.PermissionScope{
  696. types.UserScope,
  697. types.ProjectScope,
  698. types.ClusterScope,
  699. },
  700. },
  701. )
  702. installAgentHandler := cluster.NewInstallAgentHandler(
  703. config,
  704. factory.GetDecoderValidator(),
  705. factory.GetResultWriter(),
  706. )
  707. routes = append(routes, &router.Route{
  708. Endpoint: installAgentEndpoint,
  709. Handler: installAgentHandler,
  710. Router: r,
  711. })
  712. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/status -> cluster.NewGetAgentStatusHandler
  713. getAgentStatusEndpoint := factory.NewAPIEndpoint(
  714. &types.APIRequestMetadata{
  715. Verb: types.APIVerbGet,
  716. Method: types.HTTPVerbGet,
  717. Path: &types.Path{
  718. Parent: basePath,
  719. RelativePath: relPath + "/agent/status",
  720. },
  721. Scopes: []types.PermissionScope{
  722. types.UserScope,
  723. types.ProjectScope,
  724. types.ClusterScope,
  725. },
  726. },
  727. )
  728. getAgentStatusHandler := cluster.NewGetAgentStatusHandler(config, factory.GetResultWriter())
  729. routes = append(routes, &router.Route{
  730. Endpoint: getAgentStatusEndpoint,
  731. Handler: getAgentStatusHandler,
  732. Router: r,
  733. })
  734. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  735. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  736. &types.APIRequestMetadata{
  737. Verb: types.APIVerbCreate,
  738. Method: types.HTTPVerbPost,
  739. Path: &types.Path{
  740. Parent: basePath,
  741. RelativePath: relPath + "/agent/upgrade",
  742. },
  743. Scopes: []types.PermissionScope{
  744. types.UserScope,
  745. types.ProjectScope,
  746. types.ClusterScope,
  747. },
  748. },
  749. )
  750. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  751. config,
  752. factory.GetDecoderValidator(),
  753. factory.GetResultWriter(),
  754. )
  755. routes = append(routes, &router.Route{
  756. Endpoint: upgradeAgentEndpoint,
  757. Handler: upgradeAgentHandler,
  758. Router: r,
  759. })
  760. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  761. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  762. &types.APIRequestMetadata{
  763. Verb: types.APIVerbGet,
  764. Method: types.HTTPVerbGet,
  765. Path: &types.Path{
  766. Parent: basePath,
  767. RelativePath: relPath + "/prometheus/ingresses",
  768. },
  769. Scopes: []types.PermissionScope{
  770. types.UserScope,
  771. types.ProjectScope,
  772. types.ClusterScope,
  773. },
  774. },
  775. )
  776. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  777. config,
  778. factory.GetResultWriter(),
  779. )
  780. routes = append(routes, &router.Route{
  781. Endpoint: listNGINXIngressesEndpoint,
  782. Handler: listNGINXIngressesHandler,
  783. Router: r,
  784. })
  785. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  786. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  787. &types.APIRequestMetadata{
  788. Verb: types.APIVerbGet,
  789. Method: types.HTTPVerbGet,
  790. Path: &types.Path{
  791. Parent: basePath,
  792. RelativePath: relPath + "/metrics",
  793. },
  794. Scopes: []types.PermissionScope{
  795. types.UserScope,
  796. types.ProjectScope,
  797. types.ClusterScope,
  798. },
  799. },
  800. )
  801. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  802. config,
  803. factory.GetDecoderValidator(),
  804. factory.GetResultWriter(),
  805. )
  806. routes = append(routes, &router.Route{
  807. Endpoint: getPodMetricsEndpoint,
  808. Handler: getPodMetricsHandler,
  809. Router: r,
  810. })
  811. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  812. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  813. &types.APIRequestMetadata{
  814. Verb: types.APIVerbGet,
  815. Method: types.HTTPVerbGet,
  816. Path: &types.Path{
  817. Parent: basePath,
  818. RelativePath: relPath + "/helm_release",
  819. },
  820. Scopes: []types.PermissionScope{
  821. types.UserScope,
  822. types.ProjectScope,
  823. types.ClusterScope,
  824. },
  825. IsWebsocket: true,
  826. },
  827. )
  828. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  829. config,
  830. factory.GetDecoderValidator(),
  831. factory.GetResultWriter(),
  832. )
  833. routes = append(routes, &router.Route{
  834. Endpoint: streamHelmReleaseEndpoint,
  835. Handler: streamHelmReleaseHandler,
  836. Router: r,
  837. })
  838. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  839. streamStatusEndpoint := factory.NewAPIEndpoint(
  840. &types.APIRequestMetadata{
  841. Verb: types.APIVerbGet,
  842. Method: types.HTTPVerbGet,
  843. Path: &types.Path{
  844. Parent: basePath,
  845. RelativePath: fmt.Sprintf(
  846. "%s/{%s}/status",
  847. relPath,
  848. types.URLParamKind,
  849. ),
  850. },
  851. Scopes: []types.PermissionScope{
  852. types.UserScope,
  853. types.ProjectScope,
  854. types.ClusterScope,
  855. },
  856. IsWebsocket: true,
  857. },
  858. )
  859. streamStatusHandler := cluster.NewStreamStatusHandler(
  860. config,
  861. factory.GetDecoderValidator(),
  862. factory.GetResultWriter(),
  863. )
  864. routes = append(routes, &router.Route{
  865. Endpoint: streamStatusEndpoint,
  866. Handler: streamStatusHandler,
  867. Router: r,
  868. })
  869. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  870. getPodsEndpoint := factory.NewAPIEndpoint(
  871. &types.APIRequestMetadata{
  872. Verb: types.APIVerbGet,
  873. Method: types.HTTPVerbGet,
  874. Path: &types.Path{
  875. Parent: basePath,
  876. RelativePath: relPath + "/pods",
  877. },
  878. Scopes: []types.PermissionScope{
  879. types.UserScope,
  880. types.ProjectScope,
  881. types.ClusterScope,
  882. },
  883. },
  884. )
  885. getPodsHandler := cluster.NewGetPodsHandler(
  886. config,
  887. factory.GetDecoderValidator(),
  888. factory.GetResultWriter(),
  889. )
  890. routes = append(routes, &router.Route{
  891. Endpoint: getPodsEndpoint,
  892. Handler: getPodsHandler,
  893. Router: r,
  894. })
  895. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewListIncidentsHandler
  896. listIncidentsEndpoint := factory.NewAPIEndpoint(
  897. &types.APIRequestMetadata{
  898. Verb: types.APIVerbGet,
  899. Method: types.HTTPVerbGet,
  900. Path: &types.Path{
  901. Parent: basePath,
  902. RelativePath: relPath + "/incidents",
  903. },
  904. Scopes: []types.PermissionScope{
  905. types.UserScope,
  906. types.ProjectScope,
  907. types.ClusterScope,
  908. },
  909. },
  910. )
  911. listIncidentsHandler := cluster.NewListIncidentsHandler(
  912. config,
  913. factory.GetDecoderValidator(),
  914. factory.GetResultWriter(),
  915. )
  916. routes = append(routes, &router.Route{
  917. Endpoint: listIncidentsEndpoint,
  918. Handler: listIncidentsHandler,
  919. Router: r,
  920. })
  921. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/{incident_id} -> cluster.NewGetIncidentHandler
  922. getIncidentEndpoint := factory.NewAPIEndpoint(
  923. &types.APIRequestMetadata{
  924. Verb: types.APIVerbGet,
  925. Method: types.HTTPVerbGet,
  926. Path: &types.Path{
  927. Parent: basePath,
  928. RelativePath: fmt.Sprintf("%s/incidents/{%s}", relPath, types.URLParamIncidentID),
  929. },
  930. Scopes: []types.PermissionScope{
  931. types.UserScope,
  932. types.ProjectScope,
  933. types.ClusterScope,
  934. },
  935. },
  936. )
  937. getIncidentHandler := cluster.NewGetIncidentHandler(
  938. config,
  939. factory.GetDecoderValidator(),
  940. factory.GetResultWriter(),
  941. )
  942. routes = append(routes, &router.Route{
  943. Endpoint: getIncidentEndpoint,
  944. Handler: getIncidentHandler,
  945. Router: r,
  946. })
  947. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/{incident_id}/events -> cluster.NewListIncidentEventsHandler
  948. listIncidentEventsEndpoint := factory.NewAPIEndpoint(
  949. &types.APIRequestMetadata{
  950. Verb: types.APIVerbGet,
  951. Method: types.HTTPVerbGet,
  952. Path: &types.Path{
  953. Parent: basePath,
  954. RelativePath: fmt.Sprintf("%s/incidents/{%s}/events", relPath, types.URLParamIncidentID),
  955. },
  956. Scopes: []types.PermissionScope{
  957. types.UserScope,
  958. types.ProjectScope,
  959. types.ClusterScope,
  960. },
  961. },
  962. )
  963. listIncidentEventsHandler := cluster.NewListIncidentEventsHandler(
  964. config,
  965. factory.GetDecoderValidator(),
  966. factory.GetResultWriter(),
  967. )
  968. routes = append(routes, &router.Route{
  969. Endpoint: listIncidentEventsEndpoint,
  970. Handler: listIncidentEventsHandler,
  971. Router: r,
  972. })
  973. // GET /api/projects/{project_id}/clusters/{cluster_id}/logs -> cluster.NewGetLogsHandler
  974. getLogsEndpoint := factory.NewAPIEndpoint(
  975. &types.APIRequestMetadata{
  976. Verb: types.APIVerbGet,
  977. Method: types.HTTPVerbGet,
  978. Path: &types.Path{
  979. Parent: basePath,
  980. RelativePath: fmt.Sprintf("%s/logs", relPath),
  981. },
  982. Scopes: []types.PermissionScope{
  983. types.UserScope,
  984. types.ProjectScope,
  985. types.ClusterScope,
  986. },
  987. },
  988. )
  989. getLogsHandler := cluster.NewGetLogsHandler(
  990. config,
  991. factory.GetDecoderValidator(),
  992. factory.GetResultWriter(),
  993. )
  994. routes = append(routes, &router.Route{
  995. Endpoint: getLogsEndpoint,
  996. Handler: getLogsHandler,
  997. Router: r,
  998. })
  999. // GET /api/projects/{project_id}/clusters/{cluster_id}/logs/pod_values -> cluster.NewGetLogPodValuesHandler
  1000. getLogPodValuesEndpoint := factory.NewAPIEndpoint(
  1001. &types.APIRequestMetadata{
  1002. Verb: types.APIVerbGet,
  1003. Method: types.HTTPVerbGet,
  1004. Path: &types.Path{
  1005. Parent: basePath,
  1006. RelativePath: fmt.Sprintf("%s/logs/pod_values", relPath),
  1007. },
  1008. Scopes: []types.PermissionScope{
  1009. types.UserScope,
  1010. types.ProjectScope,
  1011. types.ClusterScope,
  1012. },
  1013. },
  1014. )
  1015. getLogPodValuesHandler := cluster.NewGetLogPodValuesHandler(
  1016. config,
  1017. factory.GetDecoderValidator(),
  1018. factory.GetResultWriter(),
  1019. )
  1020. routes = append(routes, &router.Route{
  1021. Endpoint: getLogPodValuesEndpoint,
  1022. Handler: getLogPodValuesHandler,
  1023. Router: r,
  1024. })
  1025. // GET /api/projects/{project_id}/clusters/{cluster_id}/events -> cluster.NewGetEventsHandler
  1026. getEventsEndpoint := factory.NewAPIEndpoint(
  1027. &types.APIRequestMetadata{
  1028. Verb: types.APIVerbGet,
  1029. Method: types.HTTPVerbGet,
  1030. Path: &types.Path{
  1031. Parent: basePath,
  1032. RelativePath: fmt.Sprintf("%s/events", relPath),
  1033. },
  1034. Scopes: []types.PermissionScope{
  1035. types.UserScope,
  1036. types.ProjectScope,
  1037. types.ClusterScope,
  1038. },
  1039. },
  1040. )
  1041. getEventsHandler := cluster.NewGetEventsHandler(
  1042. config,
  1043. factory.GetDecoderValidator(),
  1044. factory.GetResultWriter(),
  1045. )
  1046. routes = append(routes, &router.Route{
  1047. Endpoint: getEventsEndpoint,
  1048. Handler: getEventsHandler,
  1049. Router: r,
  1050. })
  1051. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1052. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1053. &types.APIRequestMetadata{
  1054. Verb: types.APIVerbCreate,
  1055. Method: types.HTTPVerbPost,
  1056. Path: &types.Path{
  1057. Parent: basePath,
  1058. RelativePath: relPath + "/incidents/notify_new",
  1059. },
  1060. Scopes: []types.PermissionScope{
  1061. types.UserScope,
  1062. types.ProjectScope,
  1063. types.ClusterScope,
  1064. },
  1065. },
  1066. )
  1067. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1068. config,
  1069. factory.GetDecoderValidator(),
  1070. factory.GetResultWriter(),
  1071. )
  1072. routes = append(routes, &router.Route{
  1073. Endpoint: notifyNewIncidentEndpoint,
  1074. Handler: notifyNewIncidentHandler,
  1075. Router: r,
  1076. })
  1077. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1078. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1079. &types.APIRequestMetadata{
  1080. Verb: types.APIVerbCreate,
  1081. Method: types.HTTPVerbPost,
  1082. Path: &types.Path{
  1083. Parent: basePath,
  1084. RelativePath: relPath + "/incidents/notify_resolved",
  1085. },
  1086. Scopes: []types.PermissionScope{
  1087. types.UserScope,
  1088. types.ProjectScope,
  1089. types.ClusterScope,
  1090. },
  1091. },
  1092. )
  1093. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1094. config,
  1095. factory.GetDecoderValidator(),
  1096. factory.GetResultWriter(),
  1097. )
  1098. routes = append(routes, &router.Route{
  1099. Endpoint: notifyResolvedIncidentEndpoint,
  1100. Handler: notifyResolvedIncidentHandler,
  1101. Router: r,
  1102. })
  1103. return routes, newPath
  1104. }