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â€
\9d);
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", {})
72 .get("resource_def", {})
74 if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
75 filepath = path.join(dirpath, rdt)
76 with open(filepath) as fh:
77 rdt_yml = yaml.load(fh)
78 nested_files.append(filepath)
80 get_list_of_nested_files(rdt_yml, dirpath))
81 if isinstance(v, dict):
83 get_list_of_nested_files(v, dirpath))
84 elif isinstance(v, list):
87 get_list_of_nested_files(d, dirpath))
91 def check_for_invalid_nesting(yml, yaml_file, dirpath):
93 return a list of all nested files
95 if not hasattr(yml, 'items'):
98 p = re.compile('^[A-z]*::[A-z]*::[A-z]*$')
100 for v in yml.values():
101 if isinstance(v, dict) and "type" in v:
103 if t.endswith(".yml") or t.endswith(".yaml"):
104 filepath = path.join(dirpath, t)
105 elif t == "OS::Heat::ResourceGroup":
106 rd = v["properties"]["resource_def"]
107 if not isinstance(rd, dict) or "type" not in rd:
108 invalid_nesting.append(yaml_file)
110 elif not p.match(rd["type"]) and not (
111 rd["type"].endswith(".yml")
112 or rd["type"].endswith(".yaml")):
113 filepath = path.join(dirpath, rd["type"])
119 with open(filepath) as fh:
121 except yaml.YAMLError as e:
122 invalid_nesting.append(filepath)
123 print(e) # pylint: disable=superfluous-parens
124 invalid_nesting.extend(check_for_invalid_nesting(
128 if isinstance(v, dict):
129 invalid_nesting.extend(check_for_invalid_nesting(
133 elif isinstance(v, list):
135 invalid_nesting.extend(check_for_invalid_nesting(
139 return invalid_nesting