Skip to content

Commit b496772

Browse files
authored
Merge pull request #368 from bmc-toolbox/taskstate-constant
Taskstate constant
2 parents b0ef181 + 6c585e6 commit b496772

File tree

13 files changed

+167
-73
lines changed

13 files changed

+167
-73
lines changed

bmc/firmware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ type FirmwareTaskVerifier interface {
406406
// return values:
407407
// state - returns one of the FirmwareTask statuses (see devices/constants.go).
408408
// status - returns firmware task progress or other arbitrary task information.
409-
FirmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string) (state string, status string, err error)
409+
FirmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error)
410410
}
411411

412412
// firmwareTaskVerifierProvider is an internal struct to correlate an implementation/provider and its name
@@ -416,7 +416,7 @@ type firmwareTaskVerifierProvider struct {
416416
}
417417

418418
// firmwareTaskStatus returns the status of the firmware upload process.
419-
func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state, status string, metadata Metadata, err error) {
419+
func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state constants.TaskState, status string, metadata Metadata, err error) {
420420
var metadataLocal Metadata
421421

422422
for _, elem := range generic {
@@ -446,7 +446,7 @@ func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, c
446446
}
447447

448448
// FirmwareTaskStatusFromInterfaces identifies implementations of the FirmwareTaskVerifier interface and passes the found implementations to the firmwareTaskStatus() wrapper.
449-
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state, status string, metadata Metadata, err error) {
449+
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state constants.TaskState, status string, metadata Metadata, err error) {
450450
metadata = newMetadata()
451451

452452
implementations := make([]firmwareTaskVerifierProvider, 0)

bmc/firmware_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/bmc-toolbox/bmclib/v2/constants"
11-
"github.com/bmc-toolbox/bmclib/v2/errors"
1211
bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
1312
"github.com/bmc-toolbox/common"
1413
"github.com/stretchr/testify/assert"
@@ -41,7 +40,7 @@ func TestFirmwareInstall(t *testing.T) {
4140
providersAttempted int
4241
}{
4342
{"success with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", nil, 5 * time.Second, "foo", 1},
44-
{"failure with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
43+
{"failure with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
4544
{"failure with context timeout", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
4645
}
4746

@@ -136,7 +135,7 @@ func TestFirmwareInstallStatus(t *testing.T) {
136135
providersAttempted int
137136
}{
138137
{"success with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallComplete, nil, 5 * time.Second, "foo", 1},
139-
{"failure with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallFailed, errors.ErrNon200Response, 5 * time.Second, "foo", 1},
138+
{"failure with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallFailed, bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
140139
{"failure with context timeout", common.SlugBIOS, "1.1", "1234", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
141140
}
142141

@@ -230,7 +229,7 @@ func TestFirmwareInstallUploaded(t *testing.T) {
230229
providersAttempted int
231230
}{
232231
{"success with metadata", common.SlugBIOS, "1234", "5678", nil, 5 * time.Second, "foo", 1},
233-
{"failure with metadata", common.SlugBIOS, "1234", "", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
232+
{"failure with metadata", common.SlugBIOS, "1234", "", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
234233
{"failure with context timeout", common.SlugBIOS, "1234", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
235234
}
236235

@@ -325,7 +324,7 @@ func TestFirmwareUpload(t *testing.T) {
325324
providersAttempted int
326325
}{
327326
{"success with metadata", common.SlugBIOS, nil, "1234", nil, 5 * time.Second, "foo", 1},
328-
{"failure with metadata", common.SlugBIOS, nil, "1234", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
327+
{"failure with metadata", common.SlugBIOS, nil, "1234", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
329328
{"failure with context timeout", common.SlugBIOS, nil, "1234", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
330329
}
331330

@@ -430,7 +429,7 @@ func TestFirmwareInstallSteps(t *testing.T) {
430429
providersAttempted int
431430
}{
432431
{"success with metadata", common.SlugBIOS, []constants.FirmwareInstallStep{constants.FirmwareInstallStepUpload, constants.FirmwareInstallStepInstallStatus}, nil, 5 * time.Second, "foo", 1},
433-
{"failure with metadata", common.SlugBIOS, nil, errors.ErrNon200Response, 5 * time.Second, "foo", 1},
432+
{"failure with metadata", common.SlugBIOS, nil, bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
434433
{"failure with context timeout", common.SlugBIOS, nil, context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
435434
}
436435

@@ -459,12 +458,12 @@ func TestFirmwareInstallSteps(t *testing.T) {
459458
}
460459

461460
type firmwareTaskStatusTester struct {
462-
returnState string
461+
returnState constants.TaskState
463462
returnStatus string
464463
returnError error
465464
}
466465

467-
func (f *firmwareTaskStatusTester) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state string, status string, err error) {
466+
func (f *firmwareTaskStatusTester) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error) {
468467
return f.returnState, f.returnStatus, f.returnError
469468
}
470469

@@ -479,15 +478,15 @@ func TestFirmwareTaskStatus(t *testing.T) {
479478
component string
480479
taskID string
481480
installVersion string
482-
returnState string
481+
returnState constants.TaskState
483482
returnStatus string
484483
returnError error
485484
ctxTimeout time.Duration
486485
providerName string
487486
providersAttempted int
488487
}{
489488
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallComplete, "Upload completed", nil, 5 * time.Second, "foo", 1},
490-
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "Upload failed", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
489+
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "Upload failed", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
491490
{"failure with context timeout", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", "", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
492491
}
493492

@@ -523,15 +522,15 @@ func TestFirmwareTaskStatusFromInterfaces(t *testing.T) {
523522
component string
524523
taskID string
525524
installVersion string
526-
returnState string
525+
returnState constants.TaskState
527526
returnStatus string
528527
returnError error
529528
ctxTimeout time.Duration
530529
providerName string
531530
providersAttempted int
532531
}{
533-
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallComplete, "uploading", nil, 5 * time.Second, "foo", 1},
534-
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "failed", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
532+
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.Complete, "uploading", nil, 5 * time.Second, "foo", 1},
533+
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.Failed, "failed", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
535534
{"failure with context timeout", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", "", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
536535
}
537536

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func (c *Client) FirmwareUpload(ctx context.Context, component string, file *os.
623623
}
624624

625625
// FirmwareTaskStatus pass through library function to check firmware task statuses
626-
func (c *Client) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state, status string, err error) {
626+
func (c *Client) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error) {
627627
ctx, span := c.traceprovider.Tracer(pkgName).Start(ctx, "FirmwareTaskStatus")
628628
defer span.End()
629629

constants/constants.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ type (
66

77
// The FirmwareInstallStep identifies each phase of a firmware install process.
88
FirmwareInstallStep string
9+
10+
TaskState string
911
)
1012

1113
const (
12-
// Unknown is the constant that defines unknown things
13-
Unknown = "Unknown"
14-
1514
// EnvEnableDebug is the const for the environment variable to cause bmclib to dump debugging debugging information.
1615
// the valid parameter for this environment variable is 'true'
1716
EnvEnableDebug = "DEBUG_BMCLIB"
@@ -49,33 +48,41 @@ const (
4948
// FirmwareInstallInitializing indicates the device is performing init actions to install the update
5049
// this covers the redfish states - 'starting', 'downloading'
5150
// no action is required from the callers part in this state
52-
FirmwareInstallInitializing = "initializing"
51+
FirmwareInstallInitializing = "initializing"
52+
Initializing TaskState = "initializing"
5353

5454
// FirmwareInstallQueued indicates the device has queued the update, but has not started the update task yet
5555
// this covers the redfish states - 'pending', 'new'
5656
// no action is required from the callers part in this state
57-
FirmwareInstallQueued = "queued"
57+
FirmwareInstallQueued = "queued"
58+
Queued TaskState = "queued"
5859

5960
// FirmwareInstallRunner indicates the device is installing the update
6061
// this covers the redfish states - 'running', 'stopping', 'cancelling'
6162
// no action is required from the callers part in this state
62-
FirmwareInstallRunning = "running"
63+
FirmwareInstallRunning = "running"
64+
Running TaskState = "running"
6365

6466
// FirmwareInstallComplete indicates the device completed the firmware install
6567
// this covers the redfish state - 'complete'
66-
FirmwareInstallComplete = "complete"
68+
FirmwareInstallComplete = "complete"
69+
Complete TaskState = "complete"
6770

6871
// FirmwareInstallFailed indicates the firmware install failed
6972
// this covers the redfish states - 'interrupted', 'killed', 'exception', 'cancelled', 'suspended'
70-
FirmwareInstallFailed = "failed"
73+
FirmwareInstallFailed = "failed"
74+
Failed TaskState = "failed"
7175

7276
// FirmwareInstallPowerCycleHost indicates the firmware install requires a host power cycle
73-
FirmwareInstallPowerCycleHost = "powercycle-host"
77+
FirmwareInstallPowerCycleHost = "powercycle-host"
78+
PowerCycleHost TaskState = "powercycle-host"
7479

7580
// FirmwareInstallPowerCycleBMC indicates the firmware install requires a BMC power cycle
76-
FirmwareInstallPowerCycleBMC = "powercycle-bmc"
81+
FirmwareInstallPowerCycleBMC = "powercycle-bmc"
82+
PowerCycleBMC TaskState = "powercycle-bmc"
7783

78-
FirmwareInstallUnknown = "unknown"
84+
FirmwareInstallUnknown = "unknown"
85+
Unknown TaskState = "unknown"
7986

8087
// FirmwareInstallStepUploadInitiateInstall identifies the step to upload _and_ initialize the firmware install.
8188
// as part of the same call.

internal/redfishwrapper/task.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
gofishrf "github.com/stmcginnis/gofish/redfish"
1212
)
1313

14+
var (
15+
errUnexpectedTaskState = errors.New("unexpected task state")
16+
)
17+
1418
func (c *Client) Task(ctx context.Context, taskID string) (*gofishrf.Task, error) {
1519
tasks, err := c.Tasks(ctx)
1620
if err != nil {
@@ -28,29 +32,43 @@ func (c *Client) Task(ctx context.Context, taskID string) (*gofishrf.Task, error
2832
return nil, bmclibErrs.ErrTaskNotFound
2933
}
3034

31-
func (c *Client) TaskStatus(ctx context.Context, taskID string) (state, status string, err error) {
35+
func (c *Client) TaskStatus(ctx context.Context, taskID string) (constants.TaskState, string, error) {
3236
task, err := c.Task(ctx, taskID)
3337
if err != nil {
3438
return "", "", errors.Wrap(err, "error querying redfish for taskID: "+taskID)
3539
}
3640
taskInfo := fmt.Sprintf("id: %s, state: %s, status: %s", task.ID, task.TaskState, task.TaskStatus)
3741

38-
state = strings.ToLower(string(task.TaskState))
42+
state := strings.ToLower(string(task.TaskState))
43+
return c.ConvertTaskState(state), taskInfo, nil
44+
}
3945

46+
func (c *Client) ConvertTaskState(state string) constants.TaskState {
4047
switch state {
4148
case "starting", "downloading", "downloaded":
42-
return constants.FirmwareInstallInitializing, taskInfo, nil
49+
return constants.Initializing
4350
case "running", "stopping", "cancelling", "scheduling":
44-
return constants.FirmwareInstallRunning, taskInfo, nil
51+
return constants.Running
4552
case "pending", "new":
46-
return constants.FirmwareInstallQueued, taskInfo, nil
53+
return constants.Queued
4754
case "scheduled":
48-
return constants.FirmwareInstallPowerCycleHost, taskInfo, nil
55+
return constants.PowerCycleHost
4956
case "interrupted", "killed", "exception", "cancelled", "suspended", "failed":
50-
return constants.FirmwareInstallFailed, taskInfo, nil
57+
return constants.Failed
5158
case "completed":
52-
return constants.FirmwareInstallComplete, taskInfo, nil
59+
return constants.Complete
60+
default:
61+
return constants.Unknown
62+
}
63+
}
64+
65+
func (c *Client) TaskStateActive(state constants.TaskState) (bool, error) {
66+
switch state {
67+
case constants.Initializing, constants.Running, constants.Queued:
68+
return true, nil
69+
case constants.Complete, constants.Failed:
70+
return false, nil
5371
default:
54-
return constants.FirmwareInstallUnknown, taskInfo, nil
72+
return false, errors.Wrap(errUnexpectedTaskState, string(state))
5573
}
5674
}

0 commit comments

Comments
 (0)