project_integration.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. project_integration "github.com/porter-dev/porter/api/server/handlers/project_integration"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/server/shared/router"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. func NewProjectIntegrationScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  11. return &router.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 ...*router.Registerer,
  22. ) []*router.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. ) ([]*router.Route, *types.Path) {
  40. relPath := "/integrations"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*router.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, &router.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, &router.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, &router.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, &router.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, &router.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, &router.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, &router.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, &router.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, &router.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, &router.Route{
  287. Endpoint: createAzureEndpoint,
  288. Handler: createAzureHandler,
  289. Router: r,
  290. })
  291. return routes, newPath
  292. }