environment_config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/environment_config"
  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 NewEnvConfigScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetEnvConfigScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetEnvConfigScopedRoutes(
  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 := getEnvConfigRoutes(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 getEnvConfigRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/env_config"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. var routes []*router.Route
  47. // GET /api/projects/{project_id}/clusters/{cluster_id}/env_config/{env_config_id}/stacks/{name}
  48. getEnvConfigStackEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: fmt.Sprintf("%s/{%s}/stacks/{%s}", relPath, types.URLParamEnvConfigID, types.URLParamStackName),
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.ClusterScope,
  60. },
  61. },
  62. )
  63. getEnvConfigStackHandler := environment_config.NewGetEnvConfigStackHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: getEnvConfigStackEndpoint,
  69. Handler: getEnvConfigStackHandler,
  70. Router: r,
  71. })
  72. // GET /api/projects/{project_id}/clusters/{cluster_id}/env_config/{env_config_id} -> env_config.NewGetEnvConfigHandler
  73. getEnvConfigEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamEnvConfigID),
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.ClusterScope,
  85. },
  86. },
  87. )
  88. getEnvConfigHandler := environment_config.NewGetEnvConfigHandler(
  89. config,
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &router.Route{
  93. Endpoint: getEnvConfigEndpoint,
  94. Handler: getEnvConfigHandler,
  95. Router: r,
  96. })
  97. return routes, newPath
  98. }