Correct response when trying to delete a volume that is attached to an EC2 instance. Created a VolumeInUse error and did a simple check on the delete_volume method.

This commit is contained in:
mickeypash 2017-10-23 19:12:49 +01:00
parent 584352aaf6
commit 4a402b01cf
2 changed files with 14 additions and 0 deletions

View File

@ -244,6 +244,15 @@ class InvalidVolumeAttachmentError(EC2ClientError):
.format(volume_id, instance_id))
class VolumeInUseError(EC2ClientError):
def __init__(self, volume_id, instance_id):
super(VolumeInUseError, self).__init__(
"VolumeInUse",
"Volume {0} is currently attached to {1}"
.format(volume_id, instance_id))
class InvalidDomainError(EC2ClientError):
def __init__(self, domain):

View File

@ -45,6 +45,7 @@ from .exceptions import (
InvalidAMIAttributeItemValueError,
InvalidSnapshotIdError,
InvalidVolumeIdError,
VolumeInUseError,
InvalidVolumeAttachmentError,
InvalidDomainError,
InvalidAddressError,
@ -1813,6 +1814,10 @@ class EBSBackend(object):
def delete_volume(self, volume_id):
if volume_id in self.volumes:
volume = self.volumes[volume_id]
instance_id = volume.attachment.instance.id
if volume.attachment is not None:
raise VolumeInUseError(volume_id, instance_id)
return self.volumes.pop(volume_id)
raise InvalidVolumeIdError(volume_id)