Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Begin refactoring process, messy template usage
Browse files Browse the repository at this point in the history
  • Loading branch information
brogand93 committed Jun 11, 2014
1 parent 853231f commit 5bb67d5
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 122 deletions.
11 changes: 9 additions & 2 deletions gstack/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@
import os
import glob

__all__ = [os.path.basename(
f)[:-3] for f in glob.glob(os.path.dirname(__file__) + '/*.py')]
__all__ = [os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__) + '/*.py')]


def filter_by_name(data, name):
for item in data:
if item['name'] == name:
return item

return None
20 changes: 11 additions & 9 deletions gstack/controllers/disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from flask import request, url_for
from gstack import app, authentication
from gstack.services import requester
from gstack.controllers import zones, helper, errors
from gstack import helpers
from gstack import controllers
from gstack.controllers import zones, errors


def _get_disks(authorization, args=None):
Expand All @@ -47,7 +49,7 @@ def get_disk_by_name(authorization, disk):
)

if disk_list['listvolumesresponse']:
response = helper.filter_by_name(
response = controllers.filter_by_name(
data=disk_list['listvolumesresponse']['volume'],
name=disk
)
Expand All @@ -66,7 +68,7 @@ def _cloudstack_volume_to_gce(cloudstack_response, projectid, zone):
response['description'] = cloudstack_response['name']
response['sizeGb'] = cloudstack_response['size']

response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
response['selfLink'] = urllib.unquote_plus(helpers.get_root_url() + url_for(
'getmachinetype',
projectid=projectid,
machinetype=cloudstack_response['name'],
Expand All @@ -88,7 +90,7 @@ def aggregatedlistdisks(projectid, authorization):
zone_list = zones.get_zone_names(authorization=authorization)

disk = None
filter = helper.get_filter(request.args)
filter = helpers.get_filter(request.args)

if 'name' in filter:
disk = filter['name']
Expand Down Expand Up @@ -130,14 +132,14 @@ def aggregatedlistdisks(projectid, authorization):
'items': items
}

return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/disks', methods=['GET'])
@authentication.required
def listdisks(projectid, authorization, zone):
disk = None
filter = helper.get_filter(request.args)
filter = helpers.get_filter(request.args)

if 'name' in filter:
disk = filter['name']
Expand All @@ -150,7 +152,7 @@ def listdisks(projectid, authorization, zone):
args={'keyword': disk}
)
if disk_list['listvolumesresponse']:
disk = helper.filter_by_name(
disk = controllers.filter_by_name(
data=disk_list['listvolumesresponse']['volume'],
name=disk
)
Expand Down Expand Up @@ -181,7 +183,7 @@ def listdisks(projectid, authorization, zone):
'items': items
}

return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/disks/<disk>', methods=['GET'])
Expand All @@ -193,7 +195,7 @@ def getdisk(projectid, authorization, zone, disk):
)

