deployment_target_legacy.go 4.3 KB

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