project.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/cluster"
  5. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  6. "github.com/porter-dev/porter/api/server/handlers/project"
  7. "github.com/porter-dev/porter/api/server/handlers/provision"
  8. "github.com/porter-dev/porter/api/server/handlers/registry"
  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/types"
  12. )
  13. func NewProjectScopedRegisterer(children ...*Registerer) *Registerer {
  14. return &Registerer{
  15. GetRoutes: GetProjectScopedRoutes,
  16. Children: children,
  17. }
  18. }
  19. func GetProjectScopedRoutes(
  20. r chi.Router,
  21. config *config.Config,
  22. basePath *types.Path,
  23. factory shared.APIEndpointFactory,
  24. children ...*Registerer,
  25. ) []*Route {
  26. routes, projPath := getProjectRoutes(r, config, basePath, factory)
  27. if len(children) > 0 {
  28. r.Route(projPath.RelativePath, func(r chi.Router) {
  29. for _, child := range children {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. }
  33. })
  34. }
  35. return routes
  36. }
  37. func getProjectRoutes(
  38. r chi.Router,
  39. config *config.Config,
  40. basePath *types.Path,
  41. factory shared.APIEndpointFactory,
  42. ) ([]*Route, *types.Path) {
  43. relPath := "/projects/{project_id}"
  44. newPath := &types.Path{
  45. Parent: basePath,
  46. RelativePath: relPath,
  47. }
  48. routes := make([]*Route, 0)
  49. // GET /api/projects/{project_id} -> project.NewProjectGetHandler
  50. getEndpoint := factory.NewAPIEndpoint(
  51. &types.APIRequestMetadata{
  52. Verb: types.APIVerbGet,
  53. Method: types.HTTPVerbGet,
  54. Path: &types.Path{
  55. Parent: basePath,
  56. RelativePath: relPath,
  57. },
  58. Scopes: []types.PermissionScope{
  59. types.UserScope,
  60. types.ProjectScope,
  61. },
  62. },
  63. )
  64. getHandler := project.NewProjectGetHandler(
  65. config,
  66. factory.GetResultWriter(),
  67. )
  68. routes = append(routes, &Route{
  69. Endpoint: getEndpoint,
  70. Handler: getHandler,
  71. Router: r,
  72. })
  73. // DELETE /api/projects/{project_id} -> project.NewProjectDeleteHandler
  74. deleteEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbDelete,
  77. Method: types.HTTPVerbDelete,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath,
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. },
  86. },
  87. )
  88. deleteHandler := project.NewProjectDeleteHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &Route{
  93. Endpoint: deleteEndpoint,
  94. Handler: deleteHandler,
  95. Router: r,
  96. })
  97. // GET /api/projects/{project_id}/policy -> project.NewProjectGetPolicyHandler
  98. getPolicyEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbGet,
  101. Method: types.HTTPVerbGet,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: relPath + "/policy",
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. },
  110. },
  111. )
  112. getPolicyHandler := project.NewProjectGetPolicyHandler(
  113. config,
  114. factory.GetResultWriter(),
  115. )
  116. routes = append(routes, &Route{
  117. Endpoint: getPolicyEndpoint,
  118. Handler: getPolicyHandler,
  119. Router: r,
  120. })
  121. // GET /api/projects/{project_id}/clusters -> cluster.NewClusterListHandler
  122. listClusterEndpoint := factory.NewAPIEndpoint(
  123. &types.APIRequestMetadata{
  124. Verb: types.APIVerbList,
  125. Method: types.HTTPVerbGet,
  126. Path: &types.Path{
  127. Parent: basePath,
  128. RelativePath: relPath + "/clusters",
  129. },
  130. Scopes: []types.PermissionScope{
  131. types.UserScope,
  132. types.ProjectScope,
  133. },
  134. },
  135. )
  136. listClusterHandler := cluster.NewClusterListHandler(
  137. config,
  138. factory.GetResultWriter(),
  139. )
  140. routes = append(routes, &Route{
  141. Endpoint: listClusterEndpoint,
  142. Handler: listClusterHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/gitrepos -> gitinstallation.NewGitRepoListHandler
  146. listGitReposEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbList,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/gitrepos",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. },
  158. },
  159. )
  160. listGitReposHandler := gitinstallation.NewGitRepoListHandler(
  161. config,
  162. factory.GetResultWriter(),
  163. )
  164. routes = append(routes, &Route{
  165. Endpoint: listGitReposEndpoint,
  166. Handler: listGitReposHandler,
  167. Router: r,
  168. })
  169. // GET /api/projects/{project_id}/collaborators -> project.NewCollaboratorsListHandler
  170. listCollaboratorsEndpoint := factory.NewAPIEndpoint(
  171. &types.APIRequestMetadata{
  172. Verb: types.APIVerbList,
  173. Method: types.HTTPVerbGet,
  174. Path: &types.Path{
  175. Parent: basePath,
  176. RelativePath: relPath + "/collaborators",
  177. },
  178. Scopes: []types.PermissionScope{
  179. types.UserScope,
  180. types.ProjectScope,
  181. },
  182. },
  183. )
  184. listCollaboratorsHandler := project.NewCollaboratorsListHandler(
  185. config,
  186. factory.GetResultWriter(),
  187. )
  188. routes = append(routes, &Route{
  189. Endpoint: listCollaboratorsEndpoint,
  190. Handler: listCollaboratorsHandler,
  191. Router: r,
  192. })
  193. // GET /api/projects/{project_id}/roles -> project.NewRolesListHandler
  194. listRolesEndpoint := factory.NewAPIEndpoint(
  195. &types.APIRequestMetadata{
  196. Verb: types.APIVerbList,
  197. Method: types.HTTPVerbGet,
  198. Path: &types.Path{
  199. Parent: basePath,
  200. RelativePath: relPath + "/roles",
  201. },
  202. Scopes: []types.PermissionScope{
  203. types.UserScope,
  204. types.ProjectScope,
  205. },
  206. },
  207. )
  208. listRolesHandler := project.NewRolesListHandler(
  209. config,
  210. factory.GetResultWriter(),
  211. )
  212. routes = append(routes, &Route{
  213. Endpoint: listRolesEndpoint,
  214. Handler: listRolesHandler,
  215. Router: r,
  216. })
  217. // POST /api/projects/{project_id}/roles -> project.NewRoleUpdateHandler
  218. updateRoleEndpoint := factory.NewAPIEndpoint(
  219. &types.APIRequestMetadata{
  220. Verb: types.APIVerbUpdate,
  221. Method: types.HTTPVerbPost,
  222. Path: &types.Path{
  223. Parent: basePath,
  224. RelativePath: relPath + "/roles",
  225. },
  226. Scopes: []types.PermissionScope{
  227. types.UserScope,
  228. types.ProjectScope,
  229. },
  230. },
  231. )
  232. updateRoleHandler := project.NewRoleUpdateHandler(
  233. config,
  234. factory.GetDecoderValidator(),
  235. factory.GetResultWriter(),
  236. )
  237. routes = append(routes, &Route{
  238. Endpoint: updateRoleEndpoint,
  239. Handler: updateRoleHandler,
  240. Router: r,
  241. })
  242. // DELETE /api/projects/{project_id}/roles -> project.NewRoleDeleteHandler
  243. deleteRoleEndpoint := factory.NewAPIEndpoint(
  244. &types.APIRequestMetadata{
  245. Verb: types.APIVerbDelete,
  246. Method: types.HTTPVerbDelete,
  247. Path: &types.Path{
  248. Parent: basePath,
  249. RelativePath: relPath + "/roles",
  250. },
  251. Scopes: []types.PermissionScope{
  252. types.UserScope,
  253. types.ProjectScope,
  254. },
  255. },
  256. )
  257. deleteRoleHandler := project.NewRoleDeleteHandler(
  258. config,
  259. factory.GetDecoderValidator(),
  260. factory.GetResultWriter(),
  261. )
  262. routes = append(routes, &Route{
  263. Endpoint: deleteRoleEndpoint,
  264. Handler: deleteRoleHandler,
  265. Router: r,
  266. })
  267. // GET /api/projects/{project_id}/registries -> registry.NewRegistryListHandler
  268. listRegistriesEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbList,
  271. Method: types.HTTPVerbGet,
  272. Path: &types.Path{
  273. Parent: basePath,
  274. RelativePath: relPath + "/registries",
  275. },
  276. Scopes: []types.PermissionScope{
  277. types.UserScope,
  278. types.ProjectScope,
  279. },
  280. },
  281. )
  282. listRegistriesHandler := registry.NewRegistryListHandler(
  283. config,
  284. factory.GetResultWriter(),
  285. )
  286. routes = append(routes, &Route{
  287. Endpoint: listRegistriesEndpoint,
  288. Handler: listRegistriesHandler,
  289. Router: r,
  290. })
  291. // POST /api/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
  292. createRegistryEndpoint := factory.NewAPIEndpoint(
  293. &types.APIRequestMetadata{
  294. Verb: types.APIVerbCreate,
  295. Method: types.HTTPVerbPost,
  296. Path: &types.Path{
  297. Parent: basePath,
  298. RelativePath: relPath + "/registries",
  299. },
  300. Scopes: []types.PermissionScope{
  301. types.UserScope,
  302. types.ProjectScope,
  303. },
  304. },
  305. )
  306. createRegistryHandler := registry.NewRegistryCreateHandler(
  307. config,
  308. factory.GetDecoderValidator(),
  309. factory.GetResultWriter(),
  310. )
  311. routes = append(routes, &Route{
  312. Endpoint: createRegistryEndpoint,
  313. Handler: createRegistryHandler,
  314. Router: r,
  315. })
  316. // GET /api/projects/{project_id}/registries/ecr/token -> registry.NewRegistryGetECRTokenHandler
  317. getECRTokenEndpoint := factory.NewAPIEndpoint(
  318. &types.APIRequestMetadata{
  319. Verb: types.APIVerbGet,
  320. Method: types.HTTPVerbGet,
  321. Path: &types.Path{
  322. Parent: basePath,
  323. RelativePath: relPath + "/registries/ecr/token",
  324. },
  325. Scopes: []types.PermissionScope{
  326. types.UserScope,
  327. types.ProjectScope,
  328. },
  329. },
  330. )
  331. getECRTokenHandler := registry.NewRegistryGetECRTokenHandler(
  332. config,
  333. factory.GetDecoderValidator(),
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &Route{
  337. Endpoint: getECRTokenEndpoint,
  338. Handler: getECRTokenHandler,
  339. Router: r,
  340. })
  341. // GET /api/projects/{project_id}/registries/docr/token -> registry.NewRegistryGetDOCRTokenHandler
  342. getDOCRTokenEndpoint := factory.NewAPIEndpoint(
  343. &types.APIRequestMetadata{
  344. Verb: types.APIVerbGet,
  345. Method: types.HTTPVerbGet,
  346. Path: &types.Path{
  347. Parent: basePath,
  348. RelativePath: relPath + "/registries/docr/token",
  349. },
  350. Scopes: []types.PermissionScope{
  351. types.UserScope,
  352. types.ProjectScope,
  353. },
  354. },
  355. )
  356. getDOCRTokenHandler := registry.NewRegistryGetDOCRTokenHandler(
  357. config,
  358. factory.GetDecoderValidator(),
  359. factory.GetResultWriter(),
  360. )
  361. routes = append(routes, &Route{
  362. Endpoint: getDOCRTokenEndpoint,
  363. Handler: getDOCRTokenHandler,
  364. Router: r,
  365. })
  366. // GET /api/projects/{project_id}/registries/gcr/token -> registry.NewRegistryGetGCRTokenHandler
  367. getGCRTokenEndpoint := factory.NewAPIEndpoint(
  368. &types.APIRequestMetadata{
  369. Verb: types.APIVerbGet,
  370. Method: types.HTTPVerbGet,
  371. Path: &types.Path{
  372. Parent: basePath,
  373. RelativePath: relPath + "/registries/gcr/token",
  374. },
  375. Scopes: []types.PermissionScope{
  376. types.UserScope,
  377. types.ProjectScope,
  378. },
  379. },
  380. )
  381. getGCRTokenHandler := registry.NewRegistryGetGCRTokenHandler(
  382. config,
  383. factory.GetDecoderValidator(),
  384. factory.GetResultWriter(),
  385. )
  386. routes = append(routes, &Route{
  387. Endpoint: getGCRTokenEndpoint,
  388. Handler: getGCRTokenHandler,
  389. Router: r,
  390. })
  391. // GET /api/projects/{project_id}/registries/dockerhub/token -> registry.NewRegistryGetDockerhubTokenHandler
  392. getDockerhubTokenEndpoint := factory.NewAPIEndpoint(
  393. &types.APIRequestMetadata{
  394. Verb: types.APIVerbGet,
  395. Method: types.HTTPVerbGet,
  396. Path: &types.Path{
  397. Parent: basePath,
  398. RelativePath: relPath + "/registries/dockerhub/token",
  399. },
  400. Scopes: []types.PermissionScope{
  401. types.UserScope,
  402. types.ProjectScope,
  403. },
  404. },
  405. )
  406. getDockerhubTokenHandler := registry.NewRegistryGetDockerhubTokenHandler(
  407. config,
  408. factory.GetDecoderValidator(),
  409. factory.GetResultWriter(),
  410. )
  411. routes = append(routes, &Route{
  412. Endpoint: getDockerhubTokenEndpoint,
  413. Handler: getDockerhubTokenHandler,
  414. Router: r,
  415. })
  416. // POST /api/projects/{project_id}/provision/ecr -> provision.NewProvisionECRHandler
  417. provisionECREndpoint := factory.NewAPIEndpoint(
  418. &types.APIRequestMetadata{
  419. Verb: types.APIVerbCreate,
  420. Method: types.HTTPVerbPost,
  421. Path: &types.Path{
  422. Parent: basePath,
  423. RelativePath: relPath + "/provision/ecr",
  424. },
  425. Scopes: []types.PermissionScope{
  426. types.UserScope,
  427. types.ProjectScope,
  428. },
  429. },
  430. )
  431. provisionECRHandler := provision.NewProvisionECRHandler(
  432. config,
  433. factory.GetDecoderValidator(),
  434. factory.GetResultWriter(),
  435. )
  436. routes = append(routes, &Route{
  437. Endpoint: provisionECREndpoint,
  438. Handler: provisionECRHandler,
  439. Router: r,
  440. })
  441. // POST /api/projects/{project_id}/provision/eks -> provision.NewProvisionEKSHandler
  442. provisionEKSEndpoint := factory.NewAPIEndpoint(
  443. &types.APIRequestMetadata{
  444. Verb: types.APIVerbCreate,
  445. Method: types.HTTPVerbPost,
  446. Path: &types.Path{
  447. Parent: basePath,
  448. RelativePath: relPath + "/provision/eks",
  449. },
  450. Scopes: []types.PermissionScope{
  451. types.UserScope,
  452. types.ProjectScope,
  453. },
  454. },
  455. )
  456. provisionEKSHandler := provision.NewProvisionEKSHandler(
  457. config,
  458. factory.GetDecoderValidator(),
  459. factory.GetResultWriter(),
  460. )
  461. routes = append(routes, &Route{
  462. Endpoint: provisionEKSEndpoint,
  463. Handler: provisionEKSHandler,
  464. Router: r,
  465. })
  466. // POST /api/projects/{project_id}/provision/docr -> provision.NewProvisionDOCRHandler
  467. provisionDOCREndpoint := factory.NewAPIEndpoint(
  468. &types.APIRequestMetadata{
  469. Verb: types.APIVerbCreate,
  470. Method: types.HTTPVerbPost,
  471. Path: &types.Path{
  472. Parent: basePath,
  473. RelativePath: relPath + "/provision/docr",
  474. },
  475. Scopes: []types.PermissionScope{
  476. types.UserScope,
  477. types.ProjectScope,
  478. },
  479. },
  480. )
  481. provisionDOCRHandler := provision.NewProvisionDOCRHandler(
  482. config,
  483. factory.GetDecoderValidator(),
  484. factory.GetResultWriter(),
  485. )
  486. routes = append(routes, &Route{
  487. Endpoint: provisionDOCREndpoint,
  488. Handler: provisionDOCRHandler,
  489. Router: r,
  490. })
  491. // POST /api/projects/{project_id}/provision/doks -> provision.NewProvisionDOKSHandler
  492. provisionDOKSEndpoint := factory.NewAPIEndpoint(
  493. &types.APIRequestMetadata{
  494. Verb: types.APIVerbCreate,
  495. Method: types.HTTPVerbPost,
  496. Path: &types.Path{
  497. Parent: basePath,
  498. RelativePath: relPath + "/provision/doks",
  499. },
  500. Scopes: []types.PermissionScope{
  501. types.UserScope,
  502. types.ProjectScope,
  503. },
  504. },
  505. )
  506. provisionDOKSHandler := provision.NewProvisionDOKSHandler(
  507. config,
  508. factory.GetDecoderValidator(),
  509. factory.GetResultWriter(),
  510. )
  511. routes = append(routes, &Route{
  512. Endpoint: provisionDOKSEndpoint,
  513. Handler: provisionDOKSHandler,
  514. Router: r,
  515. })
  516. // POST /api/projects/{project_id}/provision/gcr -> provision.NewProvisionGCRHandler
  517. provisionGCREndpoint := factory.NewAPIEndpoint(
  518. &types.APIRequestMetadata{
  519. Verb: types.APIVerbCreate,
  520. Method: types.HTTPVerbPost,
  521. Path: &types.Path{
  522. Parent: basePath,
  523. RelativePath: relPath + "/provision/gcr",
  524. },
  525. Scopes: []types.PermissionScope{
  526. types.UserScope,
  527. types.ProjectScope,
  528. },
  529. },
  530. )
  531. provisionGCRHandler := provision.NewProvisionGCRHandler(
  532. config,
  533. factory.GetDecoderValidator(),
  534. factory.GetResultWriter(),
  535. )
  536. routes = append(routes, &Route{
  537. Endpoint: provisionGCREndpoint,
  538. Handler: provisionGCRHandler,
  539. Router: r,
  540. })
  541. // POST /api/projects/{project_id}/provision/gke -> provision.NewProvisionGKEHandler
  542. provisionGKEEndpoint := factory.NewAPIEndpoint(
  543. &types.APIRequestMetadata{
  544. Verb: types.APIVerbCreate,
  545. Method: types.HTTPVerbPost,
  546. Path: &types.Path{
  547. Parent: basePath,
  548. RelativePath: relPath + "/provision/gke",
  549. },
  550. Scopes: []types.PermissionScope{
  551. types.UserScope,
  552. types.ProjectScope,
  553. },
  554. },
  555. )
  556. provisionGKEHandler := provision.NewProvisionGKEHandler(
  557. config,
  558. factory.GetDecoderValidator(),
  559. factory.GetResultWriter(),
  560. )
  561. routes = append(routes, &Route{
  562. Endpoint: provisionGKEEndpoint,
  563. Handler: provisionGKEHandler,
  564. Router: r,
  565. })
  566. return routes, newPath
  567. }