Skip to content
Permalink
Browse files
Modify how errors are handled, clean volumes a little
  • Loading branch information
brogand93 committed Feb 21, 2014
1 parent 85108b1 commit 0e2bc8e11c7de02ca8f302cc432882a1bb1ee6be
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 104 deletions.
@@ -10,19 +10,29 @@ def describe_item_by_id(item_id, request_function):
'id': item_id
}

return request_function(args)
response = request_function(args)

if 'errortext' in response:
raise Ec2stackError(
'400',
'InvalidItemId.Malformed',
'The specified Item ID is not valid'
)

return response


def get_items_from_response(response, item_type,
cloudstack_item_attributes_to_aws):
items = []

for cloudstack_item in response[item_type]:
items.append(
translator.cloudstack_item_to_aws(
cloudstack_item,
cloudstack_item_attributes_to_aws
if response:
for cloudstack_item in response[item_type]:
items.append(
translator.cloudstack_item_to_aws(
cloudstack_item,
cloudstack_item_attributes_to_aws
)
)
)

return items
@@ -28,9 +28,6 @@ def describe_image_attribute():

response = describe_item_by_id(image_id, _describe_templates_request)

if 'errortext' in response:
invalid_image_id()

template = response['template'][0]

image_attribute = translator.cloudstack_item_attribute_to_aws(
@@ -55,9 +52,6 @@ def _describe_specific_images():

for image_id in image_ids:
response = describe_item_by_id(image_id, _describe_templates_request)
if 'errortext' in response:
invalid_image_id()

images = images + get_items_from_response(
response, 'template', cloudstack_image_attributes_to_aws)

@@ -35,9 +35,6 @@ def describe_instance_attribute():
instance_id,
_describe_virtual_machines_request)

if 'errortext' in response:
invalid_instance_id()

virtual_machine = response['virtualmachine'][0]

instance_attribute = translator.cloudstack_item_attribute_to_aws(
@@ -86,14 +83,6 @@ def _describe_specific_instances():
return instances


def invalid_instance_id():
raise Ec2stackError(
'400',
'InvalidInstanceId.Malformed',
'The specified Instance ID is not valid'
)


def _create_describe_instance_attribute_response(item_attribute):
response = {
'template_name_or_list': 'instance_attribute.xml',
@@ -14,11 +14,6 @@
def cloudstack_item_to_aws(cloudstack_item, cloudstack_item_attributes_to_aws):
item = {}

# Add general cloudstack attributes map to cloudstack attributes map
# specific to item (passed in as arg)
# If there is a duplicate the second dictionary takes preference and
# ovverrides the first dictionaries value

attributes_to_aws_mapping = dict(cloudstack_attributes_to_aws.items() +
cloudstack_item_attributes_to_aws.items())

@@ -7,7 +7,8 @@

from ec2stack import helpers
from ec2stack.helpers import authentication_required
from ec2stack.providers.cloudstack import requester, translator, disk_offerings
from ec2stack.providers.cloudstack import requester, translator, \
disk_offerings, cloudstack_helpers


cloudstack_volume_attributes_to_aws = {
@@ -20,36 +21,43 @@
@authentication_required
def describe_volumes():
response = _describe_all_volumes()
return _describe_volumes_response(response)
return describe_volumes_response(response)


def _describe_all_volumes():
response = _describe_volumes_request()
return _get_volumes_from_response(response)
@authentication_required
def delete_volume():
helpers.require_parameters(['VolumeId'])
response = _delete_volume_request()
return delete_volume_response(response)


def _get_volumes_from_response(response, attribute=None):
volumes = []
if response:
for volume in response['volume']:
volumes.append(
translator.cloudstack_item_to_aws(
volume,
cloudstack_volume_attributes_to_aws
)
)
@authentication_required
def create_volume():
helpers.require_one_paramater(['SnapshotId', 'Size'])

return volumes
if helpers.contains_parameter('SnapshotId'):
args['snapshotid'] = helpers.get('SnapshotId', request.form)
else:
args['size'] = helpers.get('Size', request.form)
args['diskofferingid'] = \
disk_offerings.get_disk_offerings_id_by_name('Custom')

response = _create_volume_request()
return create_volume_response(response)


def _describe_volumes_response(volumes):
response = {
'template_name_or_list': 'volumes.xml',
'response_type': 'DescribeVolumesResponse',
'volumes': volumes,
}
@authentication_required
def attach_volume():
helpers.require_parameters(['VolumeId', 'InstanceId'])
response = _attach_volume_request()
return attach_volume_response(response)

return response

def _describe_all_volumes():
response = _describe_volumes_request()
volumes = cloudstack_helpers.get_items_from_response(
response, 'volume', cloudstack_volume_attributes_to_aws)
return volumes


def _describe_volumes_request():
@@ -62,27 +70,8 @@ def _describe_volumes_request():
return response


@authentication_required
def create_volume():
helpers.require_one_paramater(['SnapshotId', 'Size'])
response = _create_volume_request()
return _create_volume_response(response)


def _create_volume_request():
args = {}
args['command'] = 'listZones'
response = requester.make_request(args)

args = {}

if helpers.contains_parameter('SnapshotId'):
args['snapshotid'] = helpers.get('SnapshotId', request.form)
else:
args['size'] = helpers.get('Size', request.form)
args['diskofferingid'] = \
disk_offerings.get_disk_offerings_id_by_name('Custom')

args['zoneid'] = helpers.get('AvailabilityZone', request.form)
args['command'] = 'createVolume'
args['name'] = uuid.uuid4()
@@ -94,27 +83,6 @@ def _create_volume_request():
return response


def _create_volume_response(response):
response = {
'template_name_or_list': 'create_volume.xml',
'response_type': 'CreateVolumeResponse',
'id': response['id'],
'status': 'creating',
'volumeType': 'standard',
'zone': helpers.get('AvailabilityZone', request.form),
'size': helpers.get('Size', request.form)
}

return response


@authentication_required
def attach_volume():
helpers.require_parameters(['VolumeId', 'InstanceId'])
response = _attach_volume_request()
return _attach_volume_response(response)


def _attach_volume_request():
args = {}
args['command'] = 'attachVolume'
@@ -127,7 +95,17 @@ def _attach_volume_request():
return response


def _attach_volume_response(response):
def _delete_volume_request():
args = {}
args['command'] = 'deleteVolume'
args['id'] = helpers.get('VolumeId', request.form)

response = requester.make_request(args)

return response


def attach_volume_response(response):
return {
'template_name_or_list': 'attach_volume.xml',
'response_type': 'AttachVolumeResponse',
@@ -139,24 +117,31 @@ def _attach_volume_response(response):
}


@authentication_required
def delete_volume():
helpers.require_parameters(['VolumeId'])
response = _delete_volume_request()
return _delete_volume_response(response)
def describe_volumes_response(volumes):
response = {
'template_name_or_list': 'volumes.xml',
'response_type': 'DescribeVolumesResponse',
'volumes': volumes,
}

return response

def _delete_volume_request():
args = {}
args['command'] = 'deleteVolume'
args['id'] = helpers.get('VolumeId', request.form)

response = requester.make_request(args)
def create_volume_response(response):
response = {
'template_name_or_list': 'create_volume.xml',
'response_type': 'CreateVolumeResponse',
'id': response['id'],
'status': 'creating',
'volumeType': 'standard',
'zone': helpers.get('AvailabilityZone', request.form),
'size': helpers.get('Size', request.form)
}

return response


def _delete_volume_response(response):
def delete_volume_response(response):
return {
'template_name_or_list': 'delete_item.xml',
'response_type': 'DeleteVolumeResponse',

0 comments on commit 0e2bc8e

Please sign in to comment.