latest_app_revisions.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/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. "github.com/porter-dev/porter/internal/porter_app"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. // LatestAppRevisionsHandler handles requests to the /apps/revisions endpoint
  16. type LatestAppRevisionsHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewLatestAppRevisionsHandler returns a new LatestAppRevisionsHandler
  20. func NewLatestAppRevisionsHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *LatestAppRevisionsHandler {
  25. return &LatestAppRevisionsHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // LatestAppRevisionsRequest represents the request for the /apps/revisions endpoint
  30. type LatestAppRevisionsRequest struct {
  31. DeploymentTargetID string `schema:"deployment_target_id"`
  32. // if true, apps in a preview deployment target will be filtered out
  33. IgnorePreviewApps bool `schema:"ignore_preview_apps"`
  34. }
  35. // LatestRevisionWithSource is an app revision and its source porter app
  36. type LatestRevisionWithSource struct {
  37. AppRevision porter_app.Revision `json:"app_revision"`
  38. Source types.PorterApp `json:"source"`
  39. }
  40. // LatestAppRevisionsResponse represents the response from the /apps/revisions endpoint
  41. type LatestAppRevisionsResponse struct {
  42. AppRevisions []LatestRevisionWithSource `json:"app_revisions"`
  43. }
  44. func (c *LatestAppRevisionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  45. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-app-revisions")
  46. defer span.End()
  47. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  48. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  49. request := &LatestAppRevisionsRequest{}
  50. if ok := c.DecodeAndValidate(w, r, request); !ok {
  51. err := telemetry.Error(ctx, span, nil, "error decoding request")
  52. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  53. return
  54. }
  55. telemetry.WithAttributes(span,
  56. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  57. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  58. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  59. telemetry.AttributeKV{Key: "ignore-preview-apps", Value: request.IgnorePreviewApps},
  60. )
  61. var deploymentTargetIdentifier *porterv1.DeploymentTargetIdentifier
  62. if request.DeploymentTargetID != "" {
  63. deploymentTargetIdentifier = &porterv1.DeploymentTargetIdentifier{
  64. Id: request.DeploymentTargetID,
  65. }
  66. }
  67. listAppRevisionsReq := connect.NewRequest(&porterv1.LatestAppRevisionsRequest{
  68. ProjectId: int64(project.ID),
  69. DeploymentTargetIdentifier: deploymentTargetIdentifier,
  70. })
  71. latestAppRevisionsResp, err := c.Config().ClusterControlPlaneClient.LatestAppRevisions(ctx, listAppRevisionsReq)
  72. if err != nil {
  73. err = telemetry.Error(ctx, span, err, "error getting latest app revisions")
  74. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  75. return
  76. }
  77. if latestAppRevisionsResp == nil || latestAppRevisionsResp.Msg == nil {
  78. err = telemetry.Error(ctx, span, nil, "latest app revisions response is nil")
  79. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  80. return
  81. }
  82. appRevisions := latestAppRevisionsResp.Msg.AppRevisions
  83. if appRevisions == nil {
  84. appRevisions = []*porterv1.AppRevision{}
  85. }
  86. res := &LatestAppRevisionsResponse{
  87. AppRevisions: make([]LatestRevisionWithSource, 0),
  88. }
  89. deploymentTargets := map[string]*porterv1.DeploymentTarget{}
  90. for _, revision := range appRevisions {
  91. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, revision)
  92. if err != nil {
  93. err := telemetry.Error(ctx, span, err, "error getting encoded revision from proto")
  94. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  95. return
  96. }
  97. porterApp, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, revision.App.Name)
  98. if err != nil {
  99. err := telemetry.Error(ctx, span, err, "error reading porter app")
  100. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  101. return
  102. }
  103. if porterApp == nil {
  104. err := telemetry.Error(ctx, span, err, "porter app is nil")
  105. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  106. return
  107. }
  108. deploymentTarget, ok := deploymentTargets[encodedRevision.DeploymentTarget.ID]
  109. if !ok {
  110. details, err := c.Config().ClusterControlPlaneClient.DeploymentTargetDetails(ctx, connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
  111. ProjectId: int64(project.ID),
  112. DeploymentTargetId: encodedRevision.DeploymentTarget.ID,
  113. }))
  114. if err != nil {
  115. err := telemetry.Error(ctx, span, err, "error getting deployment target details")
  116. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  117. return
  118. }
  119. if details == nil || details.Msg == nil || details.Msg.DeploymentTarget == nil {
  120. err := telemetry.Error(ctx, span, err, "deployment target details are nil")
  121. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  122. return
  123. }
  124. deploymentTarget = details.Msg.DeploymentTarget
  125. deploymentTargets[encodedRevision.DeploymentTarget.ID] = deploymentTarget
  126. }
  127. // TODO: move this filtering to CCP
  128. if request.IgnorePreviewApps && deploymentTarget.IsPreview {
  129. continue
  130. }
  131. // TODO: move this filtering to CCP
  132. if cluster.ID != uint(deploymentTarget.ClusterId) {
  133. continue
  134. }
  135. encodedRevision.DeploymentTarget.Name = deploymentTarget.Name
  136. res.AppRevisions = append(res.AppRevisions, LatestRevisionWithSource{
  137. AppRevision: encodedRevision,
  138. Source: *porterApp.ToPorterAppType(),
  139. })
  140. }
  141. c.WriteResult(w, r, res)
  142. }