project.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. // GET /api/projects/{project_id}/policy -> project.NewProjectGetPolicyHandler
  74. getPolicyEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbGet,
  77. Method: types.HTTPVerbGet,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath + "/policy",
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. },
  86. },
  87. )
  88. getPolicyHandler := project.NewProjectGetPolicyHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &Route{
  93. Endpoint: getPolicyEndpoint,
  94. Handler: getPolicyHandler,
  95. Router: r,
  96. })
  97. // GET /api/projects/{project_id}/clusters -> cluster.NewClusterListHandler
  98. listClusterEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbList,
  101. Method: types.HTTPVerbGet,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: relPath + "/clusters",
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. },
  110. },
  111. )
  112. listClusterHandler := cluster.NewClusterListHandler(
  113. config,
  114. factory.GetResultWriter(),
  115. )
  116. routes = append(routes, &Route{
  117. Endpoint: listClusterEndpoint,
  118. Handler: listClusterHandler,
  119. Router: r,
  120. })
  121. // GET /api/projects/{project_id}/gitrepos -> gitinstallation.NewGitRepoListHandler
  122. listGitReposEndpoint := factory.NewAPIEndpoint(
  123. &types.APIRequestMetadata{
  124. Verb: types.APIVerbList,
  125. Method: types.HTTPVerbGet,
  126. Path: &types.Path{
  127. Parent: basePath,
  128. RelativePath: relPath + "/gitrepos",
  129. },
  130. Scopes: []types.PermissionScope{
  131. types.UserScope,
  132. types.ProjectScope,
  133. },
  134. },
  135. )
  136. listGitReposHandler := gitinstallation.NewGitRepoListHandler(
  137. config,
  138. factory.GetResultWriter(),
  139. )
  140. routes = append(routes, &Route{
  141. Endpoint: listGitReposEndpoint,
  142. Handler: listGitReposHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/collaborators -> project.NewCollaboratorsListHandler
  146. listCollaboratorsEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbList,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/collaborators",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. },
  158. },
  159. )
  160. listCollaboratorsHandler := project.NewCollaboratorsListHandler(
  161. config,
  162. factory.GetResultWriter(),
  163. )
  164. routes = append(routes, &Route{
  165. Endpoint: listCollaboratorsEndpoint,
  166. Handler: listCollaboratorsHandler,
  167. Router: r,
  168. })
  169. // GET /api/projects/{project_id}/roles -> project.NewRolesListHandler
  170. listRolesEndpoint := factory.NewAPIEndpoint(
  171. &types.APIRequestMetadata{
  172. Verb: types.APIVerbList,
  173. Method: types.HTTPVerbGet,
  174. Path: &types.Path{
  175. Parent: basePath,
  176. RelativePath: relPath + "/roles",
  177. },
  178. Scopes: []types.PermissionScope{
  179. types.UserScope,
  180. types.ProjectScope,
  181. },
  182. },
  183. )
  184. listRolesHandler := project.NewRolesListHandler(
  185. config,
  186. factory.GetResultWriter(),
  187. )
  188. routes = append(routes, &Route{
  189. Endpoint: listRolesEndpoint,
  190. Handler: listRolesHandler,
  191. Router: r,
  192. })
  193. // POST /api/projects/{project_id}/roles -> project.NewRoleUpdateHandler
  194. updateRoleEndpoint := factory.NewAPIEndpoint(
  195. &types.APIRequestMetadata{
  196. Verb: types.APIVerbUpdate,
  197. Method: types.HTTPVerbPost,
  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. updateRoleHandler := project.NewRoleUpdateHandler(
  209. config,
  210. factory.GetDecoderValidator(),
  211. factory.GetResultWriter(),
  212. )
  213. routes = append(routes, &Route{
  214. Endpoint: updateRoleEndpoint,
  215. Handler: updateRoleHandler,
  216. Router: r,
  217. })
  218. // DELETE /api/projects/{project_id}/roles -> project.NewRoleDeleteHandler
  219. deleteRoleEndpoint := factory.NewAPIEndpoint(
  220. &types.APIRequestMetadata{
  221. Verb: types.APIVerbDelete,
  222. Method: types.HTTPVerbDelete,
  223. Path: &types.Path{
  224. Parent: basePath,
  225. RelativePath: relPath + "/roles",
  226. },
  227. Scopes: []types.PermissionScope{
  228. types.UserScope,
  229. types.ProjectScope,
  230. },
  231. },
  232. )
  233. deleteRoleHandler := project.NewRoleDeleteHandler(
  234. config,
  235. factory.GetDecoderValidator(),
  236. factory.GetResultWriter(),
  237. )
  238. routes = append(routes, &Route{
  239. Endpoint: deleteRoleEndpoint,
  240. Handler: deleteRoleHandler,
  241. Router: r,
  242. })
  243. // GET /api/projects/{project_id}/registries -> registry.NewRegistryListHandler
  244. listRegistriesEndpoint := factory.NewAPIEndpoint(
  245. &types.APIRequestMetadata{
  246. Verb: types.APIVerbList,
  247. Method: types.HTTPVerbGet,
  248. Path: &types.Path{
  249. Parent: basePath,
  250. RelativePath: relPath + "/registries",
  251. },
  252. Scopes: []types.PermissionScope{
  253. types.UserScope,
  254. types.ProjectScope,
  255. },
  256. },
  257. )
  258. listRegistriesHandler := registry.NewRegistryListHandler(
  259. config,
  260. factory.GetResultWriter(),
  261. )
  262. routes = append(routes, &Route{
  263. Endpoint: listRegistriesEndpoint,
  264. Handler: listRegistriesHandler,
  265. Router: r,
  266. })
  267. // POST /api/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
  268. createRegistryEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbCreate,
  271. Method: types.HTTPVerbPost,
  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. createRegistryHandler := registry.NewRegistryCreateHandler(
  283. config,
  284. factory.GetDecoderValidator(),
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &Route{
  288. Endpoint: createRegistryEndpoint,
  289. Handler: createRegistryHandler,
  290. Router: r,
  291. })
  292. // GET /api/projects/{project_id}/registries/ecr/token -> registry.NewRegistryGetECRTokenHandler
  293. getECRTokenEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbGet,
  296. Method: types.HTTPVerbGet,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: relPath + "/registries/ecr/token",
  300. },
  301. Scopes: []types.PermissionScope{
  302. types.UserScope,
  303. types.ProjectScope,
  304. },
  305. },
  306. )
  307. getECRTokenHandler := registry.NewRegistryGetECRTokenHandler(
  308. config,
  309. factory.GetDecoderValidator(),
  310. factory.GetResultWriter(),
  311. )
  312. routes = append(routes, &Route{
  313. Endpoint: getECRTokenEndpoint,
  314. Handler: getECRTokenHandler,
  315. Router: r,
  316. })
  317. // GET /api/projects/{project_id}/registries/docr/token -> registry.NewRegistryGetDOCRTokenHandler
  318. getDOCRTokenEndpoint := factory.NewAPIEndpoint(
  319. &types.APIRequestMetadata{
  320. Verb: types.APIVerbGet,
  321. Method: types.HTTPVerbGet,
  322. Path: &types.Path{
  323. Parent: basePath,
  324. RelativePath: relPath + "/registries/docr/token",
  325. },
  326. Scopes: []types.PermissionScope{
  327. types.UserScope,
  328. types.ProjectScope,
  329. },
  330. },
  331. )
  332. getDOCRTokenHandler := registry.NewRegistryGetDOCRTokenHandler(
  333. config,
  334. factory.GetDecoderValidator(),
  335. factory.GetResultWriter(),
  336. )
  337. routes = append(routes, &Route{
  338. Endpoint: getDOCRTokenEndpoint,
  339. Handler: getDOCRTokenHandler,
  340. Router: r,
  341. })
  342. // GET /api/projects/{project_id}/registries/gcr/token -> registry.NewRegistryGetGCRTokenHandler
  343. getGCRTokenEndpoint := factory.NewAPIEndpoint(
  344. &types.APIRequestMetadata{
  345. Verb: types.APIVerbGet,
  346. Method: types.HTTPVerbGet,
  347. Path: &types.Path{
  348. Parent: basePath,
  349. RelativePath: relPath + "/registries/gcr/token",
  350. },
  351. Scopes: []types.PermissionScope{
  352. types.UserScope,
  353. types.ProjectScope,
  354. },
  355. },
  356. )
  357. getGCRTokenHandler := registry.NewRegistryGetGCRTokenHandler(
  358. config,
  359. factory.GetDecoderValidator(),
  360. factory.GetResultWriter(),
  361. )
  362. routes = append(routes, &Route{
  363. Endpoint: getGCRTokenEndpoint,
  364. Handler: getGCRTokenHandler,
  365. Router: r,
  366. })
  367. // GET /api/projects/{project_id}/registries/dockerhub/token -> registry.NewRegistryGetDockerhubTokenHandler
  368. getDockerhubTokenEndpoint := factory.NewAPIEndpoint(
  369. &types.APIRequestMetadata{
  370. Verb: types.APIVerbGet,
  371. Method: types.HTTPVerbGet,
  372. Path: &types.Path{
  373. Parent: basePath,
  374. RelativePath: relPath + "/registries/dockerhub/token",
  375. },
  376. Scopes: []types.PermissionScope{
  377. types.UserScope,
  378. types.ProjectScope,
  379. },
  380. },
  381. )
  382. getDockerhubTokenHandler := registry.NewRegistryGetDockerhubTokenHandler(
  383. config,
  384. factory.GetDecoderValidator(),
  385. factory.GetResultWriter(),
  386. )
  387. routes = append(routes, &Route{
  388. Endpoint: getDockerhubTokenEndpoint,
  389. Handler: getDockerhubTokenHandler,
  390. Router: r,
  391. })
  392. // POST /api/projects/{project_id}/provision/ecr -> provision.NewProvisionECRHandler
  393. provisionECREndpoint := factory.NewAPIEndpoint(
  394. &types.APIRequestMetadata{
  395. Verb: types.APIVerbCreate,
  396. Method: types.HTTPVerbPost,
  397. Path: &types.Path{
  398. Parent: basePath,
  399. RelativePath: relPath + "/provision/ecr",
  400. },
  401. Scopes: []types.PermissionScope{
  402. types.UserScope,
  403. types.ProjectScope,
  404. },
  405. },
  406. )
  407. provisionECRHandler := provision.NewProvisionECRHandler(
  408. config,
  409. factory.GetDecoderValidator(),
  410. factory.GetResultWriter(),
  411. )
  412. routes = append(routes, &Route{
  413. Endpoint: provisionECREndpoint,
  414. Handler: provisionECRHandler,
  415. Router: r,
  416. })
  417. // POST /api/projects/{project_id}/provision/eks -> provision.NewProvisionEKSHandler
  418. provisionEKSEndpoint := factory.NewAPIEndpoint(
  419. &types.APIRequestMetadata{
  420. Verb: types.APIVerbCreate,
  421. Method: types.HTTPVerbPost,
  422. Path: &types.Path{
  423. Parent: basePath,
  424. RelativePath: relPath + "/provision/eks",
  425. },
  426. Scopes: []types.PermissionScope{
  427. types.UserScope,
  428. types.ProjectScope,
  429. },
  430. },
  431. )
  432. provisionEKSHandler := provision.NewProvisionEKSHandler(
  433. config,
  434. factory.GetDecoderValidator(),
  435. factory.GetResultWriter(),
  436. )
  437. routes = append(routes, &Route{
  438. Endpoint: provisionEKSEndpoint,
  439. Handler: provisionEKSHandler,
  440. Router: r,
  441. })
  442. // POST /api/projects/{project_id}/provision/docr -> provision.NewProvisionDOCRHandler
  443. provisionDOCREndpoint := factory.NewAPIEndpoint(
  444. &types.APIRequestMetadata{
  445. Verb: types.APIVerbCreate,
  446. Method: types.HTTPVerbPost,
  447. Path: &types.Path{
  448. Parent: basePath,
  449. RelativePath: relPath + "/provision/docr",
  450. },
  451. Scopes: []types.PermissionScope{
  452. types.UserScope,
  453. types.ProjectScope,
  454. },
  455. },
  456. )
  457. provisionDOCRHandler := provision.NewProvisionDOCRHandler(
  458. config,
  459. factory.GetDecoderValidator(),
  460. factory.GetResultWriter(),
  461. )
  462. routes = append(routes, &Route{
  463. Endpoint: provisionDOCREndpoint,
  464. Handler: provisionDOCRHandler,
  465. Router: r,
  466. })
  467. // POST /api/projects/{project_id}/provision/doks -> provision.NewProvisionDOKSHandler
  468. provisionDOKSEndpoint := factory.NewAPIEndpoint(
  469. &types.APIRequestMetadata{
  470. Verb: types.APIVerbCreate,
  471. Method: types.HTTPVerbPost,
  472. Path: &types.Path{
  473. Parent: basePath,
  474. RelativePath: relPath + "/provision/doks",
  475. },
  476. Scopes: []types.PermissionScope{
  477. types.UserScope,
  478. types.ProjectScope,
  479. },
  480. },
  481. )
  482. provisionDOKSHandler := provision.NewProvisionDOCRHandler(
  483. config,
  484. factory.GetDecoderValidator(),
  485. factory.GetResultWriter(),
  486. )
  487. routes = append(routes, &Route{
  488. Endpoint: provisionDOKSEndpoint,
  489. Handler: provisionDOKSHandler,
  490. Router: r,
  491. })
  492. // POST /api/projects/{project_id}/provision/gcr -> provision.NewProvisionGCRHandler
  493. provisionGCREndpoint := factory.NewAPIEndpoint(
  494. &types.APIRequestMetadata{
  495. Verb: types.APIVerbCreate,
  496. Method: types.HTTPVerbPost,
  497. Path: &types.Path{
  498. Parent: basePath,
  499. RelativePath: relPath + "/provision/gcr",
  500. },
  501. Scopes: []types.PermissionScope{
  502. types.UserScope,
  503. types.ProjectScope,
  504. },
  505. },
  506. )
  507. provisionGCRHandler := provision.NewProvisionGCRHandler(
  508. config,
  509. factory.GetDecoderValidator(),
  510. factory.GetResultWriter(),
  511. )
  512. routes = append(routes, &Route{
  513. Endpoint: provisionGCREndpoint,
  514. Handler: provisionGCRHandler,
  515. Router: r,
  516. })
  517. // POST /api/projects/{project_id}/provision/gke -> provision.NewProvisionGKEHandler
  518. provisionGKEEndpoint := factory.NewAPIEndpoint(
  519. &types.APIRequestMetadata{
  520. Verb: types.APIVerbCreate,
  521. Method: types.HTTPVerbPost,
  522. Path: &types.Path{
  523. Parent: basePath,
  524. RelativePath: relPath + "/provision/gke",
  525. },
  526. Scopes: []types.PermissionScope{
  527. types.UserScope,
  528. types.ProjectScope,
  529. },
  530. },
  531. )
  532. provisionGKEHandler := provision.NewProvisionGKEHandler(
  533. config,
  534. factory.GetDecoderValidator(),
  535. factory.GetResultWriter(),
  536. )
  537. routes = append(routes, &Route{
  538. Endpoint: provisionGKEEndpoint,
  539. Handler: provisionGKEHandler,
  540. Router: r,
  541. })
  542. return routes, newPath
  543. }