8d6622011c0d8694cbb8a3365eb98ddb04f4d6ac
[vvp/validation-scripts.git] / ice_validator / tests / structures.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 '''structures
42 '''
43
44 import os
45
46 from tests import cached_yaml as yaml
47
48 from .utils import nested_dict
49
50 VERSION = '1.4.0'
51
52
53 class Heat(object):
54     """A Heat template.
55     filepath - absolute path to template file.
56     envpath - absolute path to environmnt file.
57     """
58     def __init__(self, filepath=None, envpath=None):
59         self.filepath = None
60         self.basename = None
61         self.dirname = None
62         self.yml = None
63         self.heat_template_version = None
64         self.description = None
65         self.parameter_groups = None
66         self.parameters = None
67         self.resources = None
68         self.outputs = None
69         self.conditions = None
70         if filepath:
71             self.load(filepath)
72         self.env = None
73         if envpath:
74             self.load_env(envpath)
75
76     def load(self, filepath):
77         """Load the Heat template given a filepath.
78         """
79         self.filepath = filepath
80         self.basename = os.path.basename(self.filepath)
81         self.dirname = os.path.dirname(self.filepath)
82         with open(self.filepath) as fi:
83             self.yml = yaml.load(fi)
84         self.heat_template_version = self.yml.get('heat_template_version', None)
85         self.description = self.yml.get('description', '')
86         self.parameter_groups = self.yml.get('parameter_groups', {})
87         self.parameters = self.yml.get('parameters', {})
88         self.resources = self.yml.get('resources', {})
89         self.outputs = self.yml.get('outputs', {})
90         self.conditions = self.yml.get('conditions', {})
91
92     def load_env(self, envpath):
93         """Load the Environment template given a envpath.
94         """
95         self.env = Env(filepath=envpath)
96
97     @staticmethod
98     def nested_get(dic, *keys):
99         """make utils.nested_dict.get available as a class method.
100         """
101         return nested_dict.get(dic, *keys)
102
103
104 class Env(Heat):
105     """An Environment file
106     """
107     pass
108
109
110 class Resource(object):
111     """A Resource
112     """
113     def __init__(self, resource_id=None, resource=None):
114         self.resource_id = resource_id or ''
115         self.resource = resource or {}
116
117     @staticmethod
118     def get_index_var(resource):
119         """Return the index_var for this resource.
120         """
121         index_var = nested_dict.get(resource,
122                                     'properties',
123                                     'index_var') or 'index'
124         return index_var
125