Restructure controller functions, private -> public -> routed
Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/40616111 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/40616111 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/40616111 Branch: refs/heads/master Commit: 40616111e86b83c4b7c8b85e394bb5754b79afea Parents: 60ee7e4 Author: BroganD1993 <darrenbro...@hotmail.com> Authored: Fri Jun 20 21:04:56 2014 +0100 Committer: BroganD1993 <darrenbro...@hotmail.com> Committed: Fri Jun 20 21:04:56 2014 +0100 ---------------------------------------------------------------------- gstack/controllers/OAuth2.py | 8 ++--- gstack/controllers/__init__.py | 57 +++++++++++++++++---------------- gstack/controllers/disks.py | 2 ++ gstack/controllers/errors.py | 2 +- gstack/controllers/firewalls.py | 6 ++-- gstack/controllers/images.py | 14 ++++---- gstack/controllers/index.py | 2 +- gstack/controllers/instances.py | 26 ++++++++------- gstack/controllers/machine_type.py | 14 ++++---- gstack/controllers/networks.py | 14 ++++---- gstack/controllers/operations.py | 4 ++- gstack/controllers/project.py | 10 +++--- gstack/controllers/regions.py | 2 +- gstack/controllers/zones.py | 31 +++++++++--------- 14 files changed, 105 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/OAuth2.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/OAuth2.py b/gstack/controllers/OAuth2.py index a84a0ce..b655e59 100644 --- a/gstack/controllers/OAuth2.py +++ b/gstack/controllers/OAuth2.py @@ -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 @@ -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 @@ -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 http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/__init__.py b/gstack/controllers/__init__.py index 97f7847..6e7dd61 100644 --- a/gstack/controllers/__init__.py +++ b/gstack/controllers/__init__.py @@ -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 @@ -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) @@ -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 = {} http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/disks.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/disks.py b/gstack/controllers/disks.py index c0bbbb0..4427552 100644 --- a/gstack/controllers/disks.py +++ b/gstack/controllers/disks.py @@ -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 http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/errors.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/errors.py b/gstack/controllers/errors.py index 9805ca9..d60b44a 100644 --- a/gstack/controllers/errors.py +++ b/gstack/controllers/errors.py @@ -18,9 +18,9 @@ # under the License. import urllib + from gstack import app from gstack import helpers -from flask import Response @app.errorhandler(401) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/firewalls.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/firewalls.py b/gstack/controllers/firewalls.py index b2264ac..ddca6e4 100755 --- a/gstack/controllers/firewalls.py +++ b/gstack/controllers/firewalls.py @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/images.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/images.py b/gstack/controllers/images.py index c31af2b..431679d 100755 --- a/gstack/controllers/images.py +++ b/gstack/controllers/images.py @@ -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): @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/index.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/index.py b/gstack/controllers/index.py index fc1f925..1250bdf 100755 --- a/gstack/controllers/index.py +++ b/gstack/controllers/index.py @@ -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']) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/instances.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py index 1eb0e6a..7ea1139 100755 --- a/gstack/controllers/instances.py +++ b/gstack/controllers/instances.py @@ -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 @@ -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): @@ -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) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/machine_type.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/machine_type.py b/gstack/controllers/machine_type.py index b78a6c5..d6e23ef 100755 --- a/gstack/controllers/machine_type.py +++ b/gstack/controllers/machine_type.py @@ -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): @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/networks.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/networks.py b/gstack/controllers/networks.py index d2b5fd4..e4421f1 100644 --- a/gstack/controllers/networks.py +++ b/gstack/controllers/networks.py @@ -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: @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/operations.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/operations.py b/gstack/controllers/operations.py index 9bbdd46..048595d 100644 --- a/gstack/controllers/operations.py +++ b/gstack/controllers/operations.py @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/project.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/project.py b/gstack/controllers/project.py index 35408bd..b0605b1 100755 --- a/gstack/controllers/project.py +++ b/gstack/controllers/project.py @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/regions.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/regions.py b/gstack/controllers/regions.py index dbaf5b7..a5a5e80 100755 --- a/gstack/controllers/regions.py +++ b/gstack/controllers/regions.py @@ -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): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/40616111/gstack/controllers/zones.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/zones.py b/gstack/controllers/zones.py index 8e9985c..6619546 100755 --- a/gstack/controllers/zones.py +++ b/gstack/controllers/zones.py @@ -18,17 +18,13 @@ # under the License. from flask import request, url_for + from gstack import helpers from gstack import controllers from gstack import app, authentication from gstack.services import requester -def get_zone_by_name(authorization, zone): - args = {'command': 'listZones'} - return controllers.get_item_with_name(authorization, zone, args, 'zone') - - def _get_zones(authorization): command = 'listZones' args = {} @@ -42,6 +38,21 @@ def _get_zones(authorization): return cloudstack_response +def _cloudstack_zone_to_gce(cloudstack_response): + return ({ + 'kind': 'compute#zone', + 'name': cloudstack_response['name'], + 'description': cloudstack_response['name'], + 'id': cloudstack_response['id'], + 'status': cloudstack_response['allocationstate'] + }) + + +def get_zone_by_name(authorization, zone): + args = {'command': 'listZones'} + return controllers.get_item_with_name(authorization, zone, args, 'zone') + + def get_zone_names(authorization): zone_list = _get_zones(authorization) @@ -53,16 +64,6 @@ def get_zone_names(authorization): return zones -def _cloudstack_zone_to_gce(cloudstack_response): - return ({ - 'kind': 'compute#zone', - 'name': cloudstack_response['name'], - 'description': cloudstack_response['name'], - 'id': cloudstack_response['id'], - 'status': cloudstack_response['allocationstate'] - }) - - @app.route('/compute/v1/projects/<projectid>/zones', methods=['GET']) @authentication.required def listzones(projectid, authorization):