cluster.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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/handlers/kube_events"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/server/shared/router"
  12. "github.com/porter-dev/porter/api/types"
  13. )
  14. func NewClusterScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  15. return &router.Registerer{
  16. GetRoutes: GetClusterScopedRoutes,
  17. Children: children,
  18. }
  19. }
  20. func GetClusterScopedRoutes(
  21. r chi.Router,
  22. config *config.Config,
  23. basePath *types.Path,
  24. factory shared.APIEndpointFactory,
  25. children ...*router.Registerer,
  26. ) []*router.Route {
  27. routes, projPath := getClusterRoutes(r, config, basePath, factory)
  28. if len(children) > 0 {
  29. r.Route(projPath.RelativePath, func(r chi.Router) {
  30. for _, child := range children {
  31. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  32. routes = append(routes, childRoutes...)
  33. }
  34. })
  35. }
  36. return routes
  37. }
  38. func getClusterRoutes(
  39. r chi.Router,
  40. config *config.Config,
  41. basePath *types.Path,
  42. factory shared.APIEndpointFactory,
  43. ) ([]*router.Route, *types.Path) {
  44. relPath := "/clusters/{cluster_id}"
  45. newPath := &types.Path{
  46. Parent: basePath,
  47. RelativePath: relPath,
  48. }
  49. routes := make([]*router.Route, 0)
  50. // POST /api/projects/{project_id}/clusters -> project.NewCreateClusterManualHandler
  51. createEndpoint := factory.NewAPIEndpoint(
  52. &types.APIRequestMetadata{
  53. Verb: types.APIVerbCreate,
  54. Method: types.HTTPVerbPost,
  55. Path: &types.Path{
  56. Parent: basePath,
  57. RelativePath: "/clusters",
  58. },
  59. Scopes: []types.PermissionScope{
  60. types.UserScope,
  61. types.ProjectScope,
  62. },
  63. },
  64. )
  65. createHandler := cluster.NewCreateClusterManualHandler(
  66. config,
  67. factory.GetDecoderValidator(),
  68. factory.GetResultWriter(),
  69. )
  70. routes = append(routes, &router.Route{
  71. Endpoint: createEndpoint,
  72. Handler: createHandler,
  73. Router: r,
  74. })
  75. // POST /api/projects/{project_id}/clusters/candidates -> project.NewCreateClusterCandidateHandler
  76. createCandidateEndpoint := factory.NewAPIEndpoint(
  77. &types.APIRequestMetadata{
  78. Verb: types.APIVerbCreate,
  79. Method: types.HTTPVerbPost,
  80. Path: &types.Path{
  81. Parent: basePath,
  82. RelativePath: "/clusters/candidates",
  83. },
  84. Scopes: []types.PermissionScope{
  85. types.UserScope,
  86. types.ProjectScope,
  87. },
  88. CheckUsage: true,
  89. UsageMetric: types.Clusters,
  90. },
  91. )
  92. createCandidateHandler := cluster.NewCreateClusterCandidateHandler(
  93. config,
  94. factory.GetDecoderValidator(),
  95. factory.GetResultWriter(),
  96. )
  97. routes = append(routes, &router.Route{
  98. Endpoint: createCandidateEndpoint,
  99. Handler: createCandidateHandler,
  100. Router: r,
  101. })
  102. // GET /api/projects/{project_id}/clusters/candidates -> project.NewListClusterCandidatesHandler
  103. listCandidatesEndpoint := factory.NewAPIEndpoint(
  104. &types.APIRequestMetadata{
  105. Verb: types.APIVerbList,
  106. Method: types.HTTPVerbGet,
  107. Path: &types.Path{
  108. Parent: basePath,
  109. RelativePath: "/clusters/candidates",
  110. },
  111. Scopes: []types.PermissionScope{
  112. types.UserScope,
  113. types.ProjectScope,
  114. },
  115. },
  116. )
  117. listCandidatesHandler := cluster.NewListClusterCandidatesHandler(
  118. config,
  119. factory.GetResultWriter(),
  120. )
  121. routes = append(routes, &router.Route{
  122. Endpoint: listCandidatesEndpoint,
  123. Handler: listCandidatesHandler,
  124. Router: r,
  125. })
  126. // POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve -> project.NewResolveClusterCandidateHandler
  127. resolveCandidateEndpoint := factory.NewAPIEndpoint(
  128. &types.APIRequestMetadata{
  129. Verb: types.APIVerbCreate,
  130. Method: types.HTTPVerbPost,
  131. Path: &types.Path{
  132. Parent: basePath,
  133. RelativePath: fmt.Sprintf(
  134. "/clusters/candidates/{%s}/resolve",
  135. types.URLParamCandidateID,
  136. ),
  137. },
  138. Scopes: []types.PermissionScope{
  139. types.UserScope,
  140. types.ProjectScope,
  141. },
  142. CheckUsage: true,
  143. UsageMetric: types.Clusters,
  144. },
  145. )
  146. resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
  147. config,
  148. factory.GetDecoderValidator(),
  149. factory.GetResultWriter(),
  150. )
  151. routes = append(routes, &router.Route{
  152. Endpoint: resolveCandidateEndpoint,
  153. Handler: resolveCandidateHandler,
  154. Router: r,
  155. })
  156. // POST /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterUpdateHandler
  157. updateClusterEndpoint := factory.NewAPIEndpoint(
  158. &types.APIRequestMetadata{
  159. Verb: types.APIVerbUpdate,
  160. Method: types.HTTPVerbPost,
  161. Path: &types.Path{
  162. Parent: basePath,
  163. RelativePath: relPath,
  164. },
  165. Scopes: []types.PermissionScope{
  166. types.UserScope,
  167. types.ProjectScope,
  168. types.ClusterScope,
  169. },
  170. },
  171. )
  172. updateClusterHandler := cluster.NewClusterUpdateHandler(
  173. config,
  174. factory.GetDecoderValidator(),
  175. factory.GetResultWriter(),
  176. )
  177. routes = append(routes, &router.Route{
  178. Endpoint: updateClusterEndpoint,
  179. Handler: updateClusterHandler,
  180. Router: r,
  181. })
  182. // DELETE /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterDeleteHandler
  183. deleteClusterEndpoint := factory.NewAPIEndpoint(
  184. &types.APIRequestMetadata{
  185. Verb: types.APIVerbDelete,
  186. Method: types.HTTPVerbDelete,
  187. Path: &types.Path{
  188. Parent: basePath,
  189. RelativePath: relPath,
  190. },
  191. Scopes: []types.PermissionScope{
  192. types.UserScope,
  193. types.ProjectScope,
  194. types.ClusterScope,
  195. },
  196. },
  197. )
  198. deleteClusterHandler := cluster.NewClusterDeleteHandler(
  199. config,
  200. factory.GetResultWriter(),
  201. )
  202. routes = append(routes, &router.Route{
  203. Endpoint: deleteClusterEndpoint,
  204. Handler: deleteClusterHandler,
  205. Router: r,
  206. })
  207. // GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
  208. getEndpoint := factory.NewAPIEndpoint(
  209. &types.APIRequestMetadata{
  210. Verb: types.APIVerbGet,
  211. Method: types.HTTPVerbGet,
  212. Path: &types.Path{
  213. Parent: basePath,
  214. RelativePath: relPath,
  215. },
  216. Scopes: []types.PermissionScope{
  217. types.UserScope,
  218. types.ProjectScope,
  219. types.ClusterScope,
  220. },
  221. },
  222. )
  223. getHandler := cluster.NewClusterGetHandler(
  224. config,
  225. factory.GetResultWriter(),
  226. )
  227. routes = append(routes, &router.Route{
  228. Endpoint: getEndpoint,
  229. Handler: getHandler,
  230. Router: r,
  231. })
  232. // GET /api/projects/{project_id}/clusters/{cluster_id}/databases -> database.NewDatabaseListHandler
  233. listDatabaseEndpoint := factory.NewAPIEndpoint(
  234. &types.APIRequestMetadata{
  235. Verb: types.APIVerbList,
  236. Method: types.HTTPVerbGet,
  237. Path: &types.Path{
  238. Parent: basePath,
  239. RelativePath: relPath + "/databases",
  240. },
  241. Scopes: []types.PermissionScope{
  242. types.UserScope,
  243. types.ProjectScope,
  244. types.ClusterScope,
  245. },
  246. },
  247. )
  248. listDatabaseHandler := database.NewDatabaseListHandler(
  249. config,
  250. factory.GetResultWriter(),
  251. )
  252. routes = append(routes, &router.Route{
  253. Endpoint: listDatabaseEndpoint,
  254. Handler: listDatabaseHandler,
  255. Router: r,
  256. })
  257. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  258. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments -> environment.NewListEnvironmentHandler
  259. listEnvEndpoint := factory.NewAPIEndpoint(
  260. &types.APIRequestMetadata{
  261. Verb: types.APIVerbGet,
  262. Method: types.HTTPVerbGet,
  263. Path: &types.Path{
  264. Parent: basePath,
  265. RelativePath: relPath + "/environments",
  266. },
  267. Scopes: []types.PermissionScope{
  268. types.UserScope,
  269. types.ProjectScope,
  270. types.ClusterScope,
  271. },
  272. },
  273. )
  274. listEnvHandler := environment.NewListEnvironmentHandler(
  275. config,
  276. factory.GetResultWriter(),
  277. )
  278. routes = append(routes, &router.Route{
  279. Endpoint: listEnvEndpoint,
  280. Handler: listEnvHandler,
  281. Router: r,
  282. })
  283. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id} -> environment.NewGetEnvironmentHandler
  284. getEnvEndpoint := factory.NewAPIEndpoint(
  285. &types.APIRequestMetadata{
  286. Verb: types.APIVerbGet,
  287. Method: types.HTTPVerbGet,
  288. Path: &types.Path{
  289. Parent: basePath,
  290. RelativePath: relPath + "/environments/{environment_id}",
  291. },
  292. Scopes: []types.PermissionScope{
  293. types.UserScope,
  294. types.ProjectScope,
  295. types.ClusterScope,
  296. },
  297. },
  298. )
  299. getEnvHandler := environment.NewGetEnvironmentHandler(
  300. config,
  301. factory.GetResultWriter(),
  302. )
  303. routes = append(routes, &router.Route{
  304. Endpoint: getEnvEndpoint,
  305. Handler: getEnvHandler,
  306. Router: r,
  307. })
  308. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/toggle_new_comment -> environment.NewToggleNewCommentHandler
  309. toggleNewCommentEndpoint := factory.NewAPIEndpoint(
  310. &types.APIRequestMetadata{
  311. Verb: types.APIVerbUpdate,
  312. Method: types.HTTPVerbPatch,
  313. Path: &types.Path{
  314. Parent: basePath,
  315. RelativePath: relPath + "/environments/{environment_id}/toggle_new_comment",
  316. },
  317. Scopes: []types.PermissionScope{
  318. types.UserScope,
  319. types.ProjectScope,
  320. types.ClusterScope,
  321. },
  322. },
  323. )
  324. toggleNewCommentHandler := environment.NewToggleNewCommentHandler(
  325. config,
  326. factory.GetDecoderValidator(),
  327. factory.GetResultWriter(),
  328. )
  329. routes = append(routes, &router.Route{
  330. Endpoint: toggleNewCommentEndpoint,
  331. Handler: toggleNewCommentHandler,
  332. Router: r,
  333. })
  334. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/validate_porter_yaml -> environment.NewValidatePorterYAMLHandler
  335. validtatePorterYAMLEndpoint := factory.NewAPIEndpoint(
  336. &types.APIRequestMetadata{
  337. Verb: types.APIVerbGet,
  338. Method: types.HTTPVerbGet,
  339. Path: &types.Path{
  340. Parent: basePath,
  341. RelativePath: relPath + "/environments/{environment_id}/validate_porter_yaml",
  342. },
  343. Scopes: []types.PermissionScope{
  344. types.UserScope,
  345. types.ProjectScope,
  346. types.ClusterScope,
  347. },
  348. },
  349. )
  350. validatePorterYAMLHandler := environment.NewValidatePorterYAMLHandler(
  351. config,
  352. factory.GetDecoderValidator(),
  353. factory.GetResultWriter(),
  354. )
  355. routes = append(routes, &router.Route{
  356. Endpoint: validtatePorterYAMLEndpoint,
  357. Handler: validatePorterYAMLHandler,
  358. Router: r,
  359. })
  360. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployments -> environment.NewListDeploymentsByClusterHandler
  361. listDeploymentsEndpoint := factory.NewAPIEndpoint(
  362. &types.APIRequestMetadata{
  363. Verb: types.APIVerbGet,
  364. Method: types.HTTPVerbGet,
  365. Path: &types.Path{
  366. Parent: basePath,
  367. RelativePath: relPath + "/deployments",
  368. },
  369. Scopes: []types.PermissionScope{
  370. types.UserScope,
  371. types.ProjectScope,
  372. types.ClusterScope,
  373. },
  374. },
  375. )
  376. listDeploymentsHandler := environment.NewListDeploymentsByClusterHandler(
  377. config,
  378. factory.GetDecoderValidator(),
  379. factory.GetResultWriter(),
  380. )
  381. routes = append(routes, &router.Route{
  382. Endpoint: listDeploymentsEndpoint,
  383. Handler: listDeploymentsHandler,
  384. Router: r,
  385. })
  386. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/deployment -> environment.NewGetDeploymentByClusterHandler
  387. getDeploymentEndpoint := factory.NewAPIEndpoint(
  388. &types.APIRequestMetadata{
  389. Verb: types.APIVerbGet,
  390. Method: types.HTTPVerbGet,
  391. Path: &types.Path{
  392. Parent: basePath,
  393. RelativePath: relPath + "/environments/{environment_id}/deployment",
  394. },
  395. Scopes: []types.PermissionScope{
  396. types.UserScope,
  397. types.ProjectScope,
  398. types.ClusterScope,
  399. },
  400. },
  401. )
  402. getDeploymentHandler := environment.NewGetDeploymentByEnvironmentHandler(
  403. config,
  404. factory.GetDecoderValidator(),
  405. factory.GetResultWriter(),
  406. )
  407. routes = append(routes, &router.Route{
  408. Endpoint: getDeploymentEndpoint,
  409. Handler: getDeploymentHandler,
  410. Router: r,
  411. })
  412. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/reenable -> environment.NewReenableDeploymentHandler
  413. reenableDeploymentEndpoint := factory.NewAPIEndpoint(
  414. &types.APIRequestMetadata{
  415. Verb: types.APIVerbUpdate,
  416. Method: types.HTTPVerbPatch,
  417. Path: &types.Path{
  418. Parent: basePath,
  419. RelativePath: relPath + "/deployments/{deployment_id}/reenable",
  420. },
  421. Scopes: []types.PermissionScope{
  422. types.UserScope,
  423. types.ProjectScope,
  424. types.ClusterScope,
  425. },
  426. },
  427. )
  428. reenableDeploymentHandler := environment.NewReenableDeploymentHandler(
  429. config,
  430. factory.GetDecoderValidator(),
  431. factory.GetResultWriter(),
  432. )
  433. routes = append(routes, &router.Route{
  434. Endpoint: reenableDeploymentEndpoint,
  435. Handler: reenableDeploymentHandler,
  436. Router: r,
  437. })
  438. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/trigger_workflow -> environment.NewTriggerDeploymentWorkflowHandler
  439. triggerDeploymentWorkflowEndpoint := factory.NewAPIEndpoint(
  440. &types.APIRequestMetadata{
  441. Verb: types.APIVerbCreate,
  442. Method: types.HTTPVerbPost,
  443. Path: &types.Path{
  444. Parent: basePath,
  445. RelativePath: relPath + "/deployments/{deployment_id}/trigger_workflow",
  446. },
  447. Scopes: []types.PermissionScope{
  448. types.UserScope,
  449. types.ProjectScope,
  450. types.ClusterScope,
  451. },
  452. },
  453. )
  454. triggerDeploymentWorkflowHandler := environment.NewTriggerDeploymentWorkflowHandler(
  455. config,
  456. factory.GetDecoderValidator(),
  457. factory.GetResultWriter(),
  458. )
  459. routes = append(routes, &router.Route{
  460. Endpoint: triggerDeploymentWorkflowEndpoint,
  461. Handler: triggerDeploymentWorkflowHandler,
  462. Router: r,
  463. })
  464. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/pull_request -> environment.NewEnablePullRequestHandler
  465. enablePullRequestEndpoint := factory.NewAPIEndpoint(
  466. &types.APIRequestMetadata{
  467. Verb: types.APIVerbCreate,
  468. Method: types.HTTPVerbPost,
  469. Path: &types.Path{
  470. Parent: basePath,
  471. RelativePath: relPath + "/deployments/pull_request",
  472. },
  473. Scopes: []types.PermissionScope{
  474. types.UserScope,
  475. types.ProjectScope,
  476. types.ClusterScope,
  477. },
  478. },
  479. )
  480. enablePullRequestHandler := environment.NewEnablePullRequestHandler(
  481. config,
  482. factory.GetDecoderValidator(),
  483. factory.GetResultWriter(),
  484. )
  485. routes = append(routes, &router.Route{
  486. Endpoint: enablePullRequestEndpoint,
  487. Handler: enablePullRequestHandler,
  488. Router: r,
  489. })
  490. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id} ->
  491. // environment.NewDeleteDeploymentHandler
  492. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  493. &types.APIRequestMetadata{
  494. Verb: types.APIVerbDelete,
  495. Method: types.HTTPVerbDelete,
  496. Path: &types.Path{
  497. Parent: basePath,
  498. RelativePath: relPath + "/deployments/{deployment_id}",
  499. },
  500. Scopes: []types.PermissionScope{
  501. types.UserScope,
  502. types.ProjectScope,
  503. types.ClusterScope,
  504. },
  505. },
  506. )
  507. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  508. config,
  509. factory.GetDecoderValidator(),
  510. factory.GetResultWriter(),
  511. )
  512. routes = append(routes, &router.Route{
  513. Endpoint: deleteDeploymentEndpoint,
  514. Handler: deleteDeploymentHandler,
  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. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  740. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  741. &types.APIRequestMetadata{
  742. Verb: types.APIVerbCreate,
  743. Method: types.HTTPVerbPost,
  744. Path: &types.Path{
  745. Parent: basePath,
  746. RelativePath: relPath + "/agent/upgrade",
  747. },
  748. Scopes: []types.PermissionScope{
  749. types.UserScope,
  750. types.ProjectScope,
  751. types.ClusterScope,
  752. },
  753. },
  754. )
  755. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  756. config,
  757. factory.GetDecoderValidator(),
  758. factory.GetResultWriter(),
  759. )
  760. routes = append(routes, &router.Route{
  761. Endpoint: upgradeAgentEndpoint,
  762. Handler: upgradeAgentHandler,
  763. Router: r,
  764. })
  765. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  766. listKubeEventsEndpoint := factory.NewAPIEndpoint(
  767. &types.APIRequestMetadata{
  768. Verb: types.APIVerbGet,
  769. Method: types.HTTPVerbGet,
  770. Path: &types.Path{
  771. Parent: basePath,
  772. RelativePath: relPath + "/kube_events",
  773. },
  774. Scopes: []types.PermissionScope{
  775. types.UserScope,
  776. types.ProjectScope,
  777. types.ClusterScope,
  778. },
  779. },
  780. )
  781. listKubeEventsHandler := kube_events.NewListKubeEventsHandler(
  782. config,
  783. factory.GetDecoderValidator(),
  784. factory.GetResultWriter(),
  785. )
  786. routes = append(routes, &router.Route{
  787. Endpoint: listKubeEventsEndpoint,
  788. Handler: listKubeEventsHandler,
  789. Router: r,
  790. })
  791. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  792. getKubeEventEndpoint := factory.NewAPIEndpoint(
  793. &types.APIRequestMetadata{
  794. Verb: types.APIVerbGet,
  795. Method: types.HTTPVerbGet,
  796. Path: &types.Path{
  797. Parent: basePath,
  798. RelativePath: fmt.Sprintf("%s/kube_events/{%s}", relPath, types.URLParamKubeEventID),
  799. },
  800. Scopes: []types.PermissionScope{
  801. types.UserScope,
  802. types.ProjectScope,
  803. types.ClusterScope,
  804. },
  805. },
  806. )
  807. getKubeEventHandler := kube_events.NewGetKubeEventHandler(
  808. config,
  809. factory.GetDecoderValidator(),
  810. factory.GetResultWriter(),
  811. )
  812. routes = append(routes, &router.Route{
  813. Endpoint: getKubeEventEndpoint,
  814. Handler: getKubeEventHandler,
  815. Router: r,
  816. })
  817. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/logs -> kube_events.NewGetKubeEventLogsHandler
  818. getKubeEventLogsEndpoint := factory.NewAPIEndpoint(
  819. &types.APIRequestMetadata{
  820. Verb: types.APIVerbGet,
  821. Method: types.HTTPVerbGet,
  822. Path: &types.Path{
  823. Parent: basePath,
  824. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/logs", relPath, types.URLParamKubeEventID),
  825. },
  826. Scopes: []types.PermissionScope{
  827. types.UserScope,
  828. types.ProjectScope,
  829. types.ClusterScope,
  830. },
  831. },
  832. )
  833. getKubeEventLogsHandler := kube_events.NewGetKubeEventLogsHandler(
  834. config,
  835. factory.GetDecoderValidator(),
  836. factory.GetResultWriter(),
  837. )
  838. routes = append(routes, &router.Route{
  839. Endpoint: getKubeEventLogsEndpoint,
  840. Handler: getKubeEventLogsHandler,
  841. Router: r,
  842. })
  843. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/log_buckets -> kube_events.NewGetKubeEventLogBucketsHandler
  844. getKubeEventLogBucketsEndpoint := factory.NewAPIEndpoint(
  845. &types.APIRequestMetadata{
  846. Verb: types.APIVerbGet,
  847. Method: types.HTTPVerbGet,
  848. Path: &types.Path{
  849. Parent: basePath,
  850. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/log_buckets", relPath, types.URLParamKubeEventID),
  851. },
  852. Scopes: []types.PermissionScope{
  853. types.UserScope,
  854. types.ProjectScope,
  855. types.ClusterScope,
  856. },
  857. },
  858. )
  859. getKubeEventLogBucketsHandler := kube_events.NewGetKubeEventLogBucketsHandler(
  860. config,
  861. factory.GetDecoderValidator(),
  862. factory.GetResultWriter(),
  863. )
  864. routes = append(routes, &router.Route{
  865. Endpoint: getKubeEventLogBucketsEndpoint,
  866. Handler: getKubeEventLogBucketsHandler,
  867. Router: r,
  868. })
  869. // POST /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewCreateKubeEventHandler
  870. createKubeEventsEndpoint := factory.NewAPIEndpoint(
  871. &types.APIRequestMetadata{
  872. Verb: types.APIVerbCreate,
  873. Method: types.HTTPVerbPost,
  874. Path: &types.Path{
  875. Parent: basePath,
  876. RelativePath: relPath + "/kube_events",
  877. },
  878. Scopes: []types.PermissionScope{
  879. types.UserScope,
  880. types.ProjectScope,
  881. types.ClusterScope,
  882. },
  883. },
  884. )
  885. createKubeEventsHandler := kube_events.NewCreateKubeEventHandler(
  886. config,
  887. factory.GetDecoderValidator(),
  888. factory.GetResultWriter(),
  889. )
  890. routes = append(routes, &router.Route{
  891. Endpoint: createKubeEventsEndpoint,
  892. Handler: createKubeEventsHandler,
  893. Router: r,
  894. })
  895. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  896. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  897. &types.APIRequestMetadata{
  898. Verb: types.APIVerbGet,
  899. Method: types.HTTPVerbGet,
  900. Path: &types.Path{
  901. Parent: basePath,
  902. RelativePath: relPath + "/prometheus/ingresses",
  903. },
  904. Scopes: []types.PermissionScope{
  905. types.UserScope,
  906. types.ProjectScope,
  907. types.ClusterScope,
  908. },
  909. },
  910. )
  911. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  912. config,
  913. factory.GetResultWriter(),
  914. )
  915. routes = append(routes, &router.Route{
  916. Endpoint: listNGINXIngressesEndpoint,
  917. Handler: listNGINXIngressesHandler,
  918. Router: r,
  919. })
  920. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  921. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  922. &types.APIRequestMetadata{
  923. Verb: types.APIVerbGet,
  924. Method: types.HTTPVerbGet,
  925. Path: &types.Path{
  926. Parent: basePath,
  927. RelativePath: relPath + "/metrics",
  928. },
  929. Scopes: []types.PermissionScope{
  930. types.UserScope,
  931. types.ProjectScope,
  932. types.ClusterScope,
  933. },
  934. },
  935. )
  936. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  937. config,
  938. factory.GetDecoderValidator(),
  939. factory.GetResultWriter(),
  940. )
  941. routes = append(routes, &router.Route{
  942. Endpoint: getPodMetricsEndpoint,
  943. Handler: getPodMetricsHandler,
  944. Router: r,
  945. })
  946. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  947. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  948. &types.APIRequestMetadata{
  949. Verb: types.APIVerbGet,
  950. Method: types.HTTPVerbGet,
  951. Path: &types.Path{
  952. Parent: basePath,
  953. RelativePath: relPath + "/helm_release",
  954. },
  955. Scopes: []types.PermissionScope{
  956. types.UserScope,
  957. types.ProjectScope,
  958. types.ClusterScope,
  959. },
  960. IsWebsocket: true,
  961. },
  962. )
  963. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  964. config,
  965. factory.GetDecoderValidator(),
  966. factory.GetResultWriter(),
  967. )
  968. routes = append(routes, &router.Route{
  969. Endpoint: streamHelmReleaseEndpoint,
  970. Handler: streamHelmReleaseHandler,
  971. Router: r,
  972. })
  973. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  974. streamStatusEndpoint := factory.NewAPIEndpoint(
  975. &types.APIRequestMetadata{
  976. Verb: types.APIVerbGet,
  977. Method: types.HTTPVerbGet,
  978. Path: &types.Path{
  979. Parent: basePath,
  980. RelativePath: fmt.Sprintf(
  981. "%s/{%s}/status",
  982. relPath,
  983. types.URLParamKind,
  984. ),
  985. },
  986. Scopes: []types.PermissionScope{
  987. types.UserScope,
  988. types.ProjectScope,
  989. types.ClusterScope,
  990. },
  991. IsWebsocket: true,
  992. },
  993. )
  994. streamStatusHandler := cluster.NewStreamStatusHandler(
  995. config,
  996. factory.GetDecoderValidator(),
  997. factory.GetResultWriter(),
  998. )
  999. routes = append(routes, &router.Route{
  1000. Endpoint: streamStatusEndpoint,
  1001. Handler: streamStatusHandler,
  1002. Router: r,
  1003. })
  1004. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  1005. getPodsEndpoint := factory.NewAPIEndpoint(
  1006. &types.APIRequestMetadata{
  1007. Verb: types.APIVerbGet,
  1008. Method: types.HTTPVerbGet,
  1009. Path: &types.Path{
  1010. Parent: basePath,
  1011. RelativePath: relPath + "/pods",
  1012. },
  1013. Scopes: []types.PermissionScope{
  1014. types.UserScope,
  1015. types.ProjectScope,
  1016. types.ClusterScope,
  1017. },
  1018. },
  1019. )
  1020. getPodsHandler := cluster.NewGetPodsHandler(
  1021. config,
  1022. factory.GetDecoderValidator(),
  1023. factory.GetResultWriter(),
  1024. )
  1025. routes = append(routes, &router.Route{
  1026. Endpoint: getPodsEndpoint,
  1027. Handler: getPodsHandler,
  1028. Router: r,
  1029. })
  1030. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewGetIncidentsHandler
  1031. getIncidentsEndpoint := factory.NewAPIEndpoint(
  1032. &types.APIRequestMetadata{
  1033. Verb: types.APIVerbGet,
  1034. Method: types.HTTPVerbGet,
  1035. Path: &types.Path{
  1036. Parent: basePath,
  1037. RelativePath: relPath + "/incidents",
  1038. },
  1039. Scopes: []types.PermissionScope{
  1040. types.UserScope,
  1041. types.ProjectScope,
  1042. types.ClusterScope,
  1043. },
  1044. },
  1045. )
  1046. getIncidentsHandler := cluster.NewGetIncidentsHandler(
  1047. config,
  1048. factory.GetDecoderValidator(),
  1049. factory.GetResultWriter(),
  1050. )
  1051. routes = append(routes, &router.Route{
  1052. Endpoint: getIncidentsEndpoint,
  1053. Handler: getIncidentsHandler,
  1054. Router: r,
  1055. })
  1056. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/logs -> cluster.NewGetIncidentsHandler
  1057. getIncidentEventLogsEndpoint := factory.NewAPIEndpoint(
  1058. &types.APIRequestMetadata{
  1059. Verb: types.APIVerbGet,
  1060. Method: types.HTTPVerbGet,
  1061. Path: &types.Path{
  1062. Parent: basePath,
  1063. RelativePath: relPath + "/incidents/logs",
  1064. },
  1065. Scopes: []types.PermissionScope{
  1066. types.UserScope,
  1067. types.ProjectScope,
  1068. types.ClusterScope,
  1069. },
  1070. },
  1071. )
  1072. getIncidentEventLogsHandler := cluster.NewGetIncidentEventLogsHandler(
  1073. config,
  1074. factory.GetDecoderValidator(),
  1075. factory.GetResultWriter(),
  1076. )
  1077. routes = append(routes, &router.Route{
  1078. Endpoint: getIncidentEventLogsEndpoint,
  1079. Handler: getIncidentEventLogsHandler,
  1080. Router: r,
  1081. })
  1082. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1083. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1084. &types.APIRequestMetadata{
  1085. Verb: types.APIVerbCreate,
  1086. Method: types.HTTPVerbPost,
  1087. Path: &types.Path{
  1088. Parent: basePath,
  1089. RelativePath: relPath + "/incidents/notify_new",
  1090. },
  1091. Scopes: []types.PermissionScope{
  1092. types.UserScope,
  1093. types.ProjectScope,
  1094. types.ClusterScope,
  1095. },
  1096. },
  1097. )
  1098. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1099. config,
  1100. factory.GetDecoderValidator(),
  1101. factory.GetResultWriter(),
  1102. )
  1103. routes = append(routes, &router.Route{
  1104. Endpoint: notifyNewIncidentEndpoint,
  1105. Handler: notifyNewIncidentHandler,
  1106. Router: r,
  1107. })
  1108. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1109. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1110. &types.APIRequestMetadata{
  1111. Verb: types.APIVerbCreate,
  1112. Method: types.HTTPVerbPost,
  1113. Path: &types.Path{
  1114. Parent: basePath,
  1115. RelativePath: relPath + "/incidents/notify_resolved",
  1116. },
  1117. Scopes: []types.PermissionScope{
  1118. types.UserScope,
  1119. types.ProjectScope,
  1120. types.ClusterScope,
  1121. },
  1122. },
  1123. )
  1124. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1125. config,
  1126. factory.GetDecoderValidator(),
  1127. factory.GetResultWriter(),
  1128. )
  1129. routes = append(routes, &router.Route{
  1130. Endpoint: notifyResolvedIncidentEndpoint,
  1131. Handler: notifyResolvedIncidentHandler,
  1132. Router: r,
  1133. })
  1134. return routes, newPath
  1135. }