Manually added OpenStack API code
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / base_openstack_dummy.py
1 from flask import Flask, request
2 from flask_restful import Api, Resource
3 import logging
4
5
6 class BaseOpenstackDummy(Resource):
7 """
8 This class is the base class for all openstack entrypoints of son-emu.
9 """
10
11 def __init__(self, listenip, port):
12 self.ip = listenip
13 self.port = port
14 self.compute = None
15 self.manage = None
16 self.playbook_file = '/tmp/son-emu-requests.log'
17 with open(self.playbook_file, 'w'):
18 pass
19
20 # setup Flask
21 self.app = Flask(__name__)
22 self.api = Api(self.app)
23
24 def _start_flask(self):
25 logging.info("Starting %s endpoint @ http://%s:%d" % (__name__, self.ip, self.port))
26 if self.app is not None:
27 self.app.before_request(self.dump_playbook)
28 self.app.run(self.ip, self.port, debug=True, use_reloader=False)
29
30 def dump_playbook(self):
31 with self.manage.lock:
32 with open(self.playbook_file, 'a') as logfile:
33 if len(request.data) > 0:
34 data = "# %s API\n" % str(self.__class__).split('.')[-1].rstrip('\'>')
35 data += "curl -X {type} -H \"Content-type: application/json\" -d '{data}' {url}".format(type=request.method,
36 data=request.data,
37 url=request.url)
38 logfile.write(data + "\n")