New commands: 'config-host-[create/delete]'

Change-Id: Ia9da3a05d89c8eedd21d412b2344193e3202aa2b
This commit is contained in:
aviau 2015-04-27 16:11:24 -04:00
parent fbbb5edb87
commit b70492c193
4 changed files with 102 additions and 2 deletions

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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()