blob: 271b1b2082a11d159ef319500ce266cc94515831 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070016from xosconfig import Config
17from multistructlog import create_logger
18log = create_logger(Config().get('logging'))
Matteo Scandolo920e8fd2017-08-08 13:05:24 -070019
Matteo Scandolo21dde412017-07-11 18:54:12 -070020from grpc_client.main import GRPC_Client
21from klein import Klein
Matteo Scandolo09d469c2017-07-07 11:46:48 -070022import os
23from tosca.parser import TOSCA_Parser
24from tosca.default import TOSCA_DEFS_DIR
25import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -070026
27BANNER = """
28 _ ______ _____ __________ _____ _________
29 | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ |
30 | / / / /\__ \ / / / / / /\__ \/ / / /| |
31 / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ |
32/_/|_\____//____/ /_/ \____//____/\____/_/ |_|
33"""
34
35class TOSCA_WebServer:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070036
Matteo Scandolo09d469c2017-07-07 11:46:48 -070037 current_dir = os.path.dirname(os.path.realpath(__file__))
38 template_dir = os.path.join(current_dir, 'templates/')
39
Matteo Scandolo21dde412017-07-11 18:54:12 -070040 app = Klein()
41
Andy Bavier571489f2019-02-05 19:23:51 -070042 def execute_tosca(self, parser):
43 parser.execute()
44 if parser.delete:
45 response_text = "Deleted models: %s" % str(parser.ordered_models_name)
Matteo Scandoloc4680752018-05-17 12:16:30 -070046 else:
Andy Bavier571489f2019-02-05 19:23:51 -070047 response_text = "Created models: %s" % str(parser.ordered_models_name)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070048 return response_text
49
50 def errorCallback(self, failure, request):
51 request.setResponseCode(500)
Matteo Scandolo21dde412017-07-11 18:54:12 -070052 try:
Matteo Scandolob2eff2c2018-03-02 12:58:10 -080053 f = failure.getErrorMessage()
Scott Baker30c3d752018-10-04 13:08:41 -070054 if f.startswith("XOSPermissionDenied"):
55 request.setResponseCode(401)
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070056 log.info("[XOS-TOSCA] Error while loading TOSCA: \n\n", failure=f)
Matteo Scandolob2eff2c2018-03-02 12:58:10 -080057 return f
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070058 except Exception:
59 log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070060 return "Internal server error, please report this along with the failed recipe."
Matteo Scandolo21dde412017-07-11 18:54:12 -070061
62 @app.route('/', methods=['GET'])
63 def index(self, request):
Matteo Scandolo09d469c2017-07-07 11:46:48 -070064 request.responseHeaders.addRawHeader(b"content-type", b"application/json")
65 tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')]
66
67 response = {}
68 for d in tosca_defs:
69 name = d.replace('.yaml', '')
70 response[name] = "/custom_type/%s" % name
71 return json.dumps(response)
Matteo Scandolo21dde412017-07-11 18:54:12 -070072
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070073 @app.route("/custom_type/<name>")
74 def custom_type(self, request, name):
75 request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
76 custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
77 return custom_type
78
Matteo Scandolo21dde412017-07-11 18:54:12 -070079 @app.route('/run', methods=['POST'])
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070080 def run(self, request):
Matteo Scandolo21dde412017-07-11 18:54:12 -070081 recipe = request.content.read()
82 headers = request.getAllHeaders()
83 username = headers['xos-username']
84 password = headers['xos-password']
85
Andy Bavier571489f2019-02-05 19:23:51 -070086 parser = TOSCA_Parser(recipe, username, password)
87 d = GRPC_Client().create_secure_client(username, password, parser)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070088 tosca_execution = d.addCallback(self.execute_tosca)
89 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo21dde412017-07-11 18:54:12 -070090 return d
Matteo Scandolo9ce18252017-06-22 10:48:25 -070091
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070092 @app.route('/delete', methods=['POST'])
93 def delete(self, request):
94 recipe = request.content.read()
95 headers = request.getAllHeaders()
96 username = headers['xos-username']
97 password = headers['xos-password']
98
Andy Bavier571489f2019-02-05 19:23:51 -070099 parser = TOSCA_Parser(recipe, username, password, delete=True)
100 d = GRPC_Client().create_secure_client(username, password, parser)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -0700101 tosca_execution = d.addCallback(self.execute_tosca)
102 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -0700103 return d
Matteo Scandolo09d469c2017-07-07 11:46:48 -0700104
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700105 def __init__(self):
Matteo Scandolo5c0af1b2017-07-05 14:51:21 -0700106 self.app.run('0.0.0.0', '9102')