Skip to content

Commit

Permalink
feat: support oem field of thermal and fan
Browse files Browse the repository at this point in the history
  • Loading branch information
haoli committed Jun 3, 2022
1 parent 10cd4c3 commit 2713762
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
75 changes: 75 additions & 0 deletions oem/hpe/hpe.go
Original file line number Diff line number Diff line change
@@ -0,0 1,75 @@
//
// SPDX-License-Identifier: BSD-3-Clause
//

package hpe

import (
"encoding/json"

"github.com/stmcginnis/gofish/redfish"
)

func FromThermal(thermal redfish.Thermal) (Thermal, error) {
oem := ThermalOem{}

json.Unmarshal(thermal.Oem, &oem)

fans := make([]Fan, 0, len(thermal.Fans))

for _, f := range thermal.Fans {
fan, err := FromFan(f)
if err != nil {
return Thermal{}, err
}

fans = append(fans, fan)
}

return Thermal{
Thermal: thermal,
Fans: fans,
Oem: oem,
}, nil
}

func FromFan(fan redfish.Fan) (Fan, error) {
oem := FanOem{}

json.Unmarshal(fan.Oem, &oem)

return Fan{
Fan: fan,
Oem: oem,
}, nil
}

type Fan struct {
redfish.Fan
Oem FanOem
}

type FanOem struct {
Hpe struct {
OdataContext string `json:"@odata.context"`
OdataType string `json:"@odata.type"`
Location string `json:"Location"`
Redundant bool `json:"Redundant"`
HotPluggable bool `json:"HotPluggable"`
} `json:"Hpe"`
}

type Thermal struct {
redfish.Thermal
Fans []Fan
Oem ThermalOem
}

type ThermalOem struct {
Hpe struct {
OdataContext string `json:"@odata.context"`
OdataType string `json:"@odata.type"`
ThermalConfiguration string `json:"ThermalConfiguration"`
FanPercentMinimum int `json:"FanPercentMinimum"`
} `json:"Hpe"`
}
105 changes: 105 additions & 0 deletions oem/hpe/hpe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 1,105 @@
//
// SPDX-License-Identifier: BSD-3-Clause
//

package hpe

import (
"encoding/json"
"strings"
"testing"

"github.com/stmcginnis/gofish/redfish"
)

var hpeThermalBody = `{
"@odata.context": "/redfish/v1/$metadata#Thermal.Thermal",
"@odata.etag": "W/\"AEC0B081\"",
"@odata.id": "/redfish/v1/Chassis/1/Thermal/",
"@odata.type": "#Thermal.v1_1_0.Thermal",
"Id": "Thermal",
"Fans": [
{
"@odata.id": "/redfish/v1/Chassis/1/Thermal/#Fans/0",
"MemberId": "0",
"Name": "Fan 1",
"Oem": {
"Hpe": {
"@odata.context": "/redfish/v1/$metadata#HpeServerFan.HpeServerFan",
"@odata.type": "#HpeServerFan.v2_0_0.HpeServerFan",
"HotPluggable": true,
"Location": "System",
"Redundant": true
}
},
"Reading": 28,
"ReadingUnits": "Percent",
"Status": {
"Health": "OK",
"State": "Enabled"
}
}
],
"Name": "Thermal",
"Oem": {
"Hpe": {
"@odata.context": "/redfish/v1/$metadata#HpeThermalExt.HpeThermalExt",
"@odata.type": "#HpeThermalExt.v2_0_0.HpeThermalExt",
"FanPercentMinimum": 15,
"ThermalConfiguration": "OptimalCooling"
}
},
"Temperatures": [
{
"@odata.id": "/redfish/v1/Chassis/1/Thermal/#Temperatures/0",
"MemberId": "0",
"Name": "01-Inlet Ambient",
"Oem": {
"Hpe": {
"@odata.context": "/redfish/v1/$metadata#HpeSeaOfSensors.HpeSeaOfSensors",
"@odata.type": "#HpeSeaOfSensors.v2_0_0.HpeSeaOfSensors",
"LocationXmm": 15,
"LocationYmm": 0
}
},
"PhysicalContext": "Intake",
"ReadingCelsius": 25,
"SensorNumber": 1,
"Status": {
"Health": "OK",
"State": "Enabled"
},
"UpperThresholdCritical": 42,
"UpperThresholdFatal": 47,
"UpperThresholdUser": 0
}
]
}`

// TestHpeThermalOem tests the parsing of Thermal objects and support oem field.
func TestHpeThermalOem(t *testing.T) {
var thermal redfish.Thermal
err := json.NewDecoder(strings.NewReader(hpeThermalBody)).Decode(&thermal)

if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}

hpeThermal, err := FromThermal(thermal)
if err != nil {
t.Errorf("Convert thermal failed: %s", err)
return
}

if hpeThermal.Oem.Hpe.FanPercentMinimum != 15 {
t.Errorf("Received invalid fan fan percent minimum: %d", hpeThermal.Oem.Hpe.FanPercentMinimum)
}

if hpeThermal.Oem.Hpe.ThermalConfiguration != "OptimalCooling" {
t.Errorf("Received invalid hpeThermal configuration: %s", hpeThermal.Oem.Hpe.ThermalConfiguration)
}

if hpeThermal.Fans[0].Oem.Hpe.Location != "System" {
t.Errorf("Received invalid location: %s", hpeThermal.Fans[0].Oem.Hpe.Location)
}
}
8 changes: 8 additions & 0 deletions redfish/thermal.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 109,10 @@ type Fan struct {
// range but is not critical. The units shall be the same units as the
// related Reading property.
UpperThresholdNonCritical float32
// Oem shall contain the OEM extensions. All values for properties that
// this object contains shall conform to the Redfish Specification
// described requirements.
Oem json.RawMessage
}

// UnmarshalJSON unmarshals a Fan object from the raw JSON.
Expand Down Expand Up @@ -281,6 285,10 @@ type Thermal struct {
Temperatures []Temperature
// TemperaturesCount is the number of Temperature objects
TemperaturesCount int `json:"[email protected]"`
// Oem shall contain the OEM extensions. All values for properties that
// this object contains shall conform to the Redfish Specification
// described requirements.
Oem json.RawMessage
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
Expand Down

0 comments on commit 2713762

Please sign in to comment.