]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Remove create_from* functor jump table
authorJoshua Harlow <harlowja@yahoo-inc.com>
Thu, 22 May 2014 02:26:15 +0000 (19:26 -0700)
committerJoshua Harlow <harlowja@yahoo-inc.com>
Thu, 22 May 2014 02:39:55 +0000 (19:39 -0700)
To make it easier to debug during runtime which method was
called remove the dictionary type -> functor mapping and
use a set of if statements instead. This makes it so that
if one of these methods fails we can gather more information
from the traceback than what was previously possible.

Change-Id: Iee2b0b90fe57734966d415c053bbc967335d5509

cinder/volume/flows/manager/create_volume.py

index f573ccbab6d2a9ecc9fcfa8cfe7c99fc9e0f6908..26fb55b69a0fb70486c9bd809b19dd54a3d63456 100644 (file)
@@ -323,14 +323,6 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
         super(CreateVolumeFromSpecTask, self).__init__(addons=[ACTION])
         self.db = db
         self.driver = driver
-        # This maps the different volume specification types into the methods
-        # that can create said volume type (aka this is a jump table).
-        self._create_func_mapping = {
-            'raw': self._create_raw_volume,
-            'snap': self._create_from_snapshot,
-            'source_vol': self._create_from_source_volume,
-            'image': self._create_from_image,
-        }
 
     def _handle_bootable_volume_glance_meta(self, context, volume_id,
                                             **kwargs):
@@ -566,6 +558,8 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
     def execute(self, context, volume_ref, volume_spec):
         volume_spec = dict(volume_spec)
         volume_id = volume_spec.pop('volume_id', None)
+        if not volume_id:
+            volume_id = volume_ref['id']
 
         # we can't do anything if the driver didn't init
         if not self.driver.initialized:
@@ -578,20 +572,27 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
             raise exception.DriverNotInitialized()
 
         create_type = volume_spec.pop('type', None)
-        create_functor = self._create_func_mapping.get(create_type)
-        if not create_functor:
-            raise exception.VolumeTypeNotFound(volume_type_id=create_type)
-
-        if not volume_id:
-            volume_id = volume_ref['id']
-        LOG.info(_("Volume %(volume_id)s: being created using %(functor)s "
+        LOG.info(_("Volume %(volume_id)s: being created as %(create_type)s "
                    "with specification: %(volume_spec)s") %
                  {'volume_spec': volume_spec, 'volume_id': volume_id,
-                  'functor': common.make_pretty_name(create_functor)})
-
-        # Call the given functor to make the volume.
-        model_update = create_functor(context, volume_ref=volume_ref,
-                                      **volume_spec)
+                  'create_type': create_type})
+        if create_type == 'raw':
+            model_update = self._create_raw_volume(context,
+                                                   volume_ref=volume_ref,
+                                                   **volume_spec)
+        elif create_type == 'snap':
+            model_update = self._create_from_snapshot(context,
+                                                      volume_ref=volume_ref,
+                                                      **volume_spec)
+        elif create_type == 'source_vol':
+            model_update = self._create_from_source_volume(
+                context, volume_ref=volume_ref, **volume_spec)
+        elif create_type == 'image':
+            model_update = self._create_from_image(context,
+                                                   volume_ref=volume_ref,
+                                                   **volume_spec)
+        else:
+            raise exception.VolumeTypeNotFound(volume_type_id=create_type)
 
         # Persist any model information provided on creation.
         try: