default_deployment_target.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package porter_app
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/google/uuid"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. // DefaultDeploymentTargetHandler handles requests to the /default-deployment-target endpoint
  17. type DefaultDeploymentTargetHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. // NewDefaultDeploymentTargetHandler returns a new DefaultDeploymentTargetHandler
  21. func NewDefaultDeploymentTargetHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *DefaultDeploymentTargetHandler {
  26. return &DefaultDeploymentTargetHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. // DefaultDeploymentTargetRequest is the request object for the /default-deployment-target endpoint
  31. type DefaultDeploymentTargetRequest struct{}
  32. // DefaultDeploymentTargetResponse is the response object for the /default-deployment-target endpoint
  33. type DefaultDeploymentTargetResponse struct {
  34. // Deprecated: use inline types.DeploymentTarget fields instead
  35. DeploymentTargetID string `json:"deployment_target_id"`
  36. types.DeploymentTarget `json:"deployment_target"`
  37. }
  38. const (
  39. // DeploymentTargetSelector_Default is the selector for the default deployment target in a cluster
  40. DeploymentTargetSelector_Default = "default"
  41. // DeploymentTargetSelectorType_Default is the selector type for the default deployment target in a cluster
  42. DeploymentTargetSelectorType_Default = "NAMESPACE"
  43. )
  44. // ServeHTTP receives a project id and cluster id and returns the default deployment target in the cluster
  45. func (c *DefaultDeploymentTargetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  46. ctx, span := telemetry.NewSpan(r.Context(), "serve-default-deployment-target")
  47. defer span.End()
  48. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  49. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  50. telemetry.WithAttributes(span,
  51. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  52. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  53. )
  54. defaultDeploymentTargetReq := connect.NewRequest(&porterv1.DefaultDeploymentTargetRequest{
  55. ProjectId: int64(project.ID),
  56. ClusterId: int64(cluster.ID),
  57. })
  58. defaultDeploymentTargetResp, err := c.Config().ClusterControlPlaneClient.DefaultDeploymentTarget(ctx, defaultDeploymentTargetReq)
  59. if err != nil {
  60. err := telemetry.Error(ctx, span, err, "error getting default deployment target")
  61. c.WriteResult(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  62. return
  63. }
  64. if defaultDeploymentTargetResp == nil || defaultDeploymentTargetResp.Msg == nil {
  65. err := telemetry.Error(ctx, span, nil, "default deployment target response is nil")
  66. c.WriteResult(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  67. return
  68. }
  69. defaultDeploymentTarget := defaultDeploymentTargetResp.Msg.DeploymentTarget
  70. id, err := uuid.Parse(defaultDeploymentTarget.Id)
  71. if err != nil {
  72. err := telemetry.Error(ctx, span, err, "error parsing default deployment target id")
  73. c.WriteResult(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  74. return
  75. }
  76. if id == uuid.Nil {
  77. err := telemetry.Error(ctx, span, nil, "default deployment target id is nil")
  78. c.WriteResult(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  79. return
  80. }
  81. response := &DefaultDeploymentTargetResponse{
  82. DeploymentTargetID: defaultDeploymentTarget.Id,
  83. DeploymentTarget: types.DeploymentTarget{
  84. ID: id,
  85. ProjectID: uint(defaultDeploymentTarget.ProjectId),
  86. ClusterID: uint(defaultDeploymentTarget.ClusterId),
  87. Name: defaultDeploymentTarget.Name,
  88. Namespace: defaultDeploymentTarget.Namespace,
  89. IsPreview: defaultDeploymentTarget.IsPreview,
  90. IsDefault: defaultDeploymentTarget.IsDefault,
  91. CreatedAt: time.Time{}, // not provided by default deployment target response
  92. UpdatedAt: time.Time{}, // not provided by default deployment target response
  93. },
  94. }
  95. c.WriteResult(w, r, response)
  96. }