release.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/release"
  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 NewReleaseScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetReleaseScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetReleaseScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getReleaseRoutes(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 getReleaseRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/releases/{name}/{version}"
  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}/releases/{name}/{version} -> release.NewReleaseGetHandler
  46. getEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath,
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. types.ClusterScope,
  58. types.NamespaceScope,
  59. types.ReleaseScope,
  60. },
  61. },
  62. )
  63. getHandler := release.NewReleaseGetHandler(
  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}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/controllers -> release.NewGetControllersHandler
  73. getControllersEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/controllers",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. types.NamespaceScope,
  86. types.ReleaseScope,
  87. },
  88. },
  89. )
  90. getControllersHandler := release.NewGetControllersHandler(
  91. config,
  92. factory.GetResultWriter(),
  93. )
  94. routes = append(routes, &Route{
  95. Endpoint: getControllersEndpoint,
  96. Handler: getControllersHandler,
  97. Router: r,
  98. })
  99. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/components -> release.NewGetComponentsHandler
  100. getComponentsEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbGet,
  103. Method: types.HTTPVerbGet,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: relPath + "/components",
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. types.ClusterScope,
  112. types.NamespaceScope,
  113. types.ReleaseScope,
  114. },
  115. },
  116. )
  117. getComponentsHandler := release.NewGetComponentsHandler(
  118. config,
  119. factory.GetResultWriter(),
  120. )
  121. routes = append(routes, &Route{
  122. Endpoint: getComponentsEndpoint,
  123. Handler: getComponentsHandler,
  124. Router: r,
  125. })
  126. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/history -> release.NewGetHistoryHandler
  127. getHistoryEndpoint := factory.NewAPIEndpoint(
  128. &types.APIRequestMetadata{
  129. Verb: types.APIVerbGet,
  130. Method: types.HTTPVerbGet,
  131. Path: &types.Path{
  132. Parent: basePath,
  133. RelativePath: "/releases/{name}/history",
  134. },
  135. Scopes: []types.PermissionScope{
  136. types.UserScope,
  137. types.ProjectScope,
  138. types.ClusterScope,
  139. types.NamespaceScope,
  140. },
  141. },
  142. )
  143. getHistoryHandler := release.NewGetReleaseHistoryHandler(
  144. config,
  145. factory.GetDecoderValidator(),
  146. factory.GetResultWriter(),
  147. )
  148. routes = append(routes, &Route{
  149. Endpoint: getHistoryEndpoint,
  150. Handler: getHistoryHandler,
  151. Router: r,
  152. })
  153. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/pods/all -> release.NewGetAllPodsHandler
  154. getAllPodsEndpoint := factory.NewAPIEndpoint(
  155. &types.APIRequestMetadata{
  156. Verb: types.APIVerbGet,
  157. Method: types.HTTPVerbGet,
  158. Path: &types.Path{
  159. Parent: basePath,
  160. RelativePath: relPath + "/pods/all",
  161. },
  162. Scopes: []types.PermissionScope{
  163. types.UserScope,
  164. types.ProjectScope,
  165. types.ClusterScope,
  166. types.NamespaceScope,
  167. types.ReleaseScope,
  168. },
  169. },
  170. )
  171. getAllPodsHandler := release.NewGetAllPodsHandler(
  172. config,
  173. factory.GetResultWriter(),
  174. )
  175. routes = append(routes, &Route{
  176. Endpoint: getAllPodsEndpoint,
  177. Handler: getAllPodsHandler,
  178. Router: r,
  179. })
  180. return routes, newPath
  181. }