Skip to content

Commit

Permalink
Fixes 4762: Add missing nil checks for pulp methods (#849)
Browse files Browse the repository at this point in the history
* Fixes 4762: Add missing nil checks for pulp methods

* set default response code
  • Loading branch information
rverdile authored Oct 18, 2024
1 parent 4187a8a commit 0f585c6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 0 additions & 2 deletions pkg/pulp_client/rpm_distributions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (r *pulpDaoImpl) DeleteRpmDistribution(ctx context.Context, rpmDistribution
}
return "", errorWithResponseBody("error deleting rpm distribution", httpResp, err)
}
defer httpResp.Body.Close()
return resp.Task, nil
}

Expand All @@ -83,7 +82,6 @@ func (r *pulpDaoImpl) UpdateRpmDistribution(ctx context.Context, rpmDistribution
if err != nil {
return "", errorWithResponseBody("error listing rpm distributions", httpResp, err)
}
defer httpResp.Body.Close()

return resp.Task, nil
}
2 changes: 0 additions & 2 deletions pkg/pulp_client/rpm_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (r *pulpDaoImpl) UpdateRpmRemote(ctx context.Context, pulpHref string, url
if err != nil {
return "", errorWithResponseBody("error in rpm remote partial update", httpResp, err)
}
defer httpResp.Body.Close()

return updateResp.Task, nil
}
Expand Down Expand Up @@ -123,7 +122,6 @@ func (r *pulpDaoImpl) DeleteRpmRemote(ctx context.Context, pulpHref string) (str
if err != nil {
return "", errorWithResponseBody("error deleting rpm remote", httpResp, err)
}
defer httpResp.Body.Close()

return deleteResp.Task, nil
}
5 changes: 4 additions & 1 deletion pkg/pulp_client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ func (r *pulpDaoImpl) Status(ctx context.Context) (*zest.StatusResponse, error)
ctx, client := getZestClient(ctx)
// Change this back to StatusRead(r.ctx) on next zest update
status, resp, err := client.StatusAPI.StatusRead(ctx).Execute()
if resp != nil {
defer resp.Body.Close()
}

if err != nil {
return nil, err
}
defer resp.Body.Close()

return status, nil
}
Expand Down
1 change: 0 additions & 1 deletion pkg/pulp_client/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (r pulpDaoImpl) GetTask(ctx context.Context, taskHref string) (zest.TaskRes
if err != nil {
return zest.TaskResponse{}, errorWithResponseBody("error reading task", httpResp, err)
}
defer httpResp.Body.Close()

return *task, nil
}
Expand Down
20 changes: 14 additions & 6 deletions pkg/pulp_client/uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pulp_client

import (
"context"
"net/http"
"os"

zest "github.com/content-services/zest/release/v2024"
Expand All @@ -14,44 +15,51 @@ func (r *pulpDaoImpl) CreateUpload(ctx context.Context, size int64) (*zest.Uploa
if err != nil {
return nil, 0, err
}

statusCode := http.StatusInternalServerError
upload := zest.Upload{}
upload.Size = size
readResp, httpResp, err := client.UploadsAPI.UploadsCreate(ctx, r.domainName).Upload(upload).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return nil, httpResp.StatusCode, errorWithResponseBody("creating upload", httpResp, err)
return nil, statusCode, errorWithResponseBody("creating upload", httpResp, err)
}
return readResp, httpResp.StatusCode, nil
return readResp, statusCode, nil
}

// UploadChunk Uploads a chunk for an upload
func (r *pulpDaoImpl) UploadChunk(ctx context.Context, uploadHref string, contentRange string, file *os.File, sha256 string) (*zest.UploadResponse, int, error) {
ctx, client := getZestClient(ctx)
statusCode := http.StatusInternalServerError

readResp, httpResp, err := client.UploadsAPI.UploadsUpdate(ctx, uploadHref).ContentRange(contentRange).File(file).Sha256(sha256).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return &zest.UploadResponse{}, httpResp.StatusCode, errorWithResponseBody("uploading file chunk", httpResp, err)
return &zest.UploadResponse{}, statusCode, errorWithResponseBody("uploading file chunk", httpResp, err)
}
return readResp, httpResp.StatusCode, nil
return readResp, statusCode, nil
}

// FinishUpload Finishes an upload
func (r *pulpDaoImpl) FinishUpload(ctx context.Context, uploadHref string, sha256 string) (*zest.AsyncOperationResponse, int, error) {
ctx, client := getZestClient(ctx)
uploadCommit := zest.UploadCommit{}
uploadCommit.Sha256 = sha256
statusCode := http.StatusInternalServerError

readResp, httpResp, err := client.UploadsAPI.UploadsCommit(ctx, uploadHref).UploadCommit(uploadCommit).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return nil, httpResp.StatusCode, errorWithResponseBody("finishing upload", httpResp, err)
return nil, statusCode, errorWithResponseBody("finishing upload", httpResp, err)
}
return readResp, httpResp.StatusCode, nil
return readResp, statusCode, nil
}
6 changes: 6 additions & 0 deletions pkg/tasks/snapshot_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func (sh *SnapshotHelper) findOrCreatePublication(versionHref string) (string, e
if err != nil {
return "", err
}
if publicationTaskHref == nil {
return "", fmt.Errorf("publicationTaskHref cannot be nil")
}
err = sh.payload.SavePublicationTaskHref(*publicationTaskHref)

if err != nil {
Expand Down Expand Up @@ -177,6 +180,9 @@ func (sh *SnapshotHelper) createDistribution(publicationHref string, repoConfigU
if err != nil {
return "", "", false, err
}
if distTaskHref == nil {
return "", "", false, fmt.Errorf("distTaskHref cannot be nil")
}
err = sh.payload.SaveDistributionTaskHref(*distTaskHref)
if err != nil {
return "", "", false, err
Expand Down

0 comments on commit 0f585c6

Please sign in to comment.