namespace.go 6.5 KB

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