2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
28 # https://creativecommons.org/licenses/by/4.0/
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
36 # ============LICENSE_END============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
51 def get_list_of_nested_files(yml, dirpath):
53 return a list of all nested files
56 if not hasattr(yml, "items"):
61 for v in yml.values():
62 if isinstance(v, dict) and "type" in v:
64 if t.endswith(".yml") or t.endswith(".yaml"):
65 filepath = path.join(dirpath, t)
66 with open(filepath) as fh:
68 nested_files.append(filepath)
69 nested_files.extend(get_list_of_nested_files(t_yml, dirpath))
70 elif t == "OS::Heat::ResourceGroup":
71 rdt = v.get("properties", {}).get("resource_def", {}).get("type", None)
72 if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
73 filepath = path.join(dirpath, rdt)
74 with open(filepath) as fh:
75 rdt_yml = yaml.load(fh)
76 nested_files.append(filepath)
77 nested_files.extend(get_list_of_nested_files(rdt_yml, dirpath))
78 if isinstance(v, dict):
79 nested_files.extend(get_list_of_nested_files(v, dirpath))
80 elif isinstance(v, list):
82 nested_files.extend(get_list_of_nested_files(d, dirpath))
86 def check_for_invalid_nesting(yml, yaml_file, dirpath):
88 return a list of all nested files
90 if not hasattr(yml, "items"):
93 p = re.compile("^[A-z]*::[A-z]*::[A-z]*$")
95 for v in yml.values():
96 if isinstance(v, dict) and "type" in v:
98 if t.endswith(".yml") or t.endswith(".yaml"):
99 filepath = path.join(dirpath, t)
100 elif t == "OS::Heat::ResourceGroup":
101 rd = v["properties"]["resource_def"]
102 if not isinstance(rd, dict) or "type" not in rd:
103 invalid_nesting.append(yaml_file)
105 elif not p.match(rd["type"]) and not (
106 rd["type"].endswith(".yml") or rd["type"].endswith(".yaml")
108 filepath = path.join(dirpath, rd["type"])
114 with open(filepath) as fh:
116 except yaml.YAMLError as e:
117 invalid_nesting.append(filepath)
118 print(e) # pylint: disable=superfluous-parens
119 invalid_nesting.extend(check_for_invalid_nesting(yml, filepath, dirpath))
120 if isinstance(v, dict):
121 invalid_nesting.extend(check_for_invalid_nesting(v, yaml_file, dirpath))
122 elif isinstance(v, list):
124 invalid_nesting.extend(check_for_invalid_nesting(d, yaml_file, dirpath))
125 return invalid_nesting