| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 1 | import logging |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 2 | from flask_restful import Resource |
| 3 | from flask import request |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 4 | import json |
| 5 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 6 | logging.basicConfig(level=logging.INFO) |
| 7 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 8 | dcs = {} |
| 9 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 10 | class ComputeStart(Resource): |
| 11 | """ |
| 12 | Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments) |
| 13 | :param dc_label: name of the DC |
| 14 | :param compute_name: compute container name |
| 15 | :param image: image name |
| 16 | :param command: command to execute |
| 17 | :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),... |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 18 | example networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"}) |
| 19 | :return: docker inspect dict of deployed docker |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 20 | """ |
| 21 | global dcs |
| 22 | |
| 23 | def put(self, dc_label, compute_name): |
| 24 | logging.debug("API CALL: compute start") |
| 25 | try: |
| 26 | |
| 27 | image = json.loads(request.json).get("image") |
| 28 | network = json.loads(request.json).get("network") |
| 29 | command = json.loads(request.json).get("docker_command") |
| 30 | c = dcs.get(dc_label).startCompute( |
| 31 | compute_name, image= image, command= command, network= network) |
| 32 | # return docker inspect dict |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 33 | return c.getStatus(), 200 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 34 | except Exception as ex: |
| 35 | logging.exception("API error.") |
| 36 | return ex.message, 500 |
| 37 | |
| 38 | class ComputeStop(Resource): |
| 39 | |
| 40 | global dcs |
| 41 | |
| 42 | def get(self, dc_label, compute_name): |
| 43 | logging.debug("API CALL: compute stop") |
| 44 | try: |
| 45 | return dcs.get(dc_label).stopCompute(compute_name), 200 |
| 46 | except Exception as ex: |
| 47 | logging.exception("API error.") |
| 48 | return ex.message,500 |
| 49 | |
| 50 | |
| 51 | class ComputeList(Resource): |
| 52 | |
| 53 | global dcs |
| 54 | |
| 55 | def get(self, dc_label): |
| 56 | logging.debug("API CALL: compute list") |
| 57 | try: |
| 58 | if dc_label == 'None': |
| 59 | # return list with all compute nodes in all DCs |
| 60 | all_containers = [] |
| 61 | for dc in dcs.itervalues(): |
| 62 | all_containers += dc.listCompute() |
| 63 | return [(c.name, c.getStatus()) for c in all_containers], 200 |
| 64 | else: |
| 65 | # return list of compute nodes for specified DC |
| 66 | return [(c.name, c.getStatus()) |
| 67 | for c in dcs.get(dc_label).listCompute()], 200 |
| 68 | except Exception as ex: |
| 69 | logging.exception("API error.") |
| 70 | return ex.message, 500 |
| 71 | |
| 72 | |
| 73 | class ComputeStatus(Resource): |
| 74 | |
| 75 | global dcs |
| 76 | |
| 77 | def get(self, dc_label, compute_name): |
| 78 | |
| 79 | logging.debug("API CALL: compute list") |
| 80 | |
| 81 | try: |
| 82 | return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200 |
| 83 | except Exception as ex: |
| 84 | logging.exception("API error.") |
| 85 | return ex.message, 500 |
| 86 | |
| 87 | class DatacenterList(Resource): |
| 88 | |
| 89 | global dcs |
| 90 | |
| 91 | def get(self): |
| 92 | logging.debug("API CALL: datacenter list") |
| 93 | try: |
| 94 | return [d.getStatus() for d in dcs.itervalues()], 200 |
| 95 | except Exception as ex: |
| 96 | logging.exception("API error.") |
| 97 | return ex.message, 500 |
| 98 | |
| 99 | class DatacenterStatus(Resource): |
| 100 | |
| 101 | global dcs |
| 102 | |
| 103 | def get(self, dc_label): |
| 104 | logging.debug("API CALL: datacenter status") |
| 105 | try: |
| 106 | return dcs.get(dc_label).getStatus(), 200 |
| 107 | except Exception as ex: |
| 108 | logging.exception("API error.") |
| 109 | return ex.message, 500 |