-
Notifications
You must be signed in to change notification settings - Fork 7
/
redfish_chassis.go
331 lines (309 loc) · 11 KB
/
redfish_chassis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package virtualbmc
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
// ChassisCollection represents the collection of Chassis resource instances
type ChassisCollection struct {
OdataContext string `json:"@odata.context"`
OdataID string `json:"@odata.id"`
OdataType string `json:"@odata.type"`
Description string `json:"Description"`
Members []OdataID `json:"Members"`
MembersOdataCount int `json:"Members@odata.count"`
Name string `json:"Name"`
}
// Chassis represents a Chassis resource
type Chassis struct {
OdataContext string `json:"@odata.context"`
OdataID string `json:"@odata.id"`
OdataType string `json:"@odata.type"`
Actions ChassisActions `json:"Actions"`
Assembly OdataID `json:"Assembly"`
AssetTag interface{} `json:"AssetTag"`
ChassisType string `json:"ChassisType"`
Description string `json:"Description"`
ID string `json:"Id"`
IndicatorLED string `json:"IndicatorLED"`
Links ChassisLinks `json:"Links"`
Location Location `json:"Location"`
Manufacturer string `json:"Manufacturer"`
Model string `json:"Model"`
Name string `json:"Name"`
NetworkAdapters OdataID `json:"NetworkAdapters"`
PartNumber string `json:"PartNumber"`
PhysicalSecurity PhysicalSecurity `json:"PhysicalSecurity"`
Power OdataID `json:"Power"`
PowerState PowerStatus `json:"PowerState"`
SKU string `json:"SKU"`
SerialNumber string `json:"SerialNumber"`
Status MachineStatus `json:"Status"`
Thermal OdataID `json:"Thermal"`
}
// ChassisActions represents Chassis's Actions field
type ChassisActions struct {
ChassisReset ChassisReset `json:"#Chassis.Reset"`
}
// ChassisReset represents Chassis's ChassisReset field
type ChassisReset struct {
ResetTypeRedfishAllowableValues []ResetType `json:"ResetType@Redfish.AllowableValues"`
Target string `json:"target"`
}
// ChassisLinks represents Chassis's Links field
type ChassisLinks struct {
ComputerSystems []OdataID `json:"ComputerSystems"`
ComputerSystemsOdataCount int `json:"ComputerSystems@odata.count"`
Contains []OdataID `json:"Contains"`
ContainsOdataCount int `json:"Contains@odata.count"`
CooledBy []OdataID `json:"CooledBy"`
CooledByOdataCount int `json:"CooledBy@odata.count"`
Drives []interface{} `json:"Drives"`
DrivesOdataCount int `json:"Drives@odata.count"`
ManagedBy []OdataID `json:"ManagedBy"`
ManagedByOdataCount int `json:"ManagedBy@odata.count"`
ManagersInChassis []OdataID `json:"ManagersInChassis"`
ManagersInChassisOdataCount int `json:"ManagersInChassis@odata.count"`
PCIeDevices []OdataID `json:"PCIeDevices"`
PCIeDevicesOdataCount int `json:"PCIeDevices@odata.count"`
PoweredBy []OdataID `json:"PoweredBy"`
PoweredByOdataCount int `json:"PoweredBy@odata.count"`
Storage []OdataID `json:"Storage"`
StorageOdataCount int `json:"Storage@odata.count"`
}
// Location represents Chassis's Location field
type Location struct {
Info string `json:"Info"`
InfoFormat string `json:"InfoFormat"`
Placement Placemat `json:"Placement"`
PostalAddress PostalAddress `json:"PostalAddress"`
}
// Placemat represents Chassis's Placemat field
type Placemat struct {
Rack string `json:"Rack"`
Row string `json:"Row"`
}
// PostalAddress represents Chassis's PostalAddress field
type PostalAddress struct {
Building string `json:"Building"`
Room string `json:"Room"`
}
// PhysicalSecurity represents Chassis's PhysicalSecurity field
type PhysicalSecurity struct {
IntrusionSensor string `json:"IntrusionSensor"`
IntrusionSensorNumber int `json:"IntrusionSensorNumber"`
IntrusionSensorReArm string `json:"IntrusionSensorReArm"`
}
var chassisCollectionResponse = ResourceCollection{
OdataContext: "/redfish/v1/$metadata#ChassisCollection.ChassisCollection",
OdataID: "/redfish/v1/Chassis/",
OdataType: "#ChassisCollection.ChassisCollection",
Description: "Collection of Chassis",
Members: []OdataID{
{OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s", systemID)},
},
MembersOdataCount: 1,
Name: "Chassis Collection",
}
func handleChassisCollection(c *gin.Context) {
c.JSON(http.StatusOK, chassisCollectionResponse)
}
func (r *redfishServer) handleChassis(c *gin.Context) {
id := c.Param("id")
_, ok := r.systemIDs[id]
if !ok {
c.JSON(http.StatusNotFound, createChassisNotFoundErrorResponse(id))
return
}
status, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
c.JSON(http.StatusOK, createChassisResponse(id, status))
}
func createChassisResponse(chassisID string, powerState PowerStatus) Chassis {
return Chassis{
OdataContext: "/redfish/v1/$metadata#Chassis.Chassis",
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s", chassisID),
OdataType: "#Chassis.v1_6_0.Chassis",
Actions: ChassisActions{
ChassisReset: ChassisReset{
ResetTypeRedfishAllowableValues: []ResetType{
ResetTypeOn,
ResetTypeForceOff,
},
Target: fmt.Sprintf("/redfish/v1/Chassis/%s/Actions/Chassis.Reset", chassisID),
},
},
Assembly: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Assembly", chassisID),
},
AssetTag: nil,
ChassisType: "RackMount",
Description: "It represents the properties for physical components for any system.It represent racks, rackmount servers, blades, standalone, modular systems,enclosures, and all other containers.The non-cpu/device centric parts of the schema are all accessed either directly or indirectly through this resource.",
ID: chassisID,
IndicatorLED: "Off",
Links: ChassisLinks{
ComputerSystems: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s", chassisID),
},
},
ComputerSystemsOdataCount: 1,
Contains: []OdataID{},
ContainsOdataCount: 0,
CooledBy: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Sensors/Fans/1A", chassisID),
},
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Sensors/Fans/1B", chassisID),
},
},
CooledByOdataCount: 2,
Drives: []interface{}{},
DrivesOdataCount: 0,
ManagedBy: []OdataID{
{
OdataID: "/redfish/v1/Managers/1",
},
},
ManagedByOdataCount: 1,
ManagersInChassis: []OdataID{
{
OdataID: "/redfish/v1/Managers/1",
},
},
ManagersInChassisOdataCount: 1,
PCIeDevices: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/PCIeDevice/0-31", chassisID),
},
},
PCIeDevicesOdataCount: 1,
PoweredBy: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Power/PowerSupplies/PSU.Slot.1", chassisID),
},
},
PoweredByOdataCount: 1,
Storage: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/Storage/CPU.1", chassisID),
},
},
StorageOdataCount: 1,
},
Location: Location{
Info: ";;;;1",
InfoFormat: "DataCenter;RoomName;Aisle;RackName;RackSlot",
Placement: Placemat{
Rack: "",
Row: "",
},
PostalAddress: PostalAddress{
Building: "",
Room: "",
},
},
Manufacturer: "Placemat Inc.",
Model: "Placemat XXX",
Name: "Computer System Chassis",
NetworkAdapters: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/NetworkAdapters", chassisID),
},
PartNumber: "0000",
PhysicalSecurity: PhysicalSecurity{
IntrusionSensor: "XXX",
IntrusionSensorNumber: 111,
IntrusionSensorReArm: "XXX",
},
Power: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Power", chassisID),
},
PowerState: powerState,
SKU: "XXXXXX",
SerialNumber: "XXXXXX",
Status: MachineStatus{
Health: "OK",
HealthRollup: "OK",
State: "Enabled",
},
Thermal: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Thermal", chassisID),
},
}
}
func createChassisNotFoundErrorResponse(chassisID string) ErrorResponse {
return ErrorResponse{
Error: Error{
MessageExtendedInfo: []MessageExtendedInfo{
{
Message: fmt.Sprintf("Unable to complete the operation because the resource %s entered in not found.", chassisID),
MessageArgs: []string{chassisID},
MessageArgsOdataCount: 1,
MessageID: "X.X.X",
RelatedProperties: []interface{}{},
RelatedPropertiesOdataCount: 0,
Resolution: "Enter the correct resource and retry the operation. For information about valid resource, see the Redfish Users Guide available on the support site.",
Severity: "Critical",
},
{
Message: fmt.Sprintf("The resource at the URI %s was not found.", chassisID),
MessageArgs: []string{chassisID},
MessageArgsOdataCount: 1,
MessageID: "Base.X.X.ResourceMissingAtURI",
RelatedProperties: []interface{}{""},
RelatedPropertiesOdataCount: 1,
Resolution: "Place a valid resource at the URI or correct the URI and resubmit the request.",
Severity: "Critical",
},
},
Code: "Base.X.X.GeneralError",
Message: "A general error has occurred. See ExtendedInfo for more information",
}}
}
func (r *redfishServer) handleChassisActionsReset(c *gin.Context) {
var json RequestBody
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
resetType := json.ResetType
if resetType != ResetTypeOn && resetType != ResetTypeForceOff {
c.JSON(http.StatusBadRequest, gin.H{"error": ""})
return
}
switch resetType {
case ResetTypeOn:
powerStatus, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
if powerStatus == PowerStatusOn || powerStatus == PowerStatusPoweringOn {
c.JSON(http.StatusConflict, nil)
return
}
if err := r.machine.PowerOn(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
case ResetTypeForceOff:
powerStatus, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
if powerStatus == PowerStatusOff || powerStatus == PowerStatusPoweringOff {
c.JSON(http.StatusConflict, nil)
return
}
if err := r.machine.PowerOff(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}
c.JSON(http.StatusNoContent, nil)
}