From 679824b687e8dddc5d7f964a4d833cc4133eb373 Mon Sep 17 00:00:00 2001 From: gujin Date: Tue, 30 Jul 2019 22:26:26 +0800 Subject: [PATCH] Add service and host check for API now, the action will run even though the service or the host are invaild, this PS to check them before the action executed. Change-Id: I764599673d44c33ce6622bf97988f372a7a26285 --- kolla_cli/api/control_plane.py | 4 +++ kolla_cli/api/exceptions.py | 10 ++++++++ kolla_cli/common/ansible/utils.py | 42 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 kolla_cli/common/ansible/utils.py diff --git a/kolla_cli/api/control_plane.py b/kolla_cli/api/control_plane.py index cdcf3b0..a4b8176 100644 --- a/kolla_cli/api/control_plane.py +++ b/kolla_cli/api/control_plane.py @@ -14,6 +14,7 @@ from kolla_cli.api.job import Job from kolla_cli.common.ansible.actions import KollaAction +from kolla_cli.common.ansible.utils import check_kolla_args from kolla_cli.common.inventory import Inventory from kolla_cli.common.utils import check_arg from kolla_cli.common.utils import safe_decode @@ -79,6 +80,9 @@ class ControlPlaneApi(object): check_arg(servicenames, u._('Service names'), list, empty_ok=True, none_ok=True) + check_kolla_args(hostnames=hostnames, + servicenames=servicenames) + hostnames = safe_decode(hostnames) servicenames = safe_decode(servicenames) aciton = KollaAction(verbose_level=verbose_level, diff --git a/kolla_cli/api/exceptions.py b/kolla_cli/api/exceptions.py index d6f1999..1c57e1f 100644 --- a/kolla_cli/api/exceptions.py +++ b/kolla_cli/api/exceptions.py @@ -69,6 +69,16 @@ class InvalidConfiguration(ClientException): pass +class InvalidHosts(ClientException): + """Invalid hosts""" + pass + + +class InvalidServices(ClientException): + """Invalid services""" + pass + + class FailedOperation(ClientException): pass diff --git a/kolla_cli/common/ansible/utils.py b/kolla_cli/common/ansible/utils.py new file mode 100644 index 0000000..b98fa7d --- /dev/null +++ b/kolla_cli/common/ansible/utils.py @@ -0,0 +1,42 @@ +# Copyright(c) 2019, caoyuan. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from kolla_cli.api.exceptions import InvalidHosts +from kolla_cli.api.exceptions import InvalidServices +from kolla_cli.common.inventory import Inventory +import kolla_cli.i18n as u + + +def check_kolla_args(hostnames=[], servicenames=[]): + + if not any([hostnames, servicenames]): + return + + inventory = Inventory.load() + if hostnames: + all_hosts = inventory.get_hostnames() + invalid_hosts = list(set(hostnames) - set(all_hosts)) + if invalid_hosts: + raise InvalidHosts( + u._('Hosts {hosts} are not valid.').format( + hosts=invalid_hosts)) + + if servicenames: + all_services = [service.name + for service in inventory.get_services()] + invalid_services = list(set(servicenames) - set(all_services)) + if invalid_services: + raise InvalidServices( + u._('Services {services} are not valid.').format( + services=invalid_services))