Skip to content

Commit

Permalink
Begin addition of VPC support
Browse files Browse the repository at this point in the history
  • Loading branch information
brogand93 committed Jul 15, 2014
1 parent 252e43e commit 2260ea0
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
12 changes: 12 additions & 0 deletions ec2stack/errors.py
Expand Up @@ -162,6 +162,18 @@ def invalid_resource_id():
'The specified ID for the resource you are trying to tag is not valid.'
)

def invalid_vpc_range():
"""
Invalid cidr block.
@raise Ec2stackError: Defining a bad request and message.
"""
raise Ec2stackError(
'400',
'InvalidVpcRange',
'The specified CIDR block range is not valid.'
)


def duplicate_security_group():
"""
Expand Down
4 changes: 2 additions & 2 deletions ec2stack/providers/cloudstack/keypairs.py
Expand Up @@ -6,10 +6,10 @@

from base64 import b64decode

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


@helpers.authentication_required
Expand Down
2 changes: 1 addition & 1 deletion ec2stack/providers/cloudstack/volumes.py
Expand Up @@ -107,7 +107,7 @@ def _create_volume_request():

args['zoneid'] = zone_id
args['command'] = 'createVolume'
args['name'] = uuid.uuid4()
args['name'] = uuid.uuid()

response = requester.make_request_async(args)

Expand Down
129 changes: 129 additions & 0 deletions ec2stack/providers/cloudstack/vpc.py
@@ -0,0 +1,129 @@

#!/usr/bin/env python
# encoding: utf-8

"""This module contains functions for handling requests in relation to vpcs.
"""

import uuid

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


@helpers.authentication_required
def create_vpc():
"""
Create a vpc.
@return: Response.
"""
response = _create_vpc_request()
return _create_vpc_response(response)


def _create_vpc_request():
"""
Request to create a vpc.
@return: Response.
"""
args = {'command': 'createVPC'}
name = uuid.uuid()
args['name'] = name
args['id'] = name
args['cidr'] = helpers.get('CidrBlock')

response = requester.make_request(args)

response = response['createvpcresponse']

return response


def _create_vpc_response(response):
"""
Generates a response for create vpc request.
@param response: Response from Cloudstack.
@return: Response.
"""
if 'errortext' in response:
errors.invalid_vpc_range()
else:
response = response['vpc']
return {
'template_name_or_list': 'create_vpc.xml',
'response_type': 'CreateVpcResponse',
'response': response
}

@helpers.authentication_required
def delete_vpc():
"""
Delete a vpc.
@return: Response.
"""
helpers.require_parameters(['VpcId'])
_delete_vpc_request()
return _delete_vpc_response()


def _delete_vpc_request():
"""
Request to delete a vpc.
@return: Response.
"""
args = {'command': 'deleteVPC', 'id': helpers.get('VpcId')}

response = requester.make_request(args)

return response


def _delete_vpc_response():
"""
Generates a response for delete vpc request.
@return: Response.
"""
return {
'template_name_or_list': 'status.xml',
'response_type': 'DeleteVpcResponse',
'return': 'true'
}

@helpers.authentication_required
def describe_vpcs():
"""
Describes a specific vpc or all vpcs.
@return: Response.
"""
args = {'command': 'listVPCs'}
response = cloudstack.describe_item(
args, 'vpc', {}, 'vpcId'
)

return _describe_vpc_response(
response
)


def _describe_vpc_response(response):
"""
Generates a response for describe vpc request.
@param response: Response from Cloudstack.
@return: Response.
"""
return {
'template_name_or_list': 'vpcs.xml',
'response_type': 'DescribeVpcsResponse',
'response': response
}

0 comments on commit 2260ea0

Please sign in to comment.