aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Duval <thomas.duval@orange.com>2018-01-19 13:54:41 +0100
committerThomas Duval <thomas.duval@orange.com>2018-01-19 13:54:41 +0100
commit0f9e05e3b2984ee0f10532187828605438d2699a (patch)
tree11c554ada298cd80e737fe67cce73fe253f95c4e
parentb0f7daa6f05a1ea2308e82cfc05a1b8d10d4e5dc (diff)
Add moon_get_slaves, moon_set_slave, moon_delete_slave commands.
Change-Id: I224df38ffffcad7b4e8ee4da92087fa352cc6fc7
-rw-r--r--python_moonclient/Changelog7
-rw-r--r--python_moonclient/python_moonclient/__init__.py2
-rw-r--r--python_moonclient/python_moonclient/scripts.py74
-rw-r--r--python_moonclient/python_moonclient/slaves.py61
-rw-r--r--python_moonclient/setup.py5
5 files changed, 146 insertions, 3 deletions
diff --git a/python_moonclient/Changelog b/python_moonclient/Changelog
index f6f6c3a4..64ae76ba 100644
--- a/python_moonclient/Changelog
+++ b/python_moonclient/Changelog
@@ -30,3 +30,10 @@ CHANGES
- moon_create_pdp
- moon_send_authz_to_wrapper
- Fix a bug in pdp library
+
+1.2.0
+-----
+- Add some commands:
+ - moon_get_slaves
+ - moon_set_slave
+ - moon_delete_slave
diff --git a/python_moonclient/python_moonclient/__init__.py b/python_moonclient/python_moonclient/__init__.py
index 2302dea9..2c7f8f5c 100644
--- a/python_moonclient/python_moonclient/__init__.py
+++ b/python_moonclient/python_moonclient/__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__ = "1.1.0"
+__version__ = "1.2.0"
diff --git a/python_moonclient/python_moonclient/scripts.py b/python_moonclient/python_moonclient/scripts.py
index c880e497..74ed47fc 100644
--- a/python_moonclient/python_moonclient/scripts.py
+++ b/python_moonclient/python_moonclient/scripts.py
@@ -1,6 +1,6 @@
import logging
from importlib.machinery import SourceFileLoader
-from . import parse, models, policies, pdp, authz
+from . import parse, models, policies, pdp, authz, slaves
logger = logging.getLogger("moonclient.scripts")
@@ -161,3 +161,75 @@ def map_pdp_to_project():
logger.info("Mapping: {}=>{}".format(args.filename[0], args.filename[1]))
# TODO: check if pdp_id and keystone_project_id exist
pdp.map_to_keystone(pdp_id=args.filename[0], keystone_project_id=args.filename[1])
+
+
+def get_slaves():
+ requests_log = logging.getLogger("requests.packages.urllib3")
+ requests_log.setLevel(logging.WARNING)
+ requests_log.propagate = True
+
+ args = parse.parse()
+ consul_host = args.consul_host
+ consul_port = args.consul_port
+
+ models.init(consul_host, consul_port)
+ policies.init(consul_host, consul_port)
+ pdp.init(consul_host, consul_port)
+ slaves.init(consul_host, consul_port)
+
+ for value in slaves.get_slaves().get('slaves', dict()):
+ if value['configured']:
+ print(" {} (configured)".format(value['name']))
+ else:
+ print(" {} (not configured)".format(value['name']))
+
+
+def set_slave():
+ requests_log = logging.getLogger("requests.packages.urllib3")
+ requests_log.setLevel(logging.WARNING)
+ requests_log.propagate = True
+
+ args = parse.parse()
+ consul_host = args.consul_host
+ consul_port = args.consul_port
+
+ models.init(consul_host, consul_port)
+ policies.init(consul_host, consul_port)
+ pdp.init(consul_host, consul_port)
+ slaves.init(consul_host, consul_port)
+
+ slave_name = "kubernetes-admin@kubernetes"
+ if args.filename:
+ slave_name = args.filename
+ for value in slaves.set_slave(slave_name).get('slaves', dict()):
+ if value['configured']:
+ print(" {} (configured)".format(value['name']))
+ else:
+ print(" {} (not configured)".format(value['name']))
+
+
+def delete_slave():
+ requests_log = logging.getLogger("requests.packages.urllib3")
+ requests_log.setLevel(logging.WARNING)
+ requests_log.propagate = True
+
+ args = parse.parse()
+ consul_host = args.consul_host
+ consul_port = args.consul_port
+
+ models.init(consul_host, consul_port)
+ policies.init(consul_host, consul_port)
+ pdp.init(consul_host, consul_port)
+ slaves.init(consul_host, consul_port)
+
+ slave_name = "kubernetes-admin@kubernetes"
+ if args.filename:
+ slave_name = args.filename
+ for value in slaves.delete_slave(slave_name).get('slaves', dict()):
+ if value['configured']:
+ print(" {} (configured)".format(value['name']))
+ else:
+ print(" {} (not configured)".format(value['name']))
+
+
+
diff --git a/python_moonclient/python_moonclient/slaves.py b/python_moonclient/python_moonclient/slaves.py
new file mode 100644
index 00000000..3554341d
--- /dev/null
+++ b/python_moonclient/python_moonclient/slaves.py
@@ -0,0 +1,61 @@
+import logging
+import requests
+import copy
+from . import config
+
+logger = logging.getLogger("moonclient.slaves")
+
+
+URL = None
+HEADERS = None
+
+
+def init(consul_host, consul_port):
+ conf_data = config.get_config_data(consul_host, consul_port)
+ global URL, HEADERS
+ URL = "http://{}:{}".format(
+ conf_data['manager_host'],
+ conf_data['manager_port'])
+ URL = URL + "{}"
+ HEADERS = {"content-type": "application/json"}
+
+
+def get_slaves():
+ req = requests.get(URL.format("/slaves"))
+ assert req.status_code == 200
+ result = req.json()
+ assert type(result) is dict
+ assert "slaves" in result
+ return result
+
+
+def set_slave(name):
+ slaves = get_slaves().get("slaves", [])
+ names = map(lambda x: x['name'], slaves)
+ assert name in names
+ req = requests.patch(URL.format("/slaves/{}".format(name)),
+ headers=HEADERS,
+ json={
+ "op": "replace",
+ "variable": "configured",
+ "value": True
+ })
+ assert req.status_code == 200
+ result = req.json()
+ assert type(result) is dict
+ assert "slaves" in result
+ return get_slaves()
+
+
+def delete_slave(name):
+ slaves = get_slaves().get("slaves", [])
+ names = map(lambda x: x['name'], slaves)
+ assert name in names
+ req = requests.patch(URL.format("/slaves/{}".format(name)),
+ headers=HEADERS,
+ json={
+ "op": "replace",
+ "variable": "configured",
+ "value": False
+ })
+ return get_slaves()
diff --git a/python_moonclient/setup.py b/python_moonclient/setup.py
index 709e3ffa..dcb90365 100644
--- a/python_moonclient/setup.py
+++ b/python_moonclient/setup.py
@@ -48,7 +48,10 @@ setup(
'moon_delete_pdp = python_moonclient.scripts:delete_pdp',
'moon_delete_policy = python_moonclient.scripts:delete_policy',
'moon_map_pdp_to_project = python_moonclient.scripts:map_pdp_to_project',
- 'moon_send_authz_to_wrapper = python_moonclient.scripts:send_authz_to_wrapper'
+ 'moon_send_authz_to_wrapper = python_moonclient.scripts:send_authz_to_wrapper',
+ 'moon_get_slaves = python_moonclient.scripts:get_slaves',
+ 'moon_set_slave = python_moonclient.scripts:set_slave',
+ 'moon_delete_slave = python_moonclient.scripts:delete_slave'
],
}