forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
463 lines (408 loc) · 15.4 KB
/
drive.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
454
455
456
457
458
459
460
461
462
463
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// EncryptionAbility is the drive's encryption ability.
type EncryptionAbility string
const (
// NoneEncryptionAbility indicates the drive is not capable of self encryption.
NoneEncryptionAbility EncryptionAbility = "None"
// SelfEncryptingDriveEncryptionAbility indicates the drive is capable of self
// encryption per the Trusted Computing Group's Self Encrypting Drive
// Standard.
SelfEncryptingDriveEncryptionAbility EncryptionAbility = "SelfEncryptingDrive"
// OtherEncryptionAbility indicates the drive is capable of self encryption through
// some other means.
OtherEncryptionAbility EncryptionAbility = "Other"
)
// EncryptionStatus is the drive's encryption state.
type EncryptionStatus string
const (
// UnecryptedEncryptionStatus indicates the drive is not currently encrypted.
// note: this typo occurred in the spec and was deprecated in redfish v1.1
UnecryptedEncryptionStatus EncryptionStatus = "Unecrypted"
// UnlockedEncryptionStatus indicates the drive is currently encrypted but the data
// is accessible to the user unencrypted.
UnlockedEncryptionStatus EncryptionStatus = "Unlocked"
// LockedEncryptionStatus indicates the drive is currently encrypted and the data
// is not accessible to the user, however the system has the ability to
// unlock the drive automatically.
LockedEncryptionStatus EncryptionStatus = "Locked"
// ForeignEncryptionStatus indicates the drive is currently encrypted, the data is
// not accessible to the user, and the system requires user intervention
// to expose the data.
ForeignEncryptionStatus EncryptionStatus = "Foreign"
// UnencryptedEncryptionStatus indicates the drive is not currently encrypted.
UnencryptedEncryptionStatus EncryptionStatus = "Unencrypted"
)
// HotspareReplacementModeType is the replacement operation mode of a hot spare.
type HotspareReplacementModeType string
const (
// RevertibleHotspareReplacementModeType indicates the hot spare is drive that is
// commissioned due to a drive failure will revert to being a hotspare
// once the failed drive is replaced and rebuilt.
RevertibleHotspareReplacementModeType HotspareReplacementModeType = "Revertible"
// NonRevertibleHotspareReplacementModeType indicates the hot spare is drive that is
// commissioned due to a drive failure will remain as a data drive and
// will not revert to a hotspare if the failed drive is replaced.
NonRevertibleHotspareReplacementModeType HotspareReplacementModeType = "NonRevertible"
)
// HotspareType is the type of hot spare.
type HotspareType string
const (
// NoneHotspareType indicates the drive is not currently a hotspare.
NoneHotspareType HotspareType = "None"
// GlobalHotspareType indicates the drive is currently serving as a hotspare for
// all other drives in the storage system.
GlobalHotspareType HotspareType = "Global"
// ChassisHotspareType indicates the drive is currently serving as a hotspare for
// all other drives in the chassis.
ChassisHotspareType HotspareType = "Chassis"
// DedicatedHotspareType indicates the drive is currently serving as a hotspare for
// a user defined set of drives.
DedicatedHotspareType HotspareType = "Dedicated"
)
// MediaType is the drive's type.
type MediaType string
const (
// HDDMediaType The drive media type is traditional magnetic platters.
HDDMediaType MediaType = "HDD"
// SSDMediaType The drive media type is solid state or flash memory.
SSDMediaType MediaType = "SSD"
// SMRMediaType The drive media type is shingled magnetic recording.
SMRMediaType MediaType = "SMR"
)
// StatusIndicator is the drive's status.
type StatusIndicator string
const (
// OKStatusIndicator indicates the drive is OK.
OKStatusIndicator StatusIndicator = "OK"
// FailStatusIndicator The drive has failed.
FailStatusIndicator StatusIndicator = "Fail"
// RebuildStatusIndicator indicates the drive is being rebuilt.
RebuildStatusIndicator StatusIndicator = "Rebuild"
// PredictiveFailureAnalysisStatusIndicator indicates the drive is still working
// but predicted to fail soon.
PredictiveFailureAnalysisStatusIndicator StatusIndicator = "PredictiveFailureAnalysis"
// HotspareStatusIndicator indicates the drive is marked to be automatically
// rebuilt and used as a replacement for a failed drive.
HotspareStatusIndicator StatusIndicator = "Hotspare"
// InACriticalArrayStatusIndicator The array that this drive is a part of
// is degraded.
InACriticalArrayStatusIndicator StatusIndicator = "InACriticalArray"
// InAFailedArrayStatusIndicator The array that this drive is a part of
// is failed.
InAFailedArrayStatusIndicator StatusIndicator = "InAFailedArray"
)
// Drive is used to represent a disk drive or other physical storage
// medium for a Redfish implementation.
type Drive struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// assembly shall be a link to a resource of type Assembly.
assembly string
// AssetTag is used to track the drive for inventory purposes.
AssetTag string
// BlockSizeBytes shall contain size of the smallest addressable unit of the
// associated drive.
BlockSizeBytes int
// CapableSpeedGbs shall contain fastest capable bus speed of the associated
// drive.
CapableSpeedGbs float32
// CapacityBytes shall contain the raw size in bytes of the associated drive.
CapacityBytes int64
// Description provides a description of this resource.
Description string
// EncryptionAbility shall contain the encryption ability for the associated
// drive.
EncryptionAbility EncryptionAbility
// EncryptionStatus shall contain the encryption status for the associated
// drive.
EncryptionStatus EncryptionStatus
// FailurePredicted shall contain failure information as defined by the
// manufacturer for the associated drive.
FailurePredicted bool
// HotspareReplacementMode shall specify if a commissioned hotspare will
// continue to serve as a hotspare once the failed drive is replaced.
HotspareReplacementMode HotspareReplacementModeType
// HotspareType is used as part of a Volume.
HotspareType HotspareType
// Identifiers shall contain a list of all known durable
// names for the associated drive.
Identifiers []common.Identifier
// IndicatorLED shall contain the indicator light state for the indicator
// light associated with this drive.
IndicatorLED common.IndicatorLED
// Location shall contain location information of the associated drive.
Location []common.Location
// Manufacturer shall be the name of the organization responsible for
// producing the drive. This organization might be the entity from whom the
// drive is purchased, but this is not necessarily true.
Manufacturer string
// MediaType shall contain the type of media contained in the associated
// drive.
MediaType MediaType
// Model shall be the name by which the manufacturer generally refers to the
// drive.
Model string
// Multipath shall indicate whether the drive is
// accessible by an initiator from multiple paths allowing for failover
// capabilities upon a path failure.
Multipath bool
// NegotiatedSpeedGbs shall contain current bus speed of the associated
// drive.
NegotiatedSpeedGbs float32
// Operations shall contain a list of all operations currently running on
// the Drive.
Operations []common.Operations
// PartNumber shall be a part number assigned by the organization that is
// responsible for producing or manufacturing the drive.
PartNumber string
// PhysicalLocation shall contain location information of the associated
// drive.
PhysicalLocation common.Location
// PredictedMediaLifeLeftPercent shall contain an indicator of the
// percentage of life remaining in the Drive's media.
PredictedMediaLifeLeftPercent float32
// Protocol shall contain the protocol the associated drive is using to
// communicate to the storage controller for this system.
Protocol common.Protocol
// Revision shall contain the revision as defined by the manufacturer for
// the associated drive.
Revision string
// RotationSpeedRPM shall contain rotation speed of the associated drive.
RotationSpeedRPM float32
// SKU shall be the stock-keeping unit number for this drive.
SKU string
// SerialNumber is used to identify the drive.
SerialNumber string
// Status shall contain any status or health properties of the resource.
Status common.Status
// StatusIndicator shall contain the status indicator state for the status
// indicator associated with this drive. The valid values for this property
// are specified through the Redfish.AllowableValues annotation.
StatusIndicator StatusIndicator
// WriteCacheEnabled shall indicate whether the drive
// write cache is enabled.
WriteCacheEnabled bool
// chassis shall be a reference to a resource of type Chassis that represent
// the physical container associated with this Drive.
chassis string
// endpoints shall be a reference to the resources that this drive is
// associated with and shall reference a resource of type Endpoint.
endpoints []string
// EndpointsCount is the number of endpoints.
EndpointsCount int `json:"Endpoints@odata.count"`
// volumes are the associated volumes.
volumes []string
// Volumes is the number of associated volumes.
VolumesCount int
// pcieFunctions are the associated PCIeFunction objects.
pcieFunctions []string
// PCIeFunctionCount is the number of PCIeFunctions.
PCIeFunctionCount int
// storagePools []string
StoragePoolsCount int
// secureEraseTarget is the URL for SecureErase actions.
secureEraseTarget string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a Drive object from the raw JSON.
func (drive *Drive) UnmarshalJSON(b []byte) error {
type temp Drive
type links struct {
Chassis common.Link
Endpoints common.Links
EndpointCount int `json:"Endpoints@odata.count"`
// PCIeFunctions is The value of this property shall reference a resource
// of type PCIeFunction that represents the PCIe functions associated
// with this resource.
PCIeFunctions common.Links
// PCIeFunctions@odata.count is
PCIeFunctionsCount int `json:"PCIeFunctions@odata.count"`
StoragePools common.Links
StoragePoolsCount int `json:"StoragePools@odata.count"`
Volumes common.Links
VolumeCount int `json:"Volumes@odata.count"`
}
type Actions struct {
SecureErase struct {
Target string
} `json:"#Drive.SecureErase"`
}
var t struct {
temp
Links links
Actions Actions
Assembly common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*drive = Drive(t.temp)
drive.assembly = t.Assembly.String()
drive.chassis = t.Links.Chassis.String()
drive.endpoints = t.Links.Endpoints.ToStrings()
drive.EndpointsCount = t.Links.EndpointCount
drive.volumes = t.Links.Volumes.ToStrings()
drive.VolumesCount = t.Links.VolumeCount
drive.pcieFunctions = t.Links.PCIeFunctions.ToStrings()
drive.PCIeFunctionCount = t.Links.PCIeFunctionsCount
drive.secureEraseTarget = t.Actions.SecureErase.Target
// This is a read/write object, so we need to save the raw object data for later
drive.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (drive *Drive) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(Drive)
err := original.UnmarshalJSON(drive.rawData)
if err != nil {
return err
}
readWriteFields := []string{
"AssetTag",
"HotspareReplacementMode",
"IndicatorLED",
"StatusIndicator",
"WriteCacheEnabled",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(drive).Elem()
return drive.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetDrive will get a Drive instance from the service.
func GetDrive(c common.Client, uri string) (*Drive, error) {
var drive Drive
return &drive, drive.Get(c, uri, &drive)
}
// ListReferencedDrives gets the collection of Drives from a provided reference.
func ListReferencedDrives(c common.Client, link string) ([]*Drive, error) { //nolint:dupl
var result []*Drive
if link == "" {
return result, nil
}
type GetResult struct {
Item *Drive
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
drive, err := GetDrive(c, link)
ch <- GetResult{Item: drive, Link: link, Error: err}
}
go func() {
err := common.CollectList(get, c, link)
if err != nil {
collectionError.Failures[link] = err
}
close(ch)
}()
for r := range ch {
if r.Error != nil {
collectionError.Failures[r.Link] = r.Error
} else {
result = append(result, r.Item)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}
// Assembly gets the Assembly for this drive.
func (drive *Drive) Assembly() (*Assembly, error) {
if drive.assembly == "" {
return nil, nil
}
return GetAssembly(drive.Client, drive.assembly)
}
// Chassis gets the containing chassis for this drive.
func (drive *Drive) Chassis() (*Chassis, error) {
if drive.chassis == "" {
return nil, nil
}
return GetChassis(drive.Client, drive.chassis)
}
// Endpoints references the Endpoints that this drive is associated with.
func (drive *Drive) Endpoints() ([]*Endpoint, error) {
var result []*Endpoint
collectionError := common.NewCollectionError()
for _, endpointLink := range drive.endpoints {
endpoint, err := GetEndpoint(drive.Client, endpointLink)
if err != nil {
collectionError.Failures[endpointLink] = err
} else {
result = append(result, endpoint)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}
// Volumes references the Volumes that this drive is associated with.
func (drive *Drive) Volumes() ([]*Volume, error) {
var result []*Volume
collectionError := common.NewCollectionError()
for _, volumeLink := range drive.volumes {
volume, err := GetVolume(drive.Client, volumeLink)
if err != nil {
collectionError.Failures[volumeLink] = err
} else {
result = append(result, volume)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}
// PCIeFunctions references the PCIeFunctions that this drive is associated with.
func (drive *Drive) PCIeFunctions() ([]*PCIeFunction, error) {
var result []*PCIeFunction
collectionError := common.NewCollectionError()
for _, pcieFunctionLink := range drive.pcieFunctions {
pcieFunction, err := GetPCIeFunction(drive.Client, pcieFunctionLink)
if err != nil {
collectionError.Failures[pcieFunctionLink] = err
} else {
result = append(result, pcieFunction)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}
// // StoragePools references the StoragePools that this drive is associated with.
// func (drive *Drive) StoragePools() ([]*StoragePools, error) {
// var result []*StoragePools
// for _, storagePoolLink := range drive.storagePools {
// storagePool, err := GetStoragePools(drive.Client, storagePoolLink)
// if err != nil {
// return result, err
// }
// result = append(result, storagePool)
// }
// return result, nil
// }
// SecureErase shall perform a secure erase of the drive.
func (drive *Drive) SecureErase() error {
return drive.Post(drive.secureEraseTarget, nil)
}