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

Commit

Permalink
Restructure controller functions, private -> public -> routed
Browse files Browse the repository at this point in the history
  • Loading branch information
brogand93 committed Jun 20, 2014
1 parent 60ee7e4 commit 4061611
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 87 deletions.
8 changes: 4 additions & 4 deletions gstack/controllers/OAuth2.py
Expand Up @@ -17,8 +17,8 @@
# specific language governing permissions and limitations
# under the License.

import flask
from flask import request
from flask import request, make_response

from gstack import app
from gstack.oauth2provider import CloudstackAuthorizationProvider

Expand All @@ -30,7 +30,7 @@ def authorization_code():

response = provider.get_authorization_code_from_uri(request.url)

res = flask.make_response(response.text, response.status_code)
res = make_response(response.text, response.status_code)
for k, v in response.headers.iteritems():
res.headers[k] = v
return res
Expand All @@ -44,7 +44,7 @@ def token():

response = provider.get_token_from_post_data(data)

res = flask.make_response(response.text, response.status_code)
res = make_response(response.text, response.status_code)
for k, v in response.headers.iteritems():
res.headers[k] = v
return res
57 changes: 29 additions & 28 deletions gstack/controllers/__init__.py
Expand Up @@ -20,8 +20,9 @@
import os
import glob

from gstack import helpers
from flask import request

from gstack import helpers
from gstack.services import requester
from gstack.controllers import errors

Expand Down Expand Up @@ -52,33 +53,6 @@ def _get_items(authorization, args=None):
return response


def get_item_with_name(authorization, name, args, type):
response = _get_items(
authorization=authorization,
args=args
)

if 'count' in response:
response = filter_by_name(
data=response[type],
name=name
)
return response
else:
return None


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 errors.resource_not_found(func_route)


def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs):
name = None
filter = helpers.get_filter(request.args)
Expand Down Expand Up @@ -113,6 +87,33 @@ def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs):
return items


def get_item_with_name(authorization, name, args, type):
response = _get_items(
authorization=authorization,
args=args
)

if 'count' in response:
response = filter_by_name(
data=response[type],
name=name
)
return response
else:
return None


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 errors.resource_not_found(func_route)


def describe_items_aggregated(authorization, args, type, gce_type, to_cloudstack, **kwargs):
from gstack.controllers import zones
items = {}
Expand Down
2 changes: 2 additions & 0 deletions gstack/controllers/disks.py
Expand Up @@ -18,7 +18,9 @@
# under the License.

import urllib

from flask import request, url_for

from gstack import app, authentication
from gstack import helpers
from gstack import controllers
Expand Down
2 changes: 1 addition & 1 deletion gstack/controllers/errors.py
Expand Up @@ -18,9 +18,9 @@
# under the License.

import urllib

from gstack import app
from gstack import helpers
from flask import Response


@app.errorhandler(401)
Expand Down
6 changes: 4 additions & 2 deletions gstack/controllers/firewalls.py
Expand Up @@ -17,14 +17,16 @@
# specific language governing permissions and limitations
# under the License.

import json

from flask import jsonify, request, url_for

from gstack import app
from gstack import authentication
from gstack import controllers
from gstack import helpers
from gstack.services import requester
from gstack.controllers import errors
from flask import jsonify, request, url_for
import json


def _cloudstack_securitygroup_to_gce(cloudstack_response):
Expand Down
14 changes: 8 additions & 6 deletions gstack/controllers/images.py
Expand Up @@ -18,15 +18,12 @@
# under the License.

import urllib

from flask import request, url_for

from gstack import app, authentication
from gstack import helpers
from gstack import controllers
from flask import request, url_for


def get_template_by_name(authorization, image):
args = {'templatefilter': 'executable', 'command': 'listTemplates'}
return controllers.get_item_with_name(authorization, image, args, 'template')


def _create_populated_image_response(projectid, images=None):
Expand Down Expand Up @@ -55,6 +52,11 @@ def _cloudstack_template_to_gce(cloudstack_response):
return response


def get_template_by_name(authorization, image):
args = {'templatefilter': 'executable', 'command': 'listTemplates'}
return controllers.get_item_with_name(authorization, image, args, 'template')


@app.route('/compute/v1/projects/centos-cloud/global/images', methods=['GET'])
@authentication.required
def listnocentoscloudimages(authorization):
Expand Down
2 changes: 1 addition & 1 deletion gstack/controllers/index.py
Expand Up @@ -17,10 +17,10 @@
# specific language governing permissions and limitations
# under the License.

import json

from gstack import app
from gstack import helpers
import json


@app.route('/discovery/v1/apis/compute/v1/rest', methods=['GET'])
Expand Down
26 changes: 14 additions & 12 deletions gstack/controllers/instances.py
Expand Up @@ -19,9 +19,11 @@

