refactor: improve Redfish service connection handling and update status retrieval

This commit is contained in:
Alex Lovell-Troy 2025-02-07 09:52:21 -05:00 committed by David Allen
parent 51c01df73a
commit 03bf2250a4
Signed by: towk
GPG key ID: 0430CDBE22619155

View file

@ -2,10 +2,8 @@ package magellan
import ( import (
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"github.com/OpenCHAMI/magellan/pkg/client"
"github.com/stmcginnis/gofish" "github.com/stmcginnis/gofish"
"github.com/stmcginnis/gofish/redfish" "github.com/stmcginnis/gofish/redfish"
) )
@ -37,7 +35,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
return fmt.Errorf("failed to parse URI: %w", err) return fmt.Errorf("failed to parse URI: %w", err)
} }
// Connect to the Redfish service using gofish (using insecure connection for this example) // Connect to the Redfish service using gofish (using insecure connection for this)
client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: q.Username, Password: q.Password, Insecure: true}) client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: q.Username, Password: q.Password, Insecure: true})
if err != nil { if err != nil {
return fmt.Errorf("failed to connect to Redfish service: %w", err) return fmt.Errorf("failed to connect to Redfish service: %w", err)
@ -71,18 +69,23 @@ func GetUpdateStatus(q *UpdateParams) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to parse URI: %w", err) return fmt.Errorf("failed to parse URI: %w", err)
} }
uri.User = url.UserPassword(q.Username, q.Password)
updateUrl := fmt.Sprintf("%s/redfish/v1/UpdateService", uri.String()) // Connect to the Redfish service using gofish (using insecure connection for this)
res, body, err := client.MakeRequest(nil, updateUrl, "GET", nil, nil) client, err := gofish.Connect(gofish.ClientConfig{Endpoint: uri.String(), Username: q.Username, Password: q.Password, Insecure: true})
if err != nil { if err != nil {
return fmt.Errorf("something went wrong: %v", err) return fmt.Errorf("failed to connect to Redfish service: %w", err)
} else if res == nil {
return fmt.Errorf("no response returned (url: %s)", updateUrl)
} else if res.StatusCode != http.StatusOK {
return fmt.Errorf("returned status code %d", res.StatusCode)
} }
if len(body) > 0 { defer client.Logout()
fmt.Printf("%v\n", string(body))
// Retrieve the UpdateService from the Redfish client
updateService, err := client.Service.UpdateService()
if err != nil {
return fmt.Errorf("failed to get update service: %w", err)
} }
// Get the update status
status := updateService.Status
fmt.Printf("Update Status: %v\n", status)
return nil return nil
} }