stack.go 4.9 KB

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