Fix OEM required attribute parsing

During initialization vendor id is not known and it does
not pick up Oem/Vendor section that triggers missing
attribute error when at least one field is marked as
required.

Fix this by skipping parsing if vendor id is not known.
Attributes are parsed again after setting parent resource
with vendor id.

It is expected that setting parent resource will always
be done from `common.get_resource_extension_by_vendor`
method.

Change-Id: Ib6cd982cb5def522c1a24b9914f2135ba2aeae71
This commit is contained in:
Aija Jauntēva 2021-07-09 09:27:19 -04:00
parent aed23871f7
commit 55d949cccc
5 changed files with 14 additions and 0 deletions

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Fixes OEM resource attribute parsing for OEM resources with required
fields. Before the fix an error such as "The attribute <attribute name> is
missing from the resource <resource path>" occurred.

View File

@ -55,6 +55,11 @@ class OEMResourceBase(base.ResourceBase):
:param json_doc: parsed JSON document in form of Python types
"""
# Too early to parse, need to call set_parent_resource first that
# assigns vendor_id and re-parses attributes
if self._vendor_id is None:
return
oem_json = json_doc.get(
'Oem', {}).get(self._vendor_id, {})

View File

@ -31,6 +31,7 @@ class ContosoActionsField(base.CompositeField):
class FakeOEMSystemExtension(oem_base.OEMResourceBase):
data_type = base.Field('@odata.type')
name = base.Field('Name', required=True)
production_location = ProductionLocationField('ProductionLocation')
_actions = ContosoActionsField('Actions')

View File

@ -54,6 +54,7 @@
"Oem": {
"Contoso": {
"@odata.type": "#Contoso.ComputerSystem",
"Name": "Contoso OEM system",
"ProductionLocation": {
"FacilityName": "PacWest Production Facility",
"Country": "USA"

View File

@ -37,6 +37,7 @@ class FakeOEMSystemExtensionTestCase(base.TestCase):
self.sys_instance, 'Contoso')
def test__parse_oem_attributes(self):
self.assertEqual('Contoso OEM system', self.fake_sys_oem_extn.name)
self.assertEqual('#Contoso.ComputerSystem',
self.fake_sys_oem_extn.data_type)
self.assertEqual('PacWest Production Facility', (