Skip to content
Permalink
Browse files
Fix up db for testing
  • Loading branch information
brogand93 committed Jun 18, 2014
1 parent d705e33 commit 823aee6abbf6fd90dbdc1caf0619d57df0a79c5c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 53 deletions.
@@ -68,6 +68,16 @@ 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)

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

def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs):
name = None
filter = helpers.get_filter(request.args)
@@ -19,6 +19,7 @@

import urllib
from gstack import app
from gstack import helpers
from flask import jsonify, Response


@@ -29,7 +30,7 @@ def not_found(e):

@app.errorhandler(401)
def unauthorized(e):
res = jsonify({
res = {
'error': {
'errors': [
{
@@ -43,14 +44,13 @@ def unauthorized(e):
},
'code': 401,
'message': 'Login Required',
})
}

res.status_code = 401
return res
return helpers.create_errored_response(res, 401)


def resource_not_found(func_url):
res = jsonify({
res = {
'error': {
'errors': [
{
@@ -62,9 +62,9 @@ def resource_not_found(func_url):
'code': 404,
'message': 'The resource \'' + urllib.unquote_plus(func_url) + '\' was not found'
}
})
res.status_code = 404
return res
}

return helpers.create_errored_response(res, 404)


def no_results_found(scope):
@@ -173,21 +173,10 @@ def listinstances(authorization, projectid, zone):
@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}
items = controllers.describe_items(
authorization, args, 'virtualmachine',
_cloudstack_virtual_machine_to_gce, name=instance, **kwargs)

if items:
helpers.create_response(items)
else:
function_route = url_for(
'getinstance',
projectid=projectid,
zone=zone,
instance=instance)
return errors.resource_not_found(function_route)
return controllers.get_item_with_name_or_error(authorization, zone, args, 'zone',
errors.resource_not_found(func_route))


@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['POST'])
@@ -52,17 +52,17 @@ def get_zone_names(authorization):
return zones


def _cloudstack_zone_to_gce(response_item):
def _cloudstack_zone_to_gce(cloudstack_response):
translate_zone_status = {
'Enabled': 'UP',
'Disabled': 'DOWN'
}
return ({
'kind': 'compute#zone',
'name': response_item['name'],
'description': response_item['name'],
'id': response_item['id'],
'status': translate_zone_status[str(response_item['allocationstate'])]
'name': cloudstack_response['name'],
'description': cloudstack_response['name'],
'id': cloudstack_response['id'],
'status': translate_zone_status[str(cloudstack_response['allocationstate'])]
})


@@ -89,15 +89,9 @@ def listzones(projectid, authorization):
@app.route('/compute/v1/projects/<projectid>/zones/<zone>', methods=['GET'])
@authentication.required
def getzone(projectid, authorization, zone):
response = get_zone_by_name(
authorization=authorization,
zone=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,
_cloudstack_zone_to_gce, **{})

if response:
return helpers.create_response(
data=_cloudstack_zone_to_gce(response)
)
else:
func_route = url_for('getzone', projectid=projectid, zone=zone)
return errors.resource_not_found(func_route)
@@ -29,17 +29,11 @@ def create_response(data):

return res

def create_errored_response(data, status_code):
res = jsonify(data)
res.status_code = status_code

def successful_response(**kwargs):
content = render_template(**kwargs)
response = make_response(content)
response.headers['Content-Type'] = 'application/json'
return _create_response(response, '200')


def _create_response(response, code):
response.status_code = int(code)
return response
return res


def get_filter(data):
@@ -52,8 +46,6 @@ def get_filter(data):
return filter




def get_root_url():
return 'https://' + \
app.config['GSTACK_BIND_ADDRESS'] + ':' + app.config['GSTACK_PORT']
@@ -16,3 +16,19 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from gstack.core import Service
from gstack.models.accesstoken import AccessToken
from gstack.models.refreshtoken import RefreshToken
from gstack.models.client import Client


class AccessTokenService(Service):
__model__ = AccessToken

class RefreshTokenService(Service):
__model__ = RefreshToken

class ClientService(Service):
__model__ = Client

@@ -4,12 +4,12 @@
from unittest import TestCase

import mock

import json

from gstack import app, configure_app
from gstack.helpers import read_file
from . import settings
from gstack.core import db
from .utils import FlaskTestCaseMixin

class GStackTestCase(TestCase):
@@ -22,6 +22,7 @@ class GStackAppTestCase(FlaskTestCaseMixin, GStackTestCase):
def _configure_app(self):
configure_app(settings=settings)


def _auth_example_user(self):
data = {}
data['code'] = 'hjrZryvgLYo3R833NkHHV8jYmxQhsD8TjKWzOm2f'
@@ -49,11 +50,13 @@ def setUp(self):
self._configure_app()
self.app = app
self.client = self.app.test_client()
self._auth_example_user()
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self._auth_example_user()


def tearDown(self):
super(GStackTestCase, self).tearDown()
db.drop_all()
self.app_context.pop()

0 comments on commit 823aee6

Please sign in to comment.