Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 1 | from grpc_client.main import GRPC_Client |
| 2 | from klein import Klein |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame^] | 3 | import os |
| 4 | from tosca.parser import TOSCA_Parser |
| 5 | from tosca.default import TOSCA_DEFS_DIR |
| 6 | import json |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 7 | |
| 8 | BANNER = """ |
| 9 | _ ______ _____ __________ _____ _________ |
| 10 | | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ | |
| 11 | | / / / /\__ \ / / / / / /\__ \/ / / /| | |
| 12 | / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ | |
| 13 | /_/|_\____//____/ /_/ \____//____/\____/_/ |_| |
| 14 | """ |
| 15 | |
| 16 | class TOSCA_WebServer: |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 17 | |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame^] | 18 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 19 | template_dir = os.path.join(current_dir, 'templates/') |
| 20 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 21 | 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 Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame^] | 33 | 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 Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 41 | |
| 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 53 | |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame^] | 54 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 60 | def __init__(self): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 61 | self.app.run('localhost', '9200') |