stack.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/update_config -> stacks.NewCreateStackHandler
  47. createPorterAppEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbCreate,
  50. Method: types.HTTPVerbPost,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath + "/update_config",
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.ClusterScope,
  59. },
  60. },
  61. )
  62. createPorterAppHandler := stacks.NewCreatePorterAppHandler(
  63. config,
  64. factory.GetDecoderValidator(),
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: createPorterAppEndpoint,
  69. Handler: createPorterAppHandler,
  70. Router: r,
  71. })
  72. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks -> stacks.NewCreateStackHandler
  73. createEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbCreate,
  76. Method: types.HTTPVerbPost,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath,
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. },
  86. },
  87. )
  88. createHandler := stacks.NewCreateStackHandler(
  89. config,
  90. factory.GetDecoderValidator(),
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &router.Route{
  94. Endpoint: createEndpoint,
  95. Handler: createHandler,
  96. Router: r,
  97. })
  98. // PATCH /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack} -> stacks.NewUpdateStackHandler
  99. updateEndpoint := factory.NewAPIEndpoint(
  100. &types.APIRequestMetadata{
  101. Verb: types.APIVerbCreate,
  102. Method: types.HTTPVerbPatch,
  103. Path: &types.Path{
  104. Parent: basePath,
  105. RelativePath: relPath + "/{stack}",
  106. },
  107. Scopes: []types.PermissionScope{
  108. types.UserScope,
  109. types.ProjectScope,
  110. types.ClusterScope,
  111. },
  112. },
  113. )
  114. updateHandler := stacks.NewUpdateStackHandler(
  115. config,
  116. factory.GetDecoderValidator(),
  117. factory.GetResultWriter(),
  118. )
  119. routes = append(routes, &router.Route{
  120. Endpoint: updateEndpoint,
  121. Handler: updateHandler,
  122. Router: r,
  123. })
  124. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack}/pr -> stacks.NewOpenStackPRHandler
  125. createSecretAndOpenGitHubPullRequestEndpoint := factory.NewAPIEndpoint(
  126. &types.APIRequestMetadata{
  127. Verb: types.APIVerbCreate,
  128. Method: types.HTTPVerbPost,
  129. Path: &types.Path{
  130. Parent: basePath,
  131. RelativePath: relPath + "/{stack}/pr",
  132. },
  133. Scopes: []types.PermissionScope{
  134. types.UserScope,
  135. types.ProjectScope,
  136. types.ClusterScope,
  137. },
  138. },
  139. )
  140. createSecretAndOpenGitHubPullRequestHandler := stacks.NewOpenStackPRHandler(
  141. config,
  142. factory.GetDecoderValidator(),
  143. factory.GetResultWriter(),
  144. )
  145. routes = append(routes, &router.Route{
  146. Endpoint: createSecretAndOpenGitHubPullRequestEndpoint,
  147. Handler: createSecretAndOpenGitHubPullRequestHandler,
  148. Router: r,
  149. })
  150. return routes, newPath
  151. }