infra.go 10 KB

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