Skip to content
Permalink
Browse files
Restructure and improve how responses are generated
  • Loading branch information
brogand93 committed Feb 24, 2014
1 parent 01a44ee commit d86dd2e21eb8fbf9bc17c5f48c33cc71bef5b40e
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 324 deletions.
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# encoding: utf-8

from ec2stack.core import Ec2stackError


def invalid_snapshot_id():
raise Ec2stackError(
'400',
'InvalidSnapshot.NotFound',
'The specified Snapshot Id does not exist.'
)


def invalid_zone_id():
raise Ec2stackError(
'400',
'InvalidZone.NotFound',
'The specified Availability Zone Id does not exist.'
)


def invalid_volume_id():
raise Ec2stackError(
'400',
'InvalidVolume.NotFound',
'The specified Volume Id does not exist.'
)
@@ -57,6 +57,12 @@ def require_one_paramater(parameters):
missing_paramater(parameter)


def error_to_aws(response, error_map):
for errortext, error_function in error_map.iteritems():
if errortext in response['errortext']:
error_function()


def missing_paramater(parameter):
raise Ec2stackError(
'400',

This file was deleted.

@@ -1,60 +1,47 @@
#!/usr/bin/env python
# encoding: utf-8

from ec2stack.providers.cloudstack import requester
from ec2stack.providers.cloudstack.cloudstack_helpers import *

from flask import request

cloudstack_image_attributes_to_aws = {
'isready': 'state'
}
from ec2stack import helpers
from ec2stack.providers.cloudstack import requester


@authentication_required
@helpers.authentication_required
def describe_images():
if contains_parameter('ImageId.1'):
if helpers.contains_parameter('ImageId.1'):
images = _describe_specific_images()
else:
images = _describe_all_images()

return _create_describe_images_response(images)
return _describe_images_response(images)


@authentication_required
def describe_image_attribute():
image_id = get('ImageId', request.form)
attribute = get('Attribute', request.form)
def _describe_all_images():
response = _describe_templates_request()
return response

response = describe_item_by_id(image_id, _describe_templates_request)

template = response['template'][0]
def _describe_specific_images():
image_ids = helpers.get_request_paramaters('ImageId')

image_attribute = translator.cloudstack_item_attribute_to_aws(
template, cloudstack_image_attributes_to_aws, attribute)
response = {}
response['template'] = []

response = _create_describe_image_attribute_response(image_attribute)
for image_id in image_ids:
image_response = describe_image_by_id(image_id)
response['template'].append(image_response)

return response


def _describe_all_images():
response = _describe_templates_request()
images = get_items_from_response(
response, 'template', cloudstack_image_attributes_to_aws)

return images
def describe_image_by_id(image_id):
args = {}
args['id'] = image_id
response = _describe_templates_request(args)
response = response['template'][0]


def _describe_specific_images():
image_ids = get_request_paramaters('ImageId')
images = []

for image_id in image_ids:
response = describe_item_by_id(image_id, _describe_templates_request)
images = images + get_items_from_response(
response, 'template', cloudstack_image_attributes_to_aws)

return images
return response


def _describe_templates_request(args=None):
@@ -67,29 +54,35 @@ def _describe_templates_request(args=None):
args['command'] = 'listTemplates'

cloudstack_response = requester.make_request(args)

cloudstack_response = cloudstack_response['listtemplatesresponse']

return cloudstack_response


def _create_describe_image_attribute_response(item_attribute):
response = {
'template_name_or_list': 'image_attribute.xml',
'response_type': 'DescribeImageAttributeResponse',
'attribute': get('Attribute', request.form),
'value': item_attribute.values()[0],
'id': get('ImageId', request.form)
def _describe_images_response(response):
return {
'template_name_or_list': 'images.xml',
'response_type': 'DescribeImagesResponse',
'response': response
}

return response

@helpers.authentication_required
def describe_image_attribute():
image_id = helpers.get('ImageId', request.form)

def _create_describe_images_response(images):
response = {
'template_name_or_list': 'images.xml',
'response_type': 'DescribeImagesResponse',
'images': images,
}
response = describe_image_by_id(image_id)

return response
return _describe_image_attribute_response(response)


def _describe_image_attribute_response(response):
attribute = helpers.get('Attribute', request.form)

return {
'template_name_or_list': 'image_attribute.xml',
'response_type': 'DescribeImageAttributeResponse',
'attribute': attribute,
'id': response['id'],
'value': response[attribute]
}
@@ -1,46 +1,47 @@
#!/usr/bin/env python
# encoding: utf-8

from ec2stack.providers.cloudstack import requester
from ec2stack.providers.cloudstack.cloudstack_helpers import *

from flask import request

cloudstack_instance_attributes_to_aws = {
'templateid': 'imageid',
'serviceofferingname': 'instanceType',
'id': 'instanceId',
'created': 'launchTime'
}
from ec2stack import helpers
from ec2stack.providers.cloudstack import requester


@authentication_required
@helpers.authentication_required
def describe_instances():
if contains_parameter('InstanceId.1'):
instances = _describe_specific_instances()
if helpers.contains_parameter('InstanceId.1'):
response = _describe_specific_instances()
else:
instances = _describe_all_instances()
response = _describe_all_instances()

response = _create_describe_instances_response(instances)
response = _describe_instances_response(response)

return response


@authentication_required
def describe_instance_attribute():
instance_id = get('InstanceId', request.form)
attribute = get('Attribute', request.form)
def _describe_all_instances():
response = _describe_virtual_machines_request()
return response

response = describe_item_by_id(
instance_id,
_describe_virtual_machines_request)

virtual_machine = response['virtualmachine'][0]
def _describe_specific_instances():
instance_ids = helpers.get_request_paramaters('InstanceId')

instance_attribute = translator.cloudstack_item_attribute_to_aws(
virtual_machine, cloudstack_instance_attributes_to_aws, attribute)
response = {}
response['virtualmachine'] = []

for instance_id in instance_ids:
instance_response = describe_instance_by_id(instance_id)
response['virtualmachine'].append(instance_response)

return response

response = _create_describe_instance_attribute_response(instance_attribute)

def describe_instance_by_id(instance_id):
args = {}
args['id'] = instance_id
response = _describe_virtual_machines_request(args)
response = response['virtualmachine'][0]
return response


@@ -51,53 +52,37 @@ def _describe_virtual_machines_request(args=None):
args['command'] = 'listVirtualMachines'

cloudstack_response = requester.make_request(args)

cloudstack_response = cloudstack_response['listvirtualmachinesresponse']

return cloudstack_response


def _describe_all_instances():
response = _describe_virtual_machines_request()
instances = get_items_from_response(
response, 'virtualmachine', cloudstack_instance_attributes_to_aws)

return instances
def _describe_instances_response(response):
return {
'template_name_or_list': 'instances.xml',
'response_type': 'DescribeInstancesResponse',
'response': response
}


def _describe_specific_instances():
instance_ids = get_request_paramaters('InstanceId')
instances = []
@helpers.authentication_required
def describe_instance_attribute():
instance_id = helpers.get('InstanceId', request.form)

for instance_id in instance_ids:
response = describe_item_by_id(
instance_id,
_describe_virtual_machines_request)
response = describe_instance_by_id(instance_id)

instances = instances + get_items_from_response(
response, 'virtualmachine', cloudstack_instance_attributes_to_aws)
return _describe_instance_attribute_response(response)

return instances

def _describe_instance_attribute_response(response):
attribute = helpers.get('Attribute', request.form)

def _create_describe_instance_attribute_response(item_attribute):
response = {
'template_name_or_list': 'instance_attribute.xml',
'response_type': 'DescribeInstanceAttributeResponse',
'attribute': get('Attribute', request.form),
'value': item_attribute.values()[0],
'id': get('InstanceId', request.form)
}

return response


def _create_describe_instances_response(instances):
response = {
'template_name_or_list': 'instances.xml',
'response_type': 'DescribeInstancesResponse',
'reservation_id': 'None',
'instances': instances
'attribute': attribute,
'id': response['id'],
'value': response[attribute]
}

return response
@@ -224,10 +224,10 @@ def _get_security_group(rule):
if 'count' in response:
for security_group in response['securitygroup']:
if 'securityGroupId' in rule and security_group['id'] == rule[
'securityGroupId']:
'securityGroupId']:
return security_group
elif 'securityGroupName' in rule and security_group['name'] == rule[
'securityGroupName']:
'securityGroupName']:
return security_group

raise Ec2stackError(

0 comments on commit d86dd2e

Please sign in to comment.