deployment_target.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return routes, newPath
  76. }