namespace.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.NewGetConfigMapHandler
  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. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  126. listReleasesEndpoint := factory.NewAPIEndpoint(
  127. &types.APIRequestMetadata{
  128. Verb: types.APIVerbGet,
  129. Method: types.HTTPVerbGet,
  130. Path: &types.Path{
  131. Parent: basePath,
  132. RelativePath: relPath + "/releases",
  133. },
  134. Scopes: []types.PermissionScope{
  135. types.UserScope,
  136. types.ProjectScope,
  137. types.ClusterScope,
  138. types.NamespaceScope,
  139. },
  140. },
  141. )
  142. listReleasesHandler := namespace.NewListReleasesHandler(
  143. config,
  144. factory.GetDecoderValidator(),
  145. factory.GetResultWriter(),
  146. )
  147. routes = append(routes, &Route{
  148. Endpoint: listReleasesEndpoint,
  149. Handler: listReleasesHandler,
  150. Router: r,
  151. })
  152. return routes, newPath
  153. }