2 from flask_restful
import Resource
3 from flask
import request
6 logging
.basicConfig(level
=logging
.INFO
)
10 class ComputeStart(Resource
):
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),...
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
23 def put(self
, dc_label
, compute_name
):
24 logging
.debug("API CALL: compute start")
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
33 return c
.getStatus(), 200
34 except Exception as ex
:
35 logging
.exception("API error.")
36 return ex
.message
, 500
38 class ComputeStop(Resource
):
42 def get(self
, dc_label
, compute_name
):
43 logging
.debug("API CALL: compute stop")
45 return dcs
.get(dc_label
).stopCompute(compute_name
), 200
46 except Exception as ex
:
47 logging
.exception("API error.")
51 class ComputeList(Resource
):
55 def get(self
, dc_label
):
56 logging
.debug("API CALL: compute list")
58 if dc_label
== 'None':
59 # return list with all compute nodes in all DCs
61 for dc
in dcs
.itervalues():
62 all_containers
+= dc
.listCompute()
63 return [(c
.name
, c
.getStatus()) for c
in all_containers
], 200
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
73 class ComputeStatus(Resource
):
77 def get(self
, dc_label
, compute_name
):
79 logging
.debug("API CALL: compute list")
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
87 class DatacenterList(Resource
):
92 logging
.debug("API CALL: datacenter list")
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
99 class DatacenterStatus(Resource
):
103 def get(self
, dc_label
):
104 logging
.debug("API CALL: datacenter status")
106 return dcs
.get(dc_label
).getStatus(), 200
107 except Exception as ex
:
108 logging
.exception("API error.")
109 return ex
.message
, 500