if response:
return helper.create_response(
return helpers.create_response(
data=_cloudstack_volume_to_gce(
cloudstack_response=response,
projectid=projectid,
Expand Down
51 changes: 0 additions & 51 deletions gstack/controllers/helper.py

This file was deleted.

32 changes: 20 additions & 12 deletions gstack/controllers/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import urllib
from gstack import app, authentication
from gstack.services import requester
from gstack.controllers import helper, errors
from gstack import helpers
from gstack import controllers
from gstack.controllers import errors
from flask import request, url_for


Expand Down Expand Up @@ -50,7 +52,7 @@ def get_template_by_name(authorization, image):
)

if image_list['listtemplatesresponse']:
response = helper.filter_by_name(
response = controllers.filter_by_name(
data=image_list['listtemplatesresponse']['template'],
name=image
)
Expand All @@ -62,6 +64,7 @@ def get_template_by_name(authorization, image):
def _create_populated_image_response(projectid, images=None):
if not images:
images = []

populated_response = {
'kind': 'compute#imageList',
'selfLink': request.base_url,
Expand Down Expand Up @@ -98,14 +101,14 @@ def _cloudstack_template_to_gce(cloudstack_response, selfLink=None):
@authentication.required
def listnocentoscloudimages(authorization):
populated_response = _create_populated_image_response('centos-cloud')
return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/debian-cloud/global/images', methods=['GET'])
@authentication.required
def listnodebiancloudimages(authorization):
populated_response = _create_populated_image_response('debian-cloud')
return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/global/images', methods=['GET'])
Expand All @@ -115,15 +118,20 @@ def listimages(projectid, authorization):
authorization=authorization
)

images = []
if image_list['listtemplatesresponse']:
for image in image_list['listtemplatesresponse']['template']:
images.append(_cloudstack_template_to_gce(
cloudstack_response=image,
selfLink=request.base_url + '/' + image['name']))

populated_response = _create_populated_image_response(projectid, images)
return helper.create_response(data=populated_response)
image['selflink'] = request.base_url + '/' + image['name']

kwargs = {
'template_name_or_list': 'images.json',
'selflink': request.base_url,
'request_id': 'projects/' + projectid + '/global/images',
'request_kind': 'compute#imageList',
'response': image_list['listtemplatesresponse']
}
return helpers.successful_response(
**kwargs
)


@app.route('/compute/v1/projects/<projectid>/global/images/<image>', methods=['GET'])
Expand All @@ -135,7 +143,7 @@ def getimage(projectid, authorization, image):
)

if response:
return helper.create_response(
return helpers.create_response(
data=_cloudstack_template_to_gce(response)
)
else:
Expand Down
8 changes: 4 additions & 4 deletions gstack/controllers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


from gstack import app
from gstack.controllers import helper
from gstack import helpers
import json


Expand All @@ -28,9 +28,9 @@ def discovery():
with open(app.config['DATA'] + '/v1.json') as template:
discovery_template = json.loads(template.read())

discovery_template['baseUrl'] = helper.get_root_url() + '/' + app.config['PATH']
discovery_template['baseUrl'] = helpers.get_root_url() + '/' + app.config['PATH']
discovery_template['basePath'] = '/' + app.config['PATH']
discovery_template['rootUrl'] = helper.get_root_url() + '/'
discovery_template['rootUrl'] = helpers.get_root_url() + '/'
discovery_template['servicePath'] = app.config['PATH']

return helper.create_response(data=discovery_template)
return helpers.create_response(data=discovery_template)
22 changes: 12 additions & 10 deletions gstack/controllers/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

import json
import urllib
from gstack import helpers
from gstack import controllers
from flask import request, url_for
from gstack import app, authentication
from gstack.services import requester
from gstack.controllers import zones, helper, operations, images, errors, machine_type, networks
from gstack.controllers import zones, operations, images, errors, machine_type, networks


def _get_virtual_machines(authorization, args=None):
Expand Down Expand Up @@ -134,7 +136,7 @@ def _cloudstack_virtual_machine_to_gce(cloudstack_response, zone, projectid):

response['networkInterfaces'].append(networking)

response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
response['selfLink'] = urllib.unquote_plus(helpers.get_root_url() + url_for(
'getinstance',
projectid=projectid,
instance=cloudstack_response['name'],
Expand All @@ -154,7 +156,7 @@ def _get_virtual_machine_by_name(authorization, instance):
)

if virtual_machine_list['listvirtualmachinesresponse']:
response = helper.filter_by_name(
response = controllers.filter_by_name(
data=virtual_machine_list['listvirtualmachinesresponse']['virtualmachine'],
name=instance
)
Expand All @@ -170,7 +172,7 @@ def aggregatedlistinstances(authorization, projectid):
virtual_machine_list = _get_virtual_machines(authorization=authorization)

instance = None
filter = helper.get_filter(request.args)
filter = helpers.get_filter(request.args)

if 'name' in filter:
instance = filter['name']
Expand Down Expand Up @@ -211,14 +213,14 @@ def aggregatedlistinstances(authorization, projectid):
'selfLink': request.base_url,
'items': items
}
return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['GET'])
@authentication.required
def listinstances(authorization, projectid, zone):
instance = None
filter = helper.get_filter(request.args)
filter = helpers.get_filter(request.args)

if 'name' in filter:
instance = filter['name']
Expand Down Expand Up @@ -258,7 +260,7 @@ def listinstances(authorization, projectid, zone):
'items': items
}

return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>', methods=['GET'])
Expand All @@ -270,7 +272,7 @@ def getinstance(projectid, authorization, zone, instance):
)

if response:
return helper.create_response(
return helpers.create_response(
data=_cloudstack_virtual_machine_to_gce(
cloudstack_response=response,
projectid=projectid,
Expand Down Expand Up @@ -325,7 +327,7 @@ def addinstance(authorization, projectid, zone):
authorization=authorization
)

return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>', methods=['DELETE'])
Expand All @@ -339,4 +341,4 @@ def deleteinstance(projectid, authorization, zone, instance):
authorization=authorization
)

return helper.create_response(data=populated_response)
return helpers.create_response(data=populated_response)
Loading

0 comments on commit 5bb67d5

Please sign in to comment.