blob: efeae13198e9fbd0428a80f93a374e807b6c2357 [file] [log] [blame]
Matteo Scandolo21dde412017-07-11 18:54:12 -07001from grpc_client.main import GRPC_Client
2from klein import Klein
Matteo Scandolo09d469c2017-07-07 11:46:48 -07003import os
4from tosca.parser import TOSCA_Parser
5from tosca.default import TOSCA_DEFS_DIR
6import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -07007
8BANNER = """
9 _ ______ _____ __________ _____ _________
10 | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ |
11 | / / / /\__ \ / / / / / /\__ \/ / / /| |
12 / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ |
13/_/|_\____//____/ /_/ \____//____/\____/_/ |_|
14"""
15
16class TOSCA_WebServer:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070017
Matteo Scandolo09d469c2017-07-07 11:46:48 -070018 current_dir = os.path.dirname(os.path.realpath(__file__))
19 template_dir = os.path.join(current_dir, 'templates/')
20
Matteo Scandolo21dde412017-07-11 18:54:12 -070021 app = Klein()
22
23 def execute_tosca(self, recipe):
24 try:
25 self.parser.execute()
26 response_text = "Created models: %s" % str(self.parser.ordered_models_name)
27 return response_text
28 except Exception, e:
29 return e.message
30
31 @app.route('/', methods=['GET'])
32 def index(self, request):
Matteo Scandolo09d469c2017-07-07 11:46:48 -070033 request.responseHeaders.addRawHeader(b"content-type", b"application/json")
34 tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')]
35
36 response = {}
37 for d in tosca_defs:
38 name = d.replace('.yaml', '')
39 response[name] = "/custom_type/%s" % name
40 return json.dumps(response)
Matteo Scandolo21dde412017-07-11 18:54:12 -070041
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070042 @app.route("/custom_type/<name>")
43 def custom_type(self, request, name):
44 request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
45 custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
46 return custom_type
47
Matteo Scandolo21dde412017-07-11 18:54:12 -070048 @app.route('/run', methods=['POST'])
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070049 def run(self, request):
Matteo Scandolo21dde412017-07-11 18:54:12 -070050 recipe = request.content.read()
51 headers = request.getAllHeaders()
52 username = headers['xos-username']
53 password = headers['xos-password']
54
55 d = GRPC_Client().create_secure_client(username, password, recipe)
56 self.parser = TOSCA_Parser(recipe, username, password)
57 d.addCallback(self.execute_tosca)
58 return d
Matteo Scandolo9ce18252017-06-22 10:48:25 -070059
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070060 @app.route('/delete', methods=['POST'])
61 def delete(self, request):
62 recipe = request.content.read()
63 headers = request.getAllHeaders()
64 username = headers['xos-username']
65 password = headers['xos-password']
66
67 d = GRPC_Client().create_secure_client(username, password, recipe)
68 self.parser = TOSCA_Parser(recipe, username, password, delete=True)
69 d.addCallback(self.execute_tosca)
70 return d
Matteo Scandolo09d469c2017-07-07 11:46:48 -070071
Matteo Scandolo9ce18252017-06-22 10:48:25 -070072 def __init__(self):
Matteo Scandolo5c0af1b2017-07-05 14:51:21 -070073 self.app.run('0.0.0.0', '9102')