release.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package v1
  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/handlers/release"
  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/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewV1ReleaseScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetV1ReleaseScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetV1ReleaseScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getV1ReleaseRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getV1ReleaseRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/releases"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. var routes []*router.Route
  47. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> release.NewCreateReleaseHandler
  48. createReleaseEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbCreate,
  51. Method: types.HTTPVerbPost,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath,
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. types.NamespaceScope,
  61. },
  62. },
  63. )
  64. createReleaseHandler := release.NewCreateReleaseHandler(
  65. config,
  66. factory.GetDecoderValidator(),
  67. factory.GetResultWriter(),
  68. )
  69. routes = append(routes, &router.Route{
  70. Endpoint: createReleaseEndpoint,
  71. Handler: createReleaseHandler,
  72. Router: r,
  73. })
  74. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} -> release.NewReleaseGetHandler
  75. getEndpoint := factory.NewAPIEndpoint(
  76. &types.APIRequestMetadata{
  77. Verb: types.APIVerbGet,
  78. Method: types.HTTPVerbGet,
  79. Path: &types.Path{
  80. Parent: basePath,
  81. RelativePath: relPath + "/{name}/{version}",
  82. },
  83. Scopes: []types.PermissionScope{
  84. types.UserScope,
  85. types.ProjectScope,
  86. types.ClusterScope,
  87. types.NamespaceScope,
  88. types.ReleaseScope,
  89. },
  90. },
  91. )
  92. getHandler := release.NewReleaseGetHandler(
  93. config,
  94. factory.GetResultWriter(),
  95. )
  96. routes = append(routes, &router.Route{
  97. Endpoint: getEndpoint,
  98. Handler: getHandler,
  99. Router: r,
  100. })
  101. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  102. listReleasesEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbGet,
  105. Method: types.HTTPVerbGet,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: relPath,
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. types.ClusterScope,
  114. types.NamespaceScope,
  115. },
  116. },
  117. )
  118. listReleasesHandler := namespace.NewListReleasesHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &router.Route{
  124. Endpoint: listReleasesEndpoint,
  125. Handler: listReleasesHandler,
  126. Router: r,
  127. })
  128. // PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  129. // release.NewUpgradeReleaseHandler
  130. upgradeEndpoint := factory.NewAPIEndpoint(
  131. &types.APIRequestMetadata{
  132. Verb: types.APIVerbUpdate,
  133. Method: types.HTTPVerbPatch,
  134. Path: &types.Path{
  135. Parent: basePath,
  136. RelativePath: relPath + "/{name}/{version}",
  137. },
  138. Scopes: []types.PermissionScope{
  139. types.UserScope,
  140. types.ProjectScope,
  141. types.ClusterScope,
  142. types.NamespaceScope,
  143. types.ReleaseScope,
  144. },
  145. },
  146. )
  147. upgradeHandler := release.NewUpgradeReleaseHandler(
  148. config,
  149. factory.GetDecoderValidator(),
  150. factory.GetResultWriter(),
  151. )
  152. routes = append(routes, &router.Route{
  153. Endpoint: upgradeEndpoint,
  154. Handler: upgradeHandler,
  155. Router: r,
  156. })
  157. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  158. // release.NewDeleteReleaseHandler
  159. deleteEndpoint := factory.NewAPIEndpoint(
  160. &types.APIRequestMetadata{
  161. Verb: types.APIVerbDelete,
  162. Method: types.HTTPVerbDelete,
  163. Path: &types.Path{
  164. Parent: basePath,
  165. RelativePath: relPath,
  166. },
  167. Scopes: []types.PermissionScope{
  168. types.UserScope,
  169. types.ProjectScope,
  170. types.ClusterScope,
  171. types.NamespaceScope,
  172. types.ReleaseScope,
  173. },
  174. },
  175. )
  176. deleteHandler := release.NewDeleteReleaseHandler(
  177. config,
  178. factory.GetDecoderValidator(),
  179. factory.GetResultWriter(),
  180. )
  181. routes = append(routes, &router.Route{
  182. Endpoint: deleteEndpoint,
  183. Handler: deleteHandler,
  184. Router: r,
  185. })
  186. return routes, newPath
  187. }