Skip to content

Commit

Permalink
redfish/firmware: simpler UpdateParameters handling
Browse files Browse the repository at this point in the history
This makes the UpdateParameters handling easier to grok and maintain
  • Loading branch information
joelrebel committed Sep 9, 2023
1 parent 059b754 commit 08172f2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
15 changes: 4 additions & 11 deletions providers/redfish/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Conn) FirmwareInstall(ctx context.Context, component, applyAt string, f
c.redfishwrapper.SetHttpClientTimeout(time.Until(ctxDeadline))

payload := &multipartPayload{
updateParameters: bytes.NewReader(updateParameters),
updateParameters: updateParameters,
updateFile: updateFile,
}

Expand All @@ -135,7 +135,7 @@ func (c *Conn) FirmwareInstall(ctx context.Context, component, applyAt string, f
}

type multipartPayload struct {
updateParameters io.Reader
updateParameters []byte
updateFile *os.File
}

Expand Down Expand Up @@ -237,17 +237,10 @@ func multipartPayloadSize(payload *multipartPayload) (int64, *bytes.Buffer, erro
return 0, body, err
}

// a buffer to save the contents of the updateParameters reader
buf := bytes.Buffer{}
teeReader := io.TeeReader(payload.updateParameters, &buf)

if _, err = io.Copy(part, teeReader); err != nil {
if _, err = io.Copy(part, bytes.NewReader(payload.updateParameters)); err != nil {
return 0, body, err
}

// restore the reader
payload.updateParameters = bytes.NewReader(buf.Bytes())

// Add updateFile form
_, err = form.CreateFormFile("UpdateFile", filepath.Base(payload.updateFile.Name()))
if err != nil {
Expand Down Expand Up @@ -337,7 +330,7 @@ func (c *Conn) runRequestWithMultipartPayload(method, url string, payload *multi
return
}

if _, err = io.Copy(parametersPart, payload.updateParameters); err != nil {
if _, err = io.Copy(parametersPart, bytes.NewReader(payload.updateParameters)); err != nil {
c.Log.Error(errMultiPartPayload, err.Error()+": UpdateParameters part copy error")

return
Expand Down
3 changes: 1 addition & 2 deletions providers/redfish/firmware_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package redfish

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestMultipartPayloadSize(t *testing.T) {
{
"content length as expected",
&multipartPayload{
updateParameters: bytes.NewReader(updateParameters),
updateParameters: updateParameters,
updateFile: testfileFH,
},
475,
Expand Down

0 comments on commit 08172f2

Please sign in to comment.