Skip to content

Commit

Permalink
Merge pull request #203 from bmc-toolbox/omar-m-othman/handle-redfish…
Browse files Browse the repository at this point in the history
…-errors

Errors come back inside the JSON, these have to be handled as errors.
  • Loading branch information
omar-m-othman authored Feb 18, 2021
2 parents 7c4822e + 52e76aa commit 428fe2c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
7 changes: 7 additions & 0 deletions providers/hp/hp.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ type ChassisInfo struct {
BayNumber int
}
} `json:"Oem"`
Error struct {
Code string `json:"code"`
Message string `json:"message"`
ExtendedMessage []*struct {
MessageId string `json:"MessageId"`
} `json:"@Message.ExtendedInfo"`
} `json:"error"`
}

// Mem is the struct used to render the data from https://$ip/json/mem_info, it contains the ram data
Expand Down
47 changes: 29 additions & 18 deletions providers/hp/ilo/ilo.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (i *Ilo) CheckCredentials() (err error) {
}

// get calls a given json endpoint of the iLO and returns the data
func (i *Ilo) get(endpoint string) (payload []byte, err error) {
func (i *Ilo) get(endpoint string, useSession bool) (payload []byte, err error) {
i.log.V(1).Info("retrieving data from bmc", "step", "bmc connection", "vendor", hp.VendorID, "ip", i.ip, "endpoint", endpoint)

bmcURL := fmt.Sprintf("https://%s", i.ip)
Expand All @@ -120,10 +120,14 @@ func (i *Ilo) get(endpoint string) (payload []byte, err error) {
return payload, err
}

for _, cookie := range i.httpClient.Jar.Cookies(u) {
if cookie.Name == "sessionKey" {
req.AddCookie(cookie)
if useSession {
for _, cookie := range i.httpClient.Jar.Cookies(u) {
if cookie.Name == "sessionKey" {
req.AddCookie(cookie)
}
}
} else {
req.SetBasicAuth(i.username, i.password)
}

reqDump, _ := httputil.DumpRequestOut(req, true)
Expand Down Expand Up @@ -200,7 +204,7 @@ func (i *Ilo) ChassisSerial() (string, error) {
return "", err
}

payload, err := i.get("json/rck_info")
payload, err := i.get("json/rck_info", true)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -257,7 +261,7 @@ func (i *Ilo) Name() (name string, err error) {
}

url := "json/overview"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return name, err
}
Expand All @@ -279,7 +283,7 @@ func (i *Ilo) Status() (health string, err error) {
}

url := "json/overview"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return health, err
}
Expand All @@ -305,7 +309,7 @@ func (i *Ilo) Memory() (mem int, err error) {
}

url := "json/mem_info"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return mem, err
}
Expand Down Expand Up @@ -335,7 +339,7 @@ func (i *Ilo) CPU() (cpu string, cpuCount int, coreCount int, hyperthreadCount i
}

url := "json/proc_info"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return cpu, cpuCount, coreCount, hyperthreadCount, err
}
Expand All @@ -361,7 +365,7 @@ func (i *Ilo) BiosVersion() (version string, err error) {
}

url := "json/overview"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return version, err
}
Expand All @@ -387,7 +391,7 @@ func (i *Ilo) PowerKw() (power float64, err error) {
}

url := "json/power_summary"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return power, err
}
Expand All @@ -409,7 +413,7 @@ func (i *Ilo) PowerState() (state string, err error) {
}

url := "json/power_summary"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return state, err
}
Expand All @@ -431,7 +435,7 @@ func (i *Ilo) TempC() (temp int, err error) {
}

url := "json/health_temperature"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return temp, err
}
Expand Down Expand Up @@ -484,7 +488,7 @@ func (i *Ilo) License() (name string, licType string, err error) {
}

url := "json/license"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return name, licType, err
}
Expand All @@ -506,12 +510,19 @@ func (i *Ilo) parseChassisInfo() (*hp.ChassisInfo, error) {

chassisInfo := &hp.ChassisInfo{}
// We try the new way of doing things first (RedFish).
payload, err := i.get(hp.ChassisInfoNewURL)
payload, err := i.get(hp.ChassisInfoNewURL, false)
if err == nil {
err = json.Unmarshal(payload, chassisInfo)
if err != nil {
return nil, err
}
if chassisInfo.Error.Code != "" {
e := "Code: " + chassisInfo.Error.Code + ", Message: " + chassisInfo.Error.Message
for i, s := range chassisInfo.Error.ExtendedMessage {
e += fmt.Sprintf(", Extended[%d]: %s", i, s)
}
return nil, fmt.Errorf(e)
}

// Matching the new interface to the old one, since the code still drops
// off to the old interface in case the new interface is not available.
Expand All @@ -527,7 +538,7 @@ func (i *Ilo) parseChassisInfo() (*hp.ChassisInfo, error) {
}

// This just means that we have to try the old way of doing things, since RedFish is not available.
payload, err = i.get(hp.ChassisInfoOldURL)
payload, err = i.get(hp.ChassisInfoOldURL, true)
if err != nil {
return nil, err
}
Expand All @@ -548,7 +559,7 @@ func (i *Ilo) Psus() (psus []*devices.Psu, err error) {
}

url := "json/power_supplies"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return psus, err
}
Expand Down Expand Up @@ -591,7 +602,7 @@ func (i *Ilo) Disks() (disks []*devices.Disk, err error) {
}

url := "json/health_phy_drives"
payload, err := i.get(url)
payload, err := i.get(url, true)
if err != nil {
return disks, err
}
Expand Down
14 changes: 7 additions & 7 deletions providers/hp/ilo/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (i *Ilo) Screenshot() (response []byte, extension string, err error) {
return response, extension, errors.ErrFeatureUnavailable
}

response, err = i.get(endpoint)
response, err = i.get(endpoint, true)
if err != nil {
return []byte{}, extension, err
}
Expand All @@ -60,7 +60,7 @@ func (i *Ilo) queryDirectoryGroups() (directoryGroups []DirectoryGroups, err err

endpoint := "json/directory_groups"

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down Expand Up @@ -94,7 +94,7 @@ func (i *Ilo) queryUsers() (usersInfo []UserInfo, err error) {

endpoint := "json/user_info"

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down Expand Up @@ -129,7 +129,7 @@ func (i *Ilo) queryNetworkSntp() (networkSntp NetworkSntp, err error) {

endpoint := "json/network_sntp/interface/0"

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down Expand Up @@ -163,7 +163,7 @@ func (i *Ilo) queryAccessSettings() (AccessSettings, error) {

var accessSettings AccessSettings

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down Expand Up @@ -197,7 +197,7 @@ func (i *Ilo) queryNetworkIPv4() (NetworkIPv4, error) {

var networkIPv4 NetworkIPv4

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down Expand Up @@ -231,7 +231,7 @@ func (i *Ilo) queryPowerRegulator() (PowerRegulator, error) {

var powerRegulator PowerRegulator

payload, err := i.get(endpoint)
payload, err := i.get(endpoint, true)
if err != nil {
msg := "GET request failed."
i.log.V(1).Info(msg,
Expand Down

0 comments on commit 428fe2c

Please sign in to comment.