Skip to content
Permalink
Browse files
Restructure controller functions, private -> public -> routed
  • Loading branch information
brogand93 committed Jun 20, 2014
1 parent 60ee7e4 commit 40616111e86b83c4b7c8b85e394bb5754b79afea
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 87 deletions.
@@ -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
@@ -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 = {}
@@ -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
@@ -18,9 +18,9 @@
# under the License.

import urllib

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


@app.errorhandler(401)
@@ -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):
@@ -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):
@@ -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'])
@@ -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)
@@ -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):
@@ -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):
@@ -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):
@@ -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):
@@ -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):

0 comments on commit 4061611

Please sign in to comment.