From a9dd901a96789fea8b1f4dd6a11b6fdcc01da830 Mon Sep 17 00:00:00 2001 From: hadik3r Date: Tue, 9 Aug 2016 10:51:13 +0200 Subject: [PATCH] change compute api start/stop to put/delete --- src/emuvim/api/rest/compute.py | 86 ++++++++++------------- src/emuvim/api/rest/rest_api_endpoint.py | 23 +++--- src/emuvim/cli/rest/compute.py | 36 +++++----- src/emuvim/test/unittests/test_restapi.py | 6 +- 4 files changed, 69 insertions(+), 82 deletions(-) diff --git a/src/emuvim/api/rest/compute.py b/src/emuvim/api/rest/compute.py index 9f0516b..78b49e3 100755 --- a/src/emuvim/api/rest/compute.py +++ b/src/emuvim/api/rest/compute.py @@ -34,7 +34,8 @@ logging.basicConfig(level=logging.INFO) dcs = {} -class ComputeStart(Resource): + +class Compute(Resource): """ Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments) :param dc_label: name of the DC @@ -48,28 +49,47 @@ class ComputeStart(Resource): global dcs def put(self, dc_label, compute_name): - logging.debug("API CALL: compute start") - try: - #check if json data is a dict - data = request.json - if data is None: - data = {} - elif type(data) is not dict: - data = json.loads(request.json) - - network = data.get("network") - nw_list = self._parse_network(network) - image = data.get("image") - command = data.get("docker_command") + # check if json data is a dict + data = request.json + if data is None: + data = {} + elif type(data) is not dict: + data = json.loads(request.json) + + network = data.get("network") + nw_list = self._parse_network(network) + image = data.get("image") + command = data.get("docker_command") + + try: + logging.debug("API CALL: compute start") c = dcs.get(dc_label).startCompute( - compute_name, image= image, command= command, network= nw_list) + compute_name, image=image, command=command, network=nw_list) # return docker inspect dict return c.getStatus(), 200 except Exception as ex: logging.exception("API error.") return ex.message, 500 + 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 + except Exception as ex: + logging.exception("API error.") + return ex.message, 500 + + def delete(self, dc_label, compute_name): + logging.debug("API CALL: compute stop") + try: + return dcs.get(dc_label).stopCompute(compute_name), 200 + except Exception as ex: + logging.exception("API error.") + return ex.message, 500 + def _parse_network(self, network_str): ''' parse the options for all network interfaces of the vnf @@ -79,7 +99,7 @@ class ComputeStart(Resource): nw_list = list() # TODO make this more robust with regex check - if network_str is None : + if network_str is None: return nw_list networks = network_str[1:-1].split('),(') @@ -89,21 +109,8 @@ class ComputeStart(Resource): return nw_list -class ComputeStop(Resource): - - global dcs - - def get(self, dc_label, compute_name): - logging.debug("API CALL: compute stop") - try: - return dcs.get(dc_label).stopCompute(compute_name), 200 - except Exception as ex: - logging.exception("API error.") - return ex.message,500 - class ComputeList(Resource): - global dcs def get(self, dc_label): @@ -118,28 +125,13 @@ class ComputeList(Resource): 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 except Exception as ex: logging.exception("API error.") return ex.message, 500 -class ComputeStatus(Resource): - - global dcs - - def get(self, dc_label, compute_name): - - logging.debug("API CALL: compute list") - - try: - return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200 - except Exception as ex: - logging.exception("API error.") - return ex.message, 500 - class DatacenterList(Resource): - global dcs def get(self): @@ -150,8 +142,8 @@ class DatacenterList(Resource): logging.exception("API error.") return ex.message, 500 -class DatacenterStatus(Resource): +class DatacenterStatus(Resource): global dcs def get(self, dc_label): @@ -161,5 +153,3 @@ class DatacenterStatus(Resource): except Exception as ex: logging.exception("API error.") return ex.message, 500 - - diff --git a/src/emuvim/api/rest/rest_api_endpoint.py b/src/emuvim/api/rest/rest_api_endpoint.py index 536ed7a..b586915 100755 --- a/src/emuvim/api/rest/rest_api_endpoint.py +++ b/src/emuvim/api/rest/rest_api_endpoint.py @@ -32,7 +32,7 @@ from flask_restful import Api # need to import total module to set its global variable dcs import compute -from compute import dcs, ComputeList, ComputeStart, ComputeStatus, ComputeStop, DatacenterList, DatacenterStatus +from compute import dcs, ComputeList, Compute, DatacenterList, DatacenterStatus # need to import total module to set its global variable net import network @@ -44,9 +44,7 @@ from monitor import MonitorInterfaceAction, MonitorFlowAction logging.basicConfig(level=logging.INFO) - class RestApiEndpoint(object): - """ Simple API endpoint that offers a REST interface. This interface will be used by the @@ -62,14 +60,13 @@ class RestApiEndpoint(object): self.api = Api(self.app) # setup endpoints + self.api.add_resource(Compute, "/restapi/compute//") self.api.add_resource(ComputeList, "/restapi/compute/") - self.api.add_resource(ComputeStart, "/restapi/compute///start") - self.api.add_resource(ComputeStop, "/restapi/compute///stop") - self.api.add_resource(ComputeStatus, "/restapi/compute//") - self.api.add_resource(DatacenterList, "/restapi/datacenter") + self.api.add_resource(DatacenterStatus, "/restapi/datacenter/") + self.api.add_resource(DatacenterList, "/restapi/datacenter") - self.api.add_resource(NetworkAction, "/restapi/network//",) + self.api.add_resource(NetworkAction, "/restapi/network//", ) self.api.add_resource(MonitorInterfaceAction, "/restapi/monitor//", @@ -80,13 +77,12 @@ class RestApiEndpoint(object): logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port)) - def connectDatacenter(self, dc): compute.dcs[dc.label] = dc - logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port)) + logging.info( + "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port)) def connectDCNetwork(self, DCnetwork): - network.net = DCnetwork monitor.net = DCnetwork @@ -94,11 +90,10 @@ class RestApiEndpoint(object): self.__class__.__name__, self.ip, self.port)) def start(self): - thread = threading.Thread(target= self._start_flask, args=()) + thread = threading.Thread(target=self._start_flask, args=()) thread.daemon = True thread.start() logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port)) - def _start_flask(self): - self.app.run(self.ip, self.port, debug=True, use_reloader=False) \ No newline at end of file + self.app.run(self.ip, self.port, debug=True, use_reloader=False) diff --git a/src/emuvim/cli/rest/compute.py b/src/emuvim/cli/rest/compute.py index 3a719cc..cdc0b04 100755 --- a/src/emuvim/cli/rest/compute.py +++ b/src/emuvim/cli/rest/compute.py @@ -25,7 +25,7 @@ the Horizon 2020 and 5G-PPP programmes. The authors would like to acknowledge the contributions of their colleagues of the SONATA partner consortium (www.sonata-nfv.eu). """ -from requests import get,put +from requests import get, put, delete from tabulate import tabulate import pprint import argparse @@ -33,8 +33,8 @@ import json pp = pprint.PrettyPrinter(indent=4) -class RestApiClient(): +class RestApiClient(): def __init__(self): self.cmds = {} @@ -47,29 +47,29 @@ class RestApiClient(): def start(self, args): - req = {'image':args.get("image"), - 'command':args.get("docker_command"), - 'network':args.get("network")} + req = {'image': args.get("image"), + 'command': args.get("docker_command"), + 'network': args.get("network")} - response = put("%s/restapi/compute/%s/%s/start" % + response = put("%s/restapi/compute/%s/%s" % (args.get("endpoint"), args.get("datacenter"), args.get("name")), - json = req) + json=req) pp.pprint(response.json()) def stop(self, args): - response = get("%s/restapi/compute/%s/%s/stop" % - (args.get("endpoint"), - args.get("datacenter"), - args.get("name"))) + response = delete("%s/restapi/compute/%s/%s" % + (args.get("endpoint"), + args.get("datacenter"), + args.get("name"))) pp.pprint(response.json()) - def list(self,args): + def list(self, args): - list = get('%s/restapi/compute/%s' % (args.get("endpoint"),args.get('datacenter'))).json() + list = get('%s/restapi/compute/%s' % (args.get("endpoint"), args.get('datacenter'))).json() table = [] for c in list: @@ -98,16 +98,17 @@ class RestApiClient(): "Status"] print(tabulate(table, headers=headers, tablefmt="grid")) - def status(self,args): + def status(self, args): list = get("%s/restapi/compute/%s/%s" % (args.get("endpoint"), args.get("datacenter"), args.get("name"))).json() + pp.pprint(list) -parser = argparse.ArgumentParser(description='son-emu datacenter') +parser = argparse.ArgumentParser(description='son-emu compute') parser.add_argument( "command", choices=['start', 'stop', 'list', 'status'], @@ -119,7 +120,7 @@ parser.add_argument( "--name", "-n", dest="name", help="Name of compute instance e.g. 'vnf1'.") parser.add_argument( - "--image","-i", dest="image", + "--image", "-i", dest="image", help="Name of container image to be used e.g. 'ubuntu:trusty'") parser.add_argument( "--dcmd", "-c", dest="docker_command", @@ -133,7 +134,8 @@ parser.add_argument( default="http://127.0.0.1:5001", help="UUID of the plugin to be manipulated.") + def main(argv): args = vars(parser.parse_args(argv)) c = RestApiClient() - c.execute_command(args) \ No newline at end of file + c.execute_command(args) diff --git a/src/emuvim/test/unittests/test_restapi.py b/src/emuvim/test/unittests/test_restapi.py index e5a2b96..ea0b693 100755 --- a/src/emuvim/test/unittests/test_restapi.py +++ b/src/emuvim/test/unittests/test_restapi.py @@ -37,13 +37,13 @@ import subprocess from emuvim.dcemulator.node import EmulatorCompute import ast -class testRestApi( SimpleTestTopology ): + +class testRestApi(SimpleTestTopology): """ Tests to check the REST API endpoints of the emulator. """ def testRestApi(self): - # create network self.createNet(nswitches=0, ndatacenter=2, nhosts=2, ndockers=0) @@ -121,7 +121,7 @@ class testRestApi( SimpleTestTopology ): print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') subprocess.call("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True) output = subprocess.check_output("son-emu-cli compute status -d datacenter0 -n vnf1", shell=True) - output= ast.literal_eval(output) + output = ast.literal_eval(output) # check compute status result self.assertTrue(output["name"] == "vnf1") -- 2.17.1