Refactor cinder volume v2 API changes

The handling of name and description for v2 API is duplicated for
handle_create and handle_update methods. This patch defines a new
method which handles the differences in parameter names thereby
avoiding duplication and keeping the changes restricted to this
method to the extent possible.

Change-Id: I891aeea3fe5d7cb22be27e05ab72857af668e4ed
This commit is contained in:
Unmesh Gurjar 2014-10-13 14:33:46 +05:30
parent b63b3f7500
commit e1b3d58066

View File

@ -104,6 +104,15 @@ class Volume(resource.Resource):
'metadata': tags
}
def _fetch_name_and_description(self, api_version, name=None,
description=None):
if api_version == 1:
return {'display_name': name or self._name(),
'display_description': description or self._description()}
else:
return {'name': name or self._name(),
'description': description or self._description()}
def handle_create(self):
backup_id = self.properties.get(self.BACKUP_ID)
cinder = self.cinder()
@ -111,21 +120,13 @@ class Volume(resource.Resource):
vol_id = cinder.restores.restore(backup_id).volume_id
vol = cinder.volumes.get(vol_id)
if cinder.volume_api_version == 1:
kwargs = {'display_name': self._name(),
'display_description': self._description()}
else:
kwargs = {'name': self._name(),
'description': self._description()}
kwargs = self._fetch_name_and_description(
cinder.volume_api_version)
vol.update(**kwargs)
else:
kwargs = self._create_arguments()
if cinder.volume_api_version == 1:
kwargs['display_name'] = self._name()
kwargs['display_description'] = self._description()
else:
kwargs['name'] = self._name()
kwargs['description'] = self._description()
kwargs.update(self._fetch_name_and_description(
cinder.volume_api_version))
vol = cinder.volumes.create(**kwargs)
self.resource_id_set(vol.id)
@ -628,32 +629,28 @@ class CinderVolume(Volume):
def handle_update(self, json_snippet, tmpl_diff, prop_diff):
vol = None
checkers = []
cinder = self.cinder()
# update the name and description for cinder volume
if self.NAME in prop_diff or self.DESCRIPTION in prop_diff:
vol = self.cinder().volumes.get(self.resource_id)
kwargs = {}
vol = cinder.volumes.get(self.resource_id)
update_name = (prop_diff.get(self.NAME) or
self.properties.get(self.NAME))
update_description = (prop_diff.get(self.DESCRIPTION) or
self.properties.get(self.DESCRIPTION))
if self.cinder().volume_api_version == 1:
kwargs['display_name'] = update_name
kwargs['display_description'] = update_description
else:
kwargs['name'] = update_name
kwargs['description'] = update_description
self.cinder().volumes.update(vol, **kwargs)
kwargs = self._fetch_name_and_description(
cinder.volume_api_version, update_name, update_description)
cinder.volumes.update(vol, **kwargs)
# update the metadata for cinder volume
if self.METADATA in prop_diff:
if not vol:
vol = self.cinder().volumes.get(self.resource_id)
vol = cinder.volumes.get(self.resource_id)
metadata = prop_diff.get(self.METADATA)
self.cinder().volumes.update_all_metadata(vol, metadata)
cinder.volumes.update_all_metadata(vol, metadata)
# extend volume size
if self.SIZE in prop_diff:
if not vol:
vol = self.cinder().volumes.get(self.resource_id)
vol = cinder.volumes.get(self.resource_id)
new_size = prop_diff[self.SIZE]
if new_size < vol.size: