forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualmedia.go
320 lines (276 loc) · 12.7 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
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
//
// 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"
)
type EjectPolicy string
const (
// OnPowerOffEjectPolicy The virtual media ejection occurs during a system power or reset event.
OnPowerOffEjectPolicy EjectPolicy = "OnPowerOff"
// SessionEjectPolicy The virtual media ejection occurs when a session is terminated. The session might be outside
// the Redfish service.
SessionEjectPolicy EjectPolicy = "Session"
// TimedEjectPolicy The virtual media ejection occurs when a timer configured by the EjectTimeout property expires.
TimedEjectPolicy EjectPolicy = "Timed"
// AfterUseEjectPolicy The virtual media ejection occurs after the media is used.
AfterUseEjectPolicy EjectPolicy = "AfterUse"
// PersistentEjectPolicy The virtual media mount information persists indefinitely.
PersistentEjectPolicy EjectPolicy = "Persistent"
)
// 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"`
// Certificates shall contain a link to a resource collection of type CertificateCollection that represents the
// server certificates for the server referenced by the Image property. If VerifyCertificate is 'true', services
// shall compare the certificates in this collection with the certificate obtained during handshaking with the
// image server in order to verify the identity of the image server prior to completing the remote media
// connection. If the server cannot be verified, the service shall not complete the remote media connection. If
// VerifyCertificate is 'false', the service shall not perform certificate verification with certificates in this
// collection. Regardless of the contents of this collection, services may perform additional verification based on
// other factors, such as the configuration of the SecurityPolicy resource.
certificates []string
// ClientCertificates shall contain a link to a resource collection of type CertificateCollection that represents
// the client identity certificates that are provided to the server referenced by the Image property as part of TLS
// handshaking.
clientCertificates []string
// 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
// EjectPolicy shall contain the ejection policy for the virtual media.
EjectPolicy EjectPolicy
// EjectTimeout shall indicate the amount of time before virtual media is automatically ejected when EjectPolicy
// contains 'Timed'.
EjectTimeout 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
// VerifyCertificate shall indicate whether the service will verify the certificate of the server referenced by the
// Image property prior to completing the remote media connection with the certificates found in the collection
// referenced by the Certificates property. If this property is not supported by the service, it shall be assumed
// to be 'false'. This property should default to 'false' in order to maintain compatibility with older clients.
// Regardless of the value of this property, services may perform additional verification based on other factors,
// such as the configuration of the SecurityPolicy resource.
VerifyCertificate bool
// WriteProtected shall indicate whether the remote
// device media prevents writing to that media.
WriteProtected bool
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
// ejectMediaTarget is the URL to send EjectMedia requests.
ejectMediaTarget string
// insertMediaTarget is the URL to send InsertMedia requests.
insertMediaTarget string
// 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 common.ActionTarget `json:"#VirtualMedia.EjectMedia"`
InsertMedia common.ActionTarget `json:"#VirtualMedia.InsertMedia"`
}
var t struct {
temp
Actions actions
Certificates common.LinksCollection
ClientCertificate common.LinksCollection
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*virtualmedia = VirtualMedia(t.temp)
// Extract the links to other entities for later
virtualmedia.certificates = t.Certificates.ToStrings()
virtualmedia.clientCertificates = t.ClientCertificate.ToStrings()
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
}
// Certificates gets the the server certificates for the server referenced by the
// Image property. If VerifyCertificate is `true`, services shall compare the
// certificates in this collection with the certificate obtained during handshaking
// with the image server in order to verify the identity of the image server prior
// to completing the remote media connection. If the server cannot be verified,
// the service shall not complete the remote media connection. If VerifyCertificate
// is `false`, the service shall not perform certificate verification with
// certificates in this collection. Regardless of the contents of this collection,
// services may perform additional verification based on other factors, such as the
// configuration of the SecurityPolicy resource.
func (virtualmedia *VirtualMedia) Certificates() ([]*Certificate, error) {
return common.GetObjects[Certificate](virtualmedia.GetClient(), virtualmedia.certificates)
}
// ClientCertificates gets the client identity certificates that are provided to
// the server referenced by the Image property as part of TLS handshaking.
func (virtualmedia *VirtualMedia) ClientCertificates() ([]*Certificate, error) {
return common.GetObjects[Certificate](virtualmedia.GetClient(), virtualmedia.clientCertificates)
}
// 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{
"EjectPolicy",
"EjectTimeout",
"Image",
"Inserted",
"MediaType",
"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"`
MediaType VirtualMediaType `json:",omitempty"`
Password string `json:",omitempty"`
TransferMethod TransferMethod `json:",omitempty"`
TransferProtocolType TransferProtocolType `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) {
return common.GetObject[VirtualMedia](c, uri)
}
// ListReferencedVirtualMedias gets the collection of VirtualMedia from
// a provided reference.
func ListReferencedVirtualMedias(c common.Client, link string) ([]*VirtualMedia, error) {
return common.GetCollectionObjects[VirtualMedia](c, link)
}