2
0

infra.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/database"
  6. "github.com/porter-dev/porter/api/server/handlers/infra"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/server/shared/router"
  10. "github.com/porter-dev/porter/api/types"
  11. )
  12. func NewInfraScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  13. return &router.Registerer{
  14. GetRoutes: GetInfraScopedRoutes,
  15. Children: children,
  16. }
  17. }
  18. func GetInfraScopedRoutes(
  19. r chi.Router,
  20. config *config.Config,
  21. basePath *types.Path,
  22. factory shared.APIEndpointFactory,
  23. children ...*router.Registerer,
  24. ) []*router.Route {
  25. routes, projPath := getInfraRoutes(r, config, basePath, factory)
  26. if len(children) > 0 {
  27. r.Route(projPath.RelativePath, func(r chi.Router) {
  28. for _, child := range children {
  29. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  30. routes = append(routes, childRoutes...)
  31. }
  32. })
  33. }
  34. return routes
  35. }
  36. func getInfraRoutes(
  37. r chi.Router,
  38. config *config.Config,
  39. basePath *types.Path,
  40. factory shared.APIEndpointFactory,
  41. ) ([]*router.Route, *types.Path) {
  42. relPath := "/infras/{infra_id}"
  43. newPath := &types.Path{
  44. Parent: basePath,
  45. RelativePath: relPath,
  46. }
  47. routes := make([]*router.Route, 0)
  48. // GET /api/projects/{project_id}/infra -> project.NewInfraListHandler
  49. listInfraEndpoint := factory.NewAPIEndpoint(
  50. &types.APIRequestMetadata{
  51. Verb: types.APIVerbGet,
  52. Method: types.HTTPVerbGet,
  53. Path: &types.Path{
  54. Parent: basePath,
  55. RelativePath: "/infra",
  56. },
  57. Scopes: []types.PermissionScope{
  58. types.UserScope,
  59. types.ProjectScope,
  60. },
  61. },
  62. )
  63. listInfraHandler := infra.NewInfraListHandler(
  64. config,
  65. factory.GetDecoderValidator(),
  66. factory.GetResultWriter(),
  67. )
  68. routes = append(routes, &router.Route{
  69. Endpoint: listInfraEndpoint,
  70. Handler: listInfraHandler,
  71. Router: r,
  72. })
  73. // GET /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraGetHandler
  74. getEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbGet,
  77. Method: types.HTTPVerbGet,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: relPath,
  81. },
  82. Scopes: []types.PermissionScope{
  83. types.UserScope,
  84. types.ProjectScope,
  85. types.InfraScope,
  86. },
  87. },
  88. )
  89. getHandler := infra.NewInfraGetHandler(
  90. config,
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &router.Route{
  94. Endpoint: getEndpoint,
  95. Handler: getHandler,
  96. Router: r,
  97. })
  98. // POST /api/projects/{project_id}/infras/{infra_id}/retry_create -> infra.NewInfraRetryHandler
  99. retryCreateEndpoint := factory.NewAPIEndpoint(
  100. &types.APIRequestMetadata{
  101. Verb: types.APIVerbUpdate,
  102. Method: types.HTTPVerbPost,
  103. Path: &types.Path{
  104. Parent: basePath,
  105. RelativePath: relPath + "/retry_create",
  106. },
  107. Scopes: []types.PermissionScope{
  108. types.UserScope,
  109. types.ProjectScope,
  110. types.InfraScope,
  111. },
  112. },
  113. )
  114. retryCreateHandler := infra.NewInfraRetryCreateHandler(
  115. config,
  116. factory.GetDecoderValidator(),
  117. factory.GetResultWriter(),
  118. )
  119. routes = append(routes, &router.Route{
  120. Endpoint: retryCreateEndpoint,
  121. Handler: retryCreateHandler,
  122. Router: r,
  123. })
  124. // POST /api/projects/{project_id}/infras/{infra_id}/update -> infra.NewInfraUpdateHandler
  125. updateEndpoint := factory.NewAPIEndpoint(
  126. &types.APIRequestMetadata{
  127. Verb: types.APIVerbUpdate,
  128. Method: types.HTTPVerbPost,
  129. Path: &types.Path{
  130. Parent: basePath,
  131. RelativePath: relPath + "/update",
  132. },
  133. Scopes: []types.PermissionScope{
  134. types.UserScope,
  135. types.ProjectScope,
  136. types.InfraScope,
  137. },
  138. },
  139. )
  140. updateHandler := infra.NewInfraUpdateHandler(
  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}/infras/{infra_id}/retry_delete -> infra.NewInfraRetryDeleteHandler
  151. retryDeleteEndpoint := factory.NewAPIEndpoint(
  152. &types.APIRequestMetadata{
  153. Verb: types.APIVerbUpdate,
  154. Method: types.HTTPVerbPost,
  155. Path: &types.Path{
  156. Parent: basePath,
  157. RelativePath: relPath + "/retry_delete",
  158. },
  159. Scopes: []types.PermissionScope{
  160. types.UserScope,
  161. types.ProjectScope,
  162. types.InfraScope,
  163. },
  164. },
  165. )
  166. retryDeleteHandler := infra.NewInfraRetryDeleteHandler(
  167. config,
  168. factory.GetDecoderValidator(),
  169. factory.GetResultWriter(),
  170. )
  171. routes = append(routes, &router.Route{
  172. Endpoint: retryDeleteEndpoint,
  173. Handler: retryDeleteHandler,
  174. Router: r,
  175. })
  176. // GET /api/projects/{project_id}/infras/{infra_id}/operations -> infra.NewInfraListOperationsHandler
  177. listOperationsEndpoint := factory.NewAPIEndpoint(
  178. &types.APIRequestMetadata{
  179. Verb: types.APIVerbList,
  180. Method: types.HTTPVerbGet,
  181. Path: &types.Path{
  182. Parent: basePath,
  183. RelativePath: relPath + "/operations",
  184. },
  185. Scopes: []types.PermissionScope{
  186. types.UserScope,
  187. types.ProjectScope,
  188. types.InfraScope,
  189. },
  190. },
  191. )
  192. listOperationsHandler := infra.NewInfraListOperationsHandler(
  193. config,
  194. factory.GetResultWriter(),
  195. )
  196. routes = append(routes, &router.Route{
  197. Endpoint: listOperationsEndpoint,
  198. Handler: listOperationsHandler,
  199. Router: r,
  200. })
  201. // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id} -> infra.NewInfraGetOperationHandler
  202. getOperationEndpoint := factory.NewAPIEndpoint(
  203. &types.APIRequestMetadata{
  204. Verb: types.APIVerbGet,
  205. Method: types.HTTPVerbGet,
  206. Path: &types.Path{
  207. Parent: basePath,
  208. RelativePath: fmt.Sprintf("%s/operations/{%s}", relPath, types.URLParamOperationID),
  209. },
  210. Scopes: []types.PermissionScope{
  211. types.UserScope,
  212. types.ProjectScope,
  213. types.InfraScope,
  214. types.OperationScope,
  215. },
  216. },
  217. )
  218. getOperationHandler := infra.NewInfraGetOperationHandler(
  219. config,
  220. factory.GetResultWriter(),
  221. )
  222. routes = append(routes, &router.Route{
  223. Endpoint: getOperationEndpoint,
  224. Handler: getOperationHandler,
  225. Router: r,
  226. })
  227. // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/state -> infra.NewInfraStreamStateHandler
  228. streamStateEndpoint := factory.NewAPIEndpoint(
  229. &types.APIRequestMetadata{
  230. Verb: types.APIVerbGet,
  231. Method: types.HTTPVerbGet,
  232. Path: &types.Path{
  233. Parent: basePath,
  234. RelativePath: fmt.Sprintf("%s/operations/{%s}/state", relPath, types.URLParamOperationID),
  235. },
  236. Scopes: []types.PermissionScope{
  237. types.UserScope,
  238. types.ProjectScope,
  239. types.InfraScope,
  240. types.OperationScope,
  241. },
  242. IsWebsocket: true,
  243. },
  244. )
  245. streamStateHandler := infra.NewInfraStreamStateHandler(
  246. config,
  247. factory.GetResultWriter(),
  248. )
  249. routes = append(routes, &router.Route{
  250. Endpoint: streamStateEndpoint,
  251. Handler: streamStateHandler,
  252. Router: r,
  253. })
  254. // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/log_stream -> infra.NewInfraStreamLogHandler
  255. streamLogEndpoint := factory.NewAPIEndpoint(
  256. &types.APIRequestMetadata{
  257. Verb: types.APIVerbGet,
  258. Method: types.HTTPVerbGet,
  259. Path: &types.Path{
  260. Parent: basePath,
  261. RelativePath: fmt.Sprintf("%s/operations/{%s}/log_stream", relPath, types.URLParamOperationID),
  262. },
  263. Scopes: []types.PermissionScope{
  264. types.UserScope,
  265. types.ProjectScope,
  266. types.InfraScope,
  267. types.OperationScope,
  268. },
  269. IsWebsocket: true,
  270. },
  271. )
  272. streamLogHandler := infra.NewInfraStreamLogHandler(
  273. config,
  274. factory.GetResultWriter(),
  275. )
  276. routes = append(routes, &router.Route{
  277. Endpoint: streamLogEndpoint,
  278. Handler: streamLogHandler,
  279. Router: r,
  280. })
  281. // GET /api/projects/{project_id}/infras/{infra_id}/operations/{operation_id}/logs -> infra.NewInfraGetOperationLogsHandler
  282. getOperationLogsEndpoint := factory.NewAPIEndpoint(
  283. &types.APIRequestMetadata{
  284. Verb: types.APIVerbGet,
  285. Method: types.HTTPVerbGet,
  286. Path: &types.Path{
  287. Parent: basePath,
  288. RelativePath: fmt.Sprintf("%s/operations/{%s}/logs", relPath, types.URLParamOperationID),
  289. },
  290. Scopes: []types.PermissionScope{
  291. types.UserScope,
  292. types.ProjectScope,
  293. types.InfraScope,
  294. types.OperationScope,
  295. },
  296. },
  297. )
  298. getOperationLogsHandler := infra.NewInfraGetOperationLogsHandler(
  299. config,
  300. factory.GetResultWriter(),
  301. )
  302. routes = append(routes, &router.Route{
  303. Endpoint: getOperationLogsEndpoint,
  304. Handler: getOperationLogsHandler,
  305. Router: r,
  306. })
  307. // GET /api/projects/{project_id}/infras/{infra_id}/state -> infra.NewInfraGetStateHandler
  308. getStateEndpoint := factory.NewAPIEndpoint(
  309. &types.APIRequestMetadata{
  310. Verb: types.APIVerbGet,
  311. Method: types.HTTPVerbGet,
  312. Path: &types.Path{
  313. Parent: basePath,
  314. RelativePath: relPath + "/state",
  315. },
  316. Scopes: []types.PermissionScope{
  317. types.UserScope,
  318. types.ProjectScope,
  319. types.InfraScope,
  320. },
  321. },
  322. )
  323. getStateHandler := infra.NewInfraGetStateHandler(
  324. config,
  325. factory.GetResultWriter(),
  326. )
  327. routes = append(routes, &router.Route{
  328. Endpoint: getStateEndpoint,
  329. Handler: getStateHandler,
  330. Router: r,
  331. })
  332. // DELETE /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraDeleteHandler
  333. deleteEndpoint := factory.NewAPIEndpoint(
  334. &types.APIRequestMetadata{
  335. Verb: types.APIVerbDelete,
  336. Method: types.HTTPVerbDelete,
  337. Path: &types.Path{
  338. Parent: basePath,
  339. RelativePath: relPath,
  340. },
  341. Scopes: []types.PermissionScope{
  342. types.UserScope,
  343. types.ProjectScope,
  344. types.InfraScope,
  345. },
  346. },
  347. )
  348. deleteHandler := infra.NewInfraDeleteHandler(
  349. config,
  350. factory.GetDecoderValidator(),
  351. factory.GetResultWriter(),
  352. )
  353. routes = append(routes, &router.Route{
  354. Endpoint: deleteEndpoint,
  355. Handler: deleteHandler,
  356. Router: r,
  357. })
  358. // POST /api/projects/{project_id}/infras/{infra_id}/database -> database.NewDatabaseUpdateStatusHandler
  359. updateDBStatusEndpoint := factory.NewAPIEndpoint(
  360. &types.APIRequestMetadata{
  361. Verb: types.APIVerbUpdate,
  362. Method: types.HTTPVerbPost,
  363. Path: &types.Path{
  364. Parent: basePath,
  365. RelativePath: relPath + "/database",
  366. },
  367. Scopes: []types.PermissionScope{
  368. types.UserScope,
  369. types.ProjectScope,
  370. types.InfraScope,
  371. },
  372. },
  373. )
  374. updateDBStatusHandler := database.NewDatabaseUpdateStatusHandler(
  375. config,
  376. factory.GetDecoderValidator(),
  377. )
  378. routes = append(routes, &router.Route{
  379. Endpoint: updateDBStatusEndpoint,
  380. Handler: updateDBStatusHandler,
  381. Router: r,
  382. })
  383. return routes, newPath
  384. }