import json
import urllib

from flask import request, url_for

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, operations, images, errors, machine_type, networks
Expand Down Expand Up @@ -150,6 +152,17 @@ def listinstances(authorization, projectid, zone):
return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>', methods=['GET'])
@authentication.required
def getinstance(projectid, authorization, zone, instance):
func_route = url_for('getinstance', projectid=projectid, zone=zone, instance=instance)
args = {'command': 'listVirtualMachines'}
kwargs = {'projectid': projectid, 'zone': zone}
return controllers.get_item_with_name_or_error(
authorization, instance, args, 'virtualmachine', func_route,
_cloudstack_virtual_machine_to_gce, **kwargs)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['POST'])
@authentication.required
def addinstance(authorization, projectid, zone):
Expand Down Expand Up @@ -203,14 +216,3 @@ def deleteinstance(projectid, authorization, zone, instance):
)

return helpers.create_response(data=populated_response)


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>', methods=['GET'])
@authentication.required
def getinstance(projectid, authorization, zone, instance):
func_route = url_for('getinstance', projectid=projectid, zone=zone, instance=instance)
args = {'command': 'listVirtualMachines'}
kwargs = {'projectid': projectid, 'zone': zone}
return controllers.get_item_with_name_or_error(
authorization, instance, args, 'virtualmachine', func_route,
_cloudstack_virtual_machine_to_gce, **kwargs)
14 changes: 8 additions & 6 deletions gstack/controllers/machine_type.py
Expand Up @@ -18,16 +18,13 @@
# under the License.

import urllib

from flask import request, url_for

from gstack import app
from gstack import authentication
from gstack import helpers
from gstack import controllers
from flask import request, url_for


def get_machinetype_by_name(authorization, machinetype):
args = {'command': 'listServiceOfferings'}
return controllers.get_item_with_name(authorization, machinetype, args, 'serviceoffering')


def _cloudstack_service_offering_to_gce(cloudstack_response, projectid, zone):
Expand All @@ -51,6 +48,11 @@ def _cloudstack_service_offering_to_gce(cloudstack_response, projectid, zone):
return response


def get_machinetype_by_name(authorization, machinetype):
args = {'command': 'listServiceOfferings'}
return controllers.get_item_with_name(authorization, machinetype, args, 'serviceoffering')


@app.route('/compute/v1/projects/<projectid>/aggregated/machineTypes', methods=['GET'])
@authentication.required
def aggregatedlistmachinetypes(projectid, authorization):
Expand Down
14 changes: 8 additions & 6 deletions gstack/controllers/networks.py
Expand Up @@ -19,19 +19,16 @@

import urllib
import json

from flask import request, url_for

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 errors


def get_network_by_name(authorization, network):
args = {'command': 'listSecurityGroups'}
return controllers.get_item_with_name(authorization, network, args, 'securitygroup')


def _add_network(authorization, args=None):
command = 'createSecurityGroup'
if not args:
Expand Down Expand Up @@ -91,6 +88,11 @@ def _create_populated_network_response(projectid, networks=None):
return populated_response


def get_network_by_name(authorization, network):
args = {'command': 'listSecurityGroups'}
return controllers.get_item_with_name(authorization, network, args, 'securitygroup')


@app.route('/compute/v1/projects/<projectid>/global/networks', methods=['GET'])
@authentication.required
def listnetworks(projectid, authorization):
Expand Down
4 changes: 3 additions & 1 deletion gstack/controllers/operations.py
Expand Up @@ -18,11 +18,13 @@
# under the License.

import urllib

from flask import url_for

from gstack import app, publickey_storage
from gstack import authentication
from gstack import helpers
from gstack.services import requester
from flask import url_for


def _get_async_result(authorization, args):
Expand Down
10 changes: 6 additions & 4 deletions gstack/controllers/project.py
Expand Up @@ -17,16 +17,18 @@
# specific language governing permissions and limitations
# under the License.from gcloud import app

import json
import urllib
import collections

from flask import jsonify, request, url_for

from gstack import app, publickey_storage
from gstack import authentication
from gstack import helpers
from gstack import controllers
from gstack.services import requester
from gstack.controllers import errors
from flask import jsonify, request, url_for
import json
import urllib
import collections


def _get_account_by_name(authorization, projectid):
Expand Down
2 changes: 1 addition & 1 deletion gstack/controllers/regions.py
Expand Up @@ -17,12 +17,12 @@
# specific language governing permissions and limitations
# under the License.

from flask import request, url_for

from gstack import app
from gstack import helpers
from gstack import controllers
from gstack import authentication
from flask import request, url_for


def _cloudstack_account_to_gce(cloudstack_response):
Expand Down

0 comments on commit 4061611

Please sign in to comment.