blob: 9b216d2bdde5526c15be28ae2fd4fe7203b58d4d [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
42 @app.route('/run', methods=['POST'])
43 def execute(self, request):
44 recipe = request.content.read()
45 headers = request.getAllHeaders()
46 username = headers['xos-username']
47 password = headers['xos-password']
48
49 d = GRPC_Client().create_secure_client(username, password, recipe)
50 self.parser = TOSCA_Parser(recipe, username, password)
51 d.addCallback(self.execute_tosca)
52 return d
Matteo Scandolo9ce18252017-06-22 10:48:25 -070053
Matteo Scandolo09d469c2017-07-07 11:46:48 -070054 @app.route("/custom_type/<name>")
55 def custom_type(self, request, name):
56 request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
57 custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
58 return custom_type
59
Matteo Scandolo9ce18252017-06-22 10:48:25 -070060 def __init__(self):
Matteo Scandolo21dde412017-07-11 18:54:12 -070061 self.app.run('localhost', '9200')