3 from flask
import Flask
, request
4 from flask_restful
import Resource
,Api
9 logging
.basicConfig(level
=logging
.INFO
)
14 class RestApiEndpoint(object):
17 Simple API endpoint that offers a REST
18 interface. This interface will be used by the
19 default command line client.
23 def __init__(self
, listenip
, port
):
28 self
.app
= Flask(__name__
)
29 self
.api
= Api(self
.app
)
32 self
.api
.add_resource(ComputeList
, "/restapi/compute/<dc_label>")
33 self
.api
.add_resource(ComputeStart
, "/restapi/compute/<dc_label>/<compute_name>/start")
34 self
.api
.add_resource(ComputeStop
, "/restapi/compute/<dc_label>/<compute_name>/stop")
35 self
.api
.add_resource(ComputeStatus
, "/restapi/compute/<dc_label>/<compute_name>")
36 self
.api
.add_resource(DatacenterList
, "/restapi/datacenter")
37 self
.api
.add_resource(DatacenterStatus
, "/restapi/datacenter/<dc_label>")
39 logging
.debug("Created API endpoint %s(%s:%d)" % (self
.__class
__.__name
__, self
.ip
, self
.port
))
42 def connectDatacenter(self
, dc
):
44 logging
.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc
.label
, self
.__class
__.__name
__, self
.ip
, self
.port
))
47 thread
= threading
.Thread(target
= self
._start
_flask
, args
=())
50 logging
.info("Started API endpoint @ http://%s:%d" % (self
.ip
, self
.port
))
53 def _start_flask(self
):
54 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
57 class ComputeStart(Resource
):
59 Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
60 :param dc_label: name of the DC
61 :param compute_name: compute container name
62 :param image: image name
63 :param command: command to execute
64 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
65 :return: networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
69 def put(self
, dc_label
, compute_name
):
70 logging
.debug("API CALL: compute start")
73 image
= json
.loads(request
.json
).get("image")
74 network
= json
.loads(request
.json
).get("network")
75 command
= json
.loads(request
.json
).get("docker_command")
76 c
= dcs
.get(dc_label
).startCompute(
77 compute_name
, image
= image
, command
= command
, network
= network
)
78 # return docker inspect dict
79 return c
. getStatus(), 200
80 except Exception as ex
:
81 logging
.exception("API error.")
82 return ex
.message
, 500
84 class ComputeStop(Resource
):
88 def get(self
, dc_label
, compute_name
):
89 logging
.debug("API CALL: compute stop")
91 return dcs
.get(dc_label
).stopCompute(compute_name
), 200
92 except Exception as ex
:
93 logging
.exception("API error.")
97 class ComputeList(Resource
):
101 def get(self
, dc_label
):
102 logging
.debug("API CALL: compute list")
104 if dc_label
== 'None':
105 # return list with all compute nodes in all DCs
107 for dc
in dcs
.itervalues():
108 all_containers
+= dc
.listCompute()
109 return [(c
.name
, c
.getStatus()) for c
in all_containers
], 200
111 # return list of compute nodes for specified DC
112 return [(c
.name
, c
.getStatus())
113 for c
in dcs
.get(dc_label
).listCompute()], 200
114 except Exception as ex
:
115 logging
.exception("API error.")
116 return ex
.message
, 500
119 class ComputeStatus(Resource
):
123 def get(self
, dc_label
, compute_name
):
125 logging
.debug("API CALL: compute list")
128 return dcs
.get(dc_label
).containers
.get(compute_name
).getStatus(), 200
129 except Exception as ex
:
130 logging
.exception("API error.")
131 return ex
.message
, 500
133 class DatacenterList(Resource
):
138 logging
.debug("API CALL: datacenter list")
140 return [d
.getStatus() for d
in dcs
.itervalues()], 200
141 except Exception as ex
:
142 logging
.exception("API error.")
143 return ex
.message
, 500
145 class DatacenterStatus(Resource
):
149 def get(self
, dc_label
):
150 logging
.debug("API CALL: datacenter status")
152 return dcs
.get(dc_label
).getStatus(), 200
153 except Exception as ex
:
154 logging
.exception("API error.")
155 return ex
.message
, 500