Merge "Replace test file from test_tosca_nfv_sample with vRNC"
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tosca_block_storage.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 import logging
15 from toscaparser.common.exception import InvalidPropertyValueError
16 from toscaparser.elements.scalarunit import ScalarUnit_Size
17 from toscaparser.functions import GetInput
18 from toscaparser.utils.gettextutils import _
19 from translator.hot.syntax.hot_resource import HotResource
20
21 log = logging.getLogger('heat-translator')
22
23 # Name used to dynamically load appropriate map class.
24 TARGET_CLASS_NAME = 'ToscaBlockStorage'
25
26
27 class ToscaBlockStorage(HotResource):
28     '''Translate TOSCA node type tosca.nodes.BlockStorage.'''
29
30     toscatype = 'tosca.nodes.BlockStorage'
31
32     def __init__(self, nodetemplate):
33         super(ToscaBlockStorage, self).__init__(nodetemplate,
34                                                 type='OS::Cinder::Volume')
35         pass
36
37     def handle_properties(self):
38         tosca_props = {}
39         for prop in self.nodetemplate.get_properties_objects():
40             if isinstance(prop.value, GetInput):
41                 tosca_props[prop.name] = {'get_param': prop.value.input_name}
42             else:
43                 if prop.name == "size":
44                     size_value = (ScalarUnit_Size(prop.value).
45                                   get_num_from_scalar_unit('GiB'))
46                     if size_value == 0:
47                         # OpenStack Heat expects size in GB
48                         msg = _('Cinder Volume Size unit should be in GB.')
49                         log.error(msg)
50                         raise InvalidPropertyValueError(
51                             what=msg)
52                     elif int(size_value) < size_value:
53                         size_value = int(size_value) + 1
54                         log.warning(_("Cinder unit value should be in "
55                                       "multiples of GBs. so corrected "
56                                       " %(prop_val)s to %(size_value)s GB.")
57                                     % {'prop_val': prop.value,
58                                        'size_value': size_value})
59                     tosca_props[prop.name] = int(size_value)
60                 else:
61                     tosca_props[prop.name] = prop.value
62         self.properties = tosca_props
63
64     def get_hot_attribute(self, attribute, args):
65         attr = {}
66         # Convert from a TOSCA attribute for a nodetemplate to a HOT
67         # attribute for the matching resource.  Unless there is additional
68         # runtime support, this should be a one to one mapping.
69         if attribute == 'volume_id':
70             attr['get_resource'] = self.name
71         return attr