From b70492c1932f0a234842ac9856f79c7d30a3bfd3 Mon Sep 17 00:00:00 2001 From: aviau Date: Mon, 27 Apr 2015 16:11:24 -0400 Subject: [PATCH] New commands: 'config-host-[create/delete]' Change-Id: Ia9da3a05d89c8eedd21d412b2344193e3202aa2b --- surveilclient/common/http.py | 11 +++++- surveilclient/openstack/common/cliutils.py | 42 +++++++++++++++++++++ surveilclient/v2_0/config/hosts.py | 8 ++++ surveilclient/v2_0/shell.py | 43 +++++++++++++++++++++- 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 surveilclient/openstack/common/cliutils.py diff --git a/surveilclient/common/http.py b/surveilclient/common/http.py index 7b97dac..980681a 100644 --- a/surveilclient/common/http.py +++ b/surveilclient/common/http.py @@ -75,5 +75,14 @@ class HTTPClient(object): if 'body' in kwargs: kwargs['body'] = json.dumps(kwargs['body']) + resp, body = self.request(url, method, **kwargs) + return resp, json.loads(body) + + def request(self, url, method, **kwargs): + """Send an http request with the specified characteristics. + + """ + kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {})) + resp, body = self._http_request(url, method, **kwargs) - return resp, json.loads(body.decode()) + return resp, body.decode() diff --git a/surveilclient/openstack/common/cliutils.py b/surveilclient/openstack/common/cliutils.py new file mode 100644 index 0000000..41e115d --- /dev/null +++ b/surveilclient/openstack/common/cliutils.py @@ -0,0 +1,42 @@ +# Copyright 2012 Red Hat, Inc. +# +# 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. + + +def add_arg(func, *args, **kwargs): + """Bind CLI arguments to a shell.py `do_foo` function.""" + + if not hasattr(func, 'arguments'): + func.arguments = [] + + # NOTE(sirp): avoid dups that can occur when the module is shared across + # tests. + if (args, kwargs) not in func.arguments: + # Because of the semantics of decorator composition if we just append + # to the options list positional options will appear to be backwards. + func.arguments.insert(0, (args, kwargs)) + + +def arg(*args, **kwargs): + """Decorator for CLI args. + + Example: + + >>> @arg("name", help="Name of the new entity") + ... def entity_create(args): + ... pass + """ + def _decorator(func): + add_arg(func, *args, **kwargs) + return func + return _decorator diff --git a/surveilclient/v2_0/config/hosts.py b/surveilclient/v2_0/config/hosts.py index 43cb993..1997a66 100644 --- a/surveilclient/v2_0/config/hosts.py +++ b/surveilclient/v2_0/config/hosts.py @@ -32,3 +32,11 @@ class HostsManager(surveil_manager.SurveilManager): body=kwargs ) return body + + def delete(self, host_name): + """Create a new host.""" + resp, body = self.http_client.request( + HostsManager.base_url + '/' + host_name, 'DELETE', + body='' + ) + return body diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py index 0310a50..315ab59 100644 --- a/surveilclient/v2_0/shell.py +++ b/surveilclient/v2_0/shell.py @@ -13,6 +13,17 @@ # under the License. from surveilclient.common import utils +from surveilclient.openstack.common import cliutils + + +def _dict_from_args(args, arg_names): + result = {} + for arg in arg_names: + value = getattr(args, arg, None) + if value is not None: + result[arg] = value + + return result def do_config_host_list(sc, args): @@ -34,6 +45,36 @@ def do_config_host_list(sc, args): utils.print_list(hosts, cols, formatters=formatters) +@cliutils.arg("--host_name", help="Name of the host") +@cliutils.arg("--address", help="Address of the host") +@cliutils.arg("--max_check_attempts") +@cliutils.arg("--check_period") +@cliutils.arg("--contacts") +@cliutils.arg("--contact_groups") +@cliutils.arg("--notification_interval") +@cliutils.arg("--notification_period") +@cliutils.arg("--use") +def do_config_host_create(sc, args): + """Create a config host.""" + arg_names = ['host_name', + 'address', + 'max_check_attempts', + 'check_period', + 'contacts', + 'contact_groups', + 'notification_interval', + 'notification_period', + 'use'] + host = _dict_from_args(args, arg_names) + sc.config.hosts.create(**host) + + +@cliutils.arg("--host_name", help="Name of the host") +def do_config_host_delete(sc, args): + """Create a config host.""" + sc.config.hosts.delete(args.host_name) + + def do_config_service_list(sc, args): """List all config services.""" services = sc.config.services.list() @@ -59,7 +100,7 @@ def do_config_service_list(sc, args): def do_config_reload(sc, args): """Trigger a config reload.""" - print (sc.config.reload_config()['message']) + print(sc.config.reload_config()['message']) def do_status_host_list(sc, args):