diff options
Diffstat (limited to 'python_moonclient/python_moonclient')
-rw-r--r-- | python_moonclient/python_moonclient/__init__.py | 2 | ||||
-rw-r--r-- | python_moonclient/python_moonclient/scripts.py | 74 | ||||
-rw-r--r-- | python_moonclient/python_moonclient/slaves.py | 61 |
3 files changed, 135 insertions, 2 deletions
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() |