diff --git a/internal/v1/handler.go b/internal/v1/handler.go index 3ef7cb32c..54fcc85eb 100644 --- a/internal/v1/handler.go +++ b/internal/v1/handler.go @@ -233,60 +233,46 @@ func (h *Handlers) GetPackages(ctx echo.Context, params GetPackagesParams) error } func (h *Handlers) GetComposeStatus(ctx echo.Context, composeId uuid.UUID) error { - composeEntry, err := h.getComposeByIdAndOrgId(ctx, composeId) + _, err := h.getComposeByIdAndOrgId(ctx, composeId) if err != nil { - return err + return echo.NewHTTPError(http.StatusNotFound, "Compose entry not found").SetInternal(err) } resp, err := h.server.cClient.ComposeStatus(composeId) if err != nil { - return err + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get compose status from client").SetInternal(err) } defer closeBody(ctx, resp.Body) - if resp.StatusCode == http.StatusNotFound { - body, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - // Composes can get deleted in composer, usually when the image is expired - return echo.NewHTTPError(http.StatusNotFound, string(body)) - } else if resp.StatusCode != http.StatusOK { - httpError := echo.NewHTTPError(http.StatusInternalServerError, "Failed querying compose status") - body, err := io.ReadAll(resp.Body) - if err != nil { - ctx.Logger().Errorf("Unable to parse composer's compose response: %v", err) - } else { - _ = httpError.SetInternal(fmt.Errorf("%s", body)) - } - return httpError + switch resp.StatusCode { + case http.StatusOK: + return h.handleSuccessfulResponse(ctx, resp, composeId) + case http.StatusNotFound: + return h.handleNotFoundResponse(resp) + default: + return h.handleErrorResponse(ctx, resp) } +} - var composeRequest ComposeRequest - err = json.Unmarshal(composeEntry.Request, &composeRequest) +func (h *Handlers) handleSuccessfulResponse(ctx echo.Context, resp *http.Response, composeId uuid.UUID) error { + composeRequest, err := h.parseAndRedactComposeRequest(ctx, composeId) if err != nil { return err } - if composeRequest.Customizations != nil && composeRequest.Customizations.Users != nil { - for _, u := range *composeRequest.Customizations.Users { - u.RedactPassword() - } - } - var cloudStat composer.ComposeStatus err = json.NewDecoder(resp.Body).Decode(&cloudStat) if err != nil { - return err + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to decode compose status").SetInternal(err) } - - us, err := parseComposerUploadStatus(cloudStat.ImageStatus.UploadStatus) + uploadStatus, err := parseComposerUploadStatus(cloudStat.ImageStatus.UploadStatus) if err != nil { - return err + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to parse upload status").SetInternal(err) } + status := ComposeStatus{ ImageStatus: ImageStatus{ Status: ImageStatusStatus(cloudStat.ImageStatus.Status), - UploadStatus: us, + UploadStatus: uploadStatus, }, Request: composeRequest, } @@ -298,6 +284,46 @@ func (h *Handlers) GetComposeStatus(ctx echo.Context, composeId uuid.UUID) error return ctx.JSON(http.StatusOK, status) } +func (h *Handlers) handleNotFoundResponse(resp *http.Response) error { + body, err := io.ReadAll(resp.Body) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read response body").SetInternal(err) + } + // Composes can get deleted in composer, usually when the image is expired + return echo.NewHTTPError(http.StatusNotFound, string(body)) +} + +func (h *Handlers) handleErrorResponse(ctx echo.Context, resp *http.Response) error { + httpError := echo.NewHTTPError(http.StatusInternalServerError, "Failed querying compose status") + body, err := io.ReadAll(resp.Body) + if err != nil { + ctx.Logger().Errorf("Unable to parse composer's compose response: %v", err) + } else { + _ = httpError.SetInternal(fmt.Errorf("%s", body)) + } + return httpError +} + +func (h *Handlers) parseAndRedactComposeRequest(ctx echo.Context, composeId uuid.UUID) (ComposeRequest, error) { + composeEntry, err := h.getComposeByIdAndOrgId(ctx, composeId) + if err != nil { + return ComposeRequest{}, err + } + + var composeRequest ComposeRequest + if err := json.Unmarshal(composeEntry.Request, &composeRequest); err != nil { + return ComposeRequest{}, echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal compose request").SetInternal(err) + } + + if composeRequest.Customizations != nil && composeRequest.Customizations.Users != nil { + for _, u := range *composeRequest.Customizations.Users { + u.RedactPassword() + } + } + + return composeRequest, nil +} + func parseComposerUploadStatus(us *composer.UploadStatus) (*UploadStatus, error) { if us == nil { return nil, nil