namespace.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/namespace"
  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/types"
  9. )
  10. func NewNamespaceScopedRegisterer(children ...*Registerer) *Registerer {
  11. return &Registerer{
  12. GetRoutes: GetNamespaceScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetNamespaceScopedRoutes(
  17. r chi.Router,
  18. config *config.Config,
  19. basePath *types.Path,
  20. factory shared.APIEndpointFactory,
  21. children ...*Registerer,
  22. ) []*Route {
  23. routes, projPath := getNamespaceRoutes(r, config, basePath, factory)
  24. if len(children) > 0 {
  25. r.Route(projPath.RelativePath, func(r chi.Router) {
  26. for _, child := range children {
  27. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  28. routes = append(routes, childRoutes...)
  29. }
  30. })
  31. }
  32. return routes
  33. }
  34. func getNamespaceRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*Route, *types.Path) {
  40. relPath := "/namespaces/{namespace}"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*Route, 0)
  46. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/list -> namespace.NewListConfigMapsHandler
  47. listConfigMapsEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbGet,
  50. Method: types.HTTPVerbGet,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath + "/configmap/list",
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.ClusterScope,
  59. types.NamespaceScope,
  60. },
  61. },
  62. )
  63. listConfigMapsHandler := namespace.NewListConfigMapsHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &Route{
  68. Endpoint: listConfigMapsEndpoint,
  69. Handler: listConfigMapsHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap -> namespace.NewGetConfigMapHandler
  73. getConfigMapEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/configmap",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. types.NamespaceScope,
  86. },
  87. },
  88. )
  89. getConfigMapHandler := namespace.NewGetConfigMapHandler(
  90. config,
  91. factory.GetDecoderValidator(),
  92. factory.GetResultWriter(),
  93. )
  94. routes = append(routes, &Route{
  95. Endpoint: getConfigMapEndpoint,
  96. Handler: getConfigMapHandler,
  97. Router: r,
  98. })
  99. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/create -> namespace.NewCreateConfigMapHandler
  100. createConfigMapEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbCreate,
  103. Method: types.HTTPVerbPost,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: relPath + "/configmap/create",
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. types.ClusterScope,
  112. types.NamespaceScope,
  113. },
  114. },
  115. )
  116. createConfigMapHandler := namespace.NewCreateConfigMapHandler(
  117. config,
  118. factory.GetDecoderValidator(),
  119. factory.GetResultWriter(),
  120. )
  121. routes = append(routes, &Route{
  122. Endpoint: createConfigMapEndpoint,
  123. Handler: createConfigMapHandler,
  124. Router: r,
  125. })
  126. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/update -> namespace.NewUpdateConfigMapHandler
  127. updateConfigMapEndpoint := factory.NewAPIEndpoint(
  128. &types.APIRequestMetadata{
  129. Verb: types.APIVerbUpdate,
  130. Method: types.HTTPVerbPost,
  131. Path: &types.Path{
  132. Parent: basePath,
  133. RelativePath: relPath + "/configmap/update",
  134. },
  135. Scopes: []types.PermissionScope{
  136. types.UserScope,
  137. types.ProjectScope,
  138. types.ClusterScope,
  139. types.NamespaceScope,
  140. },
  141. },
  142. )
  143. updateConfigMapHandler := namespace.NewUpdateConfigMapHandler(
  144. config,
  145. factory.GetDecoderValidator(),
  146. factory.GetResultWriter(),
  147. )
  148. routes = append(routes, &Route{
  149. Endpoint: updateConfigMapEndpoint,
  150. Handler: updateConfigMapHandler,
  151. Router: r,
  152. })
  153. // POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/rename -> namespace.NewRenameConfigMapHandler
  154. renameConfigMapEndpoint := factory.NewAPIEndpoint(
  155. &types.APIRequestMetadata{
  156. Verb: types.APIVerbUpdate,
  157. Method: types.HTTPVerbPost,
  158. Path: &types.Path{
  159. Parent: basePath,
  160. RelativePath: relPath + "/configmap/rename",
  161. },
  162. Scopes: []types.PermissionScope{
  163. types.UserScope,
  164. types.ProjectScope,
  165. types.ClusterScope,
  166. types.NamespaceScope,
  167. },
  168. },
  169. )
  170. renameConfigMapHandler := namespace.NewRenameConfigMapHandler(
  171. config,
  172. factory.GetDecoderValidator(),
  173. factory.GetResultWriter(),
  174. )
  175. routes = append(routes, &Route{
  176. Endpoint: renameConfigMapEndpoint,
  177. Handler: renameConfigMapHandler,
  178. Router: r,
  179. })
  180. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/delete -> namespace.NewDeleteConfigMapHandler
  181. deleteConfigMapEndpoint := factory.NewAPIEndpoint(
  182. &types.APIRequestMetadata{
  183. Verb: types.APIVerbDelete,
  184. Method: types.HTTPVerbDelete,
  185. Path: &types.Path{
  186. Parent: basePath,
  187. RelativePath: relPath + "/configmap/delete",
  188. },
  189. Scopes: []types.PermissionScope{
  190. types.UserScope,
  191. types.ProjectScope,
  192. types.ClusterScope,
  193. types.NamespaceScope,
  194. },
  195. },
  196. )
  197. deleteConfigMapHandler := namespace.NewDeleteConfigMapHandler(
  198. config,
  199. factory.GetDecoderValidator(),
  200. )
  201. routes = append(routes, &Route{
  202. Endpoint: deleteConfigMapEndpoint,
  203. Handler: deleteConfigMapHandler,
  204. Router: r,
  205. })
  206. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  207. listReleasesEndpoint := factory.NewAPIEndpoint(
  208. &types.APIRequestMetadata{
  209. Verb: types.APIVerbGet,
  210. Method: types.HTTPVerbGet,
  211. Path: &types.Path{
  212. Parent: basePath,
  213. RelativePath: relPath + "/releases",
  214. },
  215. Scopes: []types.PermissionScope{
  216. types.UserScope,
  217. types.ProjectScope,
  218. types.ClusterScope,
  219. types.NamespaceScope,
  220. },
  221. },
  222. )
  223. listReleasesHandler := namespace.NewListReleasesHandler(
  224. config,
  225. factory.GetDecoderValidator(),
  226. factory.GetResultWriter(),
  227. )
  228. routes = append(routes, &Route{
  229. Endpoint: listReleasesEndpoint,
  230. Handler: listReleasesHandler,
  231. Router: r,
  232. })
  233. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/pod/{name}/logs -> namespace.NewStreamPodLogsHandler
  234. streamPodLogsEndpoint := factory.NewAPIEndpoint(
  235. &types.APIRequestMetadata{
  236. Verb: types.APIVerbGet,
  237. Method: types.HTTPVerbGet,
  238. Path: &types.Path{
  239. Parent: basePath,
  240. RelativePath: fmt.Sprintf(
  241. "%s/pod/{%s}/logs",
  242. relPath,
  243. types.URLParamPodName,
  244. ),
  245. },
  246. Scopes: []types.PermissionScope{
  247. types.UserScope,
  248. types.ProjectScope,
  249. types.ClusterScope,
  250. types.NamespaceScope,
  251. },
  252. },
  253. )
  254. streamPodLogsHandler := namespace.NewStreamPodLogsHandler(
  255. config,
  256. factory.GetDecoderValidator(),
  257. factory.GetResultWriter(),
  258. )
  259. routes = append(routes, &Route{
  260. Endpoint: streamPodLogsEndpoint,
  261. Handler: streamPodLogsHandler,
  262. Router: r,
  263. })
  264. return routes, newPath
  265. }