stack.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/stacks"
  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. func NewStackScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetStackScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetStackScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getStackRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getStackRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/stacks"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. var routes []*router.Route
  47. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/update_config -> stacks.NewCreateStackHandler
  48. createPorterAppEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbCreate,
  51. Method: types.HTTPVerbPost,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath + "/update_config",
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. },
  61. },
  62. )
  63. createPorterAppHandler := stacks.NewCreatePorterAppHandler(
  64. config,
  65. factory.GetDecoderValidator(),
  66. factory.GetResultWriter(),
  67. )
  68. routes = append(routes, &router.Route{
  69. Endpoint: createPorterAppEndpoint,
  70. Handler: createPorterAppHandler,
  71. Router: r,
  72. })
  73. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks -> stacks.NewCreateStackHandler
  74. createEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbCreate,
  77. Method: types.HTTPVerbPost,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath,
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. types.ClusterScope,
  86. },
  87. },
  88. )
  89. createHandler := stacks.NewCreateStackHandler(
  90. config,
  91. factory.GetDecoderValidator(),
  92. factory.GetResultWriter(),
  93. )
  94. routes = append(routes, &router.Route{
  95. Endpoint: createEndpoint,
  96. Handler: createHandler,
  97. Router: r,
  98. })
  99. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack} -> stacks.NewUpdateStackHandler
  100. updateEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbCreate,
  103. Method: types.HTTPVerbPatch,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: relPath + "/{stack}",
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. types.ClusterScope,
  112. },
  113. },
  114. )
  115. updateHandler := stacks.NewUpdateStackHandler(
  116. config,
  117. factory.GetDecoderValidator(),
  118. factory.GetResultWriter(),
  119. )
  120. routes = append(routes, &router.Route{
  121. Endpoint: updateEndpoint,
  122. Handler: updateHandler,
  123. Router: r,
  124. })
  125. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack}/pr -> stacks.NewOpenStackPRHandler
  126. createSecretAndOpenGitHubPullRequestEndpoint := factory.NewAPIEndpoint(
  127. &types.APIRequestMetadata{
  128. Verb: types.APIVerbCreate,
  129. Method: types.HTTPVerbPost,
  130. Path: &types.Path{
  131. Parent: basePath,
  132. RelativePath: fmt.Sprintf("%s/{%s}/pr", relPath, types.URLParamStackName),
  133. },
  134. Scopes: []types.PermissionScope{
  135. types.UserScope,
  136. types.ProjectScope,
  137. types.ClusterScope,
  138. },
  139. },
  140. )
  141. createSecretAndOpenGitHubPullRequestHandler := stacks.NewOpenStackPRHandler(
  142. config,
  143. factory.GetDecoderValidator(),
  144. factory.GetResultWriter(),
  145. )
  146. routes = append(routes, &router.Route{
  147. Endpoint: createSecretAndOpenGitHubPullRequestEndpoint,
  148. Handler: createSecretAndOpenGitHubPullRequestHandler,
  149. Router: r,
  150. })
  151. return routes, newPath
  152. }