Skip to content

Commit

Permalink
cloudapi: Move most of getComposeStatusImpl into a function
Browse files Browse the repository at this point in the history
This will make it easier to use the status in responses.

Related: RHEL-60120
  • Loading branch information
bcl committed Jan 11, 2025
1 parent d586fc2 commit 0891f41
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions internal/cloudapi/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -376,32 +386,32 @@ 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
for i := 1; i < len(finalizeInfo.Deps); i++ {
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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 0891f41

Please sign in to comment.