diff --git a/providers/redfish/firmware.go b/providers/redfish/firmware.go index b6d68a81..48f95287 100644 --- a/providers/redfish/firmware.go +++ b/providers/redfish/firmware.go @@ -131,14 +131,28 @@ func (c *Conn) FirmwareInstall(ctx context.Context, component, applyAt string, f // The response contains a location header pointing to the task URI // Location: /redfish/v1/TaskService/Tasks/JID_467696020275 var location = resp.Header.Get("Location") - if strings.Contains(location, "JID_") { - taskID = strings.Split(resp.Header.Get("Location"), "JID_")[1] - } else if strings.Contains(location, "/Monitor") { + + taskID, err = TaskIDFromLocationURI(location) + + return taskID, err +} + +func TaskIDFromLocationURI(uri string) (taskID string, err error) { + + if strings.Contains(uri, "JID_") { + taskID = strings.Split(uri, "JID_")[1] + } else if strings.Contains(uri, "/Monitor") { // OpenBMC returns a monitor URL in Location // Location: /redfish/v1/TaskService/Tasks/12/Monitor - splits := strings.Split(location, "/") - taskID = splits[5] - } else { + splits := strings.Split(uri, "/") + if len(splits) >= 6 { + taskID = splits[5] + } else { + taskID = "" + } + } + + if taskID == "" { return "", bmclibErrs.ErrTaskNotFound } diff --git a/providers/redfish/firmware_test.go b/providers/redfish/firmware_test.go index 5731d16d..c42c2bfa 100644 --- a/providers/redfish/firmware_test.go +++ b/providers/redfish/firmware_test.go @@ -276,3 +276,23 @@ func Test_FirmwareInstall2(t *testing.T) { t.Fatal("Wrong test state:", state) } } + +func Test_TaskIDFromLocationURI(t *testing.T) { + var task string + var err error + + task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/JID_467696020275") + if err != nil || task != "467696020275" { + t.Fatal("Wrong task ID 467696020275. task,err=", task, err) + } + + task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/12/Monitor") + if err != nil || task != "12" { + t.Fatal("Wrong task ID 12. task,err=", task, err) + } + + task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/NO-TASK-ID") + if err == nil { + t.Fatal("Should return an error. task,err=", task, err) + } +}