[VVP] Flag duplicate parameters in .env files
[vvp/validation-scripts.git] / ice_validator / tests / utils / vm_types.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
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
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 #
22 #
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
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
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.
35 #
36 # ============LICENSE_END============================================
37 #
38 #
39
40 import re
41 from tests import cached_yaml as yaml
42
43
44 def get_vm_types_for_resource(resource):
45     """
46     Get all unique vm_types for a resource
47     Notes:
48     - Returns set([]) if the resource is not formatted
49     properly, the passed resource is not a nova server
50     - If more than one vm_type is detected all vm_types will
51     be returned
52     """
53     if not is_nova_server(resource):
54         return set()
55
56     key_values = ["name", "flavor", "image"]
57     key_value_formats = [
58         ["name", "string", re.compile(r"(.+?)_name_\d+")],
59         ["name", "comma_delimited_list", re.compile(r"(.+?)_names")],
60         ["flavor", "string", re.compile(r"(.+?)_flavor_name")],
61         ["image", "string", re.compile(r"(.+?)_image_name")],
62     ]
63
64     vm_types = []
65     for k2, v2 in resource["properties"].items():
66         if not isinstance(v2, dict) or any(
67             [k2 not in key_values, "get_param" not in v2]
68         ):
69             continue
70         formats = [v for v in key_value_formats if v[0] == k2]
71         for v3 in formats:
72             param = v2["get_param"]
73             if isinstance(param, list):
74                 param = param[0]
75             m = v3[2].match(param)
76             if m and m.group(1):
77                 vm_types.append(m.group(1))
78
79     return set(vm_types)
80
81
82 def is_nova_server(resource):
83
84     return (
85         isinstance(resource, dict)
86         and "type" in resource
87         and "properties" in resource
88         and resource.get("type") == "OS::Nova::Server"
89     )
90
91
92 def get_vm_type_for_nova_server(resource):
93     """
94     Get the vm_type for a resource
95     Note: Returns None if not exactly one vm_type
96     is detected, if the resource is not formatted properly, or
97     the passed resource is not a nova server
98     """
99     vm_types = get_vm_types_for_resource(resource)
100
101     # if more than one vm_type was identified, return None
102     if not vm_types or len(vm_types) > 1:
103         return None
104
105     return vm_types.pop()
106
107
108 def get_vm_types(resources):
109     """
110     Get all vm_types for a list of heat resources, do note that
111     some of the values retrieved may be invalid
112     """
113     vm_types = []
114     for v in resources.values():
115         vm_types.extend(list(get_vm_types_for_resource(v)))
116
117     return set(vm_types)
118
119
120 def get_all_vm_types(yaml_files):
121     """
122     Get all vm_types for a list of yaml files
123     """
124     vm_types = []
125     for yaml_file in yaml_files:
126         with open(yaml_file, "r") as f:
127             yml = yaml.load(f)
128
129         if "resources" not in yml:
130             continue
131
132         vm_types.extend(get_vm_types(yml["resources"]))
133
134     return set(vm_types)