cluster.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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}/logs/revision_values -> cluster.NewGetLogPodValuesHandler
  1026. getLogRevisionValuesEndpoint := 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/logs/revision_values", relPath),
  1033. },
  1034. Scopes: []types.PermissionScope{
  1035. types.UserScope,
  1036. types.ProjectScope,
  1037. types.ClusterScope,
  1038. },
  1039. },
  1040. )
  1041. getLogRevisionValuesHandler := cluster.NewGetLogRevisionValuesHandler(
  1042. config,
  1043. factory.GetDecoderValidator(),
  1044. factory.GetResultWriter(),
  1045. )
  1046. routes = append(routes, &router.Route{
  1047. Endpoint: getLogRevisionValuesEndpoint,
  1048. Handler: getLogRevisionValuesHandler,
  1049. Router: r,
  1050. })
  1051. // GET /api/projects/{project_id}/clusters/{cluster_id}/events -> cluster.NewGetEventsHandler
  1052. getPorterEventsEndpoint := factory.NewAPIEndpoint(
  1053. &types.APIRequestMetadata{
  1054. Verb: types.APIVerbGet,
  1055. Method: types.HTTPVerbGet,
  1056. Path: &types.Path{
  1057. Parent: basePath,
  1058. RelativePath: fmt.Sprintf("%s/events", relPath),
  1059. },
  1060. Scopes: []types.PermissionScope{
  1061. types.UserScope,
  1062. types.ProjectScope,
  1063. types.ClusterScope,
  1064. },
  1065. },
  1066. )
  1067. getPorterEventsHandler := cluster.NewGetPorterEventsHandler(
  1068. config,
  1069. factory.GetDecoderValidator(),
  1070. factory.GetResultWriter(),
  1071. )
  1072. routes = append(routes, &router.Route{
  1073. Endpoint: getPorterEventsEndpoint,
  1074. Handler: getPorterEventsHandler,
  1075. Router: r,
  1076. })
  1077. // GET /api/projects/{project_id}/clusters/{cluster_id}/k8s_events -> cluster.NewGetEventsHandler
  1078. getK8sEventsEndpoint := factory.NewAPIEndpoint(
  1079. &types.APIRequestMetadata{
  1080. Verb: types.APIVerbGet,
  1081. Method: types.HTTPVerbGet,
  1082. Path: &types.Path{
  1083. Parent: basePath,
  1084. RelativePath: fmt.Sprintf("%s/k8s_events", relPath),
  1085. },
  1086. Scopes: []types.PermissionScope{
  1087. types.UserScope,
  1088. types.ProjectScope,
  1089. types.ClusterScope,
  1090. },
  1091. },
  1092. )
  1093. getK8sEventsHandler := cluster.NewGetKubernetesEventsHandler(
  1094. config,
  1095. factory.GetDecoderValidator(),
  1096. factory.GetResultWriter(),
  1097. )
  1098. routes = append(routes, &router.Route{
  1099. Endpoint: getK8sEventsEndpoint,
  1100. Handler: getK8sEventsHandler,
  1101. Router: r,
  1102. })
  1103. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1104. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1105. &types.APIRequestMetadata{
  1106. Verb: types.APIVerbCreate,
  1107. Method: types.HTTPVerbPost,
  1108. Path: &types.Path{
  1109. Parent: basePath,
  1110. RelativePath: relPath + "/incidents/notify_new",
  1111. },
  1112. Scopes: []types.PermissionScope{
  1113. types.UserScope,
  1114. types.ProjectScope,
  1115. types.ClusterScope,
  1116. },
  1117. },
  1118. )
  1119. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1120. config,
  1121. factory.GetDecoderValidator(),
  1122. factory.GetResultWriter(),
  1123. )
  1124. routes = append(routes, &router.Route{
  1125. Endpoint: notifyNewIncidentEndpoint,
  1126. Handler: notifyNewIncidentHandler,
  1127. Router: r,
  1128. })
  1129. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1130. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1131. &types.APIRequestMetadata{
  1132. Verb: types.APIVerbCreate,
  1133. Method: types.HTTPVerbPost,
  1134. Path: &types.Path{
  1135. Parent: basePath,
  1136. RelativePath: relPath + "/incidents/notify_resolved",
  1137. },
  1138. Scopes: []types.PermissionScope{
  1139. types.UserScope,
  1140. types.ProjectScope,
  1141. types.ClusterScope,
  1142. },
  1143. },
  1144. )
  1145. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1146. config,
  1147. factory.GetDecoderValidator(),
  1148. factory.GetResultWriter(),
  1149. )
  1150. routes = append(routes, &router.Route{
  1151. Endpoint: notifyResolvedIncidentEndpoint,
  1152. Handler: notifyResolvedIncidentHandler,
  1153. Router: r,
  1154. })
  1155. return routes, newPath
  1156. }