Skip to content
Permalink
Browse files
Add incomplete create / describe volumes
  • Loading branch information
brogand93 committed Feb 20, 2014
1 parent aada258 commit f8b494a614c3bfac1a80b437d0d5c599f66f7b8b
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 5 deletions.
@@ -27,12 +27,14 @@ def _get_action(action):
actions = {
'CreateKeyPair': keypair.create_keypair,
'CreateSecurityGroup': security_group.create_security_group,
'CreateVolume': volume.create_volume,
'DeleteKeyPair': keypair.delete_keypair,
'DeleteSecurityGroup': security_group.delete_security_group,
'DeleteVolume': volume.delete_volume,
'DescribeImages': images.describe_images,
'DescribeInstanceAttribute': instances.describe_instance_attribute,
'DescribeInstances': instances.describe_instances,
'DescribeVolumes': volume.describe_volumes,
'GetPasswordData': password.get_password_data,
'ImportKeyPair': keypair.import_keypair,
'RegisterSecretKey': registerSecretKey,
@@ -100,3 +102,10 @@ def not_found(err):
@DEFAULT.app_errorhandler(400)
def bad_request(err):
return error_response('400', 'BadRequest', 'Bad Request')


#@DEFAULT.app_errorhandler(401)
#def auth_failure(err):
# return error_response('401', 'AuthFailure',
# 'AWS was not able to validate the provided '
# 'access credentials')
@@ -33,7 +33,6 @@ def decorated(*args, **kwargs):
_valid_signature_method()
_valid_signature_version()
_valid_signature()

return f(*args, **kwargs)

return decorated
@@ -48,6 +47,13 @@ def require_parameters(required_parameters):
if (get(parameter, request.form)) is None:
missing_paramater(parameter)

def require_one_paramater(parameters):
for parameter in parameters:
if (get(parameter, request.form)) is not None:
return

missing_paramater(parameter)


def missing_paramater(parameter):
raise Ec2stackError(
@@ -103,6 +109,9 @@ def _valid_signature():
signature = get('Signature', request.form)
generated_signature = _generate_signature()

print 'Supplied signature: ' + signature
print 'Generated signature: ' + generated_signature

if signature != generated_signature:
raise Ec2stackError(
'401',
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# encoding: utf-8

from flask import request

from ec2stack import helpers
from ec2stack.helpers import authentication_required
from ec2stack.providers.cloudstack import requester


def get_disk_offerings_id_by_name(name):
args = {}
args['name'] = name
response = _describe_disk_offerings_request(args)
response = response['diskoffering'][0]
return response['id']


def _describe_disk_offerings_request(args=None):
args['command'] = 'listDiskOfferings'

response = requester.make_request(args)
response = response['listdiskofferingsresponse']

return response
@@ -6,16 +6,156 @@
from ec2stack import helpers
from ec2stack.helpers import authentication_required
from ec2stack.providers.cloudstack import requester
from . import disk_offerings
import uuid


cloudstack_attributes_to_aws = {
'id': 'volumeId',
'name': 'name',
'virtualmachineid': 'instanceId',
'created': 'attachTime'
}


@authentication_required
def describe_volumes():
response = _describe_all_volumes()
return _describe_volumes_response(response)


def _describe_all_volumes():
response = _describe_volumes_request()
return _get_volumes_from_response(response)


def _get_volumes_from_response(response, attribute=None):
volumes = []
if response:
for volume in response['volume']:
volumes.append(
_cloudstack_volume_to_aws_volume(volume, attribute)
)

return volumes


def _cloudstack_volume_to_aws_volume(response, attribute=None):
volume = {}

for cloudstack_attr, aws_attr in cloudstack_attributes_to_aws.iteritems():
if cloudstack_attr in response:
volume[aws_attr] = response[cloudstack_attr]

return volume


def _describe_volumes_response(volumes):
response = {
'template_name_or_list': 'volumes.xml',
'response_type': 'DescribeVolumesResponse',
'volumes': volumes,
}

return response


def _describe_volumes_request():
args = {}
args['command'] = 'listVolumes'

response = requester.make_request(args)
response = response['listvolumesresponse']

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()

response = requester.make_request(args)

response = response['createvolumeresponse']

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'
args['id'] = helpers.get('VolumeId', request.form)
args['virtualmachineid'] = helpers.get('InstanceId', request.form)

response = requester.make_request(args)
response = response['attachvolumeresponse']

return response


def _attach_volume_response(response):
return {
'template_name_or_list': 'attach_volume.xml',
'response_type': 'AttachVolumeResponse',
'volume_id': response['id'],
'instance_id': response['virtualmachineid'],
'device': response['deviceid'],
'status': response['state'],
'attach_time': response['created']
}


@authentication_required
def delete_volume():
helpers.require_parameters(['VolumeId'])
response = _delete_security_group_request()
return _delete_security_group_response(response)
response = _delete_volume_request()
return _delete_volume_response(response)


def _delete_security_group_request():
def _delete_volume_request():
args = {}
args['command'] = 'deleteVolume'
args['id'] = helpers.get('VolumeId', request.form)
@@ -25,7 +165,7 @@ def _delete_security_group_request():
return response


def _delete_security_group_response(response):
def _delete_volume_response(response):
return {
'template_name_or_list': 'delete_item.xml',
'response_type': 'DeleteVolumeResponse',
@@ -0,0 +1,8 @@
{% extends "response.xml" %}
{% block response_content %}
<volumeId>{{ id }}</volumeId>
<size>{{ size }}</size>
<status>{{ status }}</status>
<availabilityZone>{{ zone }}</availabilityZone>
<volumeType> {{ volumeType }}</volumeType>
{% endblock %}
@@ -0,0 +1,24 @@
{% extends "response.xml" %}
{% block response_content %}
<volumeSet>
{% for volume in volumes %}
<item>
<volumeId>
{{ volume.volumeId }}
</volumeId>
<name>{{ volume.name }}</name>
<createTime>{{ volume.status }}</createTime>
<attachmentSet>
<item>
<volumeId>
{{ volume.volumeId }}
</volumeId>
<instanceId>
{{ volume.instanceId}}
</instanceId>
</item>
</attachmentSet>
</item>
{% endfor %}
</volumeSet>
{% endblock %}

0 comments on commit f8b494a

Please sign in to comment.