deployment_target.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/deployment_target"
  6. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/server/shared/router"
  10. "github.com/porter-dev/porter/api/types"
  11. )
  12. // NewDeploymentTargetScopedRegisterer applies /api/projects/{project_id}/targets routes to the gin Router
  13. func NewDeploymentTargetScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  14. return &router.Registerer{
  15. GetRoutes: GetDeploymentTargetScopedRoutes,
  16. Children: children,
  17. }
  18. }
  19. // GetDeploymentTargetScopedRoutes returns the router handlers specific to deployment targets
  20. func GetDeploymentTargetScopedRoutes(
  21. r chi.Router,
  22. config *config.Config,
  23. basePath *types.Path,
  24. factory shared.APIEndpointFactory,
  25. children ...*router.Registerer,
  26. ) []*router.Route {
  27. routes, projPath := getDeploymentTargetRoutes(r, config, basePath, factory)
  28. if len(children) > 0 {
  29. r.Route(projPath.RelativePath, func(r chi.Router) {
  30. for _, child := range children {
  31. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  32. routes = append(routes, childRoutes...)
  33. }
  34. })
  35. }
  36. return routes
  37. }
  38. // getDeploymentTargetRoutes gets the routes that use deployment targets as a first class object instead of scoped to clusters
  39. func getDeploymentTargetRoutes(
  40. r chi.Router,
  41. config *config.Config,
  42. basePath *types.Path,
  43. factory shared.APIEndpointFactory,
  44. ) ([]*router.Route, *types.Path) {
  45. relPath := "/targets/{deployment_target_identifier}"
  46. newPath := &types.Path{
  47. Parent: basePath,
  48. RelativePath: relPath,
  49. }
  50. var routes []*router.Route
  51. // GET /api/projects/{project_id}/targets/{deployment_target_identifier} -> deployment_target.GetDeploymentTargetHandler
  52. getDeploymentTargetEndpoint := factory.NewAPIEndpoint(
  53. &types.APIRequestMetadata{
  54. Verb: types.APIVerbGet,
  55. Method: types.HTTPVerbGet,
  56. Path: &types.Path{
  57. Parent: basePath,
  58. RelativePath: relPath,
  59. },
  60. Scopes: []types.PermissionScope{
  61. types.UserScope,
  62. types.ProjectScope,
  63. types.DeploymentTargetScope,
  64. },
  65. },
  66. )
  67. getDeploymentTargetHandler := deployment_target.NewGetDeploymentTargetHandler(
  68. config,
  69. factory.GetDecoderValidator(),
  70. factory.GetResultWriter(),
  71. )
  72. routes = append(routes, &router.Route{
  73. Endpoint: getDeploymentTargetEndpoint,
  74. Handler: getDeploymentTargetHandler,
  75. Router: r,
  76. })
  77. // GET /api/projects/{project_id}/targets/{deployment_target_identifier}/apps/{porter_app_name}/cloudsql -> porter_app.GetCloudSqlSecretHandler
  78. getCloudSqlSecretEndpoint := factory.NewAPIEndpoint(
  79. &types.APIRequestMetadata{
  80. Verb: types.APIVerbGet,
  81. Method: types.HTTPVerbGet,
  82. Path: &types.Path{
  83. Parent: basePath,
  84. RelativePath: fmt.Sprintf("%s/apps/{porter_app_name}/cloudsql", relPath),
  85. },
  86. Scopes: []types.PermissionScope{
  87. types.UserScope,
  88. types.ProjectScope,
  89. types.DeploymentTargetScope,
  90. },
  91. },
  92. )
  93. getCloudSqlSecretHandler := porter_app.NewGetCloudSqlSecretHandler(
  94. config,
  95. factory.GetResultWriter(),
  96. )
  97. routes = append(routes, &router.Route{
  98. Endpoint: getCloudSqlSecretEndpoint,
  99. Handler: getCloudSqlSecretHandler,
  100. Router: r,
  101. })
  102. // POST /api/projects/{project_id}/targets/{deployment_target_identifier}/apps/{porter_app_name}/cloudsql -> porter_app.CreateCloudSqlSecretHandler
  103. createCloudSqlSecretEndpoint := factory.NewAPIEndpoint(
  104. &types.APIRequestMetadata{
  105. Verb: types.APIVerbCreate,
  106. Method: types.HTTPVerbPost,
  107. Path: &types.Path{
  108. Parent: basePath,
  109. RelativePath: fmt.Sprintf("%s/apps/{porter_app_name}/cloudsql", relPath),
  110. },
  111. Scopes: []types.PermissionScope{
  112. types.UserScope,
  113. types.ProjectScope,
  114. types.DeploymentTargetScope,
  115. },
  116. },
  117. )
  118. createCloudSqlSecretHandler := porter_app.NewCreateCloudSqlSecretHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &router.Route{
  124. Endpoint: createCloudSqlSecretEndpoint,
  125. Handler: createCloudSqlSecretHandler,
  126. Router: r,
  127. })
  128. return routes, newPath
  129. }