2
0

stack.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/stacks"
  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. func NewStackScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  11. return &router.Registerer{
  12. GetRoutes: GetStackScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetStackScopedRoutes(
  17. r chi.Router,
  18. config *config.Config,
  19. basePath *types.Path,
  20. factory shared.APIEndpointFactory,
  21. children ...*router.Registerer,
  22. ) []*router.Route {
  23. routes, projPath := getStackRoutes(r, config, basePath, factory)
  24. if len(children) > 0 {
  25. r.Route(projPath.RelativePath, func(r chi.Router) {
  26. for _, child := range children {
  27. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  28. routes = append(routes, childRoutes...)
  29. }
  30. })
  31. }
  32. return routes
  33. }
  34. func getStackRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*router.Route, *types.Path) {
  40. relPath := "/stacks"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. var routes []*router.Route
  46. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks -> stacks.NewCreateStackHandler
  47. createEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbCreate,
  50. Method: types.HTTPVerbPost,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath,
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.ClusterScope,
  59. },
  60. },
  61. )
  62. createHandler := stacks.NewCreateStackHandler(
  63. config,
  64. factory.GetDecoderValidator(),
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: createEndpoint,
  69. Handler: createHandler,
  70. Router: r,
  71. })
  72. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack} -> stacks.NewUpdateStackHandler
  73. updateEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbCreate,
  76. Method: types.HTTPVerbPatch,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath + "/{stack}",
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. },
  86. },
  87. )
  88. updateHandler := stacks.NewUpdateStackHandler(
  89. config,
  90. factory.GetDecoderValidator(),
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &router.Route{
  94. Endpoint: updateEndpoint,
  95. Handler: updateHandler,
  96. Router: r,
  97. })
  98. return routes, newPath
  99. }