From 08172f2d91032140fb93567da8046fbcf5b76b0a Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Sat, 9 Sep 2023 06:56:00 +0200 Subject: [PATCH] redfish/firmware: simpler UpdateParameters handling This makes the UpdateParameters handling easier to grok and maintain --- providers/redfish/firmware.go | 15 ++++----------- providers/redfish/firmware_test.go | 3 +-- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/providers/redfish/firmware.go b/providers/redfish/firmware.go index a91bedca..ca6231f4 100644 --- a/providers/redfish/firmware.go +++ b/providers/redfish/firmware.go @@ -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, } @@ -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 } @@ -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 { @@ -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 diff --git a/providers/redfish/firmware_test.go b/providers/redfish/firmware_test.go index 62763ce5..b0fbedec 100644 --- a/providers/redfish/firmware_test.go +++ b/providers/redfish/firmware_test.go @@ -1,7 +1,6 @@ package redfish import ( - "bytes" "context" "encoding/json" "fmt" @@ -207,7 +206,7 @@ func TestMultipartPayloadSize(t *testing.T) { { "content length as expected", &multipartPayload{ - updateParameters: bytes.NewReader(updateParameters), + updateParameters: updateParameters, updateFile: testfileFH, }, 475,