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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
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
21 log = logging.getLogger('heat-translator')
23 # Name used to dynamically load appropriate map class.
24 TARGET_CLASS_NAME = 'ToscaBlockStorage'
27 class ToscaBlockStorage(HotResource):
28 '''Translate TOSCA node type tosca.nodes.BlockStorage.'''
30 toscatype = 'tosca.nodes.BlockStorage'
32 def __init__(self, nodetemplate):
33 super(ToscaBlockStorage, self).__init__(nodetemplate,
34 type='OS::Cinder::Volume')
37 def handle_properties(self):
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}
43 if prop.name == "size":
44 size_value = (ScalarUnit_Size(prop.value).
45 get_num_from_scalar_unit('GiB'))
47 # OpenStack Heat expects size in GB
48 msg = _('Cinder Volume Size unit should be in GB.')
50 raise InvalidPropertyValueError(
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)
61 tosca_props[prop.name] = prop.value
62 self.properties = tosca_props
64 def get_hot_attribute(self, attribute, args):
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