datastore.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/datastore"
  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. // NewDatastoreScopedRegisterer returns a scoped route registerer for Datastore routes
  12. func NewDatastoreScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  13. return &router.Registerer{
  14. GetRoutes: GetDatastoreScopedRoutes,
  15. Children: children,
  16. }
  17. }
  18. // GetDatastoreScopedRoutes returns scoped Datastore routes with mounted child registerers
  19. func GetDatastoreScopedRoutes(
  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 := getDatastoreRoutes(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. // getDatastoreRoutes returns Datastore routes
  38. func getDatastoreRoutes(
  39. r chi.Router,
  40. config *config.Config,
  41. basePath *types.Path,
  42. factory shared.APIEndpointFactory,
  43. ) ([]*router.Route, *types.Path) {
  44. // empty path as this is mounted onto the datastore endpoints
  45. relPath := ""
  46. newPath := &types.Path{
  47. Parent: basePath,
  48. RelativePath: relPath,
  49. }
  50. routes := make([]*router.Route, 0)
  51. // GET /api/projects/{project_id}/cloud-providers/{cloud_provider_type}/{cloud_provider_id}/datastores -> cloud_provider.NewListDatastoresHandler
  52. listEndpoint := factory.NewAPIEndpoint(
  53. &types.APIRequestMetadata{
  54. Verb: types.APIVerbGet,
  55. Method: types.HTTPVerbGet,
  56. Path: &types.Path{
  57. Parent: basePath,
  58. RelativePath: fmt.Sprintf("%s/{%s}/{%s}/datastores", relPath, types.URLParamCloudProviderType, types.URLParamCloudProviderID),
  59. },
  60. Scopes: []types.PermissionScope{
  61. types.UserScope,
  62. types.ProjectScope,
  63. },
  64. },
  65. )
  66. listHandler := datastore.NewListDatastoresHandler(
  67. config,
  68. factory.GetDecoderValidator(),
  69. factory.GetResultWriter(),
  70. )
  71. routes = append(routes, &router.Route{
  72. Endpoint: listEndpoint,
  73. Handler: listHandler,
  74. Router: r,
  75. })
  76. // DELETE /api/projects/{project_id}/cloud-providers/{cloud_provider_type}/{cloud_provider_id}/datastores -> cloud_provider.NewDeleteDatastoreHandler
  77. deleteEndpoint := factory.NewAPIEndpoint(
  78. &types.APIRequestMetadata{
  79. Verb: types.APIVerbDelete,
  80. Method: types.HTTPVerbDelete,
  81. Path: &types.Path{
  82. Parent: basePath,
  83. RelativePath: fmt.Sprintf("%s/{%s}/{%s}/datastores", relPath, types.URLParamCloudProviderType, types.URLParamCloudProviderID),
  84. },
  85. Scopes: []types.PermissionScope{
  86. types.UserScope,
  87. types.ProjectScope,
  88. },
  89. },
  90. )
  91. deleteHandler := datastore.NewDeleteDatastoreHandler(
  92. config,
  93. factory.GetDecoderValidator(),
  94. factory.GetResultWriter(),
  95. )
  96. routes = append(routes, &router.Route{
  97. Endpoint: deleteEndpoint,
  98. Handler: deleteHandler,
  99. Router: r,
  100. })
  101. // GET /api/projects/{project_id}/cloud-providers/{cloud_provider_type}/{cloud_provider_id}/datastores/{datastore_type}/{datastore_name} -> cloud_provider.NewListDatastoresHandler
  102. getEndpoint := factory.NewAPIEndpoint(
  103. &types.APIRequestMetadata{
  104. Verb: types.APIVerbGet,
  105. Method: types.HTTPVerbGet,
  106. Path: &types.Path{
  107. Parent: basePath,
  108. RelativePath: fmt.Sprintf("%s/{%s}/{%s}/datastores/{%s}/{%s}", relPath, types.URLParamCloudProviderType, types.URLParamCloudProviderID, types.URLParamDatastoreType, types.URLParamDatastoreName),
  109. },
  110. Scopes: []types.PermissionScope{
  111. types.UserScope,
  112. types.ProjectScope,
  113. },
  114. },
  115. )
  116. getHandler := datastore.NewListDatastoresHandler(
  117. config,
  118. factory.GetDecoderValidator(),
  119. factory.GetResultWriter(),
  120. )
  121. routes = append(routes, &router.Route{
  122. Endpoint: getEndpoint,
  123. Handler: getHandler,
  124. Router: r,
  125. })
  126. return routes, newPath
  127. }