forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
383 lines (336 loc) · 11.9 KB
/
storage.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// CacheSummary shall contain properties which describe the cache memory for a
// storage controller.
type CacheSummary struct {
// PersistentCacheSizeMiB shall contain the amount of
// cache memory that is persistent as measured in mebibytes. This size
// shall be less than or equal to the TotalCacheSizeMib.
PersistentCacheSizeMiB int
// Status shall contain any status or health properties
// of the resource.
Status common.Status
// TotalCacheSizeMiB shall contain the amount of
// configured cache memory as measured in mebibytes.
TotalCacheSizeMiB int
}
// Storage is used to represent resources that represent a storage
// subsystem in the Redfish specification.
type Storage struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Description provides a description of this resource.
Description string
// Drives is a collection that indicates all the drives attached to the
// storage controllers that this resource represents.
drives []string
// DrivesCount is the number of drives.
DrivesCount int `json:"Drives@odata.count"`
// Redundancy shall contain redundancy information for the storage subsystem.
Redundancy []Redundancy
// RedundancyCount is the number of Redundancy objects.
RedundancyCount int `json:"Redundancy@odata.count"`
// Status shall contain any status or health properties of the resource.
Status common.Status
// StorageControllers is a collection that indicates all the storage
// controllers that this resource represents.
StorageControllers []StorageController
// StorageControllersCount is the number of
StorageControllersCount int `json:"StorageControllers@odata.count"`
// Volumes is a collection that indicates all the volumes produced by the
// storage controllers that this resource represents.
volumes string
// Enclosures shall reference a resource of type Chassis that represents the
// physical containers attached to this resource.
enclosures []string
// EnclosuresCount is the number of enclosures.
EnclosuresCount int
// setEncryptionKeyTarget is the URL to send SetEncryptionKey requests.
setEncryptionKeyTarget string
}
// UnmarshalJSON unmarshals a Storage object from the raw JSON.
func (storage *Storage) UnmarshalJSON(b []byte) error {
type temp Storage
type links struct {
Enclosures common.Links
EnclosuresCount int `json:"Enclosures@odata.count"`
}
type actions struct {
SetEncryptionKey struct {
Target string
} `json:"#Storage.SetEncryptionKey"`
}
var t struct {
temp
Links links
Drives common.Links
Volumes common.Link
Actions actions
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*storage = Storage(t.temp)
// Extract the links to other entities for later
storage.enclosures = t.Links.Enclosures.ToStrings()
storage.EnclosuresCount = t.Links.EnclosuresCount
storage.drives = t.Drives.ToStrings()
storage.volumes = string(t.Volumes)
storage.setEncryptionKeyTarget = t.Actions.SetEncryptionKey.Target
return nil
}
// GetStorage will get a Storage instance from the service.
func GetStorage(c common.Client, uri string) (*Storage, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var storage Storage
err = json.NewDecoder(resp.Body).Decode(&storage)
if err != nil {
return nil, err
}
storage.SetClient(c)
return &storage, nil
}
// ListReferencedStorages gets the collection of Storage from a provided
// reference.
func ListReferencedStorages(c common.Client, link string) ([]*Storage, error) {
var result []*Storage
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, storageLink := range links.ItemLinks {
storage, err := GetStorage(c, storageLink)
if err != nil {
return result, err
}
result = append(result, storage)
}
return result, nil
}
// Enclosures gets the physical containers attached to this resource.
func (storage *Storage) Enclosures() ([]*Chassis, error) {
var result []*Chassis
for _, chassisLink := range storage.enclosures {
chassis, err := GetChassis(storage.Client, chassisLink)
if err != nil {
return result, nil
}
result = append(result, chassis)
}
return result, nil
}
// Drives gets the drives attached to the storage controllers that this
// resource represents.
func (storage *Storage) Drives() ([]*Drive, error) {
var result []*Drive
for _, driveLink := range storage.drives {
drive, err := GetDrive(storage.Client, driveLink)
if err != nil {
return result, nil
}
result = append(result, drive)
}
return result, nil
}
// Volumes gets the volumes associated with this storage subsystem.
func (storage *Storage) Volumes() ([]*Volume, error) {
return ListReferencedVolumes(storage.Client, storage.volumes)
}
// SetEncryptionKey shall set the encryption key for the storage subsystem.
func (storage *Storage) SetEncryptionKey(key string) error {
type temp struct {
EncryptionKey string
}
t := temp{EncryptionKey: key}
_, err := storage.Client.Post(storage.setEncryptionKeyTarget, t)
return err
}
// GetOperationApplyTimeValues returns the OperationApplyTime values applicable for this storage
func (storage *Storage) GetOperationApplyTimeValues() ([]common.OperationApplyTime, error) {
return AllowedVolumesUpdateApplyTimes(storage.Client, storage.volumes)
}
// StorageController is used to represent a resource that represents a
// storage controller in the Redfish specification.
type StorageController struct {
common.Entity
// Assembly shall be a link to a resource of type Assembly.
assembly string
// AssetTag is used to track the storage controller for inventory
// purposes.
AssetTag string
// CacheSummary shall contain properties which describe the cache memory for
// the current resource.
CacheSummary CacheSummary
// FirmwareVersion shall contain the firmware version as defined by the
// manufacturer for the associated storage controller.
FirmwareVersion string
// Identifiers shall contain a list of all known durable names for the
// associated storage controller.
Identifiers []common.Identifier
// Location shall contain location information of the
// associated storage controller.
Location common.Location
// Manufacturer shall be the name of the organization responsible for
// producing the storage controller. This organization might be the entity
// from whom the storage controller is purchased, but this is not
// necessarily true.
Manufacturer string
// MemberID shall uniquely identify the member within the collection.
MemberID string
// Model shall be the name by which the manufacturer generally refers to the
// storage controller.
Model string
// PCIeInterface is used to connect this PCIe-based controller to its host.
PCIeInterface PCIeInterface
// PartNumber shall be a part number assigned by the organization that is
// responsible for producing or manufacturing the storage controller.
PartNumber string
// SKU shall be the stock-keeping unit number for this storage storage
// controller.
SKU string
// SerialNumber is used to identify the storage controller.
SerialNumber string
// SpeedGbps shall represent the maximum supported speed of the Storage bus
// interface (in Gigabits per second). The interface specified connects the
// controller to the storage devices, not the controller to a host (e.g. SAS
// bus, not PCIe host bus).
SpeedGbps int
// Status shall contain any status or health properties of the resource.
Status common.Status
// SupportedControllerProtocols shall be the set of protocols this storage
// controller can be communicated to.
SupportedControllerProtocols []common.Protocol
// SupportedDeviceProtocols shall be the set of protocols this storage
// controller can use to communicate with attached devices.
SupportedDeviceProtocols []common.Protocol
// SupportedRAIDTypes shall contain all the RAIDType values supported by the
// current resource.
SupportedRAIDTypes []RAIDType
// Endpoints shall be a reference to the resources that this controller is
// associated with and shall reference a resource of type Endpoint.
endpoints []string
// EndpointsCount is the number of enclosures.
EndpointsCount int
// StorageServices shall be a reference to the resources that this
// controller is associated with and shall reference a resource of type
// StorageService.
storageServices []string
// StorageServicesCount is the number of storage services.
StorageServicesCount int
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a StorageController object from the raw JSON.
func (storagecontroller *StorageController) UnmarshalJSON(b []byte) error {
type temp StorageController
type links struct {
Endpoints common.Links
EndpointsCount int `json:"Endpoints@odata.count"`
StorageServices common.Links
StorageServicesCount int `json:"StorageServices@odata.count"`
}
var t struct {
temp
Assembly common.Link
Links links
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*storagecontroller = StorageController(t.temp)
// Extract the links to other entities for later
storagecontroller.assembly = string(t.Assembly)
storagecontroller.endpoints = t.Links.StorageServices.ToStrings()
storagecontroller.EndpointsCount = t.Links.EndpointsCount
storagecontroller.storageServices = t.Links.StorageServices.ToStrings()
storagecontroller.StorageServicesCount = t.Links.StorageServicesCount
// This is a read/write object, so we need to save the raw object data for later
storagecontroller.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (storagecontroller *StorageController) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(StorageController)
original.UnmarshalJSON(storagecontroller.rawData)
readWriteFields := []string{
"AssetTag",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(storagecontroller).Elem()
return storagecontroller.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetStorageController will get a Storage controller instance from the service.
func GetStorageController(c common.Client, uri string) (*StorageController, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var storage StorageController
err = json.NewDecoder(resp.Body).Decode(&storage)
if err != nil {
return nil, err
}
storage.SetClient(c)
return &storage, nil
}
// ListReferencedStorageControllers gets the collection of StorageControllers
// from a provided reference.
func ListReferencedStorageControllers(c common.Client, link string) ([]*StorageController, error) {
var result []*StorageController
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, storageLink := range links.ItemLinks {
storage, err := GetStorageController(c, storageLink)
if err != nil {
return result, err
}
result = append(result, storage)
}
return result, nil
}
// Assembly gets the storage controller's assembly.
func (storagecontroller *StorageController) Assembly() (*Assembly, error) {
if storagecontroller.assembly == "" {
return nil, nil
}
return GetAssembly(storagecontroller.Client, storagecontroller.assembly)
}
// Endpoints gets the storage controller's endpoints.
func (storagecontroller *StorageController) Endpoints() ([]*Endpoint, error) {
var result []*Endpoint
for _, endpointLink := range storagecontroller.endpoints {
endpoint, err := GetEndpoint(storagecontroller.Client, endpointLink)
if err != nil {
return result, err
}
result = append(result, endpoint)
}
return result, nil
}