Skip to content
Permalink
Browse files
Continue refactor
  • Loading branch information
brogand93 committed Jun 18, 2014
1 parent 823aee6 commit 0f104a2e95f727faa96e2edb5b12044c21409f3d
Showing 12 changed files with 146 additions and 84 deletions.
@@ -52,7 +52,7 @@ def _get_items(authorization, args=None):
return response


def _get_item_with_name(authorization, name, args, type):
def get_item_with_name(authorization, name, args, type):
response = _get_items(
authorization=authorization,
args=args
@@ -68,15 +68,15 @@ def _get_item_with_name(authorization, name, args, type):
return None


def get_item_with_name_or_error(authorization, name, args, type, error, func_route, to_cloudstack, **kwargs):
cloudstack_item = _get_item_with_name(authorization, name, args, type)
def get_item_with_name_or_error(authorization, name, args, type, func_route, to_cloudstack, **kwargs):
cloudstack_item = get_item_with_name(authorization, name, args, type)

if cloudstack_item:
return helpers.create_response(to_cloudstack(
cloudstack_response=cloudstack_item, **kwargs
))
else:
return error(func_route)
return errors.resource_not_found(func_route)

def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs):
name = None
@@ -88,7 +88,7 @@ def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs):
items = []

if name:
cloudstack_item = _get_item_with_name(
cloudstack_item = get_item_with_name(
authorization=authorization,
name=name,
args=args,
@@ -38,25 +38,19 @@ def _create_populated_image_response(projectid, images=None):
return populated_response


def _cloudstack_template_to_gce(cloudstack_response, selfLink=None):
def _cloudstack_template_to_gce(cloudstack_response):
translate_image_status = {
'True': 'Ready',
'False': 'Failed'
}
'False': 'Failed'}

response = {}
response['kind'] = 'compute#image'
response['id'] = cloudstack_response['id']
response['creationTimestamp'] = cloudstack_response['created']
response['name'] = cloudstack_response['name']
response['description'] = cloudstack_response['displaytext']
response['status'] = translate_image_status[
str(cloudstack_response['isready'])]

if selfLink:
response['selfLink'] = urllib.unquote_plus(selfLink)
else:
response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name']
response['status'] = translate_image_status[str(cloudstack_response['isready'])]
response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name']

return response

@@ -175,8 +175,9 @@ def listinstances(authorization, projectid, zone):
def getinstance(projectid, authorization, zone, instance):
func_route = url_for('getinstance', projectid=projectid, zone=zone, instance=instance)
args = {'command':'listVirtualMachines'}
return controllers.get_item_with_name_or_error(authorization, zone, args, 'zone',
errors.resource_not_found(func_route))
return controllers.get_item_with_name_or_error(
authorization, instance, args, 'zone', func_route,
_cloudstack_zone_to_gce, **{'projectid':projectid})


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['POST'])
@@ -87,23 +87,9 @@ def listmachinetype(projectid, authorization, zone):
@app.route('/compute/v1/projects/<projectid>/zones/<zone>/machineTypes/<machinetype>', methods=['GET'])
@authentication.required
def getmachinetype(projectid, authorization, zone, machinetype):
response = get_machinetype_by_name(
authorization=authorization,
machinetype=machinetype
)

if response:
return helpers.create_response(
data=_cloudstack_service_offering_to_gce(
cloudstack_response=response,
projectid=projectid,
zone=zone
)
)
else:
func_route = url_for(
'getmachinetype',
projectid=projectid,
machinetype=machinetype,
zone=zone)
return errors.resource_not_found(func_route)
func_route = url_for('getmachinetype', projectid=projectid, zone=zone, machinetype=machinetype)
args = {'command':'listServiceOfferings'}
kwargs = {'projectid':projectid, 'zone':zone}
return controllers.get_item_with_name_or_error(
authorization, machinetype, args, 'serviceoffering', func_route,
_cloudstack_service_offering_to_gce, **kwargs)
@@ -26,7 +26,6 @@
from gstack.services import requester
from gstack.controllers import errors


def _add_network(authorization, args=None):
command = 'createSecurityGroup'
if not args:
@@ -43,8 +42,8 @@ def _add_network(authorization, args=None):


def _delete_network(authorization, projectid, network):
network_response = get_network_by_name(authorization, network)

args = {'command':'SecurityGroups'}
network_response = controllers.get_item_with_name(authorization, network, args, 'securitygroup')
if not network_response:
return None

@@ -62,17 +61,13 @@ def _delete_network(authorization, projectid, network):
)


def _cloudstack_network_to_gce(cloudstack_response, selfLink=None):
def _cloudstack_network_to_gce(cloudstack_response):
response = {}
response['kind'] = 'compute#network'
response['id'] = cloudstack_response['id']
response['name'] = cloudstack_response['name']
response['description'] = cloudstack_response['description']

if selfLink:
response['selfLink'] = urllib.unquote_plus(selfLink)
else:
response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name']
response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name']

return response

@@ -109,21 +104,12 @@ def listnetworks(projectid, authorization):
@app.route('/compute/v1/projects/<projectid>/global/networks/<network>', methods=['GET'])
@authentication.required
def getnetwork(projectid, authorization, network):
response = get_network_by_name(
authorization=authorization,
securitygroup=network
)

if response:
return helpers.create_response(
data=_cloudstack_network_to_gce(response)
)
else:
func_route = url_for(
'getnetwork',
projectid=projectid,
network=network)
return errors.resource_not_found(func_route)
func_route = url_for('getnetwork', projectid=projectid, network=network)
args = {'command':'listSecurityGroups'}
kwargs = {}
return controllers.get_item_with_name_or_error(
authorization, network, args, 'securitygroup', func_route,
_cloudstack_network_to_gce, **kwargs)


@app.route('/compute/v1/projects/<projectid>/global/networks', methods=['POST'])
@@ -28,6 +28,10 @@
import urllib
import collections

def _get_account_by_name(authorization, projectid):
args = {'command':'listAccounts'}
return controllers.get_item_with_name(authorization, projectid, args, 'account')


def _list_ssh_keys(authorization):
command = 'listTags'
@@ -129,6 +133,7 @@ def _cloudstack_project_to_gce(cloudstack_response, metadata=None):
@authentication.required
def getproject(authorization, projectid):
project = _get_account_by_name(authorization, projectid)
print project

if project:
metadata = {}
@@ -42,7 +42,7 @@ def listregions(projectid, authorization):
args = {'command':'listAccounts'}
kwargs = {}
items = controllers.describe_items(
authorization, args, 'account',
authorization, args, 'region',
_cloudstack_account_to_gce, **kwargs)

populated_response = {
@@ -57,17 +57,8 @@ def listregions(projectid, authorization):
@app.route('/compute/v1/projects/<projectid>/regions/<region>', methods=['GET'])
@authentication.required
def getregion(projectid, authorization, region):
cloudstack_response = _get_regions(
authorization=authorization,
args={'name': region}
)

if region == cloudstack_response['listregionsresponse']['region'][0]['name']:
return helpers.create_response(
data=_cloudstack_account_to_gce(
cloudstack_response['listregionsresponse']['region'][0]
)
)
else:
function_route = url_for('getregion', projectid=projectid, region=region)
return errors.resource_not_found(function_route)
func_route = url_for('getregion', projectid=projectid, region=region)
args = {'command':'listAccounts'}
return controllers.get_item_with_name_or_error(
authorization, region, args, 'region', func_route,
_cloudstack_account_to_gce, **{})
@@ -92,6 +92,6 @@ def getzone(projectid, authorization, zone):
func_route = url_for('getzone', projectid=projectid, zone=zone)
args = {'command':'listZones'}
return controllers.get_item_with_name_or_error(
authorization, zone, args, 'zone', errors.resource_not_found, func_route,
authorization, zone, args, 'zone', func_route,
_cloudstack_zone_to_gce, **{})

@@ -0,0 +1,66 @@
{
"secondarystoragetotal": 0,
"primarystorageavailable": "Unlimited",
"domain": "brogand93@darrenbrogan.ie",
"domainid": "42f2b0d0-3953-485f-984d-b8d67185d358",
"vpclimit": "Unlimited",
"iplimit": "20",
"memorytotal": 0,
"secondarystorageavailable": "Unlimited",
"vmtotal": 0,
"cputotal": 0,
"vpctotal": 0,
"id": "ddbdf378-e8d9-47e0-964b-661d0d8414b8",
"networkavailable": "Unlimited",
"projectlimit": "Unlimited",
"networklimit": "Unlimited",
"iptotal": -27,
"volumetotal": 0,
"snapshotlimit": "0",
"state": "enabled",
"networktotal": 0,
"accounttype": 0,
"cpuavailable": "Unlimited",
"primarystoragetotal": 0,
"templatelimit": "0",
"snapshottotal": 0,
"templateavailable": "0",
"vmlimit": "20",
"vpcavailable": "0",
"primarystoragelimit": "Unlimited",
"volumelimit": "20",
"templatetotal": 0,
"secondarystoragelimit": "Unlimited",
"user": [
{
"username": "accountname",
"account": "brogand93@darrenbrogan.ie",
"domainid": "42f2b0d0-3953-485f-984d-b8d67185d358",
"firstname": "brogand93",
"created": "2013-09-17T15:35:34+0200",
"lastname": "darrenbrogan.ie",
"iscallerchilddomain": false,
"domain": "brogand93@darrenbrogan.ie",
"email": "brogand93@darrenbrogan.ie",
"secretkey": "lM9fLm8XQwezvLOd10Qt3wXH7j9mRgaKbEg3nRDnj7FtlF3yx54EWd9mR5sB1ec5LQDV6gjpy6sfDo6ndUeeww",
"state": "enabled",
"apikey": "0ZyexOgzlfTx076LYBFz4oT_ShJvxUcezgvsW6gvRZ_BloSsb5uUTAm-FtHvKBvAUP0S7ZN6bIcRf4zEtbm3PQ",
"accounttype": 0,
"timezone": "CET",
"id": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"isdefault": false,
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
],
"projectavailable": "Unlimited",
"isdefault": false,
"memoryavailable": "Unlimited",
"projecttotal": 0,
"volumeavailable": "20",
"name": "accountname",
"vmavailable": "20",
"ipavailable": "0",
"memorylimit": "Unlimited",
"cpulimit": "Unlimited",
"snapshotavailable": "0"
}
@@ -0,0 +1,33 @@
{
"egressrule": [],
"account": "example-account",
"domainid": "66d69e46-a95b-437b-ac6c-bcaa5331999d",
"description": "Default Security Group",
"tags": [],
"domain": "example-account",
"ingressrule": [
{
"protocol": "tcp",
"cidr": "0.0.0.0/0",
"startport": 22,
"endport": 22,
"ruleid": "3d92cc70-8c84-4e8a-9989-6efcd7ff7905"
},
{
"protocol": "icmp",
"cidr": "0.0.0.0/0",
"ruleid": "2ba7dd7b-13b2-49ae-bf8e-26ffadd32c9e",
"icmpcode": 0,
"icmptype": 0
},
{
"protocol": "tcp",
"cidr": "0.0.0.0/0",
"startport": 8080,
"endport": 8080,
"ruleid": "c4562b3c-d1b0-4844-a771-3c3434e1a5d0"
}
],
"id": "1f95ee9b-b291-48c1-9492-0eee632677e3",
"name": "networkname"
}
@@ -100,10 +100,10 @@ def test_delete_network(self):
get.return_value.status_code = 200

get_networks = mock.Mock()
get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json'))
get_networks.return_value = None

with mock.patch('requests.get', get):
with mock.patch('gstack.controllers.networks._get_networks', get_networks):
with mock.patch('gstack.controllers.get_item_with_name', get_networks):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}
@@ -119,10 +119,10 @@ def test_delete_network_network_not_found(self):
get.return_value.status_code = 200

get_networks = mock.Mock()
get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json'))
get_networks.return_value = json.loads(read_file('tests/data/valid_get_security_group.json'))

with mock.patch('requests.get', get):
with mock.patch('gstack.controllers.networks._get_networks', get_networks):
with mock.patch('gstack.controllers.get_item_with_name', get_networks):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}
@@ -11,14 +11,14 @@ class ProjectsTestCase(GStackAppTestCase):

def test_get_project(self):
get = mock.Mock()
get.return_value = json.loads(read_file('tests/data/valid_describe_account.json'))
get.return_value = json.loads(read_file('tests/data/valid_get_account.json'))

get_tags = mock.Mock()
get_tags.return_value.text = read_file('tests/data/valid_describe_tags.json')
get_tags.return_value.status_code = 200

with mock.patch('requests.get', get_tags):
with(mock.patch('gstack.controllers.project._get_accounts', get)):
with(mock.patch('gstack.controllers.get_item_with_name', get)):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/accountname', headers=headers)

@@ -33,7 +33,7 @@ def test_get_project_project_not_found(self):
get_tags.return_value.status_code = 200

with mock.patch('requests.get', get_tags):
with(mock.patch('gstack.controllers.project._get_accounts', get)):
with(mock.patch('gstack.controllers._get_items', get)):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/invalidaccountname', headers=headers)

0 comments on commit 0f104a2

Please sign in to comment.