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 is_pseudo_param(parameter):
42 pseudo_parameters = ["OS::stack_name", "OS::stack_id", "OS::project_id"]
43 return parameter in pseudo_parameters
46 def parse_nested_dict(d, key=""):
48 parse the nested dictionary and return values of
49 given key of function parameter only
52 for k, v in d.items():
53 if isinstance(v, dict):
54 sub_dict = parse_nested_dict(v, key)
55 nested_elements.extend(sub_dict)
59 nested_elements.append(v)
61 nested_elements.append(v)
63 return nested_elements
66 def find_all_get_param_in_yml(yml):
68 Recursively find all referenced parameters in a parsed yaml body
69 and return a list of parameters
72 if not hasattr(yml, "items"):
75 for k, v in yml.items():
76 if k == "get_param" and not is_pseudo_param(v):
77 if isinstance(v, list) and not isinstance(v[0], dict):
79 elif not isinstance(v, dict) and isinstance(v, str):
81 for item in v if isinstance(v, list) else [v]:
82 if isinstance(item, dict):
83 params.extend(find_all_get_param_in_yml(item))
85 elif k == "list_join":
86 for item in v if isinstance(v, list) else [v]:
87 if isinstance(item, list):
89 params.extend(find_all_get_param_in_yml(d))
91 if isinstance(v, dict):
92 params.extend(find_all_get_param_in_yml(v))
93 elif isinstance(v, list):
95 params.extend(find_all_get_param_in_yml(d))
99 def find_all_get_resource_in_yml(yml):
101 Recursively find all referenced resources
102 in a parsed yaml body and return a list of resource ids
104 if not hasattr(yml, "items"):
107 for k, v in yml.items():
108 if k == "get_resource":
109 if isinstance(v, list):
110 resources.append(v[0])
114 if isinstance(v, dict):
115 resources.extend(find_all_get_resource_in_yml(v))
116 elif isinstance(v, list):
118 resources.extend(find_all_get_resource_in_yml(d))
122 def find_all_get_file_in_yml(yml):
124 Recursively find all get_file in a parsed yaml body
125 and return the list of referenced files/urls
127 if not hasattr(yml, "items"):
130 for k, v in yml.items():
132 if isinstance(v, list):
133 resources.append(v[0])
137 if isinstance(v, dict):
138 resources.extend(find_all_get_file_in_yml(v))
139 elif isinstance(v, list):
141 resources.extend(find_all_get_file_in_yml(d))
145 def find_all_get_resource_in_resource(resource):
147 Recursively find all referenced resources
148 in a heat resource and return a list of resource ids
150 if not hasattr(resource, "items"):
154 for k, v in resource.items():
155 if k == "get_resource":
156 if isinstance(v, list):
157 resources.append(v[0])
161 if isinstance(v, dict):
162 resources.extend(find_all_get_resource_in_resource(v))
163 elif isinstance(v, list):
165 resources.extend(find_all_get_resource_in_resource(d))
169 def get_associated_resources_per_resource(resources):
171 Recursively find all referenced resources for each resource
172 in a list of resource ids
174 if not hasattr(resources, "items"):
178 resources_dict["resources"] = {}
181 for res_key, res_value in resources.items():
184 for k, v in res_value:
185 if k == "get_resource" and isinstance(v, dict):
186 get_resources = find_all_get_resource_in_resource(v)
188 # if resources found, add to dict
190 ref_resources.extend(get_resources)
191 resources_dict["resources"][res_key] = {
192 "res_value": res_value,
193 "get_resources": get_resources,
196 resources_dict["ref_resources"] = set(ref_resources)
198 return resources_dict
203 flatten items from any nested iterable
208 if isinstance(item, list):
209 sub_list = flatten(item)
210 merged_list.extend(sub_list)
212 merged_list.append(item)