project.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/invite"
  7. "github.com/porter-dev/porter/api/server/handlers/project"
  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}/infra -> project.NewListProjectInfraHandler
  98. listInfraEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbGet,
  101. Method: types.HTTPVerbGet,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: relPath + "/infra",
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. },
  110. },
  111. )
  112. listInfraHandler := project.NewProjectListInfraHandler(
  113. config,
  114. factory.GetResultWriter(),
  115. )
  116. routes = append(routes, &Route{
  117. Endpoint: listInfraEndpoint,
  118. Handler: listInfraHandler,
  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.NewProjectListCollaboratorsHandler
  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.NewProjectListCollaboratorsHandler(
  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.NewProjectListRolesHandler
  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.NewProjectListRolesHandler(
  209. config,
  210. factory.GetResultWriter(),
  211. )
  212. routes = append(routes, &Route{
  213. Endpoint: listRolesEndpoint,
  214. Handler: listRolesHandler,
  215. Router: r,
  216. })
  217. // GET /api/projects/{project_id}/registries -> registry.NewRegistryListHandler
  218. listRegistriesEndpoint := factory.NewAPIEndpoint(
  219. &types.APIRequestMetadata{
  220. Verb: types.APIVerbList,
  221. Method: types.HTTPVerbGet,
  222. Path: &types.Path{
  223. Parent: basePath,
  224. RelativePath: relPath + "/registries",
  225. },
  226. Scopes: []types.PermissionScope{
  227. types.UserScope,
  228. types.ProjectScope,
  229. },
  230. },
  231. )
  232. listRegistriesHandler := registry.NewRegistryListHandler(
  233. config,
  234. factory.GetResultWriter(),
  235. )
  236. routes = append(routes, &Route{
  237. Endpoint: listRegistriesEndpoint,
  238. Handler: listRegistriesHandler,
  239. Router: r,
  240. })
  241. // POST /api/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
  242. createRegistryEndpoint := factory.NewAPIEndpoint(
  243. &types.APIRequestMetadata{
  244. Verb: types.APIVerbCreate,
  245. Method: types.HTTPVerbPost,
  246. Path: &types.Path{
  247. Parent: basePath,
  248. RelativePath: relPath + "/registries",
  249. },
  250. Scopes: []types.PermissionScope{
  251. types.UserScope,
  252. types.ProjectScope,
  253. },
  254. },
  255. )
  256. createRegistryHandler := registry.NewRegistryCreateHandler(
  257. config,
  258. factory.GetDecoderValidator(),
  259. factory.GetResultWriter(),
  260. )
  261. routes = append(routes, &Route{
  262. Endpoint: createRegistryEndpoint,
  263. Handler: createRegistryHandler,
  264. Router: r,
  265. })
  266. // GET /api/projects/{project_id}/registries/ecr/token -> registry.NewRegistryGetECRTokenHandler
  267. getECRTokenEndpoint := factory.NewAPIEndpoint(
  268. &types.APIRequestMetadata{
  269. Verb: types.APIVerbGet,
  270. Method: types.HTTPVerbGet,
  271. Path: &types.Path{
  272. Parent: basePath,
  273. RelativePath: relPath + "/registries/ecr/token",
  274. },
  275. Scopes: []types.PermissionScope{
  276. types.UserScope,
  277. types.ProjectScope,
  278. },
  279. },
  280. )
  281. getECRTokenHandler := registry.NewRegistryGetECRTokenHandler(
  282. config,
  283. factory.GetDecoderValidator(),
  284. factory.GetResultWriter(),
  285. )
  286. routes = append(routes, &Route{
  287. Endpoint: getECRTokenEndpoint,
  288. Handler: getECRTokenHandler,
  289. Router: r,
  290. })
  291. // GET /api/projects/{project_id}/registries/docr/token -> registry.NewRegistryGetDOCRTokenHandler
  292. getDOCRTokenEndpoint := factory.NewAPIEndpoint(
  293. &types.APIRequestMetadata{
  294. Verb: types.APIVerbGet,
  295. Method: types.HTTPVerbGet,
  296. Path: &types.Path{
  297. Parent: basePath,
  298. RelativePath: relPath + "/registries/docr/token",
  299. },
  300. Scopes: []types.PermissionScope{
  301. types.UserScope,
  302. types.ProjectScope,
  303. },
  304. },
  305. )
  306. getDOCRTokenHandler := registry.NewRegistryGetDOCRTokenHandler(
  307. config,
  308. factory.GetDecoderValidator(),
  309. factory.GetResultWriter(),
  310. )
  311. routes = append(routes, &Route{
  312. Endpoint: getDOCRTokenEndpoint,
  313. Handler: getDOCRTokenHandler,
  314. Router: r,
  315. })
  316. // GET /api/projects/{project_id}/registries/gcr/token -> registry.NewRegistryGetGCRTokenHandler
  317. getGCRTokenEndpoint := factory.NewAPIEndpoint(
  318. &types.APIRequestMetadata{
  319. Verb: types.APIVerbGet,
  320. Method: types.HTTPVerbGet,
  321. Path: &types.Path{
  322. Parent: basePath,
  323. RelativePath: relPath + "/registries/gcr/token",
  324. },
  325. Scopes: []types.PermissionScope{
  326. types.UserScope,
  327. types.ProjectScope,
  328. },
  329. },
  330. )
  331. getGCRTokenHandler := registry.NewRegistryGetGCRTokenHandler(
  332. config,
  333. factory.GetDecoderValidator(),
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &Route{
  337. Endpoint: getGCRTokenEndpoint,
  338. Handler: getGCRTokenHandler,
  339. Router: r,
  340. })
  341. // GET /api/projects/{project_id}/registries/dockerhub/token -> registry.NewRegistryGetDockerhubTokenHandler
  342. getDockerhubTokenEndpoint := factory.NewAPIEndpoint(
  343. &types.APIRequestMetadata{
  344. Verb: types.APIVerbGet,
  345. Method: types.HTTPVerbGet,
  346. Path: &types.Path{
  347. Parent: basePath,
  348. RelativePath: relPath + "/registries/dockerhub/token",
  349. },
  350. Scopes: []types.PermissionScope{
  351. types.UserScope,
  352. types.ProjectScope,
  353. },
  354. },
  355. )
  356. getDockerhubTokenHandler := registry.NewRegistryGetDockerhubTokenHandler(
  357. config,
  358. factory.GetDecoderValidator(),
  359. factory.GetResultWriter(),
  360. )
  361. routes = append(routes, &Route{
  362. Endpoint: getDockerhubTokenEndpoint,
  363. Handler: getDockerhubTokenHandler,
  364. Router: r,
  365. })
  366. // GET /api/projects/{project_id}/invites -> invite.NewCreateInviteHandler
  367. listInvitesEndpoint := factory.NewAPIEndpoint(
  368. &types.APIRequestMetadata{
  369. Verb: types.APIVerbGet,
  370. Method: types.HTTPVerbGet,
  371. Path: &types.Path{
  372. Parent: basePath,
  373. RelativePath: relPath + "/invites",
  374. },
  375. Scopes: []types.PermissionScope{
  376. types.UserScope,
  377. types.ProjectScope,
  378. },
  379. },
  380. )
  381. listInvitesHandler := invite.NewListInvitesHandler(
  382. config,
  383. factory.GetResultWriter(),
  384. )
  385. routes = append(routes, &Route{
  386. Endpoint: listInvitesEndpoint,
  387. Handler: listInvitesHandler,
  388. Router: r,
  389. })
  390. // POST /api/projects/{project_id}/invites -> invite.NewCreateInviteHandler
  391. createInviteEndpoint := factory.NewAPIEndpoint(
  392. &types.APIRequestMetadata{
  393. Verb: types.APIVerbCreate,
  394. Method: types.HTTPVerbPost,
  395. Path: &types.Path{
  396. Parent: basePath,
  397. RelativePath: relPath + "/invites",
  398. },
  399. Scopes: []types.PermissionScope{
  400. types.UserScope,
  401. types.ProjectScope,
  402. },
  403. },
  404. )
  405. createInviteHandler := invite.NewCreateInviteHandler(
  406. config,
  407. factory.GetDecoderValidator(),
  408. factory.GetResultWriter(),
  409. )
  410. routes = append(routes, &Route{
  411. Endpoint: createInviteEndpoint,
  412. Handler: createInviteHandler,
  413. Router: r,
  414. })
  415. // GET /api/projects/{project_id}/invites/accept -> invite.NewInviteAcceptHandler
  416. acceptInviteEndpoint := factory.NewAPIEndpoint(
  417. &types.APIRequestMetadata{
  418. Verb: types.APIVerbGet,
  419. Method: types.HTTPVerbGet,
  420. Path: &types.Path{
  421. Parent: basePath,
  422. RelativePath: relPath + "/invites/accept",
  423. },
  424. Scopes: []types.PermissionScope{},
  425. },
  426. )
  427. acceptInviteHandler := invite.NewInviteAcceptHandler(
  428. config,
  429. factory.GetDecoderValidator(),
  430. )
  431. routes = append(routes, &Route{
  432. Endpoint: acceptInviteEndpoint,
  433. Handler: acceptInviteHandler,
  434. Router: r,
  435. })
  436. return routes, newPath
  437. }