default_deployment_target.go 3.3 KB

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