deployment_target.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/deployment_target"
  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. // NewDeploymentTargetScopedRegisterer applies /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets routes to the gin Router
  12. func NewDeploymentTargetScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  13. return &router.Registerer{
  14. GetRoutes: GetDeploymentTargetScopedRoutes,
  15. Children: children,
  16. }
  17. }
  18. // GetDeploymentTargetScopedRoutes returns the router handlers specific to deployment targets
  19. func GetDeploymentTargetScopedRoutes(
  20. r chi.Router,
  21. config *config.Config,
  22. basePath *types.Path,
  23. factory shared.APIEndpointFactory,
  24. children ...*router.Registerer,
  25. ) []*router.Route {
  26. routes, projPath := getDeploymentTargetRoutes(r, config, basePath, factory)
  27. if len(children) > 0 {
  28. r.Route(projPath.RelativePath, func(r chi.Router) {
  29. for _, child := range children {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. }
  33. })
  34. }
  35. return routes
  36. }
  37. // getDeploymentTargetRoutes gets the routes specific to deployment targets
  38. func getDeploymentTargetRoutes(
  39. r chi.Router,
  40. config *config.Config,
  41. basePath *types.Path,
  42. factory shared.APIEndpointFactory,
  43. ) ([]*router.Route, *types.Path) {
  44. relPath := "/deployment-targets"
  45. newPath := &types.Path{
  46. Parent: basePath,
  47. RelativePath: relPath,
  48. }
  49. var routes []*router.Route
  50. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets -> deployment_target.CreateDeploymentTargetHandler
  51. createDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  52. &types.APIRequestMetadata{
  53. Verb: types.APIVerbCreate,
  54. Method: types.HTTPVerbPost,
  55. Path: &types.Path{
  56. Parent: basePath,
  57. RelativePath: relPath,
  58. },
  59. Scopes: []types.PermissionScope{
  60. types.UserScope,
  61. types.ProjectScope,
  62. types.ClusterScope,
  63. },
  64. },
  65. )
  66. createDeploymentTargetHandler := deployment_target.NewCreateDeploymentTargetHandler(
  67. config,
  68. factory.GetDecoderValidator(),
  69. factory.GetResultWriter(),
  70. )
  71. routes = append(routes, &router.Route{
  72. Endpoint: createDeploymentTargetEndpoint,
  73. Handler: createDeploymentTargetHandler,
  74. Router: r,
  75. })
  76. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets -> deployment_target.ListDeploymentTargetsHandler
  77. listDeploymentTargetsEndpoint := factory.NewAPIEndpoint(
  78. &types.APIRequestMetadata{
  79. Verb: types.APIVerbList,
  80. Method: types.HTTPVerbGet,
  81. Path: &types.Path{
  82. Parent: basePath,
  83. RelativePath: relPath,
  84. },
  85. Scopes: []types.PermissionScope{
  86. types.UserScope,
  87. types.ProjectScope,
  88. types.ClusterScope,
  89. },
  90. },
  91. )
  92. listDeploymentTargetsHandler := deployment_target.NewListDeploymentTargetsHandler(
  93. config,
  94. factory.GetDecoderValidator(),
  95. factory.GetResultWriter(),
  96. )
  97. routes = append(routes, &router.Route{
  98. Endpoint: listDeploymentTargetsEndpoint,
  99. Handler: listDeploymentTargetsHandler,
  100. Router: r,
  101. })
  102. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets/{deployment_target_id} -> deployment_target.DeleteDeploymentTargetHandler
  103. deleteDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  104. &types.APIRequestMetadata{
  105. Verb: types.APIVerbDelete,
  106. Method: types.HTTPVerbDelete,
  107. Path: &types.Path{
  108. Parent: basePath,
  109. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamDeploymentTargetID),
  110. },
  111. Scopes: []types.PermissionScope{
  112. types.UserScope,
  113. types.ProjectScope,
  114. types.ClusterScope,
  115. },
  116. },
  117. )
  118. deleteDeploymentTargetHandler := deployment_target.NewDeleteDeploymentTargetHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &router.Route{
  124. Endpoint: deleteDeploymentTargetEndpoint,
  125. Handler: deleteDeploymentTargetHandler,
  126. Router: r,
  127. })
  128. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets/{deployment_target_id} -> deployment_target.GetDeploymentTargetHandler
  129. getDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  130. &types.APIRequestMetadata{
  131. Verb: types.APIVerbGet,
  132. Method: types.HTTPVerbGet,
  133. Path: &types.Path{
  134. Parent: basePath,
  135. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamDeploymentTargetID),
  136. },
  137. Scopes: []types.PermissionScope{
  138. types.UserScope,
  139. types.ProjectScope,
  140. types.ClusterScope,
  141. },
  142. },
  143. )
  144. getDeploymentTargetHandler := deployment_target.NewGetDeploymentTargetHandler(
  145. config,
  146. factory.GetDecoderValidator(),
  147. factory.GetResultWriter(),
  148. )
  149. routes = append(routes, &router.Route{
  150. Endpoint: getDeploymentTargetEndpoint,
  151. Handler: getDeploymentTargetHandler,
  152. Router: r,
  153. })
  154. return routes, newPath
  155. }