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, csar_dir=None):
33 super(ToscaBlockStorage, self).__init__(nodetemplate,
34 type='OS::Cinder::Volume',
38 def handle_properties(self):
40 for prop in self.nodetemplate.get_properties_objects():
41 if isinstance(prop.value, GetInput):
42 tosca_props[prop.name] = {'get_param': prop.value.input_name}
44 if prop.name == "size":
45 size_value = (ScalarUnit_Size(prop.value).
46 get_num_from_scalar_unit('GiB'))
48 # OpenStack Heat expects size in GB
49 msg = _('Cinder Volume Size unit should be in GB.')
51 raise InvalidPropertyValueError(
53 elif int(size_value) < size_value:
54 size_value = int(size_value) + 1
55 log.warning(_("Cinder unit value should be in "
56 "multiples of GBs. so corrected "
57 " %(prop_val)s to %(size_value)s GB.")
58 % {'prop_val': prop.value,
59 'size_value': size_value})
60 tosca_props[prop.name] = int(size_value)
62 tosca_props[prop.name] = prop.value
63 self.properties = tosca_props
65 def get_hot_attribute(self, attribute, args):
67 # Convert from a TOSCA attribute for a nodetemplate to a HOT
68 # attribute for the matching resource. Unless there is additional
69 # runtime support, this should be a one to one mapping.
70 if attribute == 'volume_id':
71 attr['get_resource'] = self.name