2
0

current_app_revision.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/shared/requestutils"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/google/uuid"
  8. "github.com/porter-dev/porter/internal/porter_app"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. )
  17. // LatestAppRevisionHandler handles requests to the /apps/{porter_app_name}/latest endpoint
  18. type LatestAppRevisionHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. }
  21. // NewLatestAppRevisionHandler returns a new LatestAppRevisionHandler
  22. func NewLatestAppRevisionHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *LatestAppRevisionHandler {
  27. return &LatestAppRevisionHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. }
  30. }
  31. // LatestAppRevisionRequest is the request object for the /apps/{porter_app_name}/latest endpoint
  32. type LatestAppRevisionRequest struct {
  33. DeploymentTargetID string `schema:"deployment_target_id"`
  34. }
  35. // LatestAppRevisionResponse is the response object for the /apps/{porter_app_name}/latest endpoint
  36. type LatestAppRevisionResponse struct {
  37. // AppRevision is the latest revision for the app
  38. AppRevision porter_app.Revision `json:"app_revision"`
  39. }
  40. // ServeHTTP translates the request into a CurrentAppRevision grpc request, forwards to the cluster control plane, and returns the response.
  41. // Multi-cluster projects are not supported, as they may have multiple porter-apps with the same name in the same project.
  42. func (c *LatestAppRevisionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  43. ctx, span := telemetry.NewSpan(r.Context(), "serve-latest-app-revision")
  44. defer span.End()
  45. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  46. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  47. telemetry.WithAttributes(span,
  48. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  49. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  50. )
  51. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  52. if reqErr != nil {
  53. e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url")
  54. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  55. return
  56. }
  57. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  58. request := &LatestAppRevisionRequest{}
  59. if ok := c.DecodeAndValidate(w, r, request); !ok {
  60. err := telemetry.Error(ctx, span, nil, "error decoding request")
  61. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  62. return
  63. }
  64. _, err := uuid.Parse(request.DeploymentTargetID)
  65. if err != nil {
  66. err := telemetry.Error(ctx, span, err, "error parsing deployment target id")
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  68. return
  69. }
  70. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  71. porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
  72. if err != nil {
  73. err := telemetry.Error(ctx, span, err, "error getting porter app from repo")
  74. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  75. return
  76. }
  77. if len(porterApps) == 0 {
  78. err := telemetry.Error(ctx, span, err, "no porter apps returned")
  79. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  80. return
  81. }
  82. if len(porterApps) > 1 {
  83. err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
  84. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  85. return
  86. }
  87. if porterApps[0].ID == 0 {
  88. err := telemetry.Error(ctx, span, err, "porter app id is missiong")
  89. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  90. return
  91. }
  92. currentAppRevisionReq := connect.NewRequest(&porterv1.CurrentAppRevisionRequest{
  93. ProjectId: int64(project.ID),
  94. AppId: int64(porterApps[0].ID),
  95. DeploymentTargetId: request.DeploymentTargetID,
  96. })
  97. currentAppRevisionResp, err := c.Config().ClusterControlPlaneClient.CurrentAppRevision(ctx, currentAppRevisionReq)
  98. if err != nil {
  99. err := telemetry.Error(ctx, span, err, "error getting current app revision from cluster control plane client")
  100. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  101. return
  102. }
  103. if currentAppRevisionResp == nil || currentAppRevisionResp.Msg == nil {
  104. err := telemetry.Error(ctx, span, err, "current app revision resp is nil")
  105. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  106. return
  107. }
  108. appRevision := currentAppRevisionResp.Msg.AppRevision
  109. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, appRevision)
  110. if err != nil {
  111. err := telemetry.Error(ctx, span, err, "error encoding revision from proto")
  112. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  113. return
  114. }
  115. response := LatestAppRevisionResponse{
  116. AppRevision: encodedRevision,
  117. }
  118. c.WriteResult(w, r, response)
  119. }