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.
46 def get_list_of_nested_files(yml, dirpath):
48 return a list of all nested files
51 if not hasattr(yml, 'items'):
56 for k, v in yml.items():
57 if isinstance(v, dict) and "type" in v:
59 if t.endswith(".yml") or t.endswith(".yaml"):
60 filepath = path.join(dirpath, t)
61 with open(filepath) as fh:
63 nested_files.append(filepath)
64 nested_files.extend(get_list_of_nested_files(t_yml, dirpath))
65 elif t == "OS::Heat::ResourceGroup":
66 rdt = v["properties"]["resource_def"]["type"]
67 if rdt.endswith(".yml") or rdt.endswith(".yaml"):
68 filepath = path.join(dirpath, rdt)
69 with open(filepath) as fh:
70 rdt_yml = yaml.load(fh)
71 nested_files.append(filepath)
73 get_list_of_nested_files(rdt_yml, dirpath))
74 if isinstance(v, dict):
76 get_list_of_nested_files(v, dirpath))
77 elif isinstance(v, list):
80 get_list_of_nested_files(d, dirpath))
84 def check_for_invalid_nesting(yml, yaml_file, dirpath):
86 return a list of all nested files
89 if not hasattr(yml, 'items'):
93 p = re.compile('^[A-z]*::[A-z]*::[A-z]*$')
95 for k, v in yml.items():
96 if isinstance(v, dict) and "type" in v:
99 if t.endswith(".yml") or t.endswith(".yaml"):
100 filepath = path.join(dirpath, t)
102 with open(filepath) as fh:
103 t_yml = yaml.load(fh)
104 except Exception as e:
105 invalid_nesting.append(filepath)
107 invalid_nesting.extend(
108 check_for_invalid_nesting(t_yml,
111 elif t == "OS::Heat::ResourceGroup":
112 rd = v["properties"]["resource_def"]
113 if not isinstance(rd, dict):
114 invalid_nesting.append(yaml_file)
115 elif "type" not in rd:
116 invalid_nesting.append(yaml_file)
117 elif not p.match(rd["type"]) and not \
118 (rd["type"].endswith(".yml")
119 or rd["type"].endswith(".yaml")):
120 filepath = path.join(dirpath, rd["type"])
122 with open(filepath) as fh:
123 rdt_yml = yaml.load(fh)
124 except Exception as e:
125 invalid_nesting.append(filepath)
127 invalid_nesting.extend(
128 check_for_invalid_nesting(rdt_yml,
131 if isinstance(v, dict):
132 invalid_nesting.extend(
133 check_for_invalid_nesting(v,
136 elif isinstance(v, list):
138 invalid_nesting.extend(
139 check_for_invalid_nesting(d,
142 return invalid_nesting