+ level: 0 1 2 3
+ file: template -> nested -> nested -> nested
+ depth: 3 2 1 0
+ """
+ bad = []
+ files = {}
+ heat = {}
+ depths = {}
+ for yaml_file in yaml_files:
+ dirname, basename = path.split(yaml_file)
+ h = Heat(filepath=yaml_file)
+ heat[basename] = h
+ files[basename] = get_dict_of_nested_files(h.yml, dirname)
+ for filename in files:
+ depths[filename] = _get_nesting_depth_start(0, filename, files, [])
+ for depth in depths[filename]:
+ if depth[0] > MAX_DEPTH:
+ bad.append("{} {}".format(filename, str(depth[1])))
+ return bad, files, heat, depths
+
+
+def _get_nesting_depth_start(depth, filename, files, context):
+ depths = []
+ for rid, nf in files[filename].items():
+ depths.append(_get_nesting_depth(1, nf, files, context))
+ return depths
+
+
+def _get_nesting_depth(depth, filename, files, context):
+ """Return a depth tuple (max_depth, current_context).
+ `context` is the list of filenames.
+ `depth` is the length of `context`.
+ Finds the max_depth of all the resources of `filename`.
+ current_context is the updated list of filenames
+ and max_depth is its length.
+ """
+ max_depth = depth + 1
+ current_context = context + [filename]
+ if depth <= MAX_DEPTH:
+ nested_filenames = files.get(filename, {})
+ if nested_filenames:
+ max_depth, current_context = max(
+ _get_nesting_depth(depth + 1, nested_filename, files, current_context)
+ for nested_filename in nested_filenames.values()
+ )
+ return max_depth, current_context
+
+
+def get_resourcegroup_nested_files(yml, dirpath):
+ """
+ return a dict.
+ key: key in yml which references a nested ResourceGroup file.
+ (resource->type is ResourceGroup
+ and resource->properties->resource_def->type is a yaml file)
+ value: the nested file name.
+
+ The keys are assumed to be unique across files.
+ A separate test checks for that.
+ """
+
+ if not hasattr(yml, "get"):
+ return {}
+
+ nested_files = {}
+ for rid, r in yml.get("resources", {}).items():
+ if isinstance(r, dict) and "type" in r:
+ t = r["type"]
+ nested_file = None
+ if t == "OS::Heat::ResourceGroup":
+ rdt = r.get("properties", {}).get("resource_def", {}).get("type", None)
+ if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
+ nested_file = rdt
+ if nested_file:
+ filepath = path.join(dirpath, nested_file)
+ if path.exists(filepath):
+ nested_files[rid] = nested_file
+ return nested_files
+
+
+def get_type_nested_files(yml, dirpath):
+ """
+ return a dict.
+ key: key in yml which references a nested type file.
+ (the resource "type" is a yaml file.)
+ value: the nested file name.
+
+ The keys are assumed to be unique across files.
+ A separate test checks for that.
+ """
+
+ if not hasattr(yml, "get"):
+ return {}
+
+ nested_files = {}
+ for rid, r in yml.get("resources", {}).items():
+ if isinstance(r, dict) and "type" in r:
+ t = r["type"]
+ nested_file = None