infra.go 11 KB

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