2
0

cloud_provider.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/cloud_provider"
  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. // NewCloudProviderScopedRegisterer returns a scoped route registerer for CloudProvider routes
  12. func NewCloudProviderScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  13. return &router.Registerer{
  14. GetRoutes: GetCloudProviderScopedRoutes,
  15. Children: children,
  16. }
  17. }
  18. // GetCloudProviderScopedRoutes returns scoped CloudProvider routes with mounted child registerers
  19. func GetCloudProviderScopedRoutes(
  20. r chi.Router,
  21. config *config.Config,
  22. basePath *types.Path,
  23. factory shared.APIEndpointFactory,
  24. children ...*router.Registerer,
  25. ) []*router.Route {
  26. routes, projPath := getCloudProviderRoutes(r, config, basePath, factory)
  27. if len(children) > 0 {
  28. r.Route(projPath.RelativePath, func(r chi.Router) {
  29. for _, child := range children {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. }
  33. })
  34. }
  35. return routes
  36. }
  37. // getCloudProviderRoutes returns CloudProvider routes
  38. func getCloudProviderRoutes(
  39. r chi.Router,
  40. config *config.Config,
  41. basePath *types.Path,
  42. factory shared.APIEndpointFactory,
  43. ) ([]*router.Route, *types.Path) {
  44. relPath := "/cloud-providers"
  45. newPath := &types.Path{
  46. Parent: basePath,
  47. RelativePath: relPath,
  48. }
  49. routes := make([]*router.Route, 0)
  50. // GET /api/projects/{project_id}/cloud-providers/aws -> cloud_provider.NewListAwsHandler
  51. listAwsEndpoint := factory.NewAPIEndpoint(
  52. &types.APIRequestMetadata{
  53. Verb: types.APIVerbGet,
  54. Method: types.HTTPVerbGet,
  55. Path: &types.Path{
  56. Parent: basePath,
  57. RelativePath: fmt.Sprintf("%s/aws", relPath),
  58. },
  59. Scopes: []types.PermissionScope{
  60. types.UserScope,
  61. types.ProjectScope,
  62. },
  63. },
  64. )
  65. listAwsHandler := cloud_provider.NewListAwsAccountsHandler(
  66. config,
  67. factory.GetResultWriter(),
  68. )
  69. routes = append(routes, &router.Route{
  70. Endpoint: listAwsEndpoint,
  71. Handler: listAwsHandler,
  72. Router: r,
  73. })
  74. return routes, newPath
  75. }