porter_app.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  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} -> porter_app.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: fmt.Sprintf("%s/{%s}", relPath, types.URLParamStackName),
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. },
  61. },
  62. )
  63. getPorterAppHandler := porter_app.NewGetPorterAppHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: getPorterAppEndpoint,
  69. Handler: getPorterAppHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/clusters/{cluster_id}/stacks/{name} -> porter_app.NewPorterAppListHandler
  73. listPorterAppEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbList,
  76. Method: types.HTTPVerbGet,
  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. listPorterAppHandler := porter_app.NewPorterAppListHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &router.Route{
  93. Endpoint: listPorterAppEndpoint,
  94. Handler: listPorterAppHandler,
  95. Router: r,
  96. })
  97. // DELETE /api/projects/{project_id}/clusters/{cluster_id}/stacks -> release.NewDeletePorterAppByNameHandler
  98. deletePorterAppByNameEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbDelete,
  101. Method: types.HTTPVerbDelete,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamStackName),
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. types.ClusterScope,
  110. },
  111. },
  112. )
  113. deletePorterAppByNameHandler := porter_app.NewDeletePorterAppByNameHandler(
  114. config,
  115. factory.GetDecoderValidator(),
  116. factory.GetResultWriter(),
  117. )
  118. routes = append(routes, &router.Route{
  119. Endpoint: deletePorterAppByNameEndpoint,
  120. Handler: deletePorterAppByNameHandler,
  121. Router: r,
  122. })
  123. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack} -> porter_app.NewCreatePorterAppHandler
  124. createPorterAppEndpoint := factory.NewAPIEndpoint(
  125. &types.APIRequestMetadata{
  126. Verb: types.APIVerbCreate,
  127. Method: types.HTTPVerbPost,
  128. Path: &types.Path{
  129. Parent: basePath,
  130. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamStackName),
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. types.ClusterScope,
  136. },
  137. },
  138. )
  139. createPorterAppHandler := porter_app.NewCreatePorterAppHandler(
  140. config,
  141. factory.GetDecoderValidator(),
  142. factory.GetResultWriter(),
  143. )
  144. routes = append(routes, &router.Route{
  145. Endpoint: createPorterAppEndpoint,
  146. Handler: createPorterAppHandler,
  147. Router: r,
  148. })
  149. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack}/rollback -> porter_app.NewRollbackPorterAppHandler
  150. rollbackPorterAppEndpoint := factory.NewAPIEndpoint(
  151. &types.APIRequestMetadata{
  152. Verb: types.APIVerbCreate,
  153. Method: types.HTTPVerbPost,
  154. Path: &types.Path{
  155. Parent: basePath,
  156. RelativePath: fmt.Sprintf("%s/{%s}/rollback", relPath, types.URLParamStackName),
  157. },
  158. Scopes: []types.PermissionScope{
  159. types.UserScope,
  160. types.ProjectScope,
  161. types.ClusterScope,
  162. },
  163. },
  164. )
  165. rollbackPorterAppHandler := porter_app.NewRollbackPorterAppHandler(
  166. config,
  167. factory.GetDecoderValidator(),
  168. factory.GetResultWriter(),
  169. )
  170. routes = append(routes, &router.Route{
  171. Endpoint: rollbackPorterAppEndpoint,
  172. Handler: rollbackPorterAppHandler,
  173. Router: r,
  174. })
  175. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{stack}/pr -> porter_app.NewOpenStackPRHandler
  176. createSecretAndOpenGitHubPullRequestEndpoint := factory.NewAPIEndpoint(
  177. &types.APIRequestMetadata{
  178. Verb: types.APIVerbCreate,
  179. Method: types.HTTPVerbPost,
  180. Path: &types.Path{
  181. Parent: basePath,
  182. RelativePath: fmt.Sprintf("%s/{%s}/pr", relPath, types.URLParamStackName),
  183. },
  184. Scopes: []types.PermissionScope{
  185. types.UserScope,
  186. types.ProjectScope,
  187. types.ClusterScope,
  188. },
  189. },
  190. )
  191. createSecretAndOpenGitHubPullRequestHandler := porter_app.NewOpenStackPRHandler(
  192. config,
  193. factory.GetDecoderValidator(),
  194. factory.GetResultWriter(),
  195. )
  196. routes = append(routes, &router.Route{
  197. Endpoint: createSecretAndOpenGitHubPullRequestEndpoint,
  198. Handler: createSecretAndOpenGitHubPullRequestHandler,
  199. Router: r,
  200. })
  201. // GET /api/projects/{project_id}/clusters/{cluster_id}/stacks/{name}/events -> porter_app.NewPorterAppEventListHandler
  202. listPorterAppEventsEndpoint := factory.NewAPIEndpoint(
  203. &types.APIRequestMetadata{
  204. Verb: types.APIVerbList,
  205. Method: types.HTTPVerbGet,
  206. Path: &types.Path{
  207. Parent: basePath,
  208. RelativePath: fmt.Sprintf("%s/{%s}/events", relPath, types.URLParamStackName),
  209. },
  210. Scopes: []types.PermissionScope{
  211. types.UserScope,
  212. types.ProjectScope,
  213. types.ClusterScope,
  214. },
  215. },
  216. )
  217. listPorterAppEventsHandler := porter_app.NewPorterAppEventListHandler(
  218. config,
  219. factory.GetResultWriter(),
  220. )
  221. routes = append(routes, &router.Route{
  222. Endpoint: listPorterAppEventsEndpoint,
  223. Handler: listPorterAppEventsHandler,
  224. Router: r,
  225. })
  226. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/{name}/events -> porter_app.NewCreatePorterAppEventEndpoint
  227. createPorterAppEventEndpoint := factory.NewAPIEndpoint(
  228. &types.APIRequestMetadata{
  229. Verb: types.APIVerbCreate,
  230. Method: types.HTTPVerbPost,
  231. Path: &types.Path{
  232. Parent: basePath,
  233. RelativePath: fmt.Sprintf("%s/{%s}/events", relPath, types.URLParamStackName),
  234. },
  235. Scopes: []types.PermissionScope{
  236. types.UserScope,
  237. types.ProjectScope,
  238. types.ClusterScope,
  239. },
  240. },
  241. )
  242. createPorterAppEventHandler := porter_app.NewCreateUpdatePorterAppEventHandler(
  243. config,
  244. factory.GetDecoderValidator(),
  245. factory.GetResultWriter(),
  246. )
  247. routes = append(routes, &router.Route{
  248. Endpoint: createPorterAppEventEndpoint,
  249. Handler: createPorterAppEventHandler,
  250. Router: r,
  251. })
  252. // POST /api/projects/{project_id}/clusters/{cluster_id}/stacks/analytics -> porter_app.NewPorterAppAnalyticsHandler
  253. porterAppAnalyticsEndpoint := factory.NewAPIEndpoint(
  254. &types.APIRequestMetadata{
  255. Verb: types.APIVerbUpdate,
  256. Method: types.HTTPVerbPost,
  257. Path: &types.Path{
  258. Parent: basePath,
  259. RelativePath: fmt.Sprintf("%s/analytics", relPath),
  260. },
  261. Scopes: []types.PermissionScope{
  262. types.UserScope,
  263. types.ProjectScope,
  264. types.ClusterScope,
  265. },
  266. },
  267. )
  268. porterAppAnalyticsHandler := porter_app.NewPorterAppAnalyticsHandler(
  269. config,
  270. factory.GetDecoderValidator(),
  271. factory.GetResultWriter(),
  272. )
  273. routes = append(routes, &router.Route{
  274. Endpoint: porterAppAnalyticsEndpoint,
  275. Handler: porterAppAnalyticsHandler,
  276. Router: r,
  277. })
  278. // GET /api/projects/{project_id}/clusters/{cluster_id}/stacks/logs -> cluster.NewGetChartLogsWithinTimeRangeHandler
  279. getChartLogsWithinTimeRangeEndpoint := factory.NewAPIEndpoint(
  280. &types.APIRequestMetadata{
  281. Verb: types.APIVerbGet,
  282. Method: types.HTTPVerbGet,
  283. Path: &types.Path{
  284. Parent: basePath,
  285. RelativePath: fmt.Sprintf("%s/logs", relPath),
  286. },
  287. Scopes: []types.PermissionScope{
  288. types.UserScope,
  289. types.ProjectScope,
  290. types.ClusterScope,
  291. },
  292. },
  293. )
  294. getChartLogsWithinTimeRangeHandler := porter_app.NewGetLogsWithinTimeRangeHandler(
  295. config,
  296. factory.GetDecoderValidator(),
  297. factory.GetResultWriter(),
  298. )
  299. routes = append(routes, &router.Route{
  300. Endpoint: getChartLogsWithinTimeRangeEndpoint,
  301. Handler: getChartLogsWithinTimeRangeHandler,
  302. Router: r,
  303. })
  304. return routes, newPath
  305. }