/
simplestorage.go
138 lines (118 loc) · 3.64 KB
/
simplestorage.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"github.com/stmcginnis/gofish/common"
)
// Device shall describe a storage device visible to SimpleStorage.
type Device struct {
// Name shall be a user-friendly name for the device.
Name string
// CapacityBytes shall represent the size (in bytes) of the Storage Device.
CapacityBytes int64
// Manufacturer shall indicate the name of the manufacturer of this storage device.
Manufacturer string
// Model shall indicate the model information as provided by the manufacturer
// of this storage device.
Model string
// Status shall contain any status or health properties
// of the resource.
Status common.Status
}
// SimpleStorage is used to represent a storage controller and its
// directly-attached devices.
type SimpleStorage struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Actions is The Actions property shall contain the available actions
// for this resource.
Actions string
// Description provides a description of this resource.
Description string
// Devices shall contain a list of storage devices
// associated with this resource.
Devices []Device
// Status shall contain any status or health properties
// of the resource.
Status common.Status
// UefiDevicePath is used to identify and locate the specific storage
// controller.
UefiDevicePath string
// chassis shall be a reference to a resource of type Chassis that
// represent the physical container associated with this Simple Storage.
chassis string
}
// UnmarshalJSON unmarshals a SimpleStorage object from the raw JSON.
func (simplestorage *SimpleStorage) UnmarshalJSON(b []byte) error {
type temp SimpleStorage
var t struct {
temp
Links struct {
// Chassis shall be a reference to a resource of type Chassis that
// represent the physical container associated with this Simple Storage.
Chassis common.Link
}
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*simplestorage = SimpleStorage(t.temp)
simplestorage.chassis = t.Links.Chassis.String()
return nil
}
// GetSimpleStorage will get a SimpleStorage instance from the service.
func GetSimpleStorage(c common.Client, uri string) (*SimpleStorage, error) {
var simpleStorage SimpleStorage
return &simpleStorage, simpleStorage.Get(c, uri, &simpleStorage)
}
// ListReferencedSimpleStorages gets the collection of SimpleStorage from
// a provided reference.
func ListReferencedSimpleStorages(c common.Client, link string) ([]*SimpleStorage, error) { //nolint:dupl
var result []*SimpleStorage
if link == "" {
return result, nil
}
type GetResult struct {
Item *SimpleStorage
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
simplestorage, err := GetSimpleStorage(c, link)
ch <- GetResult{Item: simplestorage, 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
}
// Chassis gets the chassis containing this storage service.
func (simplestorage *SimpleStorage) Chassis() (*Chassis, error) {
if simplestorage.chassis == "" {
return nil, nil
}
return GetChassis(simplestorage.GetClient(), simplestorage.chassis)
}