porter_app.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewPorterAppScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetPorterAppScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetPorterAppScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getPorterAppRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getPorterAppRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/applications"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. var routes []*router.Route
  47. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/{name} -> porter_app.NewPorterAppGetHandler
  48. getPorterAppEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamPorterAppName),
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. },
  61. },
  62. )
  63. getPorterAppHandler := porter_app.NewGetPorterAppHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: getPorterAppEndpoint,
  69. Handler: getPorterAppHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/{name}/releases/{version} -> porter_app.NewPorterAppReleaseGetHandler
  73. getPorterAppHelmReleaseEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbList,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: fmt.Sprintf("%s/{%s}/releases/{%s}", relPath, types.URLParamPorterAppName, types.URLParamReleaseVersion),
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. },
  86. },
  87. )
  88. getPorterAppHelmReleaseHandler := porter_app.NewPorterAppHelmReleaseGetHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &router.Route{
  93. Endpoint: getPorterAppHelmReleaseEndpoint,
  94. Handler: getPorterAppHelmReleaseHandler,
  95. Router: r,
  96. })
  97. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/{name}/release-history -> porter_app.NewPorterAppHelmReleaseHistoryGetHandler
  98. getPorterAppHelmReleaseHistoryGetEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbList,
  101. Method: types.HTTPVerbGet,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: fmt.Sprintf("%s/{%s}/release-history", relPath, types.URLParamPorterAppName),
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. types.ClusterScope,
  110. },
  111. },
  112. )
  113. getPorterAppHelmReleaseHistoryGetHandler := porter_app.NewPorterAppHelmReleaseHistoryGetHandler(
  114. config,
  115. factory.GetResultWriter(),
  116. )
  117. routes = append(routes, &router.Route{
  118. Endpoint: getPorterAppHelmReleaseHistoryGetEndpoint,
  119. Handler: getPorterAppHelmReleaseHistoryGetHandler,
  120. Router: r,
  121. })
  122. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/{name}/releases/{version}/pods/all -> porter_app.NewPorterAppPodsGetHandler
  123. getPorterAppPodsEndpoint := factory.NewAPIEndpoint(
  124. &types.APIRequestMetadata{
  125. Verb: types.APIVerbList,
  126. Method: types.HTTPVerbGet,
  127. Path: &types.Path{
  128. Parent: basePath,
  129. RelativePath: fmt.Sprintf("%s/{%s}/releases/{%s}/pods/all", relPath, types.URLParamPorterAppName, types.URLParamReleaseVersion),
  130. },
  131. Scopes: []types.PermissionScope{
  132. types.UserScope,
  133. types.ProjectScope,
  134. types.ClusterScope,
  135. },
  136. },
  137. )
  138. getPorterAppPodsHandler := porter_app.NewPorterAppPodsGetHandler(
  139. config,
  140. factory.GetResultWriter(),
  141. )
  142. routes = append(routes, &router.Route{
  143. Endpoint: getPorterAppPodsEndpoint,
  144. Handler: getPorterAppPodsHandler,
  145. Router: r,
  146. })
  147. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications -> porter_app.NewPorterAppListHandler
  148. listPorterAppEndpoint := factory.NewAPIEndpoint(
  149. &types.APIRequestMetadata{
  150. Verb: types.APIVerbList,
  151. Method: types.HTTPVerbGet,
  152. Path: &types.Path{
  153. Parent: basePath,
  154. RelativePath: relPath,
  155. },
  156. Scopes: []types.PermissionScope{
  157. types.UserScope,
  158. types.ProjectScope,
  159. types.ClusterScope,
  160. },
  161. },
  162. )
  163. listPorterAppHandler := porter_app.NewPorterAppListHandler(
  164. config,
  165. factory.GetResultWriter(),
  166. )
  167. routes = append(routes, &router.Route{
  168. Endpoint: listPorterAppEndpoint,
  169. Handler: listPorterAppHandler,
  170. Router: r,
  171. })
  172. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name} -> release.NewDeletePorterAppByNameHandler
  173. deletePorterAppByNameEndpoint := factory.NewAPIEndpoint(
  174. &types.APIRequestMetadata{
  175. Verb: types.APIVerbDelete,
  176. Method: types.HTTPVerbDelete,
  177. Path: &types.Path{
  178. Parent: basePath,
  179. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamPorterAppName),
  180. },
  181. Scopes: []types.PermissionScope{
  182. types.UserScope,
  183. types.ProjectScope,
  184. types.ClusterScope,
  185. },
  186. },
  187. )
  188. deletePorterAppByNameHandler := porter_app.NewDeletePorterAppByNameHandler(
  189. config,
  190. factory.GetDecoderValidator(),
  191. factory.GetResultWriter(),
  192. )
  193. routes = append(routes, &router.Route{
  194. Endpoint: deletePorterAppByNameEndpoint,
  195. Handler: deletePorterAppByNameHandler,
  196. Router: r,
  197. })
  198. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name} -> porter_app.NewCreatePorterAppHandler
  199. createPorterAppEndpoint := factory.NewAPIEndpoint(
  200. &types.APIRequestMetadata{
  201. Verb: types.APIVerbCreate,
  202. Method: types.HTTPVerbPost,
  203. Path: &types.Path{
  204. Parent: basePath,
  205. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamPorterAppName),
  206. },
  207. Scopes: []types.PermissionScope{
  208. types.UserScope,
  209. types.ProjectScope,
  210. types.ClusterScope,
  211. },
  212. },
  213. )
  214. createPorterAppHandler := porter_app.NewCreatePorterAppHandler(
  215. config,
  216. factory.GetDecoderValidator(),
  217. factory.GetResultWriter(),
  218. )
  219. routes = append(routes, &router.Route{
  220. Endpoint: createPorterAppEndpoint,
  221. Handler: createPorterAppHandler,
  222. Router: r,
  223. })
  224. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name}/rollback -> porter_app.NewRollbackPorterAppHandler
  225. rollbackPorterAppEndpoint := factory.NewAPIEndpoint(
  226. &types.APIRequestMetadata{
  227. Verb: types.APIVerbCreate,
  228. Method: types.HTTPVerbPost,
  229. Path: &types.Path{
  230. Parent: basePath,
  231. RelativePath: fmt.Sprintf("%s/{%s}/rollback", relPath, types.URLParamPorterAppName),
  232. },
  233. Scopes: []types.PermissionScope{
  234. types.UserScope,
  235. types.ProjectScope,
  236. types.ClusterScope,
  237. },
  238. },
  239. )
  240. rollbackPorterAppHandler := porter_app.NewRollbackPorterAppHandler(
  241. config,
  242. factory.GetDecoderValidator(),
  243. factory.GetResultWriter(),
  244. )
  245. routes = append(routes, &router.Route{
  246. Endpoint: rollbackPorterAppEndpoint,
  247. Handler: rollbackPorterAppHandler,
  248. Router: r,
  249. })
  250. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name}/pr -> porter_app.NewOpenStackPRHandler
  251. createSecretAndOpenGitHubPullRequestEndpoint := factory.NewAPIEndpoint(
  252. &types.APIRequestMetadata{
  253. Verb: types.APIVerbCreate,
  254. Method: types.HTTPVerbPost,
  255. Path: &types.Path{
  256. Parent: basePath,
  257. RelativePath: fmt.Sprintf("%s/{%s}/pr", relPath, types.URLParamPorterAppName),
  258. },
  259. Scopes: []types.PermissionScope{
  260. types.UserScope,
  261. types.ProjectScope,
  262. types.ClusterScope,
  263. },
  264. },
  265. )
  266. createSecretAndOpenGitHubPullRequestHandler := porter_app.NewOpenStackPRHandler(
  267. config,
  268. factory.GetDecoderValidator(),
  269. factory.GetResultWriter(),
  270. )
  271. routes = append(routes, &router.Route{
  272. Endpoint: createSecretAndOpenGitHubPullRequestEndpoint,
  273. Handler: createSecretAndOpenGitHubPullRequestHandler,
  274. Router: r,
  275. })
  276. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name}/events -> porter_app.NewPorterAppEventListHandler
  277. listPorterAppEventsEndpoint := factory.NewAPIEndpoint(
  278. &types.APIRequestMetadata{
  279. Verb: types.APIVerbList,
  280. Method: types.HTTPVerbGet,
  281. Path: &types.Path{
  282. Parent: basePath,
  283. RelativePath: fmt.Sprintf("%s/{%s}/events", relPath, types.URLParamPorterAppName),
  284. },
  285. Scopes: []types.PermissionScope{
  286. types.UserScope,
  287. types.ProjectScope,
  288. types.ClusterScope,
  289. },
  290. },
  291. )
  292. listPorterAppEventsHandler := porter_app.NewPorterAppEventListHandler(
  293. config,
  294. factory.GetResultWriter(),
  295. )
  296. routes = append(routes, &router.Route{
  297. Endpoint: listPorterAppEventsEndpoint,
  298. Handler: listPorterAppEventsHandler,
  299. Router: r,
  300. })
  301. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/{name}/events -> porter_app.NewCreatePorterAppEventHandler
  302. createPorterAppEventEndpoint := factory.NewAPIEndpoint(
  303. &types.APIRequestMetadata{
  304. Verb: types.APIVerbCreate,
  305. Method: types.HTTPVerbPost,
  306. Path: &types.Path{
  307. Parent: basePath,
  308. RelativePath: fmt.Sprintf("%s/{%s}/events", relPath, types.URLParamPorterAppName),
  309. },
  310. Scopes: []types.PermissionScope{
  311. types.UserScope,
  312. types.ProjectScope,
  313. types.ClusterScope,
  314. },
  315. },
  316. )
  317. createPorterAppEventHandler := porter_app.NewCreateUpdatePorterAppEventHandler(
  318. config,
  319. factory.GetDecoderValidator(),
  320. factory.GetResultWriter(),
  321. )
  322. routes = append(routes, &router.Route{
  323. Endpoint: createPorterAppEventEndpoint,
  324. Handler: createPorterAppEventHandler,
  325. Router: r,
  326. })
  327. // GET /api/projects/{project_id}/clusters/{cluster_id}/events/id -> porter_app.NewGetPorterAppEventHandler
  328. getPorterAppEventEndpoint := factory.NewAPIEndpoint(
  329. &types.APIRequestMetadata{
  330. Verb: types.APIVerbCreate,
  331. Method: types.HTTPVerbGet,
  332. Path: &types.Path{
  333. Parent: basePath,
  334. RelativePath: fmt.Sprintf("/events/{%s}", types.URLParamPorterAppEventID),
  335. },
  336. Scopes: []types.PermissionScope{
  337. types.UserScope,
  338. types.ProjectScope,
  339. types.ClusterScope,
  340. },
  341. },
  342. )
  343. getPorterAppEventHandler := porter_app.NewGetPorterAppEventHandler(
  344. config,
  345. factory.GetResultWriter(),
  346. )
  347. routes = append(routes, &router.Route{
  348. Endpoint: getPorterAppEventEndpoint,
  349. Handler: getPorterAppEventHandler,
  350. Router: r,
  351. })
  352. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/analytics -> porter_app.NewPorterAppAnalyticsHandler
  353. porterAppAnalyticsEndpoint := factory.NewAPIEndpoint(
  354. &types.APIRequestMetadata{
  355. Verb: types.APIVerbUpdate,
  356. Method: types.HTTPVerbPost,
  357. Path: &types.Path{
  358. Parent: basePath,
  359. RelativePath: fmt.Sprintf("%s/analytics", relPath),
  360. },
  361. Scopes: []types.PermissionScope{
  362. types.UserScope,
  363. types.ProjectScope,
  364. types.ClusterScope,
  365. },
  366. },
  367. )
  368. porterAppAnalyticsHandler := porter_app.NewPorterAppAnalyticsHandler(
  369. config,
  370. factory.GetDecoderValidator(),
  371. factory.GetResultWriter(),
  372. )
  373. routes = append(routes, &router.Route{
  374. Endpoint: porterAppAnalyticsEndpoint,
  375. Handler: porterAppAnalyticsHandler,
  376. Router: r,
  377. })
  378. // GET /api/projects/{project_id}/clusters/{cluster_id}/applications/logs -> cluster.NewGetChartLogsWithinTimeRangeHandler
  379. getChartLogsWithinTimeRangeEndpoint := factory.NewAPIEndpoint(
  380. &types.APIRequestMetadata{
  381. Verb: types.APIVerbGet,
  382. Method: types.HTTPVerbGet,
  383. Path: &types.Path{
  384. Parent: basePath,
  385. RelativePath: fmt.Sprintf("%s/logs", relPath),
  386. },
  387. Scopes: []types.PermissionScope{
  388. types.UserScope,
  389. types.ProjectScope,
  390. types.ClusterScope,
  391. },
  392. },
  393. )
  394. getChartLogsWithinTimeRangeHandler := porter_app.NewGetLogsWithinTimeRangeHandler(
  395. config,
  396. factory.GetDecoderValidator(),
  397. factory.GetResultWriter(),
  398. )
  399. routes = append(routes, &router.Route{
  400. Endpoint: getChartLogsWithinTimeRangeEndpoint,
  401. Handler: getChartLogsWithinTimeRangeHandler,
  402. Router: r,
  403. })
  404. // POST /api/projects/{project_id}/clusters/{cluster_id}/applications/{porter_app_name}/run -> porter_app.NewRunPorterAppCommandHandler
  405. runPorterAppCommandEndpoint := factory.NewAPIEndpoint(
  406. &types.APIRequestMetadata{
  407. Verb: types.APIVerbCreate,
  408. Method: types.HTTPVerbPost,
  409. Path: &types.Path{
  410. Parent: basePath,
  411. RelativePath: fmt.Sprintf("%s/{%s}/run", relPath, types.URLParamPorterAppName),
  412. },
  413. Scopes: []types.PermissionScope{
  414. types.UserScope,
  415. types.ProjectScope,
  416. types.ClusterScope,
  417. },
  418. },
  419. )
  420. runPorterAppCommandHandler := porter_app.NewRunPorterAppCommandHandler(
  421. config,
  422. factory.GetDecoderValidator(),
  423. factory.GetResultWriter(),
  424. )
  425. routes = append(routes, &router.Route{
  426. Endpoint: runPorterAppCommandEndpoint,
  427. Handler: runPorterAppCommandHandler,
  428. Router: r,
  429. })
  430. // TODO: remove these three endpoints once these three 'stacks' routes are no longer used in telemetry
  431. // GET /api/projects/{project_id}/clusters/{cluster_id}/stacks/{name} -> porter_app.NewPorterAppGetHandler
  432. LEGACY_getPorterAppEndpoint := factory.NewAPIEndpoint(
  433. &types.APIRequestMetadata{
  434. Verb: types.APIVerbGet,
  435. Method: types.HTTPVerbGet,
  436. Path: &types.Path{
  437. Parent: basePath,
  438. RelativePath: fmt.Sprintf("/stacks/{%s}", types.URLParamPorterAppName),
  439. },
  440. Scopes: []types.PermissionScope{
  441. types.UserScope,
  442. types.ProjectScope,
  443. types.ClusterScope,
  444. },
  445. },
  446. )
  447. LEGACY_getPorterAppHandler := porter_app.NewGetPorterAppHandler(
  448. config,
  449. factory.GetResultWriter(),
  450. )
  451. routes = append(routes, &router.Route{
  452. Endpoint: LEGACY_getPorterAppEndpoint,
  453. Handler: LEGACY_getPorterAppHandler,
  454. Router: r,
  455. })
  456. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{porter_app_name} -> porter_app.NewCreatePorterAppHandler
  457. LEGACY_createPorterAppEndpoint := factory.NewAPIEndpoint(
  458. &types.APIRequestMetadata{
  459. Verb: types.APIVerbCreate,
  460. Method: types.HTTPVerbPost,
  461. Path: &types.Path{
  462. Parent: basePath,
  463. RelativePath: fmt.Sprintf("/stacks/{%s}", types.URLParamPorterAppName),
  464. },
  465. Scopes: []types.PermissionScope{
  466. types.UserScope,
  467. types.ProjectScope,
  468. types.ClusterScope,
  469. },
  470. },
  471. )
  472. LEGACY_createPorterAppHandler := porter_app.NewCreatePorterAppHandler(
  473. config,
  474. factory.GetDecoderValidator(),
  475. factory.GetResultWriter(),
  476. )
  477. routes = append(routes, &router.Route{
  478. Endpoint: LEGACY_createPorterAppEndpoint,
  479. Handler: LEGACY_createPorterAppHandler,
  480. Router: r,
  481. })
  482. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{name}/events -> porter_app.NewCreatePorterAppEventHandler
  483. LEGACY_createPorterAppEventEndpoint := factory.NewAPIEndpoint(
  484. &types.APIRequestMetadata{
  485. Verb: types.APIVerbCreate,
  486. Method: types.HTTPVerbPost,
  487. Path: &types.Path{
  488. Parent: basePath,
  489. RelativePath: fmt.Sprintf("/stacks/{%s}/events", types.URLParamPorterAppName),
  490. },
  491. Scopes: []types.PermissionScope{
  492. types.UserScope,
  493. types.ProjectScope,
  494. types.ClusterScope,
  495. },
  496. },
  497. )
  498. LEGACY_createPorterAppEventHandler := porter_app.NewCreateUpdatePorterAppEventHandler(
  499. config,
  500. factory.GetDecoderValidator(),
  501. factory.GetResultWriter(),
  502. )
  503. routes = append(routes, &router.Route{
  504. Endpoint: LEGACY_createPorterAppEventEndpoint,
  505. Handler: LEGACY_createPorterAppEventHandler,
  506. Router: r,
  507. })
  508. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/parse -> porter_app.NewParsePorterYAMLToProtoHandler
  509. parsePorterYAMLToProtoEndpoint := factory.NewAPIEndpoint(
  510. &types.APIRequestMetadata{
  511. Verb: types.APIVerbGet,
  512. Method: types.HTTPVerbPost,
  513. Path: &types.Path{
  514. Parent: basePath,
  515. RelativePath: "/apps/parse",
  516. },
  517. Scopes: []types.PermissionScope{
  518. types.UserScope,
  519. types.ProjectScope,
  520. types.ClusterScope,
  521. },
  522. },
  523. )
  524. parsePorterYAMLToProtoHandler := porter_app.NewParsePorterYAMLToProtoHandler(
  525. config,
  526. factory.GetDecoderValidator(),
  527. factory.GetResultWriter(),
  528. )
  529. routes = append(routes, &router.Route{
  530. Endpoint: parsePorterYAMLToProtoEndpoint,
  531. Handler: parsePorterYAMLToProtoHandler,
  532. Router: r,
  533. })
  534. // POST /api/projects/{project_id}/clusters/{cluster_id}/apps/validate -> porter_app.NewValidatePorterAppHandler
  535. validatePorterAppEndpoint := factory.NewAPIEndpoint(
  536. &types.APIRequestMetadata{
  537. Verb: types.APIVerbGet,
  538. Method: types.HTTPVerbPost,
  539. Path: &types.Path{
  540. Parent: basePath,
  541. RelativePath: "/apps/validate",
  542. },
  543. Scopes: []types.PermissionScope{
  544. types.UserScope,
  545. types.ProjectScope,
  546. types.ClusterScope,
  547. },
  548. },
  549. )
  550. validatePorterAppHandler := porter_app.NewValidatePorterAppHandler(
  551. config,
  552. factory.GetDecoderValidator(),
  553. factory.GetResultWriter(),
  554. )
  555. routes = append(routes, &router.Route{
  556. Endpoint: validatePorterAppEndpoint,
  557. Handler: validatePorterAppHandler,
  558. Router: r,
  559. })
  560. // POST /api/projects/{project_id}/clusters/{cluster_id}/apps/create -> porter_app.NewCreateAppHandler
  561. createAppEndpoint := factory.NewAPIEndpoint(
  562. &types.APIRequestMetadata{
  563. Verb: types.APIVerbCreate,
  564. Method: types.HTTPVerbPost,
  565. Path: &types.Path{
  566. Parent: basePath,
  567. RelativePath: "/apps/create",
  568. },
  569. Scopes: []types.PermissionScope{
  570. types.UserScope,
  571. types.ProjectScope,
  572. types.ClusterScope,
  573. },
  574. },
  575. )
  576. createAppHandler := porter_app.NewCreateAppHandler(
  577. config,
  578. factory.GetDecoderValidator(),
  579. factory.GetResultWriter(),
  580. )
  581. routes = append(routes, &router.Route{
  582. Endpoint: createAppEndpoint,
  583. Handler: createAppHandler,
  584. Router: r,
  585. })
  586. // POST /api/projects/{project_id}/clusters/{cluster_id}/apps/apply -> porter_app.NewApplyPorterAppHandler
  587. applyPorterAppEndpoint := factory.NewAPIEndpoint(
  588. &types.APIRequestMetadata{
  589. Verb: types.APIVerbUpdate,
  590. Method: types.HTTPVerbPost,
  591. Path: &types.Path{
  592. Parent: basePath,
  593. RelativePath: "/apps/apply",
  594. },
  595. Scopes: []types.PermissionScope{
  596. types.UserScope,
  597. types.ProjectScope,
  598. types.ClusterScope,
  599. },
  600. },
  601. )
  602. applyPorterAppHandler := porter_app.NewApplyPorterAppHandler(
  603. config,
  604. factory.GetDecoderValidator(),
  605. factory.GetResultWriter(),
  606. )
  607. routes = append(routes, &router.Route{
  608. Endpoint: applyPorterAppEndpoint,
  609. Handler: applyPorterAppHandler,
  610. Router: r,
  611. })
  612. // GET /api/projects/{project_id}/clusters/{cluster_id}/default-deployment-target -> porter_app.NewDefaultDeploymentTargetHandler
  613. defaultDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  614. &types.APIRequestMetadata{
  615. Verb: types.APIVerbGet,
  616. Method: types.HTTPVerbGet,
  617. Path: &types.Path{
  618. Parent: basePath,
  619. RelativePath: "/default-deployment-target",
  620. },
  621. Scopes: []types.PermissionScope{
  622. types.UserScope,
  623. types.ProjectScope,
  624. types.ClusterScope,
  625. },
  626. },
  627. )
  628. defaultDeploymentTargetHandler := porter_app.NewDefaultDeploymentTargetHandler(
  629. config,
  630. factory.GetDecoderValidator(),
  631. factory.GetResultWriter(),
  632. )
  633. routes = append(routes, &router.Route{
  634. Endpoint: defaultDeploymentTargetEndpoint,
  635. Handler: defaultDeploymentTargetHandler,
  636. Router: r,
  637. })
  638. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/latest -> porter_app.NewCurrentAppRevisionHandler
  639. currentAppRevisionEndpoint := factory.NewAPIEndpoint(
  640. &types.APIRequestMetadata{
  641. Verb: types.APIVerbGet,
  642. Method: types.HTTPVerbGet,
  643. Path: &types.Path{
  644. Parent: basePath,
  645. RelativePath: fmt.Sprintf("/apps/{%s}/latest", types.URLParamPorterAppName),
  646. },
  647. Scopes: []types.PermissionScope{
  648. types.UserScope,
  649. types.ProjectScope,
  650. types.ClusterScope,
  651. },
  652. },
  653. )
  654. currentAppRevisionHandler := porter_app.NewLatestAppRevisionHandler(
  655. config,
  656. factory.GetDecoderValidator(),
  657. factory.GetResultWriter(),
  658. )
  659. routes = append(routes, &router.Route{
  660. Endpoint: currentAppRevisionEndpoint,
  661. Handler: currentAppRevisionHandler,
  662. Router: r,
  663. })
  664. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/revisions -> porter_app.NewCurrentAppRevisionHandler
  665. listAppRevisionsEndpoint := factory.NewAPIEndpoint(
  666. &types.APIRequestMetadata{
  667. Verb: types.APIVerbGet,
  668. Method: types.HTTPVerbGet,
  669. Path: &types.Path{
  670. Parent: basePath,
  671. RelativePath: fmt.Sprintf("/apps/{%s}/revisions", types.URLParamPorterAppName),
  672. },
  673. Scopes: []types.PermissionScope{
  674. types.UserScope,
  675. types.ProjectScope,
  676. types.ClusterScope,
  677. },
  678. },
  679. )
  680. listAppRevisionsHandler := porter_app.NewListAppRevisionsHandler(
  681. config,
  682. factory.GetDecoderValidator(),
  683. factory.GetResultWriter(),
  684. )
  685. routes = append(routes, &router.Route{
  686. Endpoint: listAppRevisionsEndpoint,
  687. Handler: listAppRevisionsHandler,
  688. Router: r,
  689. })
  690. // POST /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/subdomain -> porter_app.NewCreateSubdomainHandler
  691. createSubdomainEndpoint := factory.NewAPIEndpoint(
  692. &types.APIRequestMetadata{
  693. Verb: types.APIVerbUpdate,
  694. Method: types.HTTPVerbPost,
  695. Path: &types.Path{
  696. Parent: basePath,
  697. RelativePath: fmt.Sprintf("/apps/{%s}/subdomain", types.URLParamPorterAppName),
  698. },
  699. Scopes: []types.PermissionScope{
  700. types.UserScope,
  701. types.ProjectScope,
  702. types.ClusterScope,
  703. },
  704. },
  705. )
  706. createSubdomainHandler := porter_app.NewCreateSubdomainHandler(
  707. config,
  708. factory.GetDecoderValidator(),
  709. factory.GetResultWriter(),
  710. )
  711. routes = append(routes, &router.Route{
  712. Endpoint: createSubdomainEndpoint,
  713. Handler: createSubdomainHandler,
  714. Router: r,
  715. })
  716. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/{porter_app_name}/{app_revision_id}/predeploy-status -> porter_app.NewPredeployStatusHandler
  717. predeployStatusEndpoint := factory.NewAPIEndpoint(
  718. &types.APIRequestMetadata{
  719. Verb: types.APIVerbGet,
  720. Method: types.HTTPVerbGet,
  721. Path: &types.Path{
  722. Parent: basePath,
  723. RelativePath: fmt.Sprintf("/apps/{%s}/{%s}/predeploy-status", types.URLParamPorterAppName, types.URLParamAppRevisionID),
  724. },
  725. Scopes: []types.PermissionScope{
  726. types.UserScope,
  727. types.ProjectScope,
  728. types.ClusterScope,
  729. },
  730. },
  731. )
  732. predeployStatusHandler := porter_app.NewPredeployStatusHandler(
  733. config,
  734. factory.GetDecoderValidator(),
  735. factory.GetResultWriter(),
  736. )
  737. routes = append(routes, &router.Route{
  738. Endpoint: predeployStatusEndpoint,
  739. Handler: predeployStatusHandler,
  740. Router: r,
  741. })
  742. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/logs -> cluster.NewAppLogsHandler
  743. appLogsEndpoint := factory.NewAPIEndpoint(
  744. &types.APIRequestMetadata{
  745. Verb: types.APIVerbGet,
  746. Method: types.HTTPVerbGet,
  747. Path: &types.Path{
  748. Parent: basePath,
  749. RelativePath: "/apps/logs",
  750. },
  751. Scopes: []types.PermissionScope{
  752. types.UserScope,
  753. types.ProjectScope,
  754. types.ClusterScope,
  755. },
  756. },
  757. )
  758. appLogsHandler := porter_app.NewAppLogsHandler(
  759. config,
  760. factory.GetDecoderValidator(),
  761. factory.GetResultWriter(),
  762. )
  763. routes = append(routes, &router.Route{
  764. Endpoint: appLogsEndpoint,
  765. Handler: appLogsHandler,
  766. Router: r,
  767. })
  768. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/logs/loki -> namespace.NewStreamLogsLokiHandler
  769. streamLogsLokiEndpoint := factory.NewAPIEndpoint(
  770. &types.APIRequestMetadata{
  771. Verb: types.APIVerbGet,
  772. Method: types.HTTPVerbGet,
  773. Path: &types.Path{
  774. Parent: basePath,
  775. RelativePath: "/apps/logs/loki",
  776. },
  777. Scopes: []types.PermissionScope{
  778. types.UserScope,
  779. types.ProjectScope,
  780. types.ClusterScope,
  781. },
  782. IsWebsocket: true,
  783. },
  784. )
  785. streamLogsLokiHandler := porter_app.NewStreamLogsLokiHandler(
  786. config,
  787. factory.GetDecoderValidator(),
  788. factory.GetResultWriter(),
  789. )
  790. routes = append(routes, &router.Route{
  791. Endpoint: streamLogsLokiEndpoint,
  792. Handler: streamLogsLokiHandler,
  793. Router: r,
  794. })
  795. // GET /api/projects/{project_id}/clusters/{cluster_id}/apps/metrics -> cluster.NewGetPodMetricsHandler
  796. appMetricsEndpoint := factory.NewAPIEndpoint(
  797. &types.APIRequestMetadata{
  798. Verb: types.APIVerbGet,
  799. Method: types.HTTPVerbGet,
  800. Path: &types.Path{
  801. Parent: basePath,
  802. RelativePath: "/apps/metrics",
  803. },
  804. Scopes: []types.PermissionScope{
  805. types.UserScope,
  806. types.ProjectScope,
  807. types.ClusterScope,
  808. },
  809. },
  810. )
  811. appMetricsHandler := porter_app.NewAppMetricsHandler(
  812. config,
  813. factory.GetDecoderValidator(),
  814. factory.GetResultWriter(),
  815. )
  816. routes = append(routes, &router.Route{
  817. Endpoint: appMetricsEndpoint,
  818. Handler: appMetricsHandler,
  819. Router: r,
  820. })
  821. return routes, newPath
  822. }