From 0891f41e3952a6d6b58dfa6f83dd89568c36d5c8 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 17 Apr 2024 15:00:00 -0700 Subject: [PATCH] cloudapi: Move most of getComposeStatusImpl into a function This will make it easier to use the status in responses. Related: RHEL-60120 --- internal/cloudapi/v2/handler.go | 38 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/internal/cloudapi/v2/handler.go b/internal/cloudapi/v2/handler.go index 34ecd88212..f37491a0d4 100644 --- a/internal/cloudapi/v2/handler.go +++ b/internal/cloudapi/v2/handler.go @@ -324,21 +324,31 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { return HTTPError(ErrorInvalidComposeId) } + response, err := h.getJobIDComposeStatus(jobId) + if err != nil { + return err + } + return ctx.JSON(http.StatusOK, response) +} + +// getJobIDComposeStatus returns the ComposeStatus for the job +// or an HTTPError +func (h *apiHandlers) getJobIDComposeStatus(jobId uuid.UUID) (ComposeStatus, error) { jobType, err := h.server.workers.JobType(jobId) if err != nil { - return HTTPError(ErrorComposeNotFound) + return ComposeStatus{}, HTTPError(ErrorComposeNotFound) } if jobType == worker.JobTypeOSBuild { var result worker.OSBuildJobResult jobInfo, err := h.server.workers.OSBuildJobInfo(jobId, &result) if err != nil { - return HTTPError(ErrorMalformedOSBuildJobResult) + return ComposeStatus{}, HTTPError(ErrorMalformedOSBuildJobResult) } jobError, err := h.server.workers.JobDependencyChainErrors(jobId) if err != nil { - return HTTPError(ErrorGettingBuildDependencyStatus) + return ComposeStatus{}, HTTPError(ErrorGettingBuildDependencyStatus) } var uploadStatuses *[]UploadStatus @@ -349,7 +359,7 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { tr := result.TargetResults[idx] us, err := targetResultToUploadStatus(tr) if err != nil { - return HTTPError(ErrorUnknownUploadTarget) + return ComposeStatus{}, HTTPError(ErrorUnknownUploadTarget) } us.Status = uploadStatusFromJobStatus(jobInfo.JobStatus, result.JobError) statuses[idx] = *us @@ -363,7 +373,7 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { } } - return ctx.JSON(http.StatusOK, ComposeStatus{ + return ComposeStatus{ ObjectReference: ObjectReference{ Href: fmt.Sprintf("/api/image-builder-composer/v2/composes/%v", jobId), Id: jobId.String(), @@ -376,20 +386,20 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { UploadStatus: us0, // add the first upload status to the old top-level field UploadStatuses: uploadStatuses, }, - }) + }, nil } else if jobType == worker.JobTypeKojiFinalize { var result worker.KojiFinalizeJobResult finalizeInfo, err := h.server.workers.KojiFinalizeJobInfo(jobId, &result) if err != nil { - return HTTPError(ErrorMalformedOSBuildJobResult) + return ComposeStatus{}, HTTPError(ErrorMalformedOSBuildJobResult) } if len(finalizeInfo.Deps) < 2 { - return HTTPError(ErrorUnexpectedNumberOfImageBuilds) + return ComposeStatus{}, HTTPError(ErrorUnexpectedNumberOfImageBuilds) } var initResult worker.KojiInitJobResult _, err = h.server.workers.KojiInitJobInfo(finalizeInfo.Deps[0], &initResult) if err != nil { - return HTTPError(ErrorMalformedOSBuildJobResult) + return ComposeStatus{}, HTTPError(ErrorMalformedOSBuildJobResult) } var buildJobResults []worker.OSBuildJobResult var buildJobStatuses []ImageStatus @@ -397,11 +407,11 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { var buildJobResult worker.OSBuildJobResult buildInfo, err := h.server.workers.OSBuildJobInfo(finalizeInfo.Deps[i], &buildJobResult) if err != nil { - return HTTPError(ErrorMalformedOSBuildJobResult) + return ComposeStatus{}, HTTPError(ErrorMalformedOSBuildJobResult) } buildJobError, err := h.server.workers.JobDependencyChainErrors(finalizeInfo.Deps[i]) if err != nil { - return HTTPError(ErrorGettingBuildDependencyStatus) + return ComposeStatus{}, HTTPError(ErrorGettingBuildDependencyStatus) } var uploadStatuses *[]UploadStatus @@ -414,7 +424,7 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { if tr.Name != target.TargetNameKoji { us, err := targetResultToUploadStatus(tr) if err != nil { - return HTTPError(ErrorUnknownUploadTarget) + return ComposeStatus{}, HTTPError(ErrorUnknownUploadTarget) } us.Status = uploadStatusFromJobStatus(buildInfo.JobStatus, result.JobError) statuses = append(statuses, *us) @@ -452,9 +462,9 @@ func (h *apiHandlers) getComposeStatusImpl(ctx echo.Context, id string) error { if buildID != 0 { response.KojiStatus.BuildId = &buildID } - return ctx.JSON(http.StatusOK, response) + return response, nil } else { - return HTTPError(ErrorInvalidJobType) + return ComposeStatus{}, HTTPError(ErrorInvalidJobType) } }