Skip to content

Commit d621fb6

Browse files
authored
Improve virtual media ejecting and inserting: (#412)
2 parents 8209d7d + b58a061 commit d621fb6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

internal/redfishwrapper/virtual_media.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL stri
3333
return false, errors.New("invalid media type")
3434
}
3535

36+
supportedMediaTypes := []string{}
3637
for _, m := range managers {
3738
virtualMedia, err := m.VirtualMedia()
3839
if err != nil {
@@ -43,24 +44,32 @@ func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL stri
4344
}
4445

4546
for _, vm := range virtualMedia {
47+
if !slices.Contains(vm.MediaTypes, mediaKind) {
48+
for _, mt := range vm.MediaTypes {
49+
supportedMediaTypes = append(supportedMediaTypes, string(mt))
50+
}
51+
continue
52+
}
4653
if mediaURL == "" {
4754
// Only ejecting the media was requested.
48-
// For BMC's that don't support the "inserted" property, we need to eject the media if it's not already ejected.
4955
if vm.Inserted && vm.SupportsMediaEject {
5056
if err := vm.EjectMedia(); err != nil {
51-
return false, err
57+
return false, fmt.Errorf("error ejecting media: %v", err)
5258
}
5359
}
5460
return true, nil
5561
}
62+
// Ejecting the media before inserting a new new media makes the success rate of inserting the new media higher.
5663
if vm.Inserted && vm.SupportsMediaEject {
5764
if err := vm.EjectMedia(); err != nil {
58-
return false, err
65+
return false, fmt.Errorf("error ejecting media before inserting media: %v", err)
5966
}
6067
}
61-
if !slices.Contains(vm.MediaTypes, mediaKind) {
62-
return false, fmt.Errorf("media kind %s not supported by BMC, supported media kinds %q", kind, vm.MediaTypes)
68+
69+
if !vm.SupportsMediaInsert {
70+
return false, fmt.Errorf("BMC does not support inserting virtual media of kind: %s", kind)
6371
}
72+
6473
if err := vm.InsertMedia(mediaURL, true, true); err != nil {
6574
// Some BMC's (Supermicro X11SDV-4C-TLN2F, for example) don't support the "inserted" and "writeProtected" properties,
6675
// so we try to insert the media without them if the first attempt fails.
@@ -72,8 +81,7 @@ func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL stri
7281
}
7382
}
7483

75-
// If we actual get here, then something very unexpected happened as there isn't a known code path that would cause this error to be returned.
76-
return false, errors.New("unexpected error setting virtual media")
84+
return false, fmt.Errorf("not a supported media type: %s. supported media types: %v", kind, supportedMediaTypes)
7785
}
7886

7987
func (c *Client) InsertedVirtualMedia(ctx context.Context) ([]string, error) {

0 commit comments

Comments
 (0)