[VVP] fixing relative imports for VVP
[vvp/validation-scripts.git] / ice_validator / tests / structures.py
index 12bfc63..362076d 100644 (file)
@@ -36,8 +36,6 @@
 # ============LICENSE_END============================================
 #
 #
-"""structures
-"""
 import collections
 import inspect
 import os
@@ -46,7 +44,7 @@ import sys
 
 from tests import cached_yaml as yaml
 from tests.helpers import load_yaml, get_param
-from .utils import nested_dict
+from tests.utils import nested_dict
 
 VERSION = "4.2.0"
 
@@ -252,15 +250,19 @@ class ContrailV2NetworkFlavorBaseProcessor(HeatProcessor):
                     network_flavor = cls.network_flavor_internal
                 else:
                     p = param.get("get_param")
-                    if isinstance(p, str):
-                        if "_int_" in p or p.startswith("int_"):
-                            network_flavor = cls.network_flavor_internal
-                        elif "_subint_" in p:
-                            network_flavor = cls.network_flavor_subint
-                        else:
-                            network_flavor = cls.network_flavor_external
+                    network_flavor = cls.get_network_format(p)
         return network_flavor
 
+    @classmethod
+    def get_network_format(cls, param):
+        if isinstance(param, str):
+            if "_int_" in param or param.startswith("int_"):
+                return cls.network_flavor_internal
+            elif "_subint_" in param:
+                return cls.network_flavor_subint
+            else:
+                return cls.network_flavor_external
+
 
 class ContrailV2InstanceIpProcessor(ContrailV2NetworkFlavorBaseProcessor):
     """ ContrailV2 InstanceIp
@@ -425,29 +427,6 @@ class ContrailV2VirtualNetworkProcessor(HeatProcessor):
     )
 
 
-class HeatResourceGroupProcessor(HeatProcessor):
-    """ Heat ResourceGroup
-    """
-
-    resource_type = "OS::Heat::ResourceGroup"
-    re_rids = collections.OrderedDict(
-        [
-            (
-                "subint",
-                _get_regex(
-                    r"(?P<vm_type>.+)"
-                    r"_(?P<vm_type_index>\d+)"
-                    r"_subint"
-                    r"_(?P<network_role>.+)"
-                    r"_port_(?P<port_index>\d+)"
-                    r"_subinterfaces"
-                    r"$"
-                ),
-            )
-        ]
-    )
-
-
 class NeutronNetProcessor(HeatProcessor):
     """ Neutron Net resource
     """
@@ -495,7 +474,10 @@ class NeutronPortProcessor(HeatProcessor):
         An OS::Nova:Port with the property binding:vnic_type
         """
         resource_properties = nested_dict.get(resource, "properties", default={})
-        if nested_dict.get(resource, "type") == cls.resource_type and resource_properties.get("binding:vnic_type", "") == "direct":
+        if (
+            nested_dict.get(resource, "type") == cls.resource_type
+            and resource_properties.get("binding:vnic_type", "") == "direct"
+        ):
             return True
 
         return False
@@ -556,7 +538,7 @@ class NovaServerProcessor(HeatProcessor):
             d = dict(
                 flavor=cls.get_flavor(resource),
                 image=cls.get_image(resource),
-                networks=cls.get_network(resource),
+                network_role=cls.get_network(resource),
             )
             if all(d.values()):
                 vm_class.update(d)
@@ -598,6 +580,10 @@ class Heat(object):
             self.load_env(envpath)
         self.heat_processors = self.get_heat_processors()
 
+    @property
+    def is_heat(self):
+        return "heat_template_version" in self.yml
+
     @property
     def contrail_resources(self):
         """This attribute is a dict of Contrail resources.
@@ -721,6 +707,31 @@ class Heat(object):
             re.search("(^(%(x)s)_)|(_(%(x)s)_)|(_(%(x)s)$)" % dict(x=part), name)
         )
 
+    def iter_nested_heat(self):
+        """
+        Returns an iterable of tuples (int, heat) where the first parameter is the
+        depth of the nested file and the second item is an instance of Heat
+        """
+
+        def walk_nested(heat, level=1):
+            resources = [Resource(r_id, data) for r_id, data in heat.resources.items()]
+            for resource in resources:
+                if resource.is_nested():
+                    nested_path = os.path.join(
+                        self.dirname, resource.get_nested_filename()
+                    )
+                    nested_heat = Heat(nested_path)
+                    yield level, nested_heat
+                    yield from walk_nested(nested_heat, level + 1)
+
+        yield from walk_nested(self)
+
+    def __str__(self):
+        return "Heat({})".format(self.filepath)
+
+    def __repr__(self):
+        return str(self)
+
 
 class Env(Heat):
     """An Environment file
@@ -785,10 +796,12 @@ class Resource(object):
             try:
                 return int(count_value)
             except (ValueError, TypeError):
-                print((
-                    "WARNING: Invalid value for count parameter {}. Expected "
-                    "an integer, but got {}. Defaulting to 1"
-                ).format(count_param, count_value))
+                print(
+                    (
+                        "WARNING: Invalid value for count parameter {}. Expected "
+                        "an integer, but got {}. Defaulting to 1"
+                    ).format(count_param, count_value)
+                )
         return 1
 
     @property
@@ -819,6 +832,12 @@ class Resource(object):
         else:
             return {}
 
+    def __str__(self):
+        return "Resource(id={}, type={})".format(self.resource_id, self.resource_type)
+
+    def __repr__(self):
+        return str(self)
+
 
 def get_all_resources(yaml_files):
     """Return a dict, resource id: resource