Skip to content

Commit 9c3621b

Browse files
committed
providers/redfish/firmware: Add TaskIDFromLocationURI and tests
1 parent 2e973ca commit 9c3621b

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

providers/redfish/firmware.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,24 @@ func (c *Conn) FirmwareInstall(ctx context.Context, component, applyAt string, f
131131
// The response contains a location header pointing to the task URI
132132
// Location: /redfish/v1/TaskService/Tasks/JID_467696020275
133133
var location = resp.Header.Get("Location")
134-
if strings.Contains(location, "JID_") {
135-
taskID = strings.Split(resp.Header.Get("Location"), "JID_")[1]
136-
} else if strings.Contains(location, "/Monitor") {
134+
135+
taskID, err = TaskIDFromLocationURI(location)
136+
137+
return taskID, err
138+
}
139+
140+
func TaskIDFromLocationURI(uri string) (taskID string, err error) {
141+
142+
if strings.Contains(uri, "JID_") {
143+
taskID = strings.Split(uri, "JID_")[1]
144+
} else if strings.Contains(uri, "/Monitor") {
137145
// OpenBMC returns a monitor URL in Location
138146
// Location: /redfish/v1/TaskService/Tasks/12/Monitor
139-
splits := strings.Split(location, "/")
147+
splits := strings.Split(uri, "/")
140148
taskID = splits[5]
141-
} else {
149+
}
150+
151+
if taskID == "" {
142152
return "", bmclibErrs.ErrTaskNotFound
143153
}
144154

providers/redfish/firmware_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,23 @@ func Test_FirmwareInstall2(t *testing.T) {
276276
t.Fatal("Wrong test state:", state)
277277
}
278278
}
279+
280+
func Test_TaskIDFromLocationURI(t *testing.T) {
281+
var task string
282+
var err error
283+
284+
task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/JID_467696020275")
285+
if err != nil && task != "467696020275" {
286+
t.Fatal("Wrong task ID 467696020275. task,err=", task, err)
287+
}
288+
289+
task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/12/Monitor")
290+
if err != nil && task != "12" {
291+
t.Fatal("Wrong task ID 12. task,err=", task, err)
292+
}
293+
294+
task, err = TaskIDFromLocationURI("/redfish/v1/TaskService/Tasks/NO-TASK-ID")
295+
if err == nil {
296+
t.Fatal("Should return an error. task,err=", task, err)
297+
}
298+
}

0 commit comments

Comments
 (0)