diff --git a/src/utils/python/rest_proxy/src/service.json b/src/utils/python/rest_proxy/src/service.json index 43be11fa..9aee5283 100644 --- a/src/utils/python/rest_proxy/src/service.json +++ b/src/utils/python/rest_proxy/src/service.json @@ -1,6 +1,6 @@ { "host": "0.0.0.0", - "port": 8080, + "port": 5000, "debug": true, "yaks": "127.0.0.1", "sysid": "0", diff --git a/src/utils/python/rest_proxy/src/service.py b/src/utils/python/rest_proxy/src/service.py index b0948b9f..af6cea16 100755 --- a/src/utils/python/rest_proxy/src/service.py +++ b/src/utils/python/rest_proxy/src/service.py @@ -4,6 +4,9 @@ import os from flask import Flask, request, abort, send_from_directory, url_for from fog05 import FIMAPI +from fog05_sdk.interfaces import Constants +from fog05_sdk.interfaces.FDU import FDU +from fog05_sdk.interfaces.InfraFDU import InfraFDU # conf = None @@ -14,51 +17,73 @@ @app.route('/') def index(): - return json.dumps({'name':'Eclipse fog05 REST Proxy', 'version':'0.0.1', 'system':conf.get('sysid'), 'tenant':conf.get('tenantid')}) + return json.dumps({'name':'Eclipse fog05 REST Proxy', 'version':'0.2.0', 'system':conf.get('sysid'), 'tenant':conf.get('tenantid')}) # NODE -@app.route('/node/list', methods=['GET']) +@app.route('/nodes/list', methods=['GET']) def node_list(): return json.dumps(fos_api.node.list()) -@app.route('/node/info/', methods=['GET']) +@app.route('/nodes//info', methods=['GET']) def node_info(uuid): return json.dumps(fos_api.node.info(uuid)) -@app.route('/node/status/', methods=['GET']) +@app.route('/node//status', methods=['GET']) def node_status(uuid): return json.dumps(fos_api.node.status(uuid)) -@app.route('/node/plugins/', methods=['GET']) +@app.route('/nodes//plugins/list', methods=['GET']) def node_plugins(uuid): return json.dumps(fos_api.node.plugins(uuid)) # PLUGIN -@app.route('/plugin/info//', methods=['GET']) +@app.route('/nodes//plugins//info', methods=['GET']) def plugin_info(pl_uuid, node_uuid): return json.dumps(fos_api.plugin.info(node_uuid, pl_uuid)) # NETWORK -@app.route('/network/add', methods=['POST']) -def network_add(): - data = request.data - if isinstance(data,bytes): - data = data.decode() - descriptor = json.loads(data) - return json.dumps({'result':fos_api.network.add_network(descriptor)}) - - -@app.route('/network/remove/', methods=['DELETE']) -def network_remove(net_id): - return json.dumps({'result':fos_api.network.remove_network(net_id)}) +@app.route('/networks//info', methods=['GET','PUT','DELETE']) +def network(net_uuid): + + method = request.method + if method == 'GET': + nets = fos_api.network.list() + n = [n for n in nets if n.get('uuid') == net_uuid] + if len(n) == 0: + return json.dumps({}) + return json.dumps(n[0]) + elif method == 'PUT': + descriptor = json.loads(request.data) + return json.dumps(fos_api.network.add_network(descriptor)) + elif method == 'DELETE': + return json.dumps(fos_api.network.remove_network(net_uuid)) + + + +@app.route('/nodes//network//info', methods=['PUT', 'DELETE']) +def network_node(node, net_id): + method = request.method + if method == 'PUT': + descriptor = json.loads(request.data) + try: + res = json.dumps(fos_api.network.add_network_to_node(descriptor, node)) + except ValueError as ve: + return json.dumps({'error':'{}'.format(ve)}) + return res + elif method == 'DELETE': + try: + res = json.dumps(fos_api.network.remove_network_from_node(net_id, node)) + except ValueError as ve: + return json.dumps({'error':'{}'.format(ve)}) + return res @app.route('/network/list', methods=['GET']) @@ -66,144 +91,170 @@ def network_list(): return json.dumps(fos_api.network.list()) -@app.route('/connection_point/add', methods=['POST']) -def cp_add(): - data = request.data - if isinstance(data,bytes): - data = data.decode() - descriptor = json.loads(data) - return json.dumps({'result':fos_api.network.add_connection_point(descriptor)}) +# @app.route('/connection_point/add', methods=['POST']) +# def cp_add(): +# data = request.data +# if isinstance(data,bytes): +# data = data.decode() +# descriptor = json.loads(data) +# return json.dumps({'result':fos_api.network.add_connection_point(descriptor)}) -@app.route('/connection_point/remove/', methods=['DELETE']) -def cp_remove(cp_id): - return json.dumps({'result':fos_api.network.delete_connection_point(cp_id)}) +# @app.route('/connection_point/remove/', methods=['DELETE']) +# def cp_remove(cp_id): +# return json.dumps({'result':fos_api.network.delete_connection_point(cp_id)}) # FDU -@app.route('/fdu/onboard', methods=['POST']) -def fdu_onboard(): - data = request.data - if isinstance(data,bytes): - data = data.decode() - descriptor = json.loads(data) - return json.dumps({'result':fos_api.fdu.onboard(descriptor)}) - - -@app.route('/fdu/offload/', methods=['DELETE']) -def fdu_offload(fdu_id): - return json.dumps({'result':fos_api.fdu.offload(fdu_id)}) - - -@app.route('/fdu/define//', methods=['POST']) -def fdu_define(fdu_id, node_id): +@app.route('/fdu//info', methods=['GET','DELETE','PUT']) +def fdu(fduid): + + # try: + # res = json.dumps(fos_api.network.remove_network_from_node(net_id, node)) + # except ValueError as ve: + # return json.dumps({'error':'{}'.format(ve)}) + # return res + method = request.method + if method == 'GET': + try: + res = fos_api.fdu.info(fduid) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return json.dumps(res.to_json()) + elif method == 'PUT': + data = request.data + if isinstance(data,bytes): + data = data.decode() + descriptor = json.loads(data) + try: + res = json.dumps(fos_api.fdu.onboard(descriptor)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + elif method == 'DELETE': + try: + res = json.dumps(fos_api.fdu.offload(fduid)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + +@app.route('/fdu/{}/schedule', methods=['PUT']) +def fdu_define(fduid): try: - return json.dumps({'result':fos_api.fdu.define(fdu_id, node_id)}) - except ValueError as ve: + res = fos_api.fdu.define(fduid) + except Exception as ve: return json.dumps({'error':'{}'.format(ve)}) + return json.dumps(res.to_json()) -@app.route('/fdu/undefine/', methods=['DELETE']) -def fdu_undefine(instanceid): - return json.dumps({'result':fos_api.fdu.undefine(instanceid)}) - - -@app.route('/fdu/configure/', methods=['POST']) -def fdu_configure(instanceid): +@app.route('/nodes///define', methods=['PUT']) +def fdu_define_node(node, fduid): try: - return json.dumps({'result':fos_api.fdu.configure(instanceid)}) - except ValueError as ve: + res = fos_api.fdu.define(fduid, node) + except Exception as ve: return json.dumps({'error':'{}'.format(ve)}) + return json.dumps(res.to_json()) -@app.route('/fdu/clean/', methods=['POST']) -def fdu_clean(instanceid): - try: - return json.dumps({'result':fos_api.fdu.clean(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) +@app.route('/fdu/instances//info', methods=['GET','DELETE']) +def fdu_instance(instance): + method = request.method + if method == 'GET': + try: + res = fos_api.fdu.instance_info(instance) + except Exception as ve: + return json.dumps({'error':'{}'.format(ve)}) + return json.dumps(res.to_json()) + elif method == 'DELETE': + try: + res = json.dumps(fos_api.fdu.undefine(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/start/', methods=['POST']) -def fdu_run(instanceid): - try: - return json.dumps({'result':fos_api.fdu.start(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) -@app.route('/fdu/stop/', methods=['POST']) -def fdu_stop(instanceid): - try: - return json.dumps({'result':fos_api.fdu.stop(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) -@app.route('/fdu/pause/', methods=['POST']) -def fdu_pause(instanceid): +@app.route('/fdu/instances//configure', methods=['POST']) +def fdu_instance_configure(instance): try: - return json.dumps({'result':fos_api.fdu.pause(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) + res = json.dumps(fos_api.fdu.configure(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/resume/', methods=['POST']) -def fdu_resume(instanceid): +@app.route('/fdu/instances//clean', methods=['POST']) +def fdu_instance_clean(instance): try: - return json.dumps({'result':fos_api.fdu.resume(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) - + res = json.dumps(fos_api.fdu.clean(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/migrate//', methods=['POST']) -def fdu_migrate(instanceid ,destinationid): +@app.route('/fdu/instances//start', methods=['POST']) +def fdu_instance_start(instance): try: - return json.dumps({'result':fos_api.fdu.migrate(instanceid, destinationid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) + env = request.data + if isinstance(env,bytes): + env = env.decode() + res = json.dumps(fos_api.fdu.start(instance, env)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/instantiate//', methods=['POST']) -def fdu_instantiate(fdu_id, node_id): +@app.route('/fdu/instances//stop', methods=['POST']) +def fdu_instance_stop(instance): try: - return json.dumps({'result':fos_api.fdu.instantiate(fdu_id, node_id)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) - + res = json.dumps(fos_api.fdu.stop(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/terminate/', methods=['POST']) -def fdu_terminate(instanceid): +@app.route('/fdu/instances//pause', methods=['POST']) +def fdu_instance_pause(instance): try: - return json.dumps({'result':fos_api.fdu.terminate(instanceid)}) - except ValueError as ve: - return json.dumps({'error':'{}'.format(ve)}) + res = json.dumps(fos_api.fdu.pause(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res +@app.route('/fdu/instances//resume', methods=['POST']) +def fdu_instance_resume(instance): + try: + res = json.dumps(fos_api.fdu.resume(instance)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/get_nodes/', methods=['GET']) -def fdu_get_nodes(fdu_id): - return json.dumps(fos_api.fdu.get_nodes(fdu_id)) +@app.route('/fdu/instances//migrate/', methods=['POST']) +def fdu_instance_resume(instance, destination): + try: + res = json.dumps(fos_api.fdu.migrate(instance, destination)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res -@app.route('/fdu/list_node/', methods=['GET']) -def fdu_node_list(node_id): - return json.dumps(fos_api.fdu.list_node(node_id)) +@app.route('/fdu//nodes/list', methods=['GET']) +def fdu_get_nodes(fdu): + return json.dumps(fos_api.fdu.get_nodes(fdu)) -@app.route('/fdu/info/', methods=['GET']) -def fdu_info(fdu_id): - return json.dumps(fos_api.fdu.info(fdu_id)) +@app.route('/nodes//fdu/list', methods=['GET']) +def fdu_node_list(node): + return json.dumps(fos_api.fdu.list_node(node)) -@app.route('/fdu/instance_info/', methods=['GET']) -def fdu_instance_info(instanceid): - return json.dumps(fos_api.fdu.instance_info(instanceid)) -@app.route('/fdu/instance_list/', methods=['GET']) -def fdu_instance_list(fdu_id): - return json.dumps(fos_api.fdu.instance_list(fdu_id)) +@app.route('/fdu//instances/list', methods=['GET']) +def fdu_instance_list(fdu): + return json.dumps(fos_api.fdu.instance_list(fdu)) @app.route('/fdu/list', methods=['GET']) @@ -212,84 +263,72 @@ def fdu_list(): # IMAGE -@app.route('/image/add', methods=['POST']) -def image_add(): - if 'descriptor' not in request.files: - abort(403) - if 'image' not in request.files: - abort(403) - - desc_file = request.files['descriptor'] - desc_filename = desc_file.filename - desc_path = os.path.join(conf.get('image_path'), desc_filename) - desc_file.save(desc_path) - descriptor = json.loads(read_file(desc_path)) - - img_file = request.files['image'] - img_filename = img_file.filename - img_path = os.path.join(conf.get('image_path'), img_filename) - img_file.save(img_path) - img_dict.update({descriptor.get('uuid'):img_filename}) - uri = 'http://{}:{}{}'.format( conf['host'], conf['port'],url_for('get_image_file',fname=img_filename)) - descriptor.update({'uri':uri}) - return json.dumps({'result':fos_api.image.add(descriptor)}) - - -@app.route('/image/', methods=['GET']) -def image_get(img_id): - return json.dumps(fos_api.image.get(img_id)) - - -@app.route('/image/list', methods=['GET']) +@app.route('/images//info', methods=['GET','PUT','DELETE']) +def image(imgid): + + method = request.method + if method == 'GET': + try: + return json.dumps(fos_api.image.get(imgid)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + elif method == 'PUT': + data = request.data + if isinstance(data,bytes): + data = data.decode() + descriptor = json.loads(data) + try: + res = json.dumps(fos_api.image.add(descriptor)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + elif method == 'DELETE': + try: + res = json.dumps(fos_api.fdu.remove(imgid)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + + +@app.route('/images/list', methods=['GET']) def image_list(): return json.dumps(fos_api.image.list()) -@app.route('/image/remove/', methods=['DELETE']) -def image_remove(img_id): - if img_id not in img_dict: - abort(404) - else: - f_name = img_dict.pop(img_id) - if os.path.isfile(os.path.join(conf.get('image_path'), f_name)): - os.remove(os.path.join(conf.get('image_path'), f_name)) - if os.path.isfile(os.path.join(conf.get('image_path'), '{}.json'.format(img_id))): - os.remove(os.path.join(conf.get('image_path'), '{}.json'.format(img_id))) - return json.dumps({'result':fos_api.image.remove(img_id)}) - - -@app.route('/image/file/', methods=['GET']) -def get_image_file(fname): - return send_from_directory(conf.get('image_path'), fname) - #FLAVOR - -@app.route('/flavor/add', methods=['POST']) -def flavor_add(): - data = request.data - if isinstance(data,bytes): - data = data.decode() - descriptor = json.loads(data) - return json.dumps({'result':fos_api.flavor.add(descriptor)}) - - -@app.route('/flavor/', methods=['GET']) -def flavor_get(flv_id): - return json.dumps(fos_api.flavor.get(flv_id)) - - -@app.route('/flavor/list', methods=['GET']) +@app.route('/flavors//info', methods=['GET','PUT','DELETE']) +def flavor(flv_id): + + method = request.method + if method == 'GET': + try: + return json.dumps(fos_api.flavor.get(flv_id)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + elif method == 'PUT': + data = request.data + if isinstance(data,bytes): + data = data.decode() + descriptor = json.loads(data) + try: + res = json.dumps(fos_api.flavor.add(descriptor)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + elif method == 'DELETE': + try: + res = json.dumps(fos_api.fdu.flavor(flv_id)) + except Exception as e: + return json.dumps({'error':'{}'.format(e)}) + return res + + +@app.route('/flavors/list', methods=['GET']) def flavor_list(): return json.dumps(fos_api.flavor.list()) -@app.route('/flavor/', methods=['DELETE']) -def flavor_remove(flv_id): - fos_api.flavor.remove(flv_id) - return json.dumps({'result':True}) - - # MAIN and UTILS def read_file(file_path):