project_integration.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/types"
  8. )
  9. func NewProjectIntegrationScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetProjectIntegrationScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetProjectIntegrationScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getProjectIntegrationRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getProjectIntegrationRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/integrations"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/integrations/oauth -> project_integration.NewListOAuthHandler
  46. listOAuthEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath + "/oauth",
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. },
  58. },
  59. )
  60. listOAuthHandler := project_integration.NewListOAuthHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: listOAuthEndpoint,
  66. Handler: listOAuthHandler,
  67. Router: r,
  68. })
  69. // GET /api/projects/{project_id}/integrations/do -> project_integration.NewListDOHandler
  70. listDOEndpoint := factory.NewAPIEndpoint(
  71. &types.APIRequestMetadata{
  72. Verb: types.APIVerbGet,
  73. Method: types.HTTPVerbGet,
  74. Path: &types.Path{
  75. Parent: basePath,
  76. RelativePath: relPath + "/do",
  77. },
  78. Scopes: []types.PermissionScope{
  79. types.UserScope,
  80. types.ProjectScope,
  81. },
  82. },
  83. )
  84. listDOHandler := project_integration.NewListDOHandler(
  85. config,
  86. factory.GetResultWriter(),
  87. )
  88. routes = append(routes, &Route{
  89. Endpoint: listDOEndpoint,
  90. Handler: listDOHandler,
  91. Router: r,
  92. })
  93. // POST /api/projects/{project_id}/integrations/basic -> project_integration.NewCreateBasicHandler
  94. createBasicEndpoint := factory.NewAPIEndpoint(
  95. &types.APIRequestMetadata{
  96. Verb: types.APIVerbCreate,
  97. Method: types.HTTPVerbPost,
  98. Path: &types.Path{
  99. Parent: basePath,
  100. RelativePath: relPath + "/basic",
  101. },
  102. Scopes: []types.PermissionScope{
  103. types.UserScope,
  104. types.ProjectScope,
  105. },
  106. },
  107. )
  108. createBasicHandler := project_integration.NewCreateBasicHandler(
  109. config,
  110. factory.GetDecoderValidator(),
  111. factory.GetResultWriter(),
  112. )
  113. routes = append(routes, &Route{
  114. Endpoint: createBasicEndpoint,
  115. Handler: createBasicHandler,
  116. Router: r,
  117. })
  118. // POST /api/projects/{project_id}/integrations/aws -> project_integration.NewCreateAWSHandler
  119. createAWSEndpoint := factory.NewAPIEndpoint(
  120. &types.APIRequestMetadata{
  121. Verb: types.APIVerbCreate,
  122. Method: types.HTTPVerbPost,
  123. Path: &types.Path{
  124. Parent: basePath,
  125. RelativePath: relPath + "/aws",
  126. },
  127. Scopes: []types.PermissionScope{
  128. types.UserScope,
  129. types.ProjectScope,
  130. },
  131. },
  132. )
  133. createAWSHandler := project_integration.NewCreateAWSHandler(
  134. config,
  135. factory.GetDecoderValidator(),
  136. factory.GetResultWriter(),
  137. )
  138. routes = append(routes, &Route{
  139. Endpoint: createAWSEndpoint,
  140. Handler: createAWSHandler,
  141. Router: r,
  142. })
  143. // GET /api/projects/{project_id}/integrations/aws -> project_integration.NewListAWSHandler
  144. listAWSEndpoint := factory.NewAPIEndpoint(
  145. &types.APIRequestMetadata{
  146. Verb: types.APIVerbGet,
  147. Method: types.HTTPVerbGet,
  148. Path: &types.Path{
  149. Parent: basePath,
  150. RelativePath: relPath + "/aws",
  151. },
  152. Scopes: []types.PermissionScope{
  153. types.UserScope,
  154. types.ProjectScope,
  155. },
  156. },
  157. )
  158. listAWSHandler := project_integration.NewListAWSHandler(
  159. config,
  160. factory.GetResultWriter(),
  161. )
  162. routes = append(routes, &Route{
  163. Endpoint: listAWSEndpoint,
  164. Handler: listAWSHandler,
  165. Router: r,
  166. })
  167. // POST /api/projects/{project_id}/integrations/aws/overwrite -> project_integration.NewOverwriteAWSHandler
  168. overwriteAWSEndpoint := factory.NewAPIEndpoint(
  169. &types.APIRequestMetadata{
  170. Verb: types.APIVerbCreate,
  171. Method: types.HTTPVerbPost,
  172. Path: &types.Path{
  173. Parent: basePath,
  174. RelativePath: relPath + "/aws/overwrite",
  175. },
  176. Scopes: []types.PermissionScope{
  177. types.UserScope,
  178. types.ProjectScope,
  179. },
  180. },
  181. )
  182. overwriteAWSHandler := project_integration.NewOverwriteAWSHandler(
  183. config,
  184. factory.GetDecoderValidator(),
  185. factory.GetResultWriter(),
  186. )
  187. routes = append(routes, &Route{
  188. Endpoint: overwriteAWSEndpoint,
  189. Handler: overwriteAWSHandler,
  190. Router: r,
  191. })
  192. // POST /api/projects/{project_id}/integrations/gcp -> project_integration.NewCreateGCPHandler
  193. createGCPEndpoint := factory.NewAPIEndpoint(
  194. &types.APIRequestMetadata{
  195. Verb: types.APIVerbCreate,
  196. Method: types.HTTPVerbPost,
  197. Path: &types.Path{
  198. Parent: basePath,
  199. RelativePath: relPath + "/gcp",
  200. },
  201. Scopes: []types.PermissionScope{
  202. types.UserScope,
  203. types.ProjectScope,
  204. },
  205. },
  206. )
  207. createGCPHandler := project_integration.NewCreateGCPHandler(
  208. config,
  209. factory.GetDecoderValidator(),
  210. factory.GetResultWriter(),
  211. )
  212. routes = append(routes, &Route{
  213. Endpoint: createGCPEndpoint,
  214. Handler: createGCPHandler,
  215. Router: r,
  216. })
  217. // GET /api/projects/{project_id}/integrations/gcp -> project_integration.NewListGCPHandler
  218. listGCPEndpoint := factory.NewAPIEndpoint(
  219. &types.APIRequestMetadata{
  220. Verb: types.APIVerbGet,
  221. Method: types.HTTPVerbGet,
  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. listGCPHandler := project_integration.NewListGCPHandler(
  233. config,
  234. factory.GetResultWriter(),
  235. )
  236. routes = append(routes, &Route{
  237. Endpoint: listGCPEndpoint,
  238. Handler: listGCPHandler,
  239. Router: r,
  240. })
  241. // POST /api/projects/{project_id}/integrations/azure -> project_integration.NewCreateAzureHandler
  242. createAzureEndpoint := factory.NewAPIEndpoint(
  243. &types.APIRequestMetadata{
  244. Verb: types.APIVerbCreate,
  245. Method: types.HTTPVerbPost,
  246. Path: &types.Path{
  247. Parent: basePath,
  248. RelativePath: relPath + "/azure",
  249. },
  250. Scopes: []types.PermissionScope{
  251. types.UserScope,
  252. types.ProjectScope,
  253. },
  254. },
  255. )
  256. createAzureHandler := project_integration.NewCreateAzureHandler(
  257. config,
  258. factory.GetDecoderValidator(),
  259. factory.GetResultWriter(),
  260. )
  261. routes = append(routes, &Route{
  262. Endpoint: createAzureEndpoint,
  263. Handler: createAzureHandler,
  264. Router: r,
  265. })
  266. return routes, newPath
  267. }