Commit 8a038ffe authored by Daniele Venzano's avatar Daniele Venzano

Pass the api_endpoint object to the REST API classes for easier type checking

parent e649a690
......@@ -48,18 +48,19 @@ def zoe_web_main() -> int:
app = Flask(__name__, static_url_path='/does-not-exist')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.register_blueprint(zoe_api.rest_api.api_init())
app.register_blueprint(zoe_api.web.web_init())
zoe_api.db_init.init()
config.api_endpoint = zoe_api.api_endpoint.APIEndpoint()
api_endpoint = zoe_api.api_endpoint.APIEndpoint()
config.api_endpoint = api_endpoint
app.register_blueprint(zoe_api.rest_api.api_init(api_endpoint))
app.register_blueprint(zoe_api.web.web_init())
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(args.listen_port, args.listen_address)
ioloop = IOLoop.instance()
retry_cb = PeriodicCallback(config.api_endpoint.retry_submit_error_executions, 30000)
retry_cb = PeriodicCallback(api_endpoint.retry_submit_error_executions, 30000)
retry_cb.start()
try:
......
......@@ -27,15 +27,15 @@ from zoe_api.rest_api.query import QueryAPI
API_PATH = '/api/' + ZOE_API_VERSION
def api_init() -> Blueprint:
def api_init(api_endpoint) -> Blueprint:
api_bp = Blueprint('api', __name__)
api = Api(api_bp, catch_all_404s=True)
api.add_resource(InfoAPI, API_PATH + '/info')
api.add_resource(ExecutionAPI, API_PATH + '/execution/<int:execution_id>')
api.add_resource(ExecutionCollectionAPI, API_PATH + '/execution')
api.add_resource(ServiceAPI, API_PATH + '/service/<int:container_id>')
api.add_resource(QueryAPI, API_PATH + '/query')
api.add_resource(InfoAPI, API_PATH + '/info', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ExecutionAPI, API_PATH + '/execution/<int:execution_id>', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ExecutionCollectionAPI, API_PATH + '/execution', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ServiceAPI, API_PATH + '/service/<int:container_id>', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(QueryAPI, API_PATH + '/query', resource_class_kwargs={'api_endpoint': api_endpoint})
return api_bp
......
......@@ -13,28 +13,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from flask_restful import Resource, request
from werkzeug.exceptions import BadRequest
import zoe_lib.exceptions
import zoe_lib.applications
import zoe_lib.sql_manager
from zoe_api.rest_api.utils import catch_exceptions, get_auth
import zoe_api.exceptions
import zoe_api.config as config
import zoe_api.api_endpoint
class ExecutionAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
@catch_exceptions
def get(self, execution_id):
uid, role = get_auth(request)
assert isinstance(config.api_endpoint, zoe_api.api_endpoint.APIEndpoint)
e = config.api_endpoint.execution_by_id(uid, role, execution_id)
e = self.api_endpoint.execution_by_id(uid, role, execution_id)
return e.serialize()
......@@ -48,8 +43,7 @@ class ExecutionAPI(Resource):
"""
uid, role = get_auth(request)
assert isinstance(config.api_endpoint, zoe_api.api_endpoint.APIEndpoint)
success, message = config.api_endpoint.execution_terminate(uid, role, execution_id)
success, message = self.api_endpoint.execution_terminate(uid, role, execution_id)
if not success:
raise zoe_api.exceptions.ZoeRestAPIException(message, 400)
......@@ -57,6 +51,9 @@ class ExecutionAPI(Resource):
class ExecutionCollectionAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
@catch_exceptions
def get(self):
"""
......@@ -66,8 +63,7 @@ class ExecutionCollectionAPI(Resource):
"""
uid, role = get_auth(request)
assert isinstance(config.api_endpoint, zoe_api.api_endpoint.APIEndpoint)
execs = config.api_endpoint.execution_list(uid, role)
execs = self.api_endpoint.execution_list(uid, role)
return [e.serialize() for e in execs]
@catch_exceptions
......@@ -78,8 +74,6 @@ class ExecutionCollectionAPI(Resource):
"""
uid, role = get_auth(request)
assert isinstance(config.api_endpoint, zoe_api.api_endpoint.APIEndpoint)
try:
data = request.get_json()
except BadRequest:
......@@ -88,6 +82,6 @@ class ExecutionCollectionAPI(Resource):
application_description = data['application']
exec_name = data['name']
new_id = config.api_endpoint.execution_start(uid, role, exec_name, application_description)
new_id = self.api_endpoint.execution_start(uid, role, exec_name, application_description)
return {'execution_id': new_id}, 201
......@@ -18,9 +18,13 @@ from flask_restful import Resource
from zoe_lib.version import ZOE_API_VERSION, ZOE_APPLICATION_FORMAT_VERSION, ZOE_VERSION
from zoe_master.config import get_conf
from zoe_api.rest_api.utils import catch_exceptions
import zoe_api.api_endpoint
class InfoAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
@catch_exceptions
def get(self):
ret = {
......
......@@ -17,16 +17,17 @@ from flask_restful import Resource, request
from werkzeug.exceptions import BadRequest
from zoe_api.exceptions import ZoeRestAPIException
import zoe_api.config as config
import zoe_api.api_endpoint
from zoe_api.rest_api.utils import catch_exceptions, get_auth
class QueryAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
@catch_exceptions
def post(self):
uid, role = get_auth(request)
assert isinstance(config.api_endpoint, zoe_api.api_endpoint.APIEndpoint)
try:
data = request.get_json()
......@@ -51,7 +52,7 @@ class QueryAPI(Resource):
elif what == 'execution':
if role != 'admin':
filters['user_id'] = uid
execs = config.api_endpoint.execution_list(uid, role, **filters)
execs = self.api_endpoint.execution_list(uid, role, **filters)
return [x.serialize() for x in execs]
else:
raise ZoeRestAPIException('unknown query {}'.format(what))
......
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