-
Notifications
You must be signed in to change notification settings - Fork 7
/
redfish_system.go
453 lines (427 loc) · 15.3 KB
/
redfish_system.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package virtualbmc
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
// ComputerSystem represents a ComputerSystem resource
type ComputerSystem struct {
OdataContext string `json:"@odata.context"`
OdataID string `json:"@odata.id"`
OdataType string `json:"@odata.type"`
Actions ComputerSystemActions `json:"Actions"`
AssetTag string `json:"AssetTag"`
Bios OdataID `json:"Bios"`
BiosVersion string `json:"BiosVersion"`
Boot Boot `json:"Boot"`
Description string `json:"Description"`
EthernetInterfaces OdataID `json:"EthernetInterfaces"`
HostName string `json:"HostName"`
HostWatchdogTimer HostWatchdogTimer `json:"HostWatchdogTimer"`
HostingRoles []interface{} `json:"HostingRoles"`
HostingRolesOdataCount int `json:"HostingRoles@odata.count"`
ID string `json:"Id"`
IndicatorLED string `json:"IndicatorLED"`
Links ComputerSystemLinks `json:"Links"`
Manufacturer string `json:"Manufacturer"`
Memory OdataID `json:"Memory"`
MemorySummary MemorySummary `json:"MemorySummary"`
Model string `json:"Model"`
Name string `json:"Name"`
NetworkInterfaces OdataID `json:"NetworkInterfaces"`
Oem ComputerSystemOem `json:"Oem"`
PCIeDevices []OdataID `json:"PCIeDevices"`
PCIeDevicesOdataCount int `json:"PCIeDevices@odata.count"`
PCIeFunctions []OdataID `json:"PCIeFunctions"`
PCIeFunctionsOdataCount int `json:"PCIeFunctions@odata.count"`
PartNumber string `json:"PartNumber"`
PowerState PowerStatus `json:"PowerState"`
ProcessorSummary ProcessorSummary `json:"ProcessorSummary"`
Processors OdataID `json:"Processors"`
SKU string `json:"SKU"`
SecureBoot OdataID `json:"SecureBoot"`
SerialNumber string `json:"SerialNumber"`
SimpleStorage OdataID `json:"SimpleStorage"`
Status MachineStatus `json:"Status"`
Storage OdataID `json:"Storage"`
SystemType string `json:"SystemType"`
TrustedModules []TrustedModule `json:"TrustedModules"`
UUID string `json:"UUID"`
}
// ComputerSystemActions represents ComputerSystem resource's Actions field
type ComputerSystemActions struct {
ComputerSystemReset ComputerSystemReset `json:"#ComputerSystem.Reset"`
}
// ComputerSystemReset represents ComputerSystem resource's Reset field
type ComputerSystemReset struct {
ResetTypeRedfishAllowableValues []ResetType `json:"ResetType@Redfish.AllowableValues"`
Target string `json:"target"`
}
// Boot represents ComputerSystem resource's Boot field
type Boot struct {
BootOptions OdataID `json:"BootOptions"`
BootOrder []string `json:"BootOrder"`
BootOrderOdataCount int `json:"BootOrder@odata.count"`
BootSourceOverrideEnabled string `json:"BootSourceOverrideEnabled"`
BootSourceOverrideMode string `json:"BootSourceOverrideMode"`
BootSourceOverrideTarget string `json:"BootSourceOverrideTarget"`
BootSourceOverrideTargetRedfishAllowableValues []string `json:"BootSourceOverrideTarget@Redfish.AllowableValues"`
UefiTargetBootSourceOverride string `json:"UefiTargetBootSourceOverride"`
}
// HostWatchdogTimer represents ComputerSystem resource's HostWatchdogTimer field
type HostWatchdogTimer struct {
FunctionEnabled bool `json:"FunctionEnabled"`
Status Status `json:"Status"`
TimeoutAction string `json:"TimeoutAction"`
}
// Status represents ComputerSystem resource's Status field
type Status struct {
State string `json:"State"`
}
// ComputerSystemLinks represents ComputerSystem resource's Links field
type ComputerSystemLinks struct {
Chassis []OdataID `json:"Chassis"`
ChassisOdataCount int `json:"Chassis@odata.count"`
CooledBy []OdataID `json:"CooledBy"`
CooledByOdataCount int `json:"CooledBy@odata.count"`
ManagedBy []OdataID `json:"ManagedBy"`
ManagedByOdataCount int `json:"ManagedBy@odata.count"`
Oem ComputerSystemLinksOem `json:"Oem"`
PoweredBy []OdataID `json:"PoweredBy"`
PoweredByOdataCount int `json:"PoweredBy@odata.count"`
}
// ComputerSystemLinksOem represents ComputerSystem resource's Links Oem field
type ComputerSystemLinksOem struct {
}
// MemorySummary represents ComputerSystem resource's MemorySummary field
type MemorySummary struct {
MemoryMirroring string `json:"MemoryMirroring"`
Status MachineStatus `json:"Status"`
TotalSystemMemoryGiB float64 `json:"TotalSystemMemoryGiB"`
}
// MachineStatus represents ComputerSystem resource's Status field
type MachineStatus struct {
Health string `json:"Health"`
HealthRollup string `json:"HealthRollup"`
State string `json:"State"`
}
// ComputerSystemOem represents ComputerSystem resource's Oem field
type ComputerSystemOem struct {
}
// ProcessorSummary represents ComputerSystem resource's ProcessorSummary field
type ProcessorSummary struct {
Count int `json:"Count"`
LogicalProcessorCount int `json:"LogicalProcessorCount"`
Model string `json:"Model"`
Status MachineStatus `json:"Status"`
}
// TrustedModule represents ComputerSystem resource's TrustedModule field
type TrustedModule struct {
FirmwareVersion string `json:"FirmwareVersion"`
InterfaceType string `json:"InterfaceType"`
Status Status `json:"Status"`
}
var computerSystemCollectionResponse = ResourceCollection{
OdataContext: "/redfish/v1/$metadata#ComputerSystemCollection.ComputerSystemCollection",
OdataID: "/redfish/v1/Systems",
OdataType: "#ComputerSystemCollection.ComputerSystemCollection",
Description: "Collection of Computer Systems",
Members: []OdataID{
{OdataID: fmt.Sprintf("/redfish/v1/Systems/%s", systemID)},
},
MembersOdataCount: 1,
Name: "Computer System Collection",
}
var serverIsAlreadyPoweredOffResponse = ErrorResponse{
Error: Error{
MessageExtendedInfo: []MessageExtendedInfo{
{
Message: "Server is already powered OFF",
MessageArgs: []string{},
MessageArgsOdataCount: 0,
MessageID: "X.X.X",
RelatedProperties: []interface{}{},
RelatedPropertiesOdataCount: 0,
Resolution: "No response action is required.",
Severity: "Informational",
},
},
Code: "Base.X.X.GeneralError",
Message: "A general error has occurred. See ExtendedInfo for more information",
},
}
var serverIsAlreadyPoweredOnResponse = ErrorResponse{
Error: Error{
MessageExtendedInfo: []MessageExtendedInfo{
{
Message: "Server is already powered ON",
MessageArgs: []string{},
MessageArgsOdataCount: 0,
MessageID: "X.X.X",
RelatedProperties: []interface{}{},
RelatedPropertiesOdataCount: 0,
Resolution: "No response action is required.",
Severity: "Informational",
},
},
Code: "Base.X.X.GeneralError",
Message: "A general error has occurred. See ExtendedInfo for more information",
},
}
func handleComputerSystemCollection(c *gin.Context) {
c.JSON(http.StatusOK, computerSystemCollectionResponse)
}
func (r *redfishServer) handleComputerSystem(c *gin.Context) {
id := c.Param("id")
_, ok := r.systemIDs[id]
if !ok {
c.JSON(http.StatusNotFound, nil)
return
}
status, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
c.JSON(http.StatusOK, createComputerSystemResponse(id, status))
}
func createComputerSystemResponse(systemID string, powerState PowerStatus) ComputerSystem {
return ComputerSystem{
OdataContext: "/redfish/v1/$metadata#ComputerSystem.ComputerSystem",
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s", systemID),
OdataType: "#ComputerSystem.v1_5_0.ComputerSystem",
Actions: ComputerSystemActions{
ComputerSystemReset: ComputerSystemReset{
ResetTypeRedfishAllowableValues: []ResetType{
ResetTypeOn,
ResetTypeForceOff,
ResetTypeForceRestart,
ResetTypeGracefulShutdown,
ResetTypePushPowerButton,
ResetTypeNmi,
},
Target: fmt.Sprintf("/redfish/v1/Systems/%s/Actions/ComputerSystem.Reset", systemID),
},
},
AssetTag: "",
Bios: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/Bios", systemID),
},
BiosVersion: "X.X.X",
Boot: Boot{
BootOptions: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/BootOptions", systemID),
},
BootOrder: []string{
"Boot0000",
"Boot0001",
},
BootOrderOdataCount: 2,
BootSourceOverrideEnabled: "Once",
BootSourceOverrideMode: "UEFI",
BootSourceOverrideTarget: "None",
BootSourceOverrideTargetRedfishAllowableValues: []string{
"None",
"Pxe",
"Floppy",
"Cd",
"Hdd",
"BiosSetup",
"Utilities",
"UefiTarget",
"SDCard",
"UefiHttp",
},
UefiTargetBootSourceOverride: "",
},
Description: "Computer System which represents a machine (physical or virtual) and the local resources such as memory, cpu and other devices that can be accessed from that machine.",
EthernetInterfaces: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/EthernetInterfaces", systemID),
},
HostName: "",
HostWatchdogTimer: HostWatchdogTimer{
FunctionEnabled: false,
Status: Status{
State: "Disabled",
},
TimeoutAction: "None",
},
HostingRoles: []interface{}{},
HostingRolesOdataCount: 0,
ID: systemID,
IndicatorLED: "Off",
Links: ComputerSystemLinks{
Chassis: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s", systemID),
},
},
ChassisOdataCount: 1,
CooledBy: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Sensors/Fans/Fan.Embedded.1A", systemID),
},
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Sensors/Fans/Fan.Embedded.1B", systemID),
},
},
CooledByOdataCount: 2,
ManagedBy: []OdataID{
{
OdataID: "/redfish/v1/Managers/1",
},
},
ManagedByOdataCount: 1,
Oem: ComputerSystemLinksOem{},
PoweredBy: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Chassis/%s/Power/PowerSupplies/PSU.Slot.1", systemID),
},
},
PoweredByOdataCount: 1,
},
Manufacturer: "Placemat",
Memory: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/Memory", systemID),
},
MemorySummary: MemorySummary{
MemoryMirroring: "System",
Status: MachineStatus{
Health: "OK",
HealthRollup: "OK",
State: "Enabled",
},
TotalSystemMemoryGiB: 300,
},
Model: "XXX",
Name: "System",
NetworkInterfaces: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/NetworkInterfaces", systemID),
},
Oem: ComputerSystemOem{},
PCIeDevices: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/PCIeDevice/0-31", systemID),
},
},
PCIeDevicesOdataCount: 1,
PCIeFunctions: []OdataID{
{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/PCIeFunction/0-31-4", systemID),
},
},
PCIeFunctionsOdataCount: 1,
PartNumber: "XXXX",
PowerState: powerState,
ProcessorSummary: ProcessorSummary{
Count: 2,
LogicalProcessorCount: 28,
Model: "XXXX",
Status: MachineStatus{
Health: "OK",
HealthRollup: "OK",
State: "Enabled",
},
},
Processors: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/Processors", systemID),
},
SKU: "XXXX",
SecureBoot: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/SecureBoot", systemID),
},
SerialNumber: "XXXX",
SimpleStorage: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/SimpleStorage/Controllers", systemID),
},
Status: MachineStatus{
Health: "OK",
HealthRollup: "OK",
State: "Enabled",
},
Storage: OdataID{
OdataID: fmt.Sprintf("/redfish/v1/Systems/%s/Storage", systemID),
},
SystemType: "Virtual",
TrustedModules: []TrustedModule{
{
FirmwareVersion: "X.X.X.X",
InterfaceType: "TPM2_0",
Status: Status{
State: "Enabled",
},
},
},
UUID: "XXXX",
}
}
func (r *redfishServer) handleComputerSystemActionsReset(c *gin.Context) {
var json RequestBody
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
switch json.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, serverIsAlreadyPoweredOnResponse)
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, serverIsAlreadyPoweredOffResponse)
return
}
if err := r.machine.PowerOff(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
case ResetTypeGracefulShutdown:
powerStatus, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
if powerStatus == PowerStatusOff || powerStatus == PowerStatusPoweringOff {
c.JSON(http.StatusConflict, serverIsAlreadyPoweredOffResponse)
return
}
if err := r.machine.PowerOff(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
case ResetTypeForceRestart:
powerStatus, err := r.machine.PowerStatus()
if err != nil {
c.JSON(http.StatusInternalServerError, nil)
return
}
if powerStatus == PowerStatusOff || powerStatus == PowerStatusPoweringOff {
c.JSON(http.StatusConflict, serverIsAlreadyPoweredOffResponse)
return
}
if err := r.machine.PowerOff(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := r.machine.PowerOn(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}
c.JSON(http.StatusNoContent, nil)
}