project.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/registry"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. )
  12. func NewProjectScopedRegisterer(children ...*Registerer) *Registerer {
  13. return &Registerer{
  14. GetRoutes: GetProjectScopedRoutes,
  15. Children: children,
  16. }
  17. }
  18. func GetProjectScopedRoutes(
  19. r chi.Router,
  20. config *config.Config,
  21. basePath *types.Path,
  22. factory shared.APIEndpointFactory,
  23. children ...*Registerer,
  24. ) []*Route {
  25. routes, projPath := getProjectRoutes(r, config, basePath, factory)
  26. if len(children) > 0 {
  27. r.Route(projPath.RelativePath, func(r chi.Router) {
  28. for _, child := range children {
  29. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  30. routes = append(routes, childRoutes...)
  31. }
  32. })
  33. }
  34. return routes
  35. }
  36. func getProjectRoutes(
  37. r chi.Router,
  38. config *config.Config,
  39. basePath *types.Path,
  40. factory shared.APIEndpointFactory,
  41. ) ([]*Route, *types.Path) {
  42. relPath := "/projects/{project_id}"
  43. newPath := &types.Path{
  44. Parent: basePath,
  45. RelativePath: relPath,
  46. }
  47. routes := make([]*Route, 0)
  48. // GET /api/projects/{project_id} -> project.NewProjectGetHandler
  49. getEndpoint := factory.NewAPIEndpoint(
  50. &types.APIRequestMetadata{
  51. Verb: types.APIVerbGet,
  52. Method: types.HTTPVerbGet,
  53. Path: &types.Path{
  54. Parent: basePath,
  55. RelativePath: relPath,
  56. },
  57. Scopes: []types.PermissionScope{
  58. types.UserScope,
  59. types.ProjectScope,
  60. },
  61. },
  62. )
  63. getHandler := project.NewProjectGetHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &Route{
  68. Endpoint: getEndpoint,
  69. Handler: getHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/policy -> project.NewProjectGetPolicyHandler
  73. getPolicyEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/policy",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. },
  85. },
  86. )
  87. getPolicyHandler := project.NewProjectGetPolicyHandler(
  88. config,
  89. factory.GetResultWriter(),
  90. )
  91. routes = append(routes, &Route{
  92. Endpoint: getPolicyEndpoint,
  93. Handler: getPolicyHandler,
  94. Router: r,
  95. })
  96. // GET /api/projects/{project_id}/infra -> project.NewListProjectInfraHandler
  97. listInfraEndpoint := factory.NewAPIEndpoint(
  98. &types.APIRequestMetadata{
  99. Verb: types.APIVerbGet,
  100. Method: types.HTTPVerbGet,
  101. Path: &types.Path{
  102. Parent: basePath,
  103. RelativePath: relPath + "/infra",
  104. },
  105. Scopes: []types.PermissionScope{
  106. types.UserScope,
  107. types.ProjectScope,
  108. },
  109. },
  110. )
  111. listInfraHandler := project.NewProjectListInfraHandler(
  112. config,
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &Route{
  116. Endpoint: listInfraEndpoint,
  117. Handler: listInfraHandler,
  118. Router: r,
  119. })
  120. // GET /api/projects/{project_id}/clusters -> cluster.NewClusterListHandler
  121. listClusterEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbList,
  124. Method: types.HTTPVerbGet,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: relPath + "/clusters",
  128. },
  129. Scopes: []types.PermissionScope{
  130. types.UserScope,
  131. types.ProjectScope,
  132. },
  133. },
  134. )
  135. listClusterHandler := cluster.NewClusterListHandler(
  136. config,
  137. factory.GetResultWriter(),
  138. )
  139. routes = append(routes, &Route{
  140. Endpoint: listClusterEndpoint,
  141. Handler: listClusterHandler,
  142. Router: r,
  143. })
  144. // GET /api/projects/{project_id}/gitrepos -> gitinstallation.NewGitRepoListHandler
  145. listGitReposEndpoint := factory.NewAPIEndpoint(
  146. &types.APIRequestMetadata{
  147. Verb: types.APIVerbList,
  148. Method: types.HTTPVerbGet,
  149. Path: &types.Path{
  150. Parent: basePath,
  151. RelativePath: relPath + "/gitrepos",
  152. },
  153. Scopes: []types.PermissionScope{
  154. types.UserScope,
  155. types.ProjectScope,
  156. },
  157. },
  158. )
  159. listGitReposHandler := gitinstallation.NewGitRepoListHandler(
  160. config,
  161. factory.GetResultWriter(),
  162. )
  163. routes = append(routes, &Route{
  164. Endpoint: listGitReposEndpoint,
  165. Handler: listGitReposHandler,
  166. Router: r,
  167. })
  168. // GET /api/projects/{project_id}/registries -> registry.NewRegistryListHandler
  169. listRegistriesEndpoint := factory.NewAPIEndpoint(
  170. &types.APIRequestMetadata{
  171. Verb: types.APIVerbList,
  172. Method: types.HTTPVerbGet,
  173. Path: &types.Path{
  174. Parent: basePath,
  175. RelativePath: relPath + "/registries",
  176. },
  177. Scopes: []types.PermissionScope{
  178. types.UserScope,
  179. types.ProjectScope,
  180. },
  181. },
  182. )
  183. listRegistriesHandler := registry.NewRegistryListHandler(
  184. config,
  185. factory.GetResultWriter(),
  186. )
  187. routes = append(routes, &Route{
  188. Endpoint: listRegistriesEndpoint,
  189. Handler: listRegistriesHandler,
  190. Router: r,
  191. })
  192. // POST /api/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
  193. createRegistryEndpoint := factory.NewAPIEndpoint(
  194. &types.APIRequestMetadata{
  195. Verb: types.APIVerbCreate,
  196. Method: types.HTTPVerbPost,
  197. Path: &types.Path{
  198. Parent: basePath,
  199. RelativePath: relPath + "/registries",
  200. },
  201. Scopes: []types.PermissionScope{
  202. types.UserScope,
  203. types.ProjectScope,
  204. },
  205. },
  206. )
  207. createRegistryHandler := registry.NewRegistryCreateHandler(
  208. config,
  209. factory.GetDecoderValidator(),
  210. factory.GetResultWriter(),
  211. )
  212. routes = append(routes, &Route{
  213. Endpoint: createRegistryEndpoint,
  214. Handler: createRegistryHandler,
  215. Router: r,
  216. })
  217. // GET /api/projects/{project_id}/registries/ecr/token -> registry.NewRegistryGetECRTokenHandler
  218. getECRTokenEndpoint := factory.NewAPIEndpoint(
  219. &types.APIRequestMetadata{
  220. Verb: types.APIVerbGet,
  221. Method: types.HTTPVerbGet,
  222. Path: &types.Path{
  223. Parent: basePath,
  224. RelativePath: relPath + "/registries/ecr/token",
  225. },
  226. Scopes: []types.PermissionScope{
  227. types.UserScope,
  228. types.ProjectScope,
  229. },
  230. },
  231. )
  232. getECRTokenHandler := registry.NewRegistryGetECRTokenHandler(
  233. config,
  234. factory.GetDecoderValidator(),
  235. factory.GetResultWriter(),
  236. )
  237. routes = append(routes, &Route{
  238. Endpoint: getECRTokenEndpoint,
  239. Handler: getECRTokenHandler,
  240. Router: r,
  241. })
  242. // GET /api/projects/{project_id}/registries/docr/token -> registry.NewRegistryGetDOCRTokenHandler
  243. getDOCRTokenEndpoint := factory.NewAPIEndpoint(
  244. &types.APIRequestMetadata{
  245. Verb: types.APIVerbGet,
  246. Method: types.HTTPVerbGet,
  247. Path: &types.Path{
  248. Parent: basePath,
  249. RelativePath: relPath + "/registries/docr/token",
  250. },
  251. Scopes: []types.PermissionScope{
  252. types.UserScope,
  253. types.ProjectScope,
  254. },
  255. },
  256. )
  257. getDOCRTokenHandler := registry.NewRegistryGetDOCRTokenHandler(
  258. config,
  259. factory.GetDecoderValidator(),
  260. factory.GetResultWriter(),
  261. )
  262. routes = append(routes, &Route{
  263. Endpoint: getDOCRTokenEndpoint,
  264. Handler: getDOCRTokenHandler,
  265. Router: r,
  266. })
  267. // GET /api/projects/{project_id}/registries/gcr/token -> registry.NewRegistryGetGCRTokenHandler
  268. getGCRTokenEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbGet,
  271. Method: types.HTTPVerbGet,
  272. Path: &types.Path{
  273. Parent: basePath,
  274. RelativePath: relPath + "/registries/gcr/token",
  275. },
  276. Scopes: []types.PermissionScope{
  277. types.UserScope,
  278. types.ProjectScope,
  279. },
  280. },
  281. )
  282. getGCRTokenHandler := registry.NewRegistryGetGCRTokenHandler(
  283. config,
  284. factory.GetDecoderValidator(),
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &Route{
  288. Endpoint: getGCRTokenEndpoint,
  289. Handler: getGCRTokenHandler,
  290. Router: r,
  291. })
  292. // GET /api/projects/{project_id}/registries/dockerhub/token -> registry.NewRegistryGetDockerhubTokenHandler
  293. getDockerhubTokenEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbGet,
  296. Method: types.HTTPVerbGet,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: relPath + "/registries/dockerhub/token",
  300. },
  301. Scopes: []types.PermissionScope{
  302. types.UserScope,
  303. types.ProjectScope,
  304. },
  305. },
  306. )
  307. getDockerhubTokenHandler := registry.NewRegistryGetDockerhubTokenHandler(
  308. config,
  309. factory.GetDecoderValidator(),
  310. factory.GetResultWriter(),
  311. )
  312. routes = append(routes, &Route{
  313. Endpoint: getDockerhubTokenEndpoint,
  314. Handler: getDockerhubTokenHandler,
  315. Router: r,
  316. })
  317. return routes, newPath
  318. }