def create(self, name, volume, description=None, force=False): """ Adds exception handling to the default create() call. """ try: snap = super(CloudBlockStorageSnapshotManager, self).create(name=name, volume=volume, description=description, force=force) except exc.BadRequest as e: msg = str(e) if "Invalid volume: must be available" in msg: # The volume for the snapshot was attached. raise exc.VolumeNotAvailable( "Cannot create a snapshot from an " "attached volume. Detach the volume before trying " "again, or pass 'force=True' to the create_snapshot() " "call.") else: # Some other error raise except exc.ClientException as e: if e.code == 409: if "Request conflicts with in-progress" in str(e): txt = ("The volume is current creating a snapshot. You " "must wait until that completes before attempting " "to create an additional snapshot.") raise exc.VolumeNotAvailable(txt) else: raise else: raise return snap
def create_snapshot(self, name=None, description=None, force=False): """ Creates a snapshot of this volume, with an optional name and description. Normally snapshots will not happen if the volume is attached. To override this default behavior, pass force=True. """ name = name or "" description = description or "" # Note that passing in non-None values is required for the _create_body # method to distinguish between this and the request to create and # instance. try: snap = self._snapshot_manager.create(volume=self, name=name, description=description, force=force) except exc.BadRequest as e: msg = str(e) if "Invalid volume: must be available" in msg: # The volume for the snapshot was attached. raise exc.VolumeNotAvailable( "Cannot create a snapshot from an " "attached volume. Detach the volume before trying again, " "or pass 'force=True' to the create_snapshot() call.") else: # Some other error raise except exc.ClientException as e: if e.code == 409: if "Request conflicts with in-progress" in str(e): raise exc.VolumeNotAvailable( "The volume is current " "creating a snapshot. You must wait until that " "completes before attempting to create an " "additional snapshot.") else: raise else: raise return snap
def test_client_delete_volume_not_available(self): clt = self.client vol = self.volume vol.manager.delete = Mock(side_effect=exc.VolumeNotAvailable("")) self.assertRaises(exc.VolumeNotAvailable, clt.delete_volume, vol)