default_deployment_target.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/google/uuid"
  5. "github.com/porter-dev/porter/internal/telemetry"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. // DefaultDeploymentTargetHandler handles requests to the /default-deployment-target endpoint
  14. type DefaultDeploymentTargetHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. // NewDefaultDeploymentTargetHandler returns a new DefaultDeploymentTargetHandler
  18. func NewDefaultDeploymentTargetHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *DefaultDeploymentTargetHandler {
  23. return &DefaultDeploymentTargetHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. // DefaultDeploymentTargetRequest is the request object for the /default-deployment-target endpoint
  28. type DefaultDeploymentTargetRequest struct{}
  29. // DefaultDeploymentTargetResponse is the response object for the /default-deployment-target endpoint
  30. type DefaultDeploymentTargetResponse struct {
  31. DeploymentTargetID string `json:"deployment_target_id"`
  32. }
  33. const (
  34. // DeploymentTargetSelector_Default is the selector for the default deployment target in a cluster
  35. DeploymentTargetSelector_Default = "default"
  36. // DeploymentTargetSelectorType_Default is the selector type for the default deployment target in a cluster
  37. DeploymentTargetSelectorType_Default = "NAMESPACE"
  38. )
  39. // ServeHTTP receives a project id and cluster id and returns the default deployment target in the cluster
  40. func (c *DefaultDeploymentTargetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  41. ctx, span := telemetry.NewSpan(r.Context(), "serve-default-deployment-target")
  42. defer span.End()
  43. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  44. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  45. telemetry.WithAttributes(span,
  46. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  47. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  48. )
  49. defaultDeploymentTarget, err := c.Repo().DeploymentTarget().DeploymentTargetBySelectorAndSelectorType(project.ID, cluster.ID, DeploymentTargetSelector_Default, DeploymentTargetSelectorType_Default)
  50. if err != nil {
  51. err := telemetry.Error(ctx, span, err, "error getting default deployment target from repo")
  52. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  53. return
  54. }
  55. if defaultDeploymentTarget.ID == uuid.Nil {
  56. err := telemetry.Error(ctx, span, err, "default deployment target not found")
  57. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  58. return
  59. }
  60. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: defaultDeploymentTarget.ID.String()})
  61. response := &DefaultDeploymentTargetResponse{
  62. DeploymentTargetID: defaultDeploymentTarget.ID.String(),
  63. }
  64. c.WriteResult(w, r, response)
  65. }