Skip to content

Commit

Permalink
FirmwareUpload interface to accept a *os.File instead
Browse files Browse the repository at this point in the history
There has been no real use for having an io.Reader passed in
and this interface is to expect a file instead.
  • Loading branch information
joelrebel committed Nov 9, 2023
1 parent 925de5f commit 3a43b92
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
11 changes: 6 additions & 5 deletions bmc/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"os"

"github.com/bmc-toolbox/bmclib/v2/constants"
bconsts "github.com/bmc-toolbox/bmclib/v2/constants"
Expand Down Expand Up @@ -314,7 +315,7 @@ func firmwareInstallSteps(ctx context.Context, component string, generic []firmw
}

type FirmwareUploader interface {
FirmwareUpload(ctx context.Context, component string, reader io.Reader) (uploadVerifyTaskID string, err error)
FirmwareUpload(ctx context.Context, component string, file *os.File) (uploadVerifyTaskID string, err error)
}

// firmwareUploaderProvider is an internal struct to correlate an implementation/provider and its name
Expand All @@ -324,7 +325,7 @@ type firmwareUploaderProvider struct {
}

// FirmwareUploaderFromInterfaces identifies implementations of the FirmwareUploader interface and passes the found implementations to the firmwareUpload() wrapper.
func FirmwareUploadFromInterfaces(ctx context.Context, component string, reader io.Reader, generic []interface{}) (taskID string, metadata Metadata, err error) {
func FirmwareUploadFromInterfaces(ctx context.Context, component string, file *os.File, generic []interface{}) (taskID string, metadata Metadata, err error) {
implementations := make([]firmwareUploaderProvider, 0)
for _, elem := range generic {
temp := firmwareUploaderProvider{name: getProviderName(elem)}
Expand All @@ -347,10 +348,10 @@ func FirmwareUploadFromInterfaces(ctx context.Context, component string, reader
)
}

return firmwareUpload(ctx, component, reader, implementations)
return firmwareUpload(ctx, component, file, implementations)
}

func firmwareUpload(ctx context.Context, component string, reader io.Reader, generic []firmwareUploaderProvider) (taskID string, metadata Metadata, err error) {
func firmwareUpload(ctx context.Context, component string, file *os.File, generic []firmwareUploaderProvider) (taskID string, metadata Metadata, err error) {
var metadataLocal Metadata

for _, elem := range generic {
Expand All @@ -364,7 +365,7 @@ func firmwareUpload(ctx context.Context, component string, reader io.Reader, gen
return taskID, metadata, err
default:
metadataLocal.ProvidersAttempted = append(metadataLocal.ProvidersAttempted, elem.name)
taskID, vErr := elem.FirmwareUpload(ctx, component, reader)
taskID, vErr := elem.FirmwareUpload(ctx, component, file)
if vErr != nil {
err = multierror.Append(err, errors.WithMessagef(vErr, "provider: %v", elem.name))
err = multierror.Append(err, vErr)
Expand Down
7 changes: 4 additions & 3 deletions bmc/firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bmc
import (
"context"
"io"
"os"
"testing"
"time"

Expand Down Expand Up @@ -304,7 +305,7 @@ type firmwareUploadTester struct {
returnError error
}

func (f *firmwareUploadTester) FirmwareUpload(ctx context.Context, component string, reader io.Reader) (uploadVerifyTaskID string, err error) {
func (f *firmwareUploadTester) FirmwareUpload(ctx context.Context, component string, file *os.File) (uploadVerifyTaskID string, err error) {
return f.returnTaskID, f.returnError
}

Expand All @@ -316,7 +317,7 @@ func TestFirmwareUpload(t *testing.T) {
testCases := []struct {
testName string
component string
reader io.Reader
file *os.File
returnTaskID string
returnError error
ctxTimeout time.Duration
Expand All @@ -336,7 +337,7 @@ func TestFirmwareUpload(t *testing.T) {
}
ctx, cancel := context.WithTimeout(context.Background(), tc.ctxTimeout)
defer cancel()
taskID, metadata, err := firmwareUpload(ctx, tc.component, tc.reader, []firmwareUploaderProvider{{tc.providerName, &testImplementation}})
taskID, metadata, err := firmwareUpload(ctx, tc.component, tc.file, []firmwareUploaderProvider{{tc.providerName, &testImplementation}})
if tc.returnError != nil {
assert.ErrorIs(t, err, tc.returnError)
return
Expand Down
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"sync"
"time"

Expand Down Expand Up @@ -479,8 +480,8 @@ func (c *Client) FirmwareInstallSteps(ctx context.Context, component string) (ac
}

// FirmwareUpload just uploads the firmware for install, it returns a task ID to verify the upload status.
func (c *Client) FirmwareUpload(ctx context.Context, component string, reader io.Reader) (uploadVerifyTaskID string, err error) {
uploadVerifyTaskID, metadata, err := bmc.FirmwareUploadFromInterfaces(ctx, component, reader, c.Registry.GetDriverInterfaces())
func (c *Client) FirmwareUpload(ctx context.Context, component string, file *os.File) (uploadVerifyTaskID string, err error) {
uploadVerifyTaskID, metadata, err := bmc.FirmwareUploadFromInterfaces(ctx, component, file, c.Registry.GetDriverInterfaces())
c.setMetadata(metadata)
return uploadVerifyTaskID, err
}
Expand Down
8 changes: 1 addition & 7 deletions internal/redfishwrapper/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ type RedfishUpdateServiceParameters struct {
}

// FirmwareUpload uploads and initiates the firmware install process
func (c *Client) FirmwareUpload(ctx context.Context, reader io.Reader, params *RedfishUpdateServiceParameters) (taskID string, err error) {
// limit to *os.File until theres a need for other types of readers
updateFile, ok := reader.(*os.File)
if !ok {
return "", errors.Wrap(bmclibErrs.ErrFirmwareUpload, "method expects an *os.File object")
}

func (c *Client) FirmwareUpload(ctx context.Context, updateFile *os.File, params *RedfishUpdateServiceParameters) (taskID string, err error) {
parameters, err := json.Marshal(params)
if err != nil {
return "", errors.Wrap(errUpdateParams, err.Error())
Expand Down
8 changes: 4 additions & 4 deletions providers/supermicro/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package supermicro

import (
"context"
"io"
"os"
"strings"
"time"

Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *Client) FirmwareInstallSteps(ctx context.Context, component string) ([]
return nil, errors.Wrap(errUnexpectedModel, c.model)
}

func (c *Client) FirmwareUpload(ctx context.Context, component string, reader io.Reader) (taskID string, err error) {
func (c *Client) FirmwareUpload(ctx context.Context, component string, file *os.File) (taskID string, err error) {
if err := c.firmwareInstallSupported(ctx); err != nil {
return "", err
}
Expand All @@ -62,9 +62,9 @@ func (c *Client) FirmwareUpload(ctx context.Context, component string, reader io

switch {
case strings.HasPrefix(strings.ToLower(c.model), "x12"):
return c.x12().firmwareUpload(ctx, component, reader)
return c.x12().firmwareUpload(ctx, component, file)
case strings.HasPrefix(strings.ToLower(c.model), "x11"):
return c.x11().firmwareUpload(ctx, component, reader)
return c.x11().firmwareUpload(ctx, component, file)
}

return "", errors.Wrap(errUnexpectedModel, c.model)
Expand Down
6 changes: 3 additions & 3 deletions providers/supermicro/x12_firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/bmc-toolbox/bmclib/v2/constants"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (c *x12) firmwareInstallSteps(component string) ([]constants.FirmwareInstal
}

// upload firmware
func (c *x12) firmwareUpload(ctx context.Context, component string, reader io.Reader) (taskID string, err error) {
func (c *x12) firmwareUpload(ctx context.Context, component string, file *os.File) (taskID string, err error) {
if err = c.componentSupported(component); err != nil {
return "", err
}
Expand All @@ -65,7 +65,7 @@ func (c *x12) firmwareUpload(ctx context.Context, component string, reader io.Re
return "", err
}

taskID, err = c.redfish.FirmwareUpload(ctx, reader, params)
taskID, err = c.redfish.FirmwareUpload(ctx, file, params)
if err != nil {
if strings.Contains(err.Error(), "OemFirmwareAlreadyInUpdateMode") {
return "", errors.Wrap(brrs.ErrBMCColdResetRequired, "BMC currently in update mode, either continue the update OR if no update is currently running - reset the BMC")
Expand Down

0 comments on commit 3a43b92

Please sign in to comment.