forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualmedia.go
295 lines (251 loc) · 8.97 KB
/
virtualmedia.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"errors"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// ConnectedVia are the ways the media may be connected.
type ConnectedVia string
const (
// NotConnectedConnectedVia No current connection.
NotConnectedConnectedVia ConnectedVia = "NotConnected"
// URIConnectedVia Connected to a URI location.
URIConnectedVia ConnectedVia = "URI"
// AppletConnectedVia Connected to a client application.
AppletConnectedVia ConnectedVia = "Applet"
// OemConnectedVia Connected through an OEM-defined method.
OemConnectedVia ConnectedVia = "Oem"
)
// VirtualMediaType is the type of media.
type VirtualMediaType string
const (
// CDMediaType A CD-ROM format (ISO) image.
CDMediaType VirtualMediaType = "CD"
// FloppyMediaType A floppy disk image.
FloppyMediaType VirtualMediaType = "Floppy"
// USBStickMediaType An emulation of a USB storage device.
USBStickMediaType VirtualMediaType = "USBStick"
// DVDMediaType A DVD-ROM format image.
DVDMediaType VirtualMediaType = "DVD"
)
// TransferMethod is how the data is transferred.
type TransferMethod string
const (
// StreamTransferMethod Stream image file data from the source URI.
StreamTransferMethod TransferMethod = "Stream"
// UploadTransferMethod Upload the entire image file from the source URI
// to the service.
UploadTransferMethod TransferMethod = "Upload"
)
// TransferProtocolType is the protocol used to transfer.
type TransferProtocolType string
const (
// CIFSTransferProtocolType Common Internet File System (CIFS).
CIFSTransferProtocolType TransferProtocolType = "CIFS"
// FTPTransferProtocolType File Transfer Protocol (FTP).
FTPTransferProtocolType TransferProtocolType = "FTP"
// SFTPTransferProtocolType Secure File Transfer Protocol (SFTP).
SFTPTransferProtocolType TransferProtocolType = "SFTP"
// HTTPTransferProtocolType Hypertext Transfer Protocol (HTTP).
HTTPTransferProtocolType TransferProtocolType = "HTTP"
// HTTPSTransferProtocolType Hypertext Transfer Protocol Secure (HTTPS).
HTTPSTransferProtocolType TransferProtocolType = "HTTPS"
// NFSTransferProtocolType Network File System (NFS).
NFSTransferProtocolType TransferProtocolType = "NFS"
// SCPTransferProtocolType Secure Copy Protocol (SCP).
SCPTransferProtocolType TransferProtocolType = "SCP"
// TFTPTransferProtocolType Trivial File Transfer Protocol (TFTP).
TFTPTransferProtocolType TransferProtocolType = "TFTP"
// OEMTransferProtocolType A manufacturer-defined protocol.
OEMTransferProtocolType TransferProtocolType = "OEM"
)
// VirtualMedia shall represent a virtual media service for a Redfish
// implementation.
type VirtualMedia struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// ConnectedVia shall contain the current connection
// method from a client to the virtual media that this Resource
// represents.
ConnectedVia ConnectedVia
// Description provides a description of this resource.
Description string
// Image shall contain an URI. A null value indicated
// no image connection.
Image string
// ImageName shall contain the name of the image.
ImageName string
// Inserted shall indicate whether media is present in
// the virtual media device.
Inserted bool
// MediaTypes shall be the supported media
// types for this connection.
MediaTypes []VirtualMediaType
// Password shall represent the password to access the
// Image parameter-specified URI. The value shall be null in responses.
Password string
// TransferMethod shall describe how the image transfer
// occurs.
TransferMethod TransferMethod
// TransferProtocolType shall represent the network
// protocol to use with the specified image URI.
TransferProtocolType TransferProtocolType
// UserName shall represent the user name to access the
// Image parameter-specified URI.
UserName string
// WriteProtected shall indicate whether the remote
// device media prevents writing to that media.
WriteProtected bool
// ejectMediaTarget is the URL to send EjectMedia requests.
ejectMediaTarget string
// insertMediaTarget is the URL to send InsertMedia requests.
insertMediaTarget string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
// SupportsMediaEject indicates if this implementation supports ejecting
// virtual media or not (added in schema 1.2.0).
SupportsMediaEject bool
// SupportsMediaInsert indicates if this implementation supports inserting
// virtual media or not (added in schema 1.2.0).
SupportsMediaInsert bool
}
// UnmarshalJSON unmarshals a VirtualMedia object from the raw JSON.
func (virtualmedia *VirtualMedia) UnmarshalJSON(b []byte) error {
type temp VirtualMedia
type actions struct {
EjectMedia struct {
Target string
} `json:"#VirtualMedia.EjectMedia"`
InsertMedia struct {
Target string
} `json:"#VirtualMedia.InsertMedia"`
}
var t struct {
temp
Actions actions
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*virtualmedia = VirtualMedia(t.temp)
// Extract the links to other entities for later
virtualmedia.ejectMediaTarget = t.Actions.EjectMedia.Target
virtualmedia.insertMediaTarget = t.Actions.InsertMedia.Target
virtualmedia.SupportsMediaEject = (virtualmedia.ejectMediaTarget != "")
virtualmedia.SupportsMediaInsert = (virtualmedia.insertMediaTarget != "")
// This is a read/write object, so we need to save the raw object data for later
virtualmedia.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (virtualmedia *VirtualMedia) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(VirtualMedia)
err := original.UnmarshalJSON(virtualmedia.rawData)
if err != nil {
return err
}
readWriteFields := []string{
"Image",
"Inserted",
"Password",
"TransferMethod",
"TransferProtocolType",
"UserName",
"WriteProtected",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(virtualmedia).Elem()
return virtualmedia.Entity.Update(originalElement, currentElement, readWriteFields)
}
// EjectMedia sends a request to eject the media.
func (virtualmedia *VirtualMedia) EjectMedia() error {
if !virtualmedia.SupportsMediaEject {
return errors.New("redfish service does not support VirtualMedia.EjectMedia calls")
}
return virtualmedia.Post(virtualmedia.ejectMediaTarget, struct{}{})
}
// InsertMedia sends a request to insert virtual media.
func (virtualmedia *VirtualMedia) InsertMedia(image string, inserted, writeProtected bool) error {
if !virtualmedia.SupportsMediaInsert {
return errors.New("redfish service does not support VirtualMedia.InsertMedia calls")
}
t := struct {
Image string
Inserted bool
WriteProtected bool
}{
Image: image,
Inserted: inserted,
WriteProtected: writeProtected,
}
return virtualmedia.Post(virtualmedia.insertMediaTarget, t)
}
// VirtualMediaConfig is an struct used to pass config data to build the HTTP body when inserting media
type VirtualMediaConfig struct {
Image string
Inserted bool `json:",omitempty"`
Password string `json:",omitempty"`
TransferMethod string `json:",omitempty"`
TransferProtocolType string `json:",omitempty"`
UserName string `json:",omitempty"`
WriteProtected bool `json:",omitempty"`
}
// InsertMediaConfig sends a request to insert virtual media using the VirtualMediaConfig struct
func (virtualmedia *VirtualMedia) InsertMediaConfig(config VirtualMediaConfig) error { //nolint
if !virtualmedia.SupportsMediaInsert {
return errors.New("redfish service does not support VirtualMedia.InsertMedia calls")
}
return virtualmedia.Post(virtualmedia.insertMediaTarget, config)
}
// GetVirtualMedia will get a VirtualMedia instance from the service.
func GetVirtualMedia(c common.Client, uri string) (*VirtualMedia, error) {
var virtualMedia VirtualMedia
return &virtualMedia, virtualMedia.Get(c, uri, &virtualMedia)
}
// ListReferencedVirtualMedias gets the collection of VirtualMedia from
// a provided reference.
func ListReferencedVirtualMedias(c common.Client, link string) ([]*VirtualMedia, error) { //nolint:dupl
var result []*VirtualMedia
if link == "" {
return result, nil
}
type GetResult struct {
Item *VirtualMedia
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
virtualmedia, err := GetVirtualMedia(c, link)
ch <- GetResult{Item: virtualmedia, 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
}