Fix KeyError exception when stack snapshot deletion

There is no 'backup_id' data in snapshot if the cinder
volume snapshot failed. This patch will use 'get'
instead __getitem__ to get the 'backup_id' data to avoid
KeyError exception raise.

Change-Id: Iae7e22ccdaf8f48a85d90afac3a38011f14cbd34
Closes-Bug: #1465173
This commit is contained in:
huangtianhua 2015-06-15 16:38:25 +08:00
parent 7d9b5a265f
commit f500132879
2 changed files with 17 additions and 1 deletions

View File

@ -394,7 +394,9 @@ class CinderVolume(vb.BaseVolume):
raise exception.Error(backup.fail_reason)
def handle_delete_snapshot(self, snapshot):
backup_id = snapshot['resource_data']['backup_id']
backup_id = snapshot['resource_data'].get('backup_id')
if not backup_id:
return
try:
self.client().backups.delete(backup_id)
except Exception as ex:

View File

@ -22,6 +22,7 @@ from heat.common import exception
from heat.common import template_format
from heat.engine.clients.os import cinder
from heat.engine.clients.os import glance
from heat.engine.resources.openstack.cinder import volume as c_vol
from heat.engine import rsrc_defn
from heat.engine import scheduler
from heat.objects import resource_data as resource_data_object
@ -1044,3 +1045,16 @@ class CinderVolumeTest(vt_base.BaseVolumeTest):
self.assertEqual((stack.RESTORE, stack.COMPLETE), stack.state)
self.m.VerifyAll()
def test_handle_delete_snapshot_no_backup(self):
stack_name = 'test_handle_delete_snapshot_no_backup'
mock_vs = {
'resource_data': {}
}
t = template_format.parse(single_cinder_volume_template)
stack = utils.parse_stack(t, stack_name=stack_name)
rsrc = c_vol.CinderVolume(
'volume',
stack.t.resource_definitions(stack)['volume'],
stack)
self.assertIsNone(rsrc.handle_delete_snapshot(mock_vs))