cluster.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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}/deployments -> environment.NewListDeploymentsByClusterHandler
  284. listDeploymentsEndpoint := factory.NewAPIEndpoint(
  285. &types.APIRequestMetadata{
  286. Verb: types.APIVerbGet,
  287. Method: types.HTTPVerbGet,
  288. Path: &types.Path{
  289. Parent: basePath,
  290. RelativePath: relPath + "/deployments",
  291. },
  292. Scopes: []types.PermissionScope{
  293. types.UserScope,
  294. types.ProjectScope,
  295. types.ClusterScope,
  296. },
  297. },
  298. )
  299. listDeploymentsHandler := environment.NewListDeploymentsByClusterHandler(
  300. config,
  301. factory.GetDecoderValidator(),
  302. factory.GetResultWriter(),
  303. )
  304. routes = append(routes, &router.Route{
  305. Endpoint: listDeploymentsEndpoint,
  306. Handler: listDeploymentsHandler,
  307. Router: r,
  308. })
  309. // GET /api/projects/{project_id}/clusters/{cluster_id}/environments/{environment_id}/deployment -> environment.NewGetDeploymentByClusterHandler
  310. getDeploymentEndpoint := factory.NewAPIEndpoint(
  311. &types.APIRequestMetadata{
  312. Verb: types.APIVerbGet,
  313. Method: types.HTTPVerbGet,
  314. Path: &types.Path{
  315. Parent: basePath,
  316. RelativePath: relPath + "/environments/{environment_id}/deployment",
  317. },
  318. Scopes: []types.PermissionScope{
  319. types.UserScope,
  320. types.ProjectScope,
  321. types.ClusterScope,
  322. },
  323. },
  324. )
  325. getDeploymentHandler := environment.NewGetDeploymentByEnvironmentHandler(
  326. config,
  327. factory.GetDecoderValidator(),
  328. factory.GetResultWriter(),
  329. )
  330. routes = append(routes, &router.Route{
  331. Endpoint: getDeploymentEndpoint,
  332. Handler: getDeploymentHandler,
  333. Router: r,
  334. })
  335. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/reenable -> environment.NewReenableDeploymentHandler
  336. reenableDeploymentEndpoint := factory.NewAPIEndpoint(
  337. &types.APIRequestMetadata{
  338. Verb: types.APIVerbUpdate,
  339. Method: types.HTTPVerbPatch,
  340. Path: &types.Path{
  341. Parent: basePath,
  342. RelativePath: relPath + "/deployments/{deployment_id}/reenable",
  343. },
  344. Scopes: []types.PermissionScope{
  345. types.UserScope,
  346. types.ProjectScope,
  347. types.ClusterScope,
  348. },
  349. },
  350. )
  351. reenableDeploymentHandler := environment.NewReenableDeploymentHandler(
  352. config,
  353. factory.GetDecoderValidator(),
  354. factory.GetResultWriter(),
  355. )
  356. routes = append(routes, &router.Route{
  357. Endpoint: reenableDeploymentEndpoint,
  358. Handler: reenableDeploymentHandler,
  359. Router: r,
  360. })
  361. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id}/trigger_workflow -> environment.NewTriggerDeploymentWorkflowHandler
  362. triggerDeploymentWorkflowEndpoint := factory.NewAPIEndpoint(
  363. &types.APIRequestMetadata{
  364. Verb: types.APIVerbCreate,
  365. Method: types.HTTPVerbPost,
  366. Path: &types.Path{
  367. Parent: basePath,
  368. RelativePath: relPath + "/deployments/{deployment_id}/trigger_workflow",
  369. },
  370. Scopes: []types.PermissionScope{
  371. types.UserScope,
  372. types.ProjectScope,
  373. types.ClusterScope,
  374. },
  375. },
  376. )
  377. triggerDeploymentWorkflowHandler := environment.NewTriggerDeploymentWorkflowHandler(
  378. config,
  379. factory.GetDecoderValidator(),
  380. factory.GetResultWriter(),
  381. )
  382. routes = append(routes, &router.Route{
  383. Endpoint: triggerDeploymentWorkflowEndpoint,
  384. Handler: triggerDeploymentWorkflowHandler,
  385. Router: r,
  386. })
  387. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployments/pull_request -> environment.NewEnablePullRequestHandler
  388. enablePullRequestEndpoint := factory.NewAPIEndpoint(
  389. &types.APIRequestMetadata{
  390. Verb: types.APIVerbCreate,
  391. Method: types.HTTPVerbPost,
  392. Path: &types.Path{
  393. Parent: basePath,
  394. RelativePath: relPath + "/deployments/pull_request",
  395. },
  396. Scopes: []types.PermissionScope{
  397. types.UserScope,
  398. types.ProjectScope,
  399. types.ClusterScope,
  400. },
  401. },
  402. )
  403. enablePullRequestHandler := environment.NewEnablePullRequestHandler(
  404. config,
  405. factory.GetDecoderValidator(),
  406. factory.GetResultWriter(),
  407. )
  408. routes = append(routes, &router.Route{
  409. Endpoint: enablePullRequestEndpoint,
  410. Handler: enablePullRequestHandler,
  411. Router: r,
  412. })
  413. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployments/{deployment_id} ->
  414. // environment.NewDeleteDeploymentHandler
  415. deleteDeploymentEndpoint := factory.NewAPIEndpoint(
  416. &types.APIRequestMetadata{
  417. Verb: types.APIVerbDelete,
  418. Method: types.HTTPVerbDelete,
  419. Path: &types.Path{
  420. Parent: basePath,
  421. RelativePath: relPath + "/deployments/{deployment_id}",
  422. },
  423. Scopes: []types.PermissionScope{
  424. types.UserScope,
  425. types.ProjectScope,
  426. types.ClusterScope,
  427. },
  428. },
  429. )
  430. deleteDeploymentHandler := environment.NewDeleteDeploymentHandler(
  431. config,
  432. factory.GetDecoderValidator(),
  433. factory.GetResultWriter(),
  434. )
  435. routes = append(routes, &router.Route{
  436. Endpoint: deleteDeploymentEndpoint,
  437. Handler: deleteDeploymentHandler,
  438. Router: r,
  439. })
  440. // POST /api/projects/{project_id}/clusters/{cluster_id}/environment/{environment_id}/toggle_new_comment -> environment.NewToggleNewCommentHandler
  441. toggleNewCommentEndpoint := factory.NewAPIEndpoint(
  442. &types.APIRequestMetadata{
  443. Verb: types.APIVerbCreate,
  444. Method: types.HTTPVerbPost,
  445. Path: &types.Path{
  446. Parent: basePath,
  447. RelativePath: relPath + "/environment/{environment_id}/toggle_new_comment",
  448. },
  449. Scopes: []types.PermissionScope{
  450. types.UserScope,
  451. types.ProjectScope,
  452. types.ClusterScope,
  453. },
  454. },
  455. )
  456. toggleNewCommentHandler := environment.NewToggleNewCommentHandler(
  457. config,
  458. factory.GetDecoderValidator(),
  459. factory.GetResultWriter(),
  460. )
  461. routes = append(routes, &router.Route{
  462. Endpoint: toggleNewCommentEndpoint,
  463. Handler: toggleNewCommentHandler,
  464. Router: r,
  465. })
  466. }
  467. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  468. listNamespacesEndpoint := factory.NewAPIEndpoint(
  469. &types.APIRequestMetadata{
  470. Verb: types.APIVerbGet,
  471. Method: types.HTTPVerbGet,
  472. Path: &types.Path{
  473. Parent: basePath,
  474. RelativePath: relPath + "/namespaces",
  475. },
  476. Scopes: []types.PermissionScope{
  477. types.UserScope,
  478. types.ProjectScope,
  479. types.ClusterScope,
  480. },
  481. },
  482. )
  483. listNamespacesHandler := cluster.NewListNamespacesHandler(
  484. config,
  485. factory.GetResultWriter(),
  486. )
  487. routes = append(routes, &router.Route{
  488. Endpoint: listNamespacesEndpoint,
  489. Handler: listNamespacesHandler,
  490. Router: r,
  491. })
  492. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  493. listNodesEndpoint := factory.NewAPIEndpoint(
  494. &types.APIRequestMetadata{
  495. Verb: types.APIVerbGet,
  496. Method: types.HTTPVerbGet,
  497. Path: &types.Path{
  498. Parent: basePath,
  499. RelativePath: relPath + "/nodes",
  500. },
  501. Scopes: []types.PermissionScope{
  502. types.UserScope,
  503. types.ProjectScope,
  504. types.ClusterScope,
  505. },
  506. },
  507. )
  508. listNodesHandler := cluster.NewListNodesHandler(
  509. config,
  510. factory.GetResultWriter(),
  511. )
  512. routes = append(routes, &router.Route{
  513. Endpoint: listNodesEndpoint,
  514. Handler: listNodesHandler,
  515. Router: r,
  516. })
  517. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  518. getNodeEndpoint := factory.NewAPIEndpoint(
  519. &types.APIRequestMetadata{
  520. Verb: types.APIVerbGet,
  521. Method: types.HTTPVerbGet,
  522. Path: &types.Path{
  523. Parent: basePath,
  524. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  525. },
  526. Scopes: []types.PermissionScope{
  527. types.UserScope,
  528. types.ProjectScope,
  529. types.ClusterScope,
  530. },
  531. },
  532. )
  533. getNodeHandler := cluster.NewGetNodeHandler(
  534. config,
  535. factory.GetResultWriter(),
  536. )
  537. routes = append(routes, &router.Route{
  538. Endpoint: getNodeEndpoint,
  539. Handler: getNodeHandler,
  540. Router: r,
  541. })
  542. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  543. createNamespaceEndpoint := factory.NewAPIEndpoint(
  544. &types.APIRequestMetadata{
  545. Verb: types.APIVerbCreate,
  546. Method: types.HTTPVerbPost,
  547. Path: &types.Path{
  548. Parent: basePath,
  549. RelativePath: relPath + "/namespaces/create",
  550. },
  551. Scopes: []types.PermissionScope{
  552. types.UserScope,
  553. types.ProjectScope,
  554. types.ClusterScope,
  555. },
  556. },
  557. )
  558. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  559. config,
  560. factory.GetDecoderValidator(),
  561. factory.GetResultWriter(),
  562. )
  563. routes = append(routes, &router.Route{
  564. Endpoint: createNamespaceEndpoint,
  565. Handler: createNamespaceHandler,
  566. Router: r,
  567. })
  568. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  569. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  570. &types.APIRequestMetadata{
  571. Verb: types.APIVerbDelete,
  572. Method: types.HTTPVerbDelete,
  573. Path: &types.Path{
  574. Parent: basePath,
  575. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  576. },
  577. Scopes: []types.PermissionScope{
  578. types.UserScope,
  579. types.ProjectScope,
  580. types.ClusterScope,
  581. },
  582. },
  583. )
  584. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  585. config,
  586. factory.GetDecoderValidator(),
  587. )
  588. routes = append(routes, &router.Route{
  589. Endpoint: deleteNamespaceEndpoint,
  590. Handler: deleteNamespaceHandler,
  591. Router: r,
  592. })
  593. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  594. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  595. &types.APIRequestMetadata{
  596. Verb: types.APIVerbGet,
  597. Method: types.HTTPVerbGet,
  598. Path: &types.Path{
  599. Parent: basePath,
  600. RelativePath: relPath + "/kubeconfig",
  601. },
  602. Scopes: []types.PermissionScope{
  603. types.UserScope,
  604. types.ProjectScope,
  605. types.ClusterScope,
  606. },
  607. },
  608. )
  609. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  610. config,
  611. factory.GetResultWriter(),
  612. )
  613. routes = append(routes, &router.Route{
  614. Endpoint: getTemporaryKubeconfigEndpoint,
  615. Handler: getTemporaryKubeconfigHandler,
  616. Router: r,
  617. })
  618. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  619. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  620. &types.APIRequestMetadata{
  621. Verb: types.APIVerbGet,
  622. Method: types.HTTPVerbGet,
  623. Path: &types.Path{
  624. Parent: basePath,
  625. RelativePath: relPath + "/prometheus/detect",
  626. },
  627. Scopes: []types.PermissionScope{
  628. types.UserScope,
  629. types.ProjectScope,
  630. types.ClusterScope,
  631. },
  632. },
  633. )
  634. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  635. routes = append(routes, &router.Route{
  636. Endpoint: detectPrometheusInstalledEndpoint,
  637. Handler: detectPrometheusInstalledHandler,
  638. Router: r,
  639. })
  640. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  641. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  642. &types.APIRequestMetadata{
  643. Verb: types.APIVerbGet,
  644. Method: types.HTTPVerbGet,
  645. Path: &types.Path{
  646. Parent: basePath,
  647. RelativePath: relPath + "/agent/detect",
  648. },
  649. Scopes: []types.PermissionScope{
  650. types.UserScope,
  651. types.ProjectScope,
  652. types.ClusterScope,
  653. },
  654. },
  655. )
  656. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  657. routes = append(routes, &router.Route{
  658. Endpoint: detectAgentInstalledEndpoint,
  659. Handler: detectAgentInstalledHandler,
  660. Router: r,
  661. })
  662. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  663. installAgentEndpoint := factory.NewAPIEndpoint(
  664. &types.APIRequestMetadata{
  665. Verb: types.APIVerbCreate,
  666. Method: types.HTTPVerbPost,
  667. Path: &types.Path{
  668. Parent: basePath,
  669. RelativePath: relPath + "/agent/install",
  670. },
  671. Scopes: []types.PermissionScope{
  672. types.UserScope,
  673. types.ProjectScope,
  674. types.ClusterScope,
  675. },
  676. },
  677. )
  678. installAgentHandler := cluster.NewInstallAgentHandler(
  679. config,
  680. factory.GetDecoderValidator(),
  681. factory.GetResultWriter(),
  682. )
  683. routes = append(routes, &router.Route{
  684. Endpoint: installAgentEndpoint,
  685. Handler: installAgentHandler,
  686. Router: r,
  687. })
  688. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  689. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  690. &types.APIRequestMetadata{
  691. Verb: types.APIVerbCreate,
  692. Method: types.HTTPVerbPost,
  693. Path: &types.Path{
  694. Parent: basePath,
  695. RelativePath: relPath + "/agent/upgrade",
  696. },
  697. Scopes: []types.PermissionScope{
  698. types.UserScope,
  699. types.ProjectScope,
  700. types.ClusterScope,
  701. },
  702. },
  703. )
  704. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  705. config,
  706. factory.GetDecoderValidator(),
  707. factory.GetResultWriter(),
  708. )
  709. routes = append(routes, &router.Route{
  710. Endpoint: upgradeAgentEndpoint,
  711. Handler: upgradeAgentHandler,
  712. Router: r,
  713. })
  714. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  715. listKubeEventsEndpoint := factory.NewAPIEndpoint(
  716. &types.APIRequestMetadata{
  717. Verb: types.APIVerbGet,
  718. Method: types.HTTPVerbGet,
  719. Path: &types.Path{
  720. Parent: basePath,
  721. RelativePath: relPath + "/kube_events",
  722. },
  723. Scopes: []types.PermissionScope{
  724. types.UserScope,
  725. types.ProjectScope,
  726. types.ClusterScope,
  727. },
  728. },
  729. )
  730. listKubeEventsHandler := kube_events.NewListKubeEventsHandler(
  731. config,
  732. factory.GetDecoderValidator(),
  733. factory.GetResultWriter(),
  734. )
  735. routes = append(routes, &router.Route{
  736. Endpoint: listKubeEventsEndpoint,
  737. Handler: listKubeEventsHandler,
  738. Router: r,
  739. })
  740. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  741. getKubeEventEndpoint := factory.NewAPIEndpoint(
  742. &types.APIRequestMetadata{
  743. Verb: types.APIVerbGet,
  744. Method: types.HTTPVerbGet,
  745. Path: &types.Path{
  746. Parent: basePath,
  747. RelativePath: fmt.Sprintf("%s/kube_events/{%s}", relPath, types.URLParamKubeEventID),
  748. },
  749. Scopes: []types.PermissionScope{
  750. types.UserScope,
  751. types.ProjectScope,
  752. types.ClusterScope,
  753. },
  754. },
  755. )
  756. getKubeEventHandler := kube_events.NewGetKubeEventHandler(
  757. config,
  758. factory.GetDecoderValidator(),
  759. factory.GetResultWriter(),
  760. )
  761. routes = append(routes, &router.Route{
  762. Endpoint: getKubeEventEndpoint,
  763. Handler: getKubeEventHandler,
  764. Router: r,
  765. })
  766. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/logs -> kube_events.NewGetKubeEventLogsHandler
  767. getKubeEventLogsEndpoint := factory.NewAPIEndpoint(
  768. &types.APIRequestMetadata{
  769. Verb: types.APIVerbGet,
  770. Method: types.HTTPVerbGet,
  771. Path: &types.Path{
  772. Parent: basePath,
  773. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/logs", relPath, types.URLParamKubeEventID),
  774. },
  775. Scopes: []types.PermissionScope{
  776. types.UserScope,
  777. types.ProjectScope,
  778. types.ClusterScope,
  779. },
  780. },
  781. )
  782. getKubeEventLogsHandler := kube_events.NewGetKubeEventLogsHandler(
  783. config,
  784. factory.GetDecoderValidator(),
  785. factory.GetResultWriter(),
  786. )
  787. routes = append(routes, &router.Route{
  788. Endpoint: getKubeEventLogsEndpoint,
  789. Handler: getKubeEventLogsHandler,
  790. Router: r,
  791. })
  792. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/log_buckets -> kube_events.NewGetKubeEventLogBucketsHandler
  793. getKubeEventLogBucketsEndpoint := factory.NewAPIEndpoint(
  794. &types.APIRequestMetadata{
  795. Verb: types.APIVerbGet,
  796. Method: types.HTTPVerbGet,
  797. Path: &types.Path{
  798. Parent: basePath,
  799. RelativePath: fmt.Sprintf("%s/kube_events/{%s}/log_buckets", relPath, types.URLParamKubeEventID),
  800. },
  801. Scopes: []types.PermissionScope{
  802. types.UserScope,
  803. types.ProjectScope,
  804. types.ClusterScope,
  805. },
  806. },
  807. )
  808. getKubeEventLogBucketsHandler := kube_events.NewGetKubeEventLogBucketsHandler(
  809. config,
  810. factory.GetDecoderValidator(),
  811. factory.GetResultWriter(),
  812. )
  813. routes = append(routes, &router.Route{
  814. Endpoint: getKubeEventLogBucketsEndpoint,
  815. Handler: getKubeEventLogBucketsHandler,
  816. Router: r,
  817. })
  818. // POST /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewCreateKubeEventHandler
  819. createKubeEventsEndpoint := factory.NewAPIEndpoint(
  820. &types.APIRequestMetadata{
  821. Verb: types.APIVerbCreate,
  822. Method: types.HTTPVerbPost,
  823. Path: &types.Path{
  824. Parent: basePath,
  825. RelativePath: relPath + "/kube_events",
  826. },
  827. Scopes: []types.PermissionScope{
  828. types.UserScope,
  829. types.ProjectScope,
  830. types.ClusterScope,
  831. },
  832. },
  833. )
  834. createKubeEventsHandler := kube_events.NewCreateKubeEventHandler(
  835. config,
  836. factory.GetDecoderValidator(),
  837. factory.GetResultWriter(),
  838. )
  839. routes = append(routes, &router.Route{
  840. Endpoint: createKubeEventsEndpoint,
  841. Handler: createKubeEventsHandler,
  842. Router: r,
  843. })
  844. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  845. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  846. &types.APIRequestMetadata{
  847. Verb: types.APIVerbGet,
  848. Method: types.HTTPVerbGet,
  849. Path: &types.Path{
  850. Parent: basePath,
  851. RelativePath: relPath + "/prometheus/ingresses",
  852. },
  853. Scopes: []types.PermissionScope{
  854. types.UserScope,
  855. types.ProjectScope,
  856. types.ClusterScope,
  857. },
  858. },
  859. )
  860. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  861. config,
  862. factory.GetResultWriter(),
  863. )
  864. routes = append(routes, &router.Route{
  865. Endpoint: listNGINXIngressesEndpoint,
  866. Handler: listNGINXIngressesHandler,
  867. Router: r,
  868. })
  869. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  870. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  871. &types.APIRequestMetadata{
  872. Verb: types.APIVerbGet,
  873. Method: types.HTTPVerbGet,
  874. Path: &types.Path{
  875. Parent: basePath,
  876. RelativePath: relPath + "/metrics",
  877. },
  878. Scopes: []types.PermissionScope{
  879. types.UserScope,
  880. types.ProjectScope,
  881. types.ClusterScope,
  882. },
  883. },
  884. )
  885. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  886. config,
  887. factory.GetDecoderValidator(),
  888. factory.GetResultWriter(),
  889. )
  890. routes = append(routes, &router.Route{
  891. Endpoint: getPodMetricsEndpoint,
  892. Handler: getPodMetricsHandler,
  893. Router: r,
  894. })
  895. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  896. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  897. &types.APIRequestMetadata{
  898. Verb: types.APIVerbGet,
  899. Method: types.HTTPVerbGet,
  900. Path: &types.Path{
  901. Parent: basePath,
  902. RelativePath: relPath + "/helm_release",
  903. },
  904. Scopes: []types.PermissionScope{
  905. types.UserScope,
  906. types.ProjectScope,
  907. types.ClusterScope,
  908. },
  909. IsWebsocket: true,
  910. },
  911. )
  912. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  913. config,
  914. factory.GetDecoderValidator(),
  915. factory.GetResultWriter(),
  916. )
  917. routes = append(routes, &router.Route{
  918. Endpoint: streamHelmReleaseEndpoint,
  919. Handler: streamHelmReleaseHandler,
  920. Router: r,
  921. })
  922. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  923. streamStatusEndpoint := factory.NewAPIEndpoint(
  924. &types.APIRequestMetadata{
  925. Verb: types.APIVerbGet,
  926. Method: types.HTTPVerbGet,
  927. Path: &types.Path{
  928. Parent: basePath,
  929. RelativePath: fmt.Sprintf(
  930. "%s/{%s}/status",
  931. relPath,
  932. types.URLParamKind,
  933. ),
  934. },
  935. Scopes: []types.PermissionScope{
  936. types.UserScope,
  937. types.ProjectScope,
  938. types.ClusterScope,
  939. },
  940. IsWebsocket: true,
  941. },
  942. )
  943. streamStatusHandler := cluster.NewStreamStatusHandler(
  944. config,
  945. factory.GetDecoderValidator(),
  946. factory.GetResultWriter(),
  947. )
  948. routes = append(routes, &router.Route{
  949. Endpoint: streamStatusEndpoint,
  950. Handler: streamStatusHandler,
  951. Router: r,
  952. })
  953. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  954. getPodsEndpoint := factory.NewAPIEndpoint(
  955. &types.APIRequestMetadata{
  956. Verb: types.APIVerbGet,
  957. Method: types.HTTPVerbGet,
  958. Path: &types.Path{
  959. Parent: basePath,
  960. RelativePath: relPath + "/pods",
  961. },
  962. Scopes: []types.PermissionScope{
  963. types.UserScope,
  964. types.ProjectScope,
  965. types.ClusterScope,
  966. },
  967. },
  968. )
  969. getPodsHandler := cluster.NewGetPodsHandler(
  970. config,
  971. factory.GetDecoderValidator(),
  972. factory.GetResultWriter(),
  973. )
  974. routes = append(routes, &router.Route{
  975. Endpoint: getPodsEndpoint,
  976. Handler: getPodsHandler,
  977. Router: r,
  978. })
  979. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewGetIncidentsHandler
  980. getIncidentsEndpoint := factory.NewAPIEndpoint(
  981. &types.APIRequestMetadata{
  982. Verb: types.APIVerbGet,
  983. Method: types.HTTPVerbGet,
  984. Path: &types.Path{
  985. Parent: basePath,
  986. RelativePath: relPath + "/incidents",
  987. },
  988. Scopes: []types.PermissionScope{
  989. types.UserScope,
  990. types.ProjectScope,
  991. types.ClusterScope,
  992. },
  993. },
  994. )
  995. getIncidentsHandler := cluster.NewGetIncidentsHandler(
  996. config,
  997. factory.GetDecoderValidator(),
  998. factory.GetResultWriter(),
  999. )
  1000. routes = append(routes, &router.Route{
  1001. Endpoint: getIncidentsEndpoint,
  1002. Handler: getIncidentsHandler,
  1003. Router: r,
  1004. })
  1005. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/logs -> cluster.NewGetIncidentsHandler
  1006. getIncidentEventLogsEndpoint := factory.NewAPIEndpoint(
  1007. &types.APIRequestMetadata{
  1008. Verb: types.APIVerbGet,
  1009. Method: types.HTTPVerbGet,
  1010. Path: &types.Path{
  1011. Parent: basePath,
  1012. RelativePath: relPath + "/incidents/logs",
  1013. },
  1014. Scopes: []types.PermissionScope{
  1015. types.UserScope,
  1016. types.ProjectScope,
  1017. types.ClusterScope,
  1018. },
  1019. },
  1020. )
  1021. getIncidentEventLogsHandler := cluster.NewGetIncidentEventLogsHandler(
  1022. config,
  1023. factory.GetDecoderValidator(),
  1024. factory.GetResultWriter(),
  1025. )
  1026. routes = append(routes, &router.Route{
  1027. Endpoint: getIncidentEventLogsEndpoint,
  1028. Handler: getIncidentEventLogsHandler,
  1029. Router: r,
  1030. })
  1031. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1032. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1033. &types.APIRequestMetadata{
  1034. Verb: types.APIVerbCreate,
  1035. Method: types.HTTPVerbPost,
  1036. Path: &types.Path{
  1037. Parent: basePath,
  1038. RelativePath: relPath + "/incidents/notify_new",
  1039. },
  1040. Scopes: []types.PermissionScope{
  1041. types.UserScope,
  1042. types.ProjectScope,
  1043. types.ClusterScope,
  1044. },
  1045. },
  1046. )
  1047. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1048. config,
  1049. factory.GetDecoderValidator(),
  1050. factory.GetResultWriter(),
  1051. )
  1052. routes = append(routes, &router.Route{
  1053. Endpoint: notifyNewIncidentEndpoint,
  1054. Handler: notifyNewIncidentHandler,
  1055. Router: r,
  1056. })
  1057. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1058. notifyResolvedIncidentEndpoint := factory.NewAPIEndpoint(
  1059. &types.APIRequestMetadata{
  1060. Verb: types.APIVerbCreate,
  1061. Method: types.HTTPVerbPost,
  1062. Path: &types.Path{
  1063. Parent: basePath,
  1064. RelativePath: relPath + "/incidents/notify_resolved",
  1065. },
  1066. Scopes: []types.PermissionScope{
  1067. types.UserScope,
  1068. types.ProjectScope,
  1069. types.ClusterScope,
  1070. },
  1071. },
  1072. )
  1073. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1074. config,
  1075. factory.GetDecoderValidator(),
  1076. factory.GetResultWriter(),
  1077. )
  1078. routes = append(routes, &router.Route{
  1079. Endpoint: notifyResolvedIncidentEndpoint,
  1080. Handler: notifyResolvedIncidentHandler,
  1081. Router: r,
  1082. })
  1083. return routes, newPath
  1084. }