project_integration.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. project_integration "github.com/porter-dev/porter/api/server/handlers/project_integration"
  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 NewProjectIntegrationScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetProjectIntegrationScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetProjectIntegrationScopedRoutes(
  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 := getProjectIntegrationRoutes(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 getProjectIntegrationRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/integrations"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. routes := make([]*router.Route, 0)
  47. // GET /api/projects/{project_id}/integrations/oauth -> project_integration.NewListOAuthHandler
  48. listOAuthEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath + "/oauth",
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. },
  60. },
  61. )
  62. listOAuthHandler := project_integration.NewListOAuthHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &router.Route{
  67. Endpoint: listOAuthEndpoint,
  68. Handler: listOAuthHandler,
  69. Router: r,
  70. })
  71. // GET /api/projects/{project_id}/integrations/do -> project_integration.NewListDOHandler
  72. listDOEndpoint := factory.NewAPIEndpoint(
  73. &types.APIRequestMetadata{
  74. Verb: types.APIVerbGet,
  75. Method: types.HTTPVerbGet,
  76. Path: &types.Path{
  77. Parent: basePath,
  78. RelativePath: relPath + "/do",
  79. },
  80. Scopes: []types.PermissionScope{
  81. types.UserScope,
  82. types.ProjectScope,
  83. },
  84. },
  85. )
  86. listDOHandler := project_integration.NewListDOHandler(
  87. config,
  88. factory.GetResultWriter(),
  89. )
  90. routes = append(routes, &router.Route{
  91. Endpoint: listDOEndpoint,
  92. Handler: listDOHandler,
  93. Router: r,
  94. })
  95. // POST /api/projects/{project_id}/integrations/basic -> project_integration.NewCreateBasicHandler
  96. createBasicEndpoint := factory.NewAPIEndpoint(
  97. &types.APIRequestMetadata{
  98. Verb: types.APIVerbCreate,
  99. Method: types.HTTPVerbPost,
  100. Path: &types.Path{
  101. Parent: basePath,
  102. RelativePath: relPath + "/basic",
  103. },
  104. Scopes: []types.PermissionScope{
  105. types.UserScope,
  106. types.ProjectScope,
  107. },
  108. },
  109. )
  110. createBasicHandler := project_integration.NewCreateBasicHandler(
  111. config,
  112. factory.GetDecoderValidator(),
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &router.Route{
  116. Endpoint: createBasicEndpoint,
  117. Handler: createBasicHandler,
  118. Router: r,
  119. })
  120. // POST /api/projects/{project_id}/integrations/aws -> project_integration.NewCreateAWSHandler
  121. createAWSEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbCreate,
  124. Method: types.HTTPVerbPost,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: relPath + "/aws",
  128. },
  129. Scopes: []types.PermissionScope{
  130. types.UserScope,
  131. types.ProjectScope,
  132. },
  133. },
  134. )
  135. createAWSHandler := project_integration.NewCreateAWSHandler(
  136. config,
  137. factory.GetDecoderValidator(),
  138. factory.GetResultWriter(),
  139. )
  140. routes = append(routes, &router.Route{
  141. Endpoint: createAWSEndpoint,
  142. Handler: createAWSHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/integrations/aws -> project_integration.NewListAWSHandler
  146. listAWSEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbGet,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/aws",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. },
  158. },
  159. )
  160. listAWSHandler := project_integration.NewListAWSHandler(
  161. config,
  162. factory.GetResultWriter(),
  163. )
  164. routes = append(routes, &router.Route{
  165. Endpoint: listAWSEndpoint,
  166. Handler: listAWSHandler,
  167. Router: r,
  168. })
  169. // POST /api/projects/{project_id}/integrations/aws/overwrite -> project_integration.NewOverwriteAWSHandler
  170. overwriteAWSEndpoint := factory.NewAPIEndpoint(
  171. &types.APIRequestMetadata{
  172. Verb: types.APIVerbCreate,
  173. Method: types.HTTPVerbPost,
  174. Path: &types.Path{
  175. Parent: basePath,
  176. RelativePath: relPath + "/aws/overwrite",
  177. },
  178. Scopes: []types.PermissionScope{
  179. types.UserScope,
  180. types.ProjectScope,
  181. },
  182. },
  183. )
  184. overwriteAWSHandler := project_integration.NewOverwriteAWSHandler(
  185. config,
  186. factory.GetDecoderValidator(),
  187. factory.GetResultWriter(),
  188. )
  189. routes = append(routes, &router.Route{
  190. Endpoint: overwriteAWSEndpoint,
  191. Handler: overwriteAWSHandler,
  192. Router: r,
  193. })
  194. // POST /api/projects/{project_id}/integrations/aws/preflightcheck/usage -> project_integration.NewCreatePreflightCheckAWSHandler
  195. preflightCheckAWSUsageEndpoint := factory.NewAPIEndpoint(
  196. &types.APIRequestMetadata{
  197. Verb: types.APIVerbCreate,
  198. Method: types.HTTPVerbPost,
  199. Path: &types.Path{
  200. Parent: basePath,
  201. RelativePath: relPath + "/aws/preflight/usage",
  202. },
  203. Scopes: []types.PermissionScope{
  204. types.UserScope,
  205. types.ProjectScope,
  206. },
  207. },
  208. )
  209. preflightCheckAWSUsageHandler := project_integration.NewCreatePreflightCheckAWSUsageHandler(
  210. config,
  211. factory.GetDecoderValidator(),
  212. factory.GetResultWriter(),
  213. )
  214. routes = append(routes, &router.Route{
  215. Endpoint: preflightCheckAWSUsageEndpoint,
  216. Handler: preflightCheckAWSUsageHandler,
  217. Router: r,
  218. })
  219. // GET /api/projects/{project_id}/integrations/azure -> project_integration.NewListAzureHandler
  220. listAzureEndpoint := factory.NewAPIEndpoint(
  221. &types.APIRequestMetadata{
  222. Verb: types.APIVerbGet,
  223. Method: types.HTTPVerbGet,
  224. Path: &types.Path{
  225. Parent: basePath,
  226. RelativePath: relPath + "/azure",
  227. },
  228. Scopes: []types.PermissionScope{
  229. types.UserScope,
  230. types.ProjectScope,
  231. },
  232. },
  233. )
  234. listAzureHandler := project_integration.NewListAzureHandler(
  235. config,
  236. factory.GetResultWriter(),
  237. )
  238. routes = append(routes, &router.Route{
  239. Endpoint: listAzureEndpoint,
  240. Handler: listAzureHandler,
  241. Router: r,
  242. })
  243. // POST /api/projects/{project_id}/integrations/gcp -> project_integration.NewCreateGCPHandler
  244. createGCPEndpoint := factory.NewAPIEndpoint(
  245. &types.APIRequestMetadata{
  246. Verb: types.APIVerbCreate,
  247. Method: types.HTTPVerbPost,
  248. Path: &types.Path{
  249. Parent: basePath,
  250. RelativePath: relPath + "/gcp",
  251. },
  252. Scopes: []types.PermissionScope{
  253. types.UserScope,
  254. types.ProjectScope,
  255. },
  256. },
  257. )
  258. createGCPHandler := project_integration.NewCreateGCPHandler(
  259. config,
  260. factory.GetDecoderValidator(),
  261. factory.GetResultWriter(),
  262. )
  263. routes = append(routes, &router.Route{
  264. Endpoint: createGCPEndpoint,
  265. Handler: createGCPHandler,
  266. Router: r,
  267. })
  268. // GET /api/projects/{project_id}/integrations/gcp -> project_integration.NewListGCPHandler
  269. listGCPEndpoint := factory.NewAPIEndpoint(
  270. &types.APIRequestMetadata{
  271. Verb: types.APIVerbGet,
  272. Method: types.HTTPVerbGet,
  273. Path: &types.Path{
  274. Parent: basePath,
  275. RelativePath: relPath + "/gcp",
  276. },
  277. Scopes: []types.PermissionScope{
  278. types.UserScope,
  279. types.ProjectScope,
  280. },
  281. },
  282. )
  283. listGCPHandler := project_integration.NewListGCPHandler(
  284. config,
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &router.Route{
  288. Endpoint: listGCPEndpoint,
  289. Handler: listGCPHandler,
  290. Router: r,
  291. })
  292. // POST /api/projects/{project_id}/integrations/azure -> project_integration.NewCreateAzureHandler
  293. createAzureEndpoint := factory.NewAPIEndpoint(
  294. &types.APIRequestMetadata{
  295. Verb: types.APIVerbCreate,
  296. Method: types.HTTPVerbPost,
  297. Path: &types.Path{
  298. Parent: basePath,
  299. RelativePath: relPath + "/azure",
  300. },
  301. Scopes: []types.PermissionScope{
  302. types.UserScope,
  303. types.ProjectScope,
  304. },
  305. },
  306. )
  307. createAzureHandler := project_integration.NewCreateAzureHandler(
  308. config,
  309. factory.GetDecoderValidator(),
  310. factory.GetResultWriter(),
  311. )
  312. routes = append(routes, &router.Route{
  313. Endpoint: createAzureEndpoint,
  314. Handler: createAzureHandler,
  315. Router: r,
  316. })
  317. // GET /api/projects/{project_id}/integrations/gitlab
  318. listGitlabEndpoint := factory.NewAPIEndpoint(
  319. &types.APIRequestMetadata{
  320. Verb: types.APIVerbGet,
  321. Method: types.HTTPVerbGet,
  322. Path: &types.Path{
  323. Parent: basePath,
  324. RelativePath: relPath + "/gitlab",
  325. },
  326. Scopes: []types.PermissionScope{
  327. types.UserScope,
  328. types.ProjectScope,
  329. },
  330. },
  331. )
  332. listGitlabHandler := project_integration.NewListGitlabHandler(
  333. config,
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &router.Route{
  337. Endpoint: listGitlabEndpoint,
  338. Handler: listGitlabHandler,
  339. Router: r,
  340. })
  341. // POST /api/projects/{project_id}/integrations/gitlab
  342. createGitlabEndpoint := factory.NewAPIEndpoint(
  343. &types.APIRequestMetadata{
  344. Verb: types.APIVerbCreate,
  345. Method: types.HTTPVerbPost,
  346. Path: &types.Path{
  347. Parent: basePath,
  348. RelativePath: relPath + "/gitlab",
  349. },
  350. Scopes: []types.PermissionScope{
  351. types.UserScope,
  352. types.ProjectScope,
  353. },
  354. },
  355. )
  356. createGitlabHandler := project_integration.NewCreateGitlabIntegration(
  357. config,
  358. factory.GetDecoderValidator(),
  359. factory.GetResultWriter(),
  360. )
  361. routes = append(routes, &router.Route{
  362. Endpoint: createGitlabEndpoint,
  363. Handler: createGitlabHandler,
  364. Router: r,
  365. })
  366. // PATCH /api/projects/{project_id}/integrations/gitlab/{integration_id}
  367. // DELETE /api/projects/{project_id}/integrations/gitlab/{integration_id}
  368. // GET /api/projects/{project_id}/integrations/git
  369. listGitIntegrationsEndpoint := factory.NewAPIEndpoint(
  370. &types.APIRequestMetadata{
  371. Verb: types.APIVerbGet,
  372. Method: types.HTTPVerbGet,
  373. Path: &types.Path{
  374. Parent: basePath,
  375. RelativePath: relPath + "/git",
  376. },
  377. Scopes: []types.PermissionScope{
  378. types.UserScope,
  379. types.ProjectScope,
  380. },
  381. },
  382. )
  383. listGitIntegrationsHandler := project_integration.NewListGitIntegrationHandler(
  384. config,
  385. factory.GetDecoderValidator(),
  386. factory.GetResultWriter(),
  387. )
  388. routes = append(routes, &router.Route{
  389. Endpoint: listGitIntegrationsEndpoint,
  390. Handler: listGitIntegrationsHandler,
  391. Router: r,
  392. })
  393. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos
  394. listGitlabReposEndpoint := factory.NewAPIEndpoint(
  395. &types.APIRequestMetadata{
  396. Verb: types.APIVerbGet,
  397. Method: types.HTTPVerbGet,
  398. Path: &types.Path{
  399. Parent: basePath,
  400. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos", relPath, types.URLParamIntegrationID),
  401. },
  402. Scopes: []types.PermissionScope{
  403. types.UserScope,
  404. types.ProjectScope,
  405. types.GitlabIntegrationScope,
  406. },
  407. },
  408. )
  409. listGitlabReposHandler := project_integration.NewListGitlabReposHandler(
  410. config,
  411. factory.GetDecoderValidator(),
  412. factory.GetResultWriter(),
  413. )
  414. routes = append(routes, &router.Route{
  415. Endpoint: listGitlabReposEndpoint,
  416. Handler: listGitlabReposHandler,
  417. Router: r,
  418. })
  419. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/branches
  420. listGitlabRepoBranchesEndpoint := factory.NewAPIEndpoint(
  421. &types.APIRequestMetadata{
  422. Verb: types.APIVerbGet,
  423. Method: types.HTTPVerbGet,
  424. Path: &types.Path{
  425. Parent: basePath,
  426. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/branches",
  427. relPath, types.URLParamIntegrationID, types.URLParamGitRepoOwner, types.URLParamGitRepoName),
  428. },
  429. Scopes: []types.PermissionScope{
  430. types.UserScope,
  431. types.ProjectScope,
  432. types.GitlabIntegrationScope,
  433. },
  434. },
  435. )
  436. listGitlabRepoBranchesHandler := project_integration.NewListGitlabRepoBranchesHandler(
  437. config,
  438. factory.GetDecoderValidator(),
  439. factory.GetResultWriter(),
  440. )
  441. routes = append(routes, &router.Route{
  442. Endpoint: listGitlabRepoBranchesEndpoint,
  443. Handler: listGitlabRepoBranchesHandler,
  444. Router: r,
  445. })
  446. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/contents
  447. getGitlabRepoContentsEndpoint := factory.NewAPIEndpoint(
  448. &types.APIRequestMetadata{
  449. Verb: types.APIVerbGet,
  450. Method: types.HTTPVerbGet,
  451. Path: &types.Path{
  452. Parent: basePath,
  453. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/contents", relPath,
  454. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  455. types.URLParamGitRepoName, types.URLParamGitBranch),
  456. },
  457. Scopes: []types.PermissionScope{
  458. types.UserScope,
  459. types.ProjectScope,
  460. types.GitlabIntegrationScope,
  461. },
  462. },
  463. )
  464. getGitlabRepoContentsHandler := project_integration.NewGetGitlabRepoContentsHandler(
  465. config,
  466. factory.GetDecoderValidator(),
  467. factory.GetResultWriter(),
  468. )
  469. routes = append(routes, &router.Route{
  470. Endpoint: getGitlabRepoContentsEndpoint,
  471. Handler: getGitlabRepoContentsHandler,
  472. Router: r,
  473. })
  474. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/buildpack/detect
  475. getGitlabRepoBuildpackEndpoint := factory.NewAPIEndpoint(
  476. &types.APIRequestMetadata{
  477. Verb: types.APIVerbGet,
  478. Method: types.HTTPVerbGet,
  479. Path: &types.Path{
  480. Parent: basePath,
  481. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/buildpack/detect", relPath,
  482. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  483. types.URLParamGitRepoName, types.URLParamGitBranch),
  484. },
  485. Scopes: []types.PermissionScope{
  486. types.UserScope,
  487. types.ProjectScope,
  488. types.GitlabIntegrationScope,
  489. },
  490. },
  491. )
  492. getGitlabRepoBuildpackHandler := project_integration.NewGetGitlabRepoBuildpackHandler(
  493. config,
  494. factory.GetDecoderValidator(),
  495. factory.GetResultWriter(),
  496. )
  497. routes = append(routes, &router.Route{
  498. Endpoint: getGitlabRepoBuildpackEndpoint,
  499. Handler: getGitlabRepoBuildpackHandler,
  500. Router: r,
  501. })
  502. // GET /api/projects/{project_id}/integrations/gitlab/{integration_id}/repos/{owner}/{name}/{branch}/procfile
  503. getGitlabRepoProcfileEndpoint := factory.NewAPIEndpoint(
  504. &types.APIRequestMetadata{
  505. Verb: types.APIVerbGet,
  506. Method: types.HTTPVerbGet,
  507. Path: &types.Path{
  508. Parent: basePath,
  509. RelativePath: fmt.Sprintf("%s/gitlab/{%s}/repos/{%s}/{%s}/{%s}/procfile", relPath,
  510. types.URLParamIntegrationID, types.URLParamGitRepoOwner,
  511. types.URLParamGitRepoName, types.URLParamGitBranch),
  512. },
  513. Scopes: []types.PermissionScope{
  514. types.UserScope,
  515. types.ProjectScope,
  516. types.GitlabIntegrationScope,
  517. },
  518. },
  519. )
  520. getGitlabRepoProcfileHandler := project_integration.NewGetGitlabRepoProcfileHandler(
  521. config,
  522. factory.GetDecoderValidator(),
  523. factory.GetResultWriter(),
  524. )
  525. routes = append(routes, &router.Route{
  526. Endpoint: getGitlabRepoProcfileEndpoint,
  527. Handler: getGitlabRepoProcfileHandler,
  528. Router: r,
  529. })
  530. return routes, newPath
  531. }