helm_repo.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/helmrepo"
  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 NewHelmRepoScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  11. return &router.Registerer{
  12. GetRoutes: GetHelmRepoScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetHelmRepoScopedRoutes(
  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 := getHelmRepoRoutes(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 getHelmRepoRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*router.Route, *types.Path) {
  40. relPath := "/helmrepos/{helm_repo_id}"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*router.Route, 0)
  46. // GET /api/projects/{project_id}/helmrepos/{helm_repo_id} -> registry.NewHelmRepoGetHandler
  47. getEndpoint := factory.NewAPIEndpoint(
  48. &types.APIRequestMetadata{
  49. Verb: types.APIVerbGet,
  50. Method: types.HTTPVerbGet,
  51. Path: &types.Path{
  52. Parent: basePath,
  53. RelativePath: relPath,
  54. },
  55. Scopes: []types.PermissionScope{
  56. types.UserScope,
  57. types.ProjectScope,
  58. types.HelmRepoScope,
  59. },
  60. },
  61. )
  62. getHandler := helmrepo.NewHelmRepoGetHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &router.Route{
  67. Endpoint: getEndpoint,
  68. Handler: getHandler,
  69. Router: r,
  70. })
  71. // DELETE /api/projects/{project_id}/helmrepos/{helm_repo_id} -> registry.NewHelmRepoDeleteHandler
  72. deleteEndpoint := factory.NewAPIEndpoint(
  73. &types.APIRequestMetadata{
  74. Verb: types.APIVerbDelete,
  75. Method: types.HTTPVerbDelete,
  76. Path: &types.Path{
  77. Parent: basePath,
  78. RelativePath: relPath,
  79. },
  80. Scopes: []types.PermissionScope{
  81. types.UserScope,
  82. types.ProjectScope,
  83. types.HelmRepoScope,
  84. },
  85. },
  86. )
  87. deleteHandler := helmrepo.NewHelmRepoDeleteHandler(
  88. config,
  89. factory.GetDecoderValidator(),
  90. factory.GetResultWriter(),
  91. )
  92. routes = append(routes, &router.Route{
  93. Endpoint: deleteEndpoint,
  94. Handler: deleteHandler,
  95. Router: r,
  96. })
  97. // GET /api/projects/{project_id}/helmrepos/{helm_repo_id}/charts -> helmrepo.NewChartListHandler
  98. hrListEndpoint := factory.NewAPIEndpoint(
  99. &types.APIRequestMetadata{
  100. Verb: types.APIVerbList,
  101. Method: types.HTTPVerbGet,
  102. Path: &types.Path{
  103. Parent: basePath,
  104. RelativePath: relPath + "/charts",
  105. },
  106. Scopes: []types.PermissionScope{
  107. types.UserScope,
  108. types.ProjectScope,
  109. types.HelmRepoScope,
  110. },
  111. },
  112. )
  113. hrListHandler := helmrepo.NewChartListHandler(
  114. config,
  115. factory.GetDecoderValidator(),
  116. factory.GetResultWriter(),
  117. )
  118. routes = append(routes, &router.Route{
  119. Endpoint: hrListEndpoint,
  120. Handler: hrListHandler,
  121. Router: r,
  122. })
  123. // GET /api/projects/{project_id}/helmrepos/{helm_repo_id}/charts/{name}/{version} -> helmrepo.NewChartGetHandler
  124. chartGetEndpoint := factory.NewAPIEndpoint(
  125. &types.APIRequestMetadata{
  126. Verb: types.APIVerbGet,
  127. Method: types.HTTPVerbGet,
  128. Path: &types.Path{
  129. Parent: basePath,
  130. RelativePath: relPath + "/charts/{name}/{version}",
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. types.HelmRepoScope,
  136. },
  137. },
  138. )
  139. chartGetHandler := helmrepo.NewChartGetHandler(
  140. config,
  141. factory.GetDecoderValidator(),
  142. factory.GetResultWriter(),
  143. )
  144. routes = append(routes, &router.Route{
  145. Endpoint: chartGetEndpoint,
  146. Handler: chartGetHandler,
  147. Router: r,
  148. })
  149. return routes, newPath
  150. }