Fixed HPA attribute translation validation 67/39867/1
authorDileep Ranganathan <dileep.ranganathan@intel.com>
Wed, 28 Mar 2018 15:37:36 +0000 (08:37 -0700)
committerDileep Ranganathan <dileep.ranganathan@intel.com>
Wed, 28 Mar 2018 15:37:36 +0000 (08:37 -0700)
Fixed HPA Feature Atrribute parameters validation in translation

Change-Id: I2dc0c65826e07d26d49f142042e848f66f0176e8
Issue-ID: OPTFRA-168
Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com>
conductor/conductor/controller/translator.py
conductor/conductor/tests/unit/controller/test_translator.py

index 7bc212b..2913c69 100644 (file)
@@ -107,8 +107,8 @@ CONSTRAINTS = {
 HPA_FEATURES = ['architecture', 'hpa-feature', 'hpa-feature-attributes',
                 'hpa-version', 'mandatory']
 HPA_OPTIONAL = ['score']
-HPA_ATTRIBUTES = ['hpa-attribute-key', 'hpa-attribute-value', 'operator',
-                  'unit']
+HPA_ATTRIBUTES = ['hpa-attribute-key', 'hpa-attribute-value', 'operator']
+HPA_ATTRIBUTES_OPTIONAL = ['unit']
 
 
 class TranslatorException(Exception):
@@ -584,16 +584,22 @@ class Translator(object):
                     if type(attr) is not dict:
                         raise TranslatorException(
                             "HPA feature attributes must be a dict")
-                    for name in attr.keys():
-                        if name not in HPA_ATTRIBUTES:
-                            raise TranslatorException(
-                                "Invalid attribute '{}' found inside HPA "
-                                "feature attributes".format(name))
-                    if list(attr.keys()) < ['hpa-attribute-key',
-                                            'hpa-attribute-value', 'operator']:
+
+                    # process mandatory hpa attribute parameter
+                    hpa_attr_mandatory = set(HPA_ATTRIBUTES).difference(
+                        attr.keys())
+                    if bool(hpa_attr_mandatory):
+                        raise TranslatorException(
+                            "Lack of compulsory elements inside HPA "
+                            "feature atrributes")
+                    # process optional hpa attribute parameter
+                    hpa_attr_optional = set(attr.keys()).difference(
+                        HPA_ATTRIBUTES)
+                    if hpa_attr_optional and not hpa_attr_optional.issubset(
+                            HPA_ATTRIBUTES_OPTIONAL):
                         raise TranslatorException(
-                            "Lack of compulsory elements "
-                            "inside HPA feature attributes")
+                            "Invalid attributes '{}' found inside HPA "
+                            "feature attributes".format(hpa_attr_optional))
 
     def parse_constraints(self, constraints):
         """Validate/prepare constraints for use by the solver."""
index 3dedfdf..ba0e3ec 100644 (file)
@@ -392,23 +392,24 @@ class TestNoExceptionTranslator(unittest.TestCase):
                 ],
                 "properties": {
                     "evaluate": [
-                        {'flavorLabel': 'xx',
-                         'flavorProperties': [
-                             {
-                                 'hpa-feature': 'BasicCapabilities',
-                                 'hpa-version': 'v1',
-                                 'architecture': 'generic',
-                                 'mandatory': 'False',
-                                 'score': '5',
-                                 'hpa-feature-attributes': [
-                                     {
-                                         'hpa-attribute-key': 'numVirtualCpu',
-                                         'hpa-attribute-value': '4',
-
-                                     },
-                                 ]
-                             }
-                         ], }
+                        {
+                            "flavorLabel": "xx",
+                            "flavorProperties": [
+                                {
+                                    "hpa-feature": "BasicCapabilities",
+                                    "hpa-version": "v1",
+                                    "architecture": "generic",
+                                    "mandatory": "False",
+                                    "score": "5",
+                                    "hpa-feature-attributes": [
+                                        {
+                                            "hpa-attribute-key": "numVirtualCpu",
+                                            "hpa-attribute-value": "4"
+                                        }
+                                    ]
+                                }
+                            ]
+                        }
                     ]
                 }
             }