Commit e43253b9 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Sanitize mock calls and organize code rationally

Introduce two new modules:
compute.py: replacement functions will be held there, as well as
    the map of replacements
utils.py: common methods that can be used out of context
parent bf85138d
# Copyright (C) 2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from utils import empty_list_200
def snf_index(cls, req):
"""Synnefo: list servers"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'servers_get'
response = req.get_response(cls.app)
return cls.get_from_response(response, "servers", [])
def snf_get_flavors(cls, req):
"""Synnefo: list flavors"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'flavors_get'
req.environ['kwargs'] = {'detail': True}
response = req.get_response(cls.app)
return cls.get_from_response(response, 'flavors', [])
def snf_get_images(cls, req):
"""Synnefo: list images"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'images_get'
req.environ['kwargs'] = {'detail': True}
response = req.get_response(cls.app)
return cls.get_from_response(response, 'images', [])
function_map = {
'index': snf_index,
'get_flavors': snf_get_flavors,
'get_images': snf_get_images,
'get_floating_ip_pools': empty_list_200,
}
......@@ -86,42 +86,37 @@ def call_kamaki(environ, start_response, *args, **kwargs):
service_type = environ.pop('service_type')
method_name = environ.pop('method_name')
kwargs = environ.pop('kwargs', {})
print '\t', service_type, method_name, kwargs
endpoint = endpoints[service_type]
token = environ['HTTP_X_AUTH_TOKEN']
cls = client_classes[service_type]
client = cls(endpoint, token)
method = getattr(client, method_name)
if service_type in ('mock', ):
code, status, headers, body = {
'empty_list': (200, 'OK', {}, {'empty list': []}),
}.get(method_name, kwargs.get('req_args', (200, 'OK', {}, None)))
r = method(*args, **kwargs)
else: # Normal case
endpoint = endpoints[service_type]
token = environ['HTTP_X_AUTH_TOKEN']
cls = client_classes[service_type]
client = cls(endpoint, token)
method = getattr(client, method_name)
body = _stringify_json_values(r.json)
bodystr = json.dumps(body)
r = method(*args, **kwargs)
code, status, headers = r.status_code, r.status, r.headers
body = _stringify_json_values(r.json)
headers, key = r.headers, 'content-length'
if key in headers:
headers[key] = '{0}'.format(len(bodystr))
if body is not None:
bodystr = json.dumps(body)
headers['content-length'] = '{0}'.format(len(bodystr))
headers.setdefault('content-type', 'application/json')
start_response('{0} {1}'.format(r.status_code, r.status), headers.items())
start_response('{0} {1}'.format(code, status), headers.items())
return bodystr
def _stringify_json_values(data):
"""If a sinlge value is not a string, make it"""
if isinstance(data, dict):
return dict(
map(lambda (k, v): (k, _stringify_json_values(v)), data.items()))
# new_d = dict(data)
# for k, v in data.items():
# new_d[k] = _stringify_json_values(v)
# return new_d
return dict((k, _stringify_json_values(v)) for k, v in data.items())
if isinstance(data, list):
return map(_stringify_json_values, data)
return '{0}'.format(data) if data else data
# Set up OOI and run it
# factory = OCCIMiddleware.factory({'openstack_version': 'v2.1', })
# httpserver.serve(factory(call_kamaki))
# Copyright (C) 2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def patch_class_methods(cls, function_map):
"""Replace the methods of a class with different ones
:param cls: the class to patch its methods
:param function_map: {'class_method_name': replace_method, ...}
"""
for method_name, new_method in function_map.items():
setattr(cls, method_name, new_method)
def empty_list_200(cls, req):
"""return OK 200 and empty list"""
req.environ['service_type'] = 'mock'
req.environ['method_name'] = 'empty_list'
response = req.get_response(cls.app)
return cls.get_from_response(response, 'empty list', [])
......@@ -17,47 +17,11 @@ from ooi.wsgi import OCCIMiddleware
from ooi.api.helpers import OpenStackHelper
from soi.config import KEYSTONE_URL
from soi.synnefo import AstakosClient, AUTH_URL
from soi import utils, compute
from kamaki.clients import ClientError
def snf_index(cls, req):
"""Synnefo: list servers"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'servers_get'
response = req.get_response(cls.app)
return cls.get_from_response(response, "servers", [])
def snf_get_flavors(cls, req):
"""Synnefo: list flavors"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'flavors_get'
req.environ['kwargs'] = {'detail': True}
response = req.get_response(cls.app)
return cls.get_from_response(response, 'flavors', [])
def snf_get_images(cls, req):
"""Synnefo: list images"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'images_get'
req.environ['kwargs'] = {'detail': True}
response = req.get_response(cls.app)
return cls.get_from_response(response, 'images', [])
def snf_empty_list_200(cls, req):
"""return OK 200 and empty list"""
req.environ['service_type'] = 'compute'
req.environ['method_name'] = 'servers_get'
response = req.get_response(cls.app)
return cls.get_from_response(response, 'return_empty_list', [])
OpenStackHelper.index = snf_index
OpenStackHelper.get_flavors = snf_get_flavors
OpenStackHelper.get_images = snf_get_images
OpenStackHelper.get_floating_ip_pools = snf_empty_list_200
utils.patch_class_methods(OpenStackHelper, compute.function_map)
class SNFOCCIMiddleware(OCCIMiddleware):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment