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============================================
41 def parse_nested_dict(d, key=""):
43 parse the nested dictionary and return values of
44 given key of function parameter only
47 for k, v in d.items():
48 if isinstance(v, dict):
49 sub_dict = parse_nested_dict(v, key)
50 nested_elements.extend(sub_dict)
54 nested_elements.append(v)
56 nested_elements.append(v)
58 return nested_elements
61 def find_all_get_param_in_yml(yml):
63 Recursively find all referenced parameters in a parsed yaml body
64 and return a list of parameters
66 os_pseudo_parameters = ["OS::stack_name", "OS::stack_id", "OS::project_id"]
68 if not hasattr(yml, "items"):
71 for k, v in yml.items():
72 if k == "get_param" and v not in os_pseudo_parameters:
73 if isinstance(v, list) and not isinstance(v[0], dict):
75 elif not isinstance(v, dict) and isinstance(v, str):
77 for item in v if isinstance(v, list) else [v]:
78 if isinstance(item, dict):
79 params.extend(find_all_get_param_in_yml(item))
81 elif k == "list_join":
82 for item in v if isinstance(v, list) else [v]:
83 if isinstance(item, list):
85 params.extend(find_all_get_param_in_yml(d))
87 if isinstance(v, dict):
88 params.extend(find_all_get_param_in_yml(v))
89 elif isinstance(v, list):
91 params.extend(find_all_get_param_in_yml(d))
95 def find_all_get_resource_in_yml(yml):
97 Recursively find all referenced resources
98 in a parsed yaml body and return a list of resource ids
100 if not hasattr(yml, "items"):
103 for k, v in yml.items():
104 if k == "get_resource":
105 if isinstance(v, list):
106 resources.append(v[0])
110 if isinstance(v, dict):
111 resources.extend(find_all_get_resource_in_yml(v))
112 elif isinstance(v, list):
114 resources.extend(find_all_get_resource_in_yml(d))
118 def find_all_get_file_in_yml(yml):
120 Recursively find all get_file in a parsed yaml body
121 and return the list of referenced files/urls
123 if not hasattr(yml, "items"):
126 for k, v in yml.items():
128 if isinstance(v, list):
129 resources.append(v[0])
133 if isinstance(v, dict):
134 resources.extend(find_all_get_file_in_yml(v))
135 elif isinstance(v, list):
137 resources.extend(find_all_get_file_in_yml(d))
141 def find_all_get_resource_in_resource(resource):
143 Recursively find all referenced resources
144 in a heat resource and return a list of resource ids
146 if not hasattr(resource, "items"):
150 for k, v in resource.items():
151 if k == "get_resource":
152 if isinstance(v, list):
153 resources.append(v[0])
157 if isinstance(v, dict):
158 resources.extend(find_all_get_resource_in_resource(v))
159 elif isinstance(v, list):
161 resources.extend(find_all_get_resource_in_resource(d))
165 def get_associated_resources_per_resource(resources):
167 Recursively find all referenced resources for each resource
168 in a list of resource ids
170 if not hasattr(resources, "items"):
174 resources_dict["resources"] = {}
177 for res_key, res_value in resources.items():
180 for k, v in res_value:
181 if k == "get_resource" and isinstance(v, dict):
182 get_resources = find_all_get_resource_in_resource(v)
184 # if resources found, add to dict
186 ref_resources.extend(get_resources)
187 resources_dict["resources"][res_key] = {
188 "res_value": res_value,
189 "get_resources": get_resources,
192 resources_dict["ref_resources"] = set(ref_resources)
194 return resources_dict
199 flatten items from any nested iterable
204 if isinstance(item, list):
205 sub_list = flatten(item)
206 merged_list.extend(sub_list)
208 merged_list.append(item)