|
| 1 | +package porter_app |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "connectrpc.com/connect" |
| 7 | + porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1" |
| 8 | + "github.com/porter-dev/porter/api/server/authz" |
| 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/server/shared/requestutils" |
| 14 | + "github.com/porter-dev/porter/api/types" |
| 15 | + "github.com/porter-dev/porter/internal/models" |
| 16 | + "github.com/porter-dev/porter/internal/porter_app" |
| 17 | + "github.com/porter-dev/porter/internal/telemetry" |
| 18 | +) |
| 19 | + |
| 20 | +// GetAppRevisionStatusHandler handles requests to the /apps/{porter_app_name}/revisions/{app_revision_id}/status endpoint |
| 21 | +type GetAppRevisionStatusHandler struct { |
| 22 | + handlers.PorterHandlerReadWriter |
| 23 | + authz.KubernetesAgentGetter |
| 24 | +} |
| 25 | + |
| 26 | +// NewGetAppRevisionStatusHandler returns a new GetAppRevisionStatusHandler |
| 27 | +func NewGetAppRevisionStatusHandler( |
| 28 | + config *config.Config, |
| 29 | + decoderValidator shared.RequestDecoderValidator, |
| 30 | + writer shared.ResultWriter, |
| 31 | +) *GetAppRevisionHandler { |
| 32 | + return &GetAppRevisionHandler{ |
| 33 | + PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), |
| 34 | + KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// GetAppRevisionStatusResponse represents the response from the /apps/{porter_app_name}/revisions/{app_revision_id}/status endpoint |
| 39 | +type GetAppRevisionStatusResponse struct { |
| 40 | + AppRevisionStatus porter_app.RevisionStatus `json:"app_revision_status"` |
| 41 | +} |
| 42 | + |
| 43 | +// GetAppRevisionStatusHandler returns the status of an app revision |
| 44 | +func (c *GetAppRevisionStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 45 | + ctx, span := telemetry.NewSpan(r.Context(), "serve-get-app-revision") |
| 46 | + defer span.End() |
| 47 | + |
| 48 | + project, _ := r.Context().Value(types.ProjectScope).(*models.Project) |
| 49 | + |
| 50 | + appRevisionID, reqErr := requestutils.GetURLParamString(r, types.URLParamAppRevisionID) |
| 51 | + if reqErr != nil { |
| 52 | + err := telemetry.Error(ctx, span, nil, "error parsing app revision id") |
| 53 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + getRevisionStatusReq := connect.NewRequest(&porterv1.AppRevisionStatusRequest{ |
| 58 | + ProjectId: int64(project.ID), |
| 59 | + AppRevisionId: appRevisionID, |
| 60 | + }) |
| 61 | + ccpResp, err := c.Config().ClusterControlPlaneClient.AppRevisionStatus(ctx, getRevisionStatusReq) |
| 62 | + if err != nil { |
| 63 | + err = telemetry.Error(ctx, span, err, "error getting app revision status") |
| 64 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + if ccpResp == nil || ccpResp.Msg == nil { |
| 69 | + err = telemetry.Error(ctx, span, nil, "get app revision response is nil") |
| 70 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + revisionStatus := porter_app.RevisionStatus{ |
| 75 | + PredeployStarted: ccpResp.Msg.PredeployStarted, |
| 76 | + PredeploySuccessful: ccpResp.Msg.PredeploySuccessful, |
| 77 | + PredeployFailed: ccpResp.Msg.PredeployFailed, |
| 78 | + InstallStarted: ccpResp.Msg.InstallStarted, |
| 79 | + InstallSuccessful: ccpResp.Msg.InstallSuccessful, |
| 80 | + InstallFailed: ccpResp.Msg.InstallFailed, |
| 81 | + DeploymentStarted: ccpResp.Msg.DeploymentStarted, |
| 82 | + DeploymentSuccessful: ccpResp.Msg.DeploymentSuccessful, |
| 83 | + DeploymentFailed: ccpResp.Msg.DeploymentFailed, |
| 84 | + IsInTerminalStatus: ccpResp.Msg.IsInTerminalStatus, |
| 85 | + } |
| 86 | + |
| 87 | + res := &GetAppRevisionStatusResponse{ |
| 88 | + AppRevisionStatus: revisionStatus, |
| 89 | + } |
| 90 | + |
| 91 | + c.WriteResult(w, r, res) |
| 92 | +} |
0 commit comments