From dfc1460d7f4d0eae6412ad0c80ab9a9deb6e657d Mon Sep 17 00:00:00 2001 From: peusterm Date: Fri, 13 Jan 2017 08:22:45 +0100 Subject: [PATCH] Fix: Added CORS header to REST API to allow cross-domain calls from modern browsers. --- src/emuvim/api/rest/compute.py | 28 +++++++++++++++------------- src/emuvim/api/rest/monitor.py | 18 ++++++++++-------- src/emuvim/api/rest/network.py | 6 ++++-- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/emuvim/api/rest/compute.py b/src/emuvim/api/rest/compute.py index c3680a0..22e9d6d 100755 --- a/src/emuvim/api/rest/compute.py +++ b/src/emuvim/api/rest/compute.py @@ -32,6 +32,8 @@ import json logging.basicConfig(level=logging.INFO) +CORS_HEADER = {'Access-Control-Allow-Origin': '*'} + dcs = {} @@ -67,28 +69,28 @@ class Compute(Resource): c = dcs.get(dc_label).startCompute( compute_name, image=image, command=command, network=nw_list) # return docker inspect dict - return c.getStatus(), 200 + return c.getStatus(), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER def get(self, dc_label, compute_name): logging.debug("API CALL: compute status") try: - return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200 + return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER def delete(self, dc_label, compute_name): logging.debug("API CALL: compute stop") try: - return dcs.get(dc_label).stopCompute(compute_name), 200 + return dcs.get(dc_label).stopCompute(compute_name), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER def _parse_network(self, network_str): ''' @@ -121,14 +123,14 @@ class ComputeList(Resource): all_containers = [] for dc in dcs.itervalues(): all_containers += dc.listCompute() - return [(c.name, c.getStatus()) for c in all_containers], 200 + return [(c.name, c.getStatus()) for c in all_containers], 200, CORS_HEADER else: # return list of compute nodes for specified DC return [(c.name, c.getStatus()) - for c in dcs.get(dc_label).listCompute()], 200 + for c in dcs.get(dc_label).listCompute()], 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER class DatacenterList(Resource): @@ -137,10 +139,10 @@ class DatacenterList(Resource): def get(self): logging.debug("API CALL: datacenter list") try: - return [d.getStatus() for d in dcs.itervalues()], 200 + return [d.getStatus() for d in dcs.itervalues()], 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER class DatacenterStatus(Resource): @@ -149,7 +151,7 @@ class DatacenterStatus(Resource): def get(self, dc_label): logging.debug("API CALL: datacenter status") try: - return dcs.get(dc_label).getStatus(), 200 + return dcs.get(dc_label).getStatus(), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER diff --git a/src/emuvim/api/rest/monitor.py b/src/emuvim/api/rest/monitor.py index 45d9541..5558b87 100755 --- a/src/emuvim/api/rest/monitor.py +++ b/src/emuvim/api/rest/monitor.py @@ -39,6 +39,8 @@ import json logging.basicConfig(level=logging.INFO) +CORS_HEADER = {'Access-Control-Allow-Origin': '*'} + net = None @@ -58,20 +60,20 @@ class MonitorInterfaceAction(Resource): try: c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric) # return monitor message response - return str(c), 200 + return str(c), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER def delete(self, vnf_name, vnf_interface=None, metric='tx_packets'): logging.debug("REST CALL: stop monitor VNF interface") try: c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric) # return monitor message response - return str(c), 200 + return str(c), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER class MonitorFlowAction(Resource): @@ -90,17 +92,17 @@ class MonitorFlowAction(Resource): try: c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie) # return monitor message response - return str(c), 200 + return str(c), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 + return ex.message, 500, CORS_HEADER def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0): logging.debug("REST CALL: stop monitor VNF interface") try: c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie) # return monitor message response - return str(c), 200 + return str(c), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 \ No newline at end of file + return ex.message, 500, CORS_HEADER diff --git a/src/emuvim/api/rest/network.py b/src/emuvim/api/rest/network.py index 83fbde7..2745602 100755 --- a/src/emuvim/api/rest/network.py +++ b/src/emuvim/api/rest/network.py @@ -39,6 +39,8 @@ import json logging.basicConfig(level=logging.INFO) +CORS_HEADER = {'Access-Control-Allow-Origin': '*'} + net = None @@ -98,7 +100,7 @@ class NetworkAction(Resource): cookie=cookie, priority=priority) # return setChain response - return str(c), 200 + return str(c), 200, CORS_HEADER except Exception as ex: logging.exception("API error.") - return ex.message, 500 \ No newline at end of file + return ex.message, 500, CORS_HEADER -- 2.25.1