Handle TransferMethod in vmedia insertion

This commit adds the TransferMethod to insertmedia
calls if necessary.

Some HW require that the TransferMethod is set before
inserting virtual media.

Change-Id: I7fc5d41cdcf79d61cb1906f2755f55f284efe766
This commit is contained in:
Iury Gregory Melo Ferreira 2023-04-07 13:03:35 -03:00
parent 2506b87d87
commit c416bf158e
4 changed files with 52 additions and 1 deletions

View File

@ -0,0 +1,4 @@
---
fixes:
- |
Add TransferMethod for any general error that mentions it.

View File

@ -121,6 +121,16 @@ class VirtualMedia(base.ResourceBase):
and "#/TransferProtocolType" in error.related_properties
)
def is_transfer_method_required(self, error=None):
"""Check the response code and body and in case of failure
Try to determine if it happened due to missing TransferMethod
"""
if (error.code.endswith('GeneralError')
and 'TransferMethod' in error.detail):
return True
return False
def insert_media(self, image, inserted=True, write_protected=True,
username=None, password=None, transfer_method=None):
"""Attach remote media to virtual media
@ -186,7 +196,18 @@ class VirtualMedia(base.ResourceBase):
payload['TransferProtocolType'] = "HTTPS"
elif payload['Image'].startswith('http://'):
payload['TransferProtocolType'] = "HTTP"
self._conn.post(target_uri, data=payload)
# NOTE (iurygregory) we try to handle the case where a
# a TransferMethod is also required in the payload.
try:
self._conn.post(target_uri, data=payload)
except exceptions.HTTPError as error2:
if self.is_transfer_method_required(error2):
payload['TransferMethod'] = "Stream"
self._conn.post(target_uri, data=payload)
else:
raise
else:
raise
self.invalidate()

View File

@ -0,0 +1,16 @@
{
"error": {
"code": "Base.1.4.0.GeneralError",
"message": "See ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"@odata.type": "Message.v1_0_6.Message",
"MessageId": "Base.1.4.0.GeneralError",
"Message": "'TransferMethod' property which is mandatory to complete the action is missing in the request body.",
"MessageArgs": [],
"Severity": "Critical"
}
]
}
}

View File

@ -207,6 +207,16 @@ class VirtualMediaTestCase(base.TestCase):
retval = self.sys_virtual_media.is_transfer_protocol_required(error)
self.assertTrue(retval)
def test_is_transfer_method_required(self):
with open('sushy/tests/unit/json_samples/'
'transfer_method_required_error.json') as f:
response_obj = json.load(f)
response = mock.Mock(spec=['json', 'status_code'])
response.json.return_value = response_obj
error = exceptions.HTTPError('POST', 'VirtualMedia', response)
retval = self.sys_virtual_media.is_transfer_method_required(error)
self.assertTrue(retval)
def test_eject_media_none(self):
self.sys_virtual_media._actions.eject_media = None
self.assertRaisesRegex(