cluster.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. }
  441. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
  442. listNamespacesEndpoint := factory.NewAPIEndpoint(
  443. &types.APIRequestMetadata{
  444. Verb: types.APIVerbGet,
  445. Method: types.HTTPVerbGet,
  446. Path: &types.Path{
  447. Parent: basePath,
  448. RelativePath: relPath + "/namespaces",
  449. },
  450. Scopes: []types.PermissionScope{
  451. types.UserScope,
  452. types.ProjectScope,
  453. types.ClusterScope,
  454. },
  455. },
  456. )
  457. listNamespacesHandler := cluster.NewListNamespacesHandler(
  458. config,
  459. factory.GetResultWriter(),
  460. )
  461. routes = append(routes, &router.Route{
  462. Endpoint: listNamespacesEndpoint,
  463. Handler: listNamespacesHandler,
  464. Router: r,
  465. })
  466. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes -> cluster.NewListNodesHandler
  467. listNodesEndpoint := factory.NewAPIEndpoint(
  468. &types.APIRequestMetadata{
  469. Verb: types.APIVerbGet,
  470. Method: types.HTTPVerbGet,
  471. Path: &types.Path{
  472. Parent: basePath,
  473. RelativePath: relPath + "/nodes",
  474. },
  475. Scopes: []types.PermissionScope{
  476. types.UserScope,
  477. types.ProjectScope,
  478. types.ClusterScope,
  479. },
  480. },
  481. )
  482. listNodesHandler := cluster.NewListNodesHandler(
  483. config,
  484. factory.GetResultWriter(),
  485. )
  486. routes = append(routes, &router.Route{
  487. Endpoint: listNodesEndpoint,
  488. Handler: listNodesHandler,
  489. Router: r,
  490. })
  491. // GET /api/projects/{project_id}/clusters/{cluster_id}/nodes/{node_name} -> cluster.NewGetNodeHandler
  492. getNodeEndpoint := factory.NewAPIEndpoint(
  493. &types.APIRequestMetadata{
  494. Verb: types.APIVerbGet,
  495. Method: types.HTTPVerbGet,
  496. Path: &types.Path{
  497. Parent: basePath,
  498. RelativePath: fmt.Sprintf("%s/nodes/{%s}", relPath, types.URLParamNodeName),
  499. },
  500. Scopes: []types.PermissionScope{
  501. types.UserScope,
  502. types.ProjectScope,
  503. types.ClusterScope,
  504. },
  505. },
  506. )
  507. getNodeHandler := cluster.NewGetNodeHandler(
  508. config,
  509. factory.GetResultWriter(),
  510. )
  511. routes = append(routes, &router.Route{
  512. Endpoint: getNodeEndpoint,
  513. Handler: getNodeHandler,
  514. Router: r,
  515. })
  516. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
  517. createNamespaceEndpoint := factory.NewAPIEndpoint(
  518. &types.APIRequestMetadata{
  519. Verb: types.APIVerbCreate,
  520. Method: types.HTTPVerbPost,
  521. Path: &types.Path{
  522. Parent: basePath,
  523. RelativePath: relPath + "/namespaces/create",
  524. },
  525. Scopes: []types.PermissionScope{
  526. types.UserScope,
  527. types.ProjectScope,
  528. types.ClusterScope,
  529. },
  530. },
  531. )
  532. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  533. config,
  534. factory.GetDecoderValidator(),
  535. factory.GetResultWriter(),
  536. )
  537. routes = append(routes, &router.Route{
  538. Endpoint: createNamespaceEndpoint,
  539. Handler: createNamespaceHandler,
  540. Router: r,
  541. })
  542. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  543. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  544. &types.APIRequestMetadata{
  545. Verb: types.APIVerbDelete,
  546. Method: types.HTTPVerbDelete,
  547. Path: &types.Path{
  548. Parent: basePath,
  549. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  550. },
  551. Scopes: []types.PermissionScope{
  552. types.UserScope,
  553. types.ProjectScope,
  554. types.ClusterScope,
  555. },
  556. },
  557. )
  558. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  559. config,
  560. factory.GetDecoderValidator(),
  561. )
  562. routes = append(routes, &router.Route{
  563. Endpoint: deleteNamespaceEndpoint,
  564. Handler: deleteNamespaceHandler,
  565. Router: r,
  566. })
  567. // GET /api/projects/{project_id}/clusters/{cluster_id}/kubeconfig -> cluster.NewGetTemporaryKubeconfigHandler
  568. getTemporaryKubeconfigEndpoint := factory.NewAPIEndpoint(
  569. &types.APIRequestMetadata{
  570. Verb: types.APIVerbGet,
  571. Method: types.HTTPVerbGet,
  572. Path: &types.Path{
  573. Parent: basePath,
  574. RelativePath: relPath + "/kubeconfig",
  575. },
  576. Scopes: []types.PermissionScope{
  577. types.UserScope,
  578. types.ProjectScope,
  579. types.ClusterScope,
  580. },
  581. },
  582. )
  583. getTemporaryKubeconfigHandler := cluster.NewGetTemporaryKubeconfigHandler(
  584. config,
  585. factory.GetResultWriter(),
  586. )
  587. routes = append(routes, &router.Route{
  588. Endpoint: getTemporaryKubeconfigEndpoint,
  589. Handler: getTemporaryKubeconfigHandler,
  590. Router: r,
  591. })
  592. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
  593. detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
  594. &types.APIRequestMetadata{
  595. Verb: types.APIVerbGet,
  596. Method: types.HTTPVerbGet,
  597. Path: &types.Path{
  598. Parent: basePath,
  599. RelativePath: relPath + "/prometheus/detect",
  600. },
  601. Scopes: []types.PermissionScope{
  602. types.UserScope,
  603. types.ProjectScope,
  604. types.ClusterScope,
  605. },
  606. },
  607. )
  608. detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
  609. routes = append(routes, &router.Route{
  610. Endpoint: detectPrometheusInstalledEndpoint,
  611. Handler: detectPrometheusInstalledHandler,
  612. Router: r,
  613. })
  614. // GET /api/projects/{project_id}/clusters/{cluster_id}/agent/detect -> cluster.NewDetectAgentInstalledHandler
  615. detectAgentInstalledEndpoint := factory.NewAPIEndpoint(
  616. &types.APIRequestMetadata{
  617. Verb: types.APIVerbGet,
  618. Method: types.HTTPVerbGet,
  619. Path: &types.Path{
  620. Parent: basePath,
  621. RelativePath: relPath + "/agent/detect",
  622. },
  623. Scopes: []types.PermissionScope{
  624. types.UserScope,
  625. types.ProjectScope,
  626. types.ClusterScope,
  627. },
  628. },
  629. )
  630. detectAgentInstalledHandler := cluster.NewDetectAgentInstalledHandler(config, factory.GetResultWriter())
  631. routes = append(routes, &router.Route{
  632. Endpoint: detectAgentInstalledEndpoint,
  633. Handler: detectAgentInstalledHandler,
  634. Router: r,
  635. })
  636. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/install -> cluster.NewInstallAgentHandler
  637. installAgentEndpoint := factory.NewAPIEndpoint(
  638. &types.APIRequestMetadata{
  639. Verb: types.APIVerbCreate,
  640. Method: types.HTTPVerbPost,
  641. Path: &types.Path{
  642. Parent: basePath,
  643. RelativePath: relPath + "/agent/install",
  644. },
  645. Scopes: []types.PermissionScope{
  646. types.UserScope,
  647. types.ProjectScope,
  648. types.ClusterScope,
  649. },
  650. },
  651. )
  652. installAgentHandler := cluster.NewInstallAgentHandler(
  653. config,
  654. factory.GetDecoderValidator(),
  655. factory.GetResultWriter(),
  656. )
  657. routes = append(routes, &router.Route{
  658. Endpoint: installAgentEndpoint,
  659. Handler: installAgentHandler,
  660. Router: r,
  661. })
  662. // POST /api/projects/{project_id}/clusters/{cluster_id}/agent/upgrade -> cluster.NewInstallAgentHandler
  663. upgradeAgentEndpoint := factory.NewAPIEndpoint(
  664. &types.APIRequestMetadata{
  665. Verb: types.APIVerbCreate,
  666. Method: types.HTTPVerbPost,
  667. Path: &types.Path{
  668. Parent: basePath,
  669. RelativePath: relPath + "/agent/upgrade",
  670. },
  671. Scopes: []types.PermissionScope{
  672. types.UserScope,
  673. types.ProjectScope,
  674. types.ClusterScope,
  675. },
  676. },
  677. )
  678. upgradeAgentHandler := cluster.NewUpgradeAgentHandler(
  679. config,
  680. factory.GetDecoderValidator(),
  681. factory.GetResultWriter(),
  682. )
  683. routes = append(routes, &router.Route{
  684. Endpoint: upgradeAgentEndpoint,
  685. Handler: upgradeAgentHandler,
  686. Router: r,
  687. })
  688. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  689. listKubeEventsEndpoint := factory.NewAPIEndpoint(
  690. &types.APIRequestMetadata{
  691. Verb: types.APIVerbGet,
  692. Method: types.HTTPVerbGet,
  693. Path: &types.Path{
  694. Parent: basePath,
  695. RelativePath: relPath + "/kube_events",
  696. },
  697. Scopes: []types.PermissionScope{
  698. types.UserScope,
  699. types.ProjectScope,
  700. types.ClusterScope,
  701. },
  702. },
  703. )
  704. listKubeEventsHandler := kube_events.NewListKubeEventsHandler(
  705. config,
  706. factory.GetDecoderValidator(),
  707. factory.GetResultWriter(),
  708. )
  709. routes = append(routes, &router.Route{
  710. Endpoint: listKubeEventsEndpoint,
  711. Handler: listKubeEventsHandler,
  712. Router: r,
  713. })
  714. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewGetKubeEventHandler
  715. getKubeEventEndpoint := factory.NewAPIEndpoint(
  716. &types.APIRequestMetadata{
  717. Verb: types.APIVerbGet,
  718. Method: types.HTTPVerbGet,
  719. Path: &types.Path{
  720. Parent: basePath,
  721. RelativePath: fmt.Sprintf("%s/kube_events/{%s}", relPath, types.URLParamKubeEventID),
  722. },
  723. Scopes: []types.PermissionScope{
  724. types.UserScope,
  725. types.ProjectScope,
  726. types.ClusterScope,
  727. },
  728. },
  729. )
  730. getKubeEventHandler := kube_events.NewGetKubeEventHandler(
  731. config,
  732. factory.GetDecoderValidator(),
  733. factory.GetResultWriter(),
  734. )
  735. routes = append(routes, &router.Route{
  736. Endpoint: getKubeEventEndpoint,
  737. Handler: getKubeEventHandler,
  738. Router: r,
  739. })
  740. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/logs -> kube_events.NewGetKubeEventLogsHandler
  741. getKubeEventLogsEndpoint := 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}/logs", relPath, types.URLParamKubeEventID),
  748. },
  749. Scopes: []types.PermissionScope{
  750. types.UserScope,
  751. types.ProjectScope,
  752. types.ClusterScope,
  753. },
  754. },
  755. )
  756. getKubeEventLogsHandler := kube_events.NewGetKubeEventLogsHandler(
  757. config,
  758. factory.GetDecoderValidator(),
  759. factory.GetResultWriter(),
  760. )
  761. routes = append(routes, &router.Route{
  762. Endpoint: getKubeEventLogsEndpoint,
  763. Handler: getKubeEventLogsHandler,
  764. Router: r,
  765. })
  766. // GET /api/projects/{project_id}/clusters/{cluster_id}/kube_events/{kube_event_id}/log_buckets -> kube_events.NewGetKubeEventLogBucketsHandler
  767. getKubeEventLogBucketsEndpoint := 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}/log_buckets", relPath, types.URLParamKubeEventID),
  774. },
  775. Scopes: []types.PermissionScope{
  776. types.UserScope,
  777. types.ProjectScope,
  778. types.ClusterScope,
  779. },
  780. },
  781. )
  782. getKubeEventLogBucketsHandler := kube_events.NewGetKubeEventLogBucketsHandler(
  783. config,
  784. factory.GetDecoderValidator(),
  785. factory.GetResultWriter(),
  786. )
  787. routes = append(routes, &router.Route{
  788. Endpoint: getKubeEventLogBucketsEndpoint,
  789. Handler: getKubeEventLogBucketsHandler,
  790. Router: r,
  791. })
  792. // POST /api/projects/{project_id}/clusters/{cluster_id}/kube_events -> kube_events.NewCreateKubeEventHandler
  793. createKubeEventsEndpoint := factory.NewAPIEndpoint(
  794. &types.APIRequestMetadata{
  795. Verb: types.APIVerbCreate,
  796. Method: types.HTTPVerbPost,
  797. Path: &types.Path{
  798. Parent: basePath,
  799. RelativePath: relPath + "/kube_events",
  800. },
  801. Scopes: []types.PermissionScope{
  802. types.UserScope,
  803. types.ProjectScope,
  804. types.ClusterScope,
  805. },
  806. },
  807. )
  808. createKubeEventsHandler := kube_events.NewCreateKubeEventHandler(
  809. config,
  810. factory.GetDecoderValidator(),
  811. factory.GetResultWriter(),
  812. )
  813. routes = append(routes, &router.Route{
  814. Endpoint: createKubeEventsEndpoint,
  815. Handler: createKubeEventsHandler,
  816. Router: r,
  817. })
  818. // GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/ingresses -> cluster.NewListNGINXIngressesHandler
  819. listNGINXIngressesEndpoint := factory.NewAPIEndpoint(
  820. &types.APIRequestMetadata{
  821. Verb: types.APIVerbGet,
  822. Method: types.HTTPVerbGet,
  823. Path: &types.Path{
  824. Parent: basePath,
  825. RelativePath: relPath + "/prometheus/ingresses",
  826. },
  827. Scopes: []types.PermissionScope{
  828. types.UserScope,
  829. types.ProjectScope,
  830. types.ClusterScope,
  831. },
  832. },
  833. )
  834. listNGINXIngressesHandler := cluster.NewListNGINXIngressesHandler(
  835. config,
  836. factory.GetResultWriter(),
  837. )
  838. routes = append(routes, &router.Route{
  839. Endpoint: listNGINXIngressesEndpoint,
  840. Handler: listNGINXIngressesHandler,
  841. Router: r,
  842. })
  843. // GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
  844. getPodMetricsEndpoint := factory.NewAPIEndpoint(
  845. &types.APIRequestMetadata{
  846. Verb: types.APIVerbGet,
  847. Method: types.HTTPVerbGet,
  848. Path: &types.Path{
  849. Parent: basePath,
  850. RelativePath: relPath + "/metrics",
  851. },
  852. Scopes: []types.PermissionScope{
  853. types.UserScope,
  854. types.ProjectScope,
  855. types.ClusterScope,
  856. },
  857. },
  858. )
  859. getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
  860. config,
  861. factory.GetDecoderValidator(),
  862. factory.GetResultWriter(),
  863. )
  864. routes = append(routes, &router.Route{
  865. Endpoint: getPodMetricsEndpoint,
  866. Handler: getPodMetricsHandler,
  867. Router: r,
  868. })
  869. // GET /api/projects/{project_id}/clusters/{cluster_id}/helm_release -> cluster.NewStreamHelmReleaseHandler
  870. streamHelmReleaseEndpoint := factory.NewAPIEndpoint(
  871. &types.APIRequestMetadata{
  872. Verb: types.APIVerbGet,
  873. Method: types.HTTPVerbGet,
  874. Path: &types.Path{
  875. Parent: basePath,
  876. RelativePath: relPath + "/helm_release",
  877. },
  878. Scopes: []types.PermissionScope{
  879. types.UserScope,
  880. types.ProjectScope,
  881. types.ClusterScope,
  882. },
  883. IsWebsocket: true,
  884. },
  885. )
  886. streamHelmReleaseHandler := cluster.NewStreamHelmReleaseHandler(
  887. config,
  888. factory.GetDecoderValidator(),
  889. factory.GetResultWriter(),
  890. )
  891. routes = append(routes, &router.Route{
  892. Endpoint: streamHelmReleaseEndpoint,
  893. Handler: streamHelmReleaseHandler,
  894. Router: r,
  895. })
  896. // GET /api/projects/{project_id}/clusters/{cluster_id}/{kind}/status -> cluster.NewStreamStatusHandler
  897. streamStatusEndpoint := factory.NewAPIEndpoint(
  898. &types.APIRequestMetadata{
  899. Verb: types.APIVerbGet,
  900. Method: types.HTTPVerbGet,
  901. Path: &types.Path{
  902. Parent: basePath,
  903. RelativePath: fmt.Sprintf(
  904. "%s/{%s}/status",
  905. relPath,
  906. types.URLParamKind,
  907. ),
  908. },
  909. Scopes: []types.PermissionScope{
  910. types.UserScope,
  911. types.ProjectScope,
  912. types.ClusterScope,
  913. },
  914. IsWebsocket: true,
  915. },
  916. )
  917. streamStatusHandler := cluster.NewStreamStatusHandler(
  918. config,
  919. factory.GetDecoderValidator(),
  920. factory.GetResultWriter(),
  921. )
  922. routes = append(routes, &router.Route{
  923. Endpoint: streamStatusEndpoint,
  924. Handler: streamStatusHandler,
  925. Router: r,
  926. })
  927. // GET /api/projects/{project_id}/clusters/{cluster_id}/pods -> cluster.NewGetPodsHandler
  928. getPodsEndpoint := factory.NewAPIEndpoint(
  929. &types.APIRequestMetadata{
  930. Verb: types.APIVerbGet,
  931. Method: types.HTTPVerbGet,
  932. Path: &types.Path{
  933. Parent: basePath,
  934. RelativePath: relPath + "/pods",
  935. },
  936. Scopes: []types.PermissionScope{
  937. types.UserScope,
  938. types.ProjectScope,
  939. types.ClusterScope,
  940. },
  941. },
  942. )
  943. getPodsHandler := cluster.NewGetPodsHandler(
  944. config,
  945. factory.GetDecoderValidator(),
  946. factory.GetResultWriter(),
  947. )
  948. routes = append(routes, &router.Route{
  949. Endpoint: getPodsEndpoint,
  950. Handler: getPodsHandler,
  951. Router: r,
  952. })
  953. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents -> cluster.NewGetIncidentsHandler
  954. getIncidentsEndpoint := factory.NewAPIEndpoint(
  955. &types.APIRequestMetadata{
  956. Verb: types.APIVerbGet,
  957. Method: types.HTTPVerbGet,
  958. Path: &types.Path{
  959. Parent: basePath,
  960. RelativePath: relPath + "/incidents",
  961. },
  962. Scopes: []types.PermissionScope{
  963. types.UserScope,
  964. types.ProjectScope,
  965. types.ClusterScope,
  966. },
  967. },
  968. )
  969. getIncidentsHandler := cluster.NewGetIncidentsHandler(
  970. config,
  971. factory.GetDecoderValidator(),
  972. factory.GetResultWriter(),
  973. )
  974. routes = append(routes, &router.Route{
  975. Endpoint: getIncidentsEndpoint,
  976. Handler: getIncidentsHandler,
  977. Router: r,
  978. })
  979. // GET /api/projects/{project_id}/clusters/{cluster_id}/incidents/logs -> cluster.NewGetIncidentsHandler
  980. getIncidentEventLogsEndpoint := factory.NewAPIEndpoint(
  981. &types.APIRequestMetadata{
  982. Verb: types.APIVerbGet,
  983. Method: types.HTTPVerbGet,
  984. Path: &types.Path{
  985. Parent: basePath,
  986. RelativePath: relPath + "/incidents/logs",
  987. },
  988. Scopes: []types.PermissionScope{
  989. types.UserScope,
  990. types.ProjectScope,
  991. types.ClusterScope,
  992. },
  993. },
  994. )
  995. getIncidentEventLogsHandler := cluster.NewGetIncidentEventLogsHandler(
  996. config,
  997. factory.GetDecoderValidator(),
  998. factory.GetResultWriter(),
  999. )
  1000. routes = append(routes, &router.Route{
  1001. Endpoint: getIncidentEventLogsEndpoint,
  1002. Handler: getIncidentEventLogsHandler,
  1003. Router: r,
  1004. })
  1005. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_new -> cluster.NewNotifyNewIncidentHandler
  1006. notifyNewIncidentEndpoint := factory.NewAPIEndpoint(
  1007. &types.APIRequestMetadata{
  1008. Verb: types.APIVerbCreate,
  1009. Method: types.HTTPVerbPost,
  1010. Path: &types.Path{
  1011. Parent: basePath,
  1012. RelativePath: relPath + "/incidents/notify_new",
  1013. },
  1014. Scopes: []types.PermissionScope{
  1015. types.UserScope,
  1016. types.ProjectScope,
  1017. types.ClusterScope,
  1018. },
  1019. },
  1020. )
  1021. notifyNewIncidentHandler := cluster.NewNotifyNewIncidentHandler(
  1022. config,
  1023. factory.GetDecoderValidator(),
  1024. factory.GetResultWriter(),
  1025. )
  1026. routes = append(routes, &router.Route{
  1027. Endpoint: notifyNewIncidentEndpoint,
  1028. Handler: notifyNewIncidentHandler,
  1029. Router: r,
  1030. })
  1031. // POST /api/projects/{project_id}/clusters/{cluster_id}/incidents/notify_resolved -> cluster.NewNotifyResolvedIncidentHandler
  1032. notifyResolvedIncidentEndpoint := 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_resolved",
  1039. },
  1040. Scopes: []types.PermissionScope{
  1041. types.UserScope,
  1042. types.ProjectScope,
  1043. types.ClusterScope,
  1044. },
  1045. },
  1046. )
  1047. notifyResolvedIncidentHandler := cluster.NewNotifyResolvedIncidentHandler(
  1048. config,
  1049. factory.GetDecoderValidator(),
  1050. factory.GetResultWriter(),
  1051. )
  1052. routes = append(routes, &router.Route{
  1053. Endpoint: notifyResolvedIncidentEndpoint,
  1054. Handler: notifyResolvedIncidentHandler,
  1055. Router: r,
  1056. })
  1057. return routes, newPath
  1058. }