aboutsummaryrefslogtreecommitdiffstats
path: root/moon_interface
diff options
context:
space:
mode:
authorThomas Duval <thomas.duval@orange.com>2018-10-05 16:54:37 +0200
committerThomas Duval <thomas.duval@orange.com>2018-10-05 16:58:48 +0200
commit2e35a7e46f0929438c1c206e3116caa829f07dc6 (patch)
tree759a83b3dfefe70faeada1c3af7377f4cd89b8eb /moon_interface
parent2dbe655587ca98b67c1a3e3798c63fd47229adc0 (diff)
Update code to 4.6 official version
Change-Id: Ibd0da0e476e24b2685f54693efc11f7a58d40a62
Diffstat (limited to 'moon_interface')
-rw-r--r--moon_interface/Changelog4
-rw-r--r--moon_interface/moon_interface/__init__.py2
-rw-r--r--moon_interface/moon_interface/api/update.py49
-rw-r--r--moon_interface/moon_interface/authz_requests.py22
-rw-r--r--moon_interface/moon_interface/http_server.py10
5 files changed, 77 insertions, 10 deletions
diff --git a/moon_interface/Changelog b/moon_interface/Changelog
index cea475f2..361d7840 100644
--- a/moon_interface/Changelog
+++ b/moon_interface/Changelog
@@ -30,3 +30,7 @@ CHANGES
4.3.3-1
-----
- Fix a bug in authz_requests
+
+4.4.0
+-----
+- Add the update API
diff --git a/moon_interface/moon_interface/__init__.py b/moon_interface/moon_interface/__init__.py
index a8cd9455..85c245e0 100644
--- a/moon_interface/moon_interface/__init__.py
+++ b/moon_interface/moon_interface/__init__.py
@@ -3,4 +3,4 @@
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-__version__ = "4.3.3-1"
+__version__ = "4.4.0"
diff --git a/moon_interface/moon_interface/api/update.py b/moon_interface/moon_interface/api/update.py
new file mode 100644
index 00000000..e798059c
--- /dev/null
+++ b/moon_interface/moon_interface/api/update.py
@@ -0,0 +1,49 @@
+# 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'.
+"""
+Authz is the endpoint to get authorization response
+"""
+
+from flask import request
+from flask_restful import Resource
+import requests
+import logging
+
+__version__ = "4.3.1"
+
+logger = logging.getLogger("moon.interface.api." + __name__)
+
+
+class Update(Resource):
+ """
+ Endpoint for update requests
+ """
+
+ __urls__ = (
+ "/update",
+ )
+
+ def __init__(self, **kwargs):
+ self.CACHE = kwargs.get("cache")
+ self.INTERFACE_NAME = kwargs.get("interface_name", "interface")
+ self.MANAGER_URL = kwargs.get("manager_url", "http://manager:8080")
+ self.TIMEOUT = 5
+
+ def put(self):
+ try:
+ self.CACHE.update_assignments(
+ request.form.get("policy_id", None),
+ request.form.get("perimeter_id", None),
+ )
+ for project_id in self.CACHE.container_chaining:
+ hostname = self.CACHE.container_chaining[project_id][0]["hostip"]
+ port = self.CACHE.container_chaining[project_id][0]["port"]
+ req = requests.put("http://{}:{}/update".format(hostname, port), request.form)
+ if req.status_code != 200:
+ logger.error("Cannot connect to {} on port {}".format(hostname, port))
+ except Exception as e:
+ logger.exception(e)
+ return {"result": False, "reason": str(e)}
+ return {"result": True}
diff --git a/moon_interface/moon_interface/authz_requests.py b/moon_interface/moon_interface/authz_requests.py
index c809053b..cf50dfe5 100644
--- a/moon_interface/moon_interface/authz_requests.py
+++ b/moon_interface/moon_interface/authz_requests.py
@@ -53,7 +53,9 @@ class AuthzRequest:
raise exceptions.AuthzException(
"error in address no hostname or hostip"
)
+ tried_hostnames = []
while tries < 2:
+ tried_hostnames.append(hostname)
try:
req = requests.post("http://{}:{}/authz".format(
hostname,
@@ -62,18 +64,20 @@ class AuthzRequest:
if req.status_code != 200:
raise exceptions.AuthzException(
"Receive bad response from Authz function "
- "(with address - {})".format(req.status_code)
+ "(with {} -> {})".format(hostname, req.status_code)
)
success = True
- break
except requests.exceptions.ConnectionError:
- logger.error("Cannot connect to {}".format(
- "http://{}:{}/authz".format(
- hostname,
- self.container_chaining[0]["port"]
- )))
- except:
- logger.error("Unexpected error:", sys.exc_info()[0])
+ if tries > 1:
+ logger.error("Cannot connect to {}".format(
+ "http://[{}]:{}/authz".format(
+ ", ".join(tried_hostnames),
+ self.container_chaining[0]["port"]
+ )))
+ except Exception as e:
+ logger.exception(e)
+ else:
+ break
hostname = self.container_chaining[0]["hostname"],
tries += 1
diff --git a/moon_interface/moon_interface/http_server.py b/moon_interface/moon_interface/http_server.py
index 1e0858c0..50bf2a62 100644
--- a/moon_interface/moon_interface/http_server.py
+++ b/moon_interface/moon_interface/http_server.py
@@ -9,6 +9,7 @@ import logging
from moon_interface import __version__
from moon_interface.api.generic import Status, API
from moon_interface.api.authz import Authz
+from moon_interface.api.update import Update
from moon_interface.authz_requests import CACHE
from python_moonutilities import configuration, exceptions
@@ -131,6 +132,15 @@ class HTTPServer(Server):
self.manager_port),
}
)
+ self.api.add_resource(Update, *Update.__urls__,
+ resource_class_kwargs={
+ "cache": CACHE,
+ "interface_name": self.host,
+ "manager_url": "http://{}:{}".format(
+ self.manager_hostname,
+ self.manager_port),
+ }
+ )
def run(self):
self.app.run(host=self._host, port=self._port, threaded=True) # nosec