aboutsummaryrefslogtreecommitdiffstats
path: root/python_moonutilities/python_moonutilities
diff options
context:
space:
mode:
Diffstat (limited to 'python_moonutilities/python_moonutilities')
-rw-r--r--python_moonutilities/python_moonutilities/configuration.py37
-rw-r--r--python_moonutilities/python_moonutilities/exceptions.py5
-rw-r--r--python_moonutilities/python_moonutilities/request_wrapper.py10
3 files changed, 38 insertions, 14 deletions
diff --git a/python_moonutilities/python_moonutilities/configuration.py b/python_moonutilities/python_moonutilities/configuration.py
index 51587582..9a044db7 100644
--- a/python_moonutilities/python_moonutilities/configuration.py
+++ b/python_moonutilities/python_moonutilities/configuration.py
@@ -6,7 +6,7 @@
import base64
import json
-import requests
+import python_moonutilities.request_wrapper as requests
import logging.config
from python_moonutilities import exceptions
@@ -25,18 +25,20 @@ def init_logging():
config = get_configuration("logging")
logging.config.dictConfig(config['logging'])
-
def increment_port():
- components_port_start = int(get_configuration("components_port_start")['components_port_start'])
- components_port_start += 1
- url = "http://{}:{}/v1/kv/components_port_start".format(CONSUL_HOST, CONSUL_PORT)
+ components_object = get_configuration("components/port_start")
+ if 'port_start' in components_object:
+ components_port_start = int(get_configuration("components/port_start")['port_start'])
+ components_port_start += 1
+ else:
+ raise exceptions.ConsulComponentContentError("error={}".format(components_object))
+ url = "http://{}:{}/v1/kv/components/port_start".format(CONSUL_HOST, CONSUL_PORT)
req = requests.put(url, json=str(components_port_start))
if req.status_code != 200:
logger.info("url={}".format(url))
raise exceptions.ConsulError
return components_port_start
-
def get_configuration(key):
url = "http://{}:{}/v1/kv/{}".format(CONSUL_HOST, CONSUL_PORT, key)
req = requests.get(url)
@@ -46,14 +48,17 @@ def get_configuration(key):
data = req.json()
if len(data) == 1:
data = data[0]
- return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ if all( k in data for k in ("Key", "Value")) :
+ return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ raise exceptions.ConsulComponentContentError("error={}".format(data))
else:
return [
- {item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8"))}
- for item in data
+ {
+ item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+ if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
+ } for item in data
]
-
def add_component(name, uuid, port=None, bind="127.0.0.1", keystone_id="", extra=None, container=None):
data = {
"hostname": name,
@@ -75,7 +80,6 @@ def add_component(name, uuid, port=None, bind="127.0.0.1", keystone_id="", extra
logger.info("Add component {}".format(req.text))
return configuration.get_configuration("components/"+uuid)
-
def get_plugins():
url = "http://{}:{}/v1/kv/plugins?recurse=true".format(CONSUL_HOST, CONSUL_PORT)
req = requests.get(url)
@@ -85,14 +89,16 @@ def get_plugins():
data = req.json()
if len(data) == 1:
data = data[0]
- return {data["Key"].replace("plugins/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ if all(k in data for k in ("Key", "Value")):
+ return {data["Key"].replace("plugins/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ raise exceptions.ConsulComponentContentError("error={}".format(data))
else:
return {
item["Key"].replace("plugins/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+ if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
for item in data
}
-
def get_components():
url = "http://{}:{}/v1/kv/components?recurse=true".format(CONSUL_HOST, CONSUL_PORT)
req = requests.get(url)
@@ -102,10 +108,13 @@ def get_components():
data = req.json()
if len(data) == 1:
data = data[0]
- return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ if all(k in data for k in ("Key", "Value")):
+ return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+ raise exceptions.ConsulComponentContentError("error={}".format(data))
else:
return {
item["Key"].replace("components/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+ if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
for item in data
}
diff --git a/python_moonutilities/python_moonutilities/exceptions.py b/python_moonutilities/python_moonutilities/exceptions.py
index 1abb5aa7..e6c9f6de 100644
--- a/python_moonutilities/python_moonutilities/exceptions.py
+++ b/python_moonutilities/python_moonutilities/exceptions.py
@@ -504,6 +504,11 @@ class ConsulComponentNotFound(ConsulError):
title = 'Consul error'
logger = "WARNING"
+class ConsulComponentContentError(ConsulError):
+ description = _("invalid content of component .")
+ code = 500
+ title = 'Consul error'
+ logger = "WARNING"
# Containers exceptions
diff --git a/python_moonutilities/python_moonutilities/request_wrapper.py b/python_moonutilities/python_moonutilities/request_wrapper.py
index 8cf5b997..f1603b9d 100644
--- a/python_moonutilities/python_moonutilities/request_wrapper.py
+++ b/python_moonutilities/python_moonutilities/request_wrapper.py
@@ -9,4 +9,14 @@ def get(url):
raise exceptions.ConsulError("request failure ",e)
except:
raise exceptions.ConsulError("Unexpected error ", sys.exc_info()[0])
+ return response
+
+
+def put(url, json=""):
+ try:
+ response = requests.put(url,json=json)
+ except requests.exceptions.RequestException as e:
+ raise exceptions.ConsulError("request failure ",e)
+ except:
+ raise exceptions.ConsulError("Unexpected error ", sys.exc_info()[0])
return response \ No newline at end of file