deployment_target.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package router
  2. import (
  3. "github.com/go-chi/chi/v5"
  4. "github.com/porter-dev/porter/api/server/handlers/deployment_target"
  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/server/shared/router"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. // NewDeploymentTargetScopedRegisterer applies /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets routes to the gin Router
  11. func NewDeploymentTargetScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetDeploymentTargetScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. // GetDeploymentTargetScopedRoutes returns the router handlers specific to deployment targets
  18. func GetDeploymentTargetScopedRoutes(
  19. r chi.Router,
  20. config *config.Config,
  21. basePath *types.Path,
  22. factory shared.APIEndpointFactory,
  23. children ...*router.Registerer,
  24. ) []*router.Route {
  25. routes, projPath := getDeploymentTargetRoutes(r, config, basePath, factory)
  26. if len(children) > 0 {
  27. r.Route(projPath.RelativePath, func(r chi.Router) {
  28. for _, child := range children {
  29. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  30. routes = append(routes, childRoutes...)
  31. }
  32. })
  33. }
  34. return routes
  35. }
  36. // getDeploymentTargetRoutes gets the routes specific to deployment targets
  37. func getDeploymentTargetRoutes(
  38. r chi.Router,
  39. config *config.Config,
  40. basePath *types.Path,
  41. factory shared.APIEndpointFactory,
  42. ) ([]*router.Route, *types.Path) {
  43. relPath := "/deployment-targets"
  44. newPath := &types.Path{
  45. Parent: basePath,
  46. RelativePath: relPath,
  47. }
  48. var routes []*router.Route
  49. // POST /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets -> deployment_target.CreateDeploymentTargetHandler
  50. createDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  51. &types.APIRequestMetadata{
  52. Verb: types.APIVerbCreate,
  53. Method: types.HTTPVerbPost,
  54. Path: &types.Path{
  55. Parent: basePath,
  56. RelativePath: relPath,
  57. },
  58. Scopes: []types.PermissionScope{
  59. types.UserScope,
  60. types.ProjectScope,
  61. types.ClusterScope,
  62. },
  63. },
  64. )
  65. createDeploymentTargetHandler := deployment_target.NewCreateDeploymentTargetHandler(
  66. config,
  67. factory.GetDecoderValidator(),
  68. factory.GetResultWriter(),
  69. )
  70. routes = append(routes, &router.Route{
  71. Endpoint: createDeploymentTargetEndpoint,
  72. Handler: createDeploymentTargetHandler,
  73. Router: r,
  74. })
  75. // GET /api/projects/{project_id}/clusters/{cluster_id}/deployment-targets -> deployment_target.ListDeploymentTargetsHandler
  76. listDeploymentTargetsEndpoint := factory.NewAPIEndpoint(
  77. &types.APIRequestMetadata{
  78. Verb: types.APIVerbList,
  79. Method: types.HTTPVerbGet,
  80. Path: &types.Path{
  81. Parent: basePath,
  82. RelativePath: relPath,
  83. },
  84. Scopes: []types.PermissionScope{
  85. types.UserScope,
  86. types.ProjectScope,
  87. types.ClusterScope,
  88. },
  89. },
  90. )
  91. listDeploymentTargetsHandler := deployment_target.NewListDeploymentTargetsHandler(
  92. config,
  93. factory.GetDecoderValidator(),
  94. factory.GetResultWriter(),
  95. )
  96. routes = append(routes, &router.Route{
  97. Endpoint: listDeploymentTargetsEndpoint,
  98. Handler: listDeploymentTargetsHandler,
  99. Router: r,
  100. })
  101. return routes, newPath
  102. }