diff options
Diffstat (limited to 'moonv4/moon_wrapper/moon_wrapper')
-rw-r--r-- | moonv4/moon_wrapper/moon_wrapper/__init__.py | 6 | ||||
-rw-r--r-- | moonv4/moon_wrapper/moon_wrapper/__main__.py | 3 | ||||
-rw-r--r-- | moonv4/moon_wrapper/moon_wrapper/http_server.py | 76 | ||||
-rw-r--r-- | moonv4/moon_wrapper/moon_wrapper/server.py | 32 |
4 files changed, 117 insertions, 0 deletions
diff --git a/moonv4/moon_wrapper/moon_wrapper/__init__.py b/moonv4/moon_wrapper/moon_wrapper/__init__.py new file mode 100644 index 00000000..903c6518 --- /dev/null +++ b/moonv4/moon_wrapper/moon_wrapper/__init__.py @@ -0,0 +1,6 @@ +# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors +# This software is distributed under the terms and conditions of the 'Apache-2.0' +# license which can be found in the file 'LICENSE' in this package distribution +# or at 'http://www.apache.org/licenses/LICENSE-2.0'. + +__version__ = "0.1.0" diff --git a/moonv4/moon_wrapper/moon_wrapper/__main__.py b/moonv4/moon_wrapper/moon_wrapper/__main__.py new file mode 100644 index 00000000..1a04e9ce --- /dev/null +++ b/moonv4/moon_wrapper/moon_wrapper/__main__.py @@ -0,0 +1,3 @@ +from moon_wrapper.server import main + +main() diff --git a/moonv4/moon_wrapper/moon_wrapper/http_server.py b/moonv4/moon_wrapper/moon_wrapper/http_server.py new file mode 100644 index 00000000..ba2caf84 --- /dev/null +++ b/moonv4/moon_wrapper/moon_wrapper/http_server.py @@ -0,0 +1,76 @@ +# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors +# This software is distributed under the terms and conditions of the 'Apache-2.0' +# license which can be found in the file 'LICENSE' in this package distribution +# or at 'http://www.apache.org/licenses/LICENSE-2.0'. + +import requests +import json +from flask import Flask, request +import logging +from moon_utilities import configuration + +logger = logging.getLogger("moon.wrapper.http") + + +def __get_subject(target, credentials): + _subject = target.get("user_id", "") + if not _subject: + _subject = credentials.get("user_id", "") + return _subject + + +def __get_object(target, credentials): + try: + # note: case of Glance + return target['target']['name'] + except KeyError: + pass + + # note: default case + return target.get("project_id", "") + + +def __get_project_id(target, credentials): + return target.get("project_id", "") + + +def HTTPServer(host, port): + app = Flask(__name__) + conf = configuration.get_configuration("components/wrapper") + timeout = conf["components/wrapper"].get("timeout", 5) + conf = configuration.get_configuration("components/interface") + interface_hostname = conf["components/interface"].get("hostname", "interface") + interface_port = conf["components/interface"].get("port", 80) + conf = configuration.get_configuration("logging") + try: + debug = conf["logging"]["loggers"]['moon']['level'] == "DEBUG" + except KeyError: + debug = False + + @app.route("/", methods=['POST', 'GET']) + def wrapper(): + try: + target = json.loads(request.form.get('target', {})) + credentials = json.loads(request.form.get('credentials', {})) + rule = request.form.get('rule', "") + _subject = __get_subject(target, credentials) + _object = __get_object(target, credentials) + _project_id = __get_project_id(target, credentials) + logger.info("GET with args {} / {} - {} - {}".format(_project_id, _subject, _object, rule)) + _url = "http://{}:{}/{}/{}/{}/{}".format( + interface_hostname, + interface_port, + _project_id, + _subject, + _object, + rule + ) + req = requests.get(url=_url, timeout=timeout) + logger.info("req txt={}".format(req.text)) + if req.json()["result"] == True: + return "True" + except Exception as e: + logger.exception("An exception occurred: {}".format(e)) + return "False" + + app.run(debug=debug, host=host, port=port) diff --git a/moonv4/moon_wrapper/moon_wrapper/server.py b/moonv4/moon_wrapper/moon_wrapper/server.py new file mode 100644 index 00000000..2e5f15b9 --- /dev/null +++ b/moonv4/moon_wrapper/moon_wrapper/server.py @@ -0,0 +1,32 @@ +# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors +# This software is distributed under the terms and conditions of the 'Apache-2.0' +# license which can be found in the file 'LICENSE' in this package distribution +# or at 'http://www.apache.org/licenses/LICENSE-2.0'. + +import logging +from moon_utilities import configuration, exceptions +from moon_wrapper.http_server import HTTPServer + +LOG = logging.getLogger("moon.wrapper") + + +def main(): + configuration.init_logging() + try: + conf = configuration.get_configuration("components/wrapper") + LOG.debug("wrapper.conf={}".format(conf)) + hostname = conf["components/wrapper"].get("hostname", "wrapper") + port = conf["components/wrapper"].get("port", 80) + bind = conf["components/wrapper"].get("bind", "127.0.0.1") + except exceptions.ConsulComponentNotFound: + hostname = "wrapper" + bind = "127.0.0.1" + port = 80 + configuration.add_component(uuid="wrapper", name=hostname, port=port, bind=bind) + LOG.info("Starting server with IP {} on port {} bind to {}".format(hostname, port, bind)) + server = HTTPServer(host=bind, port=port) + server.run() + + +if __name__ == '__main__': + main() |