project_integration.go 14 KB

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