diff --git a/.gitignore b/.gitignore index 8f6f9c0..019a2cf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ # documentation doc/build doc/source/_build +AUTHORS +ChangeLog .testrepository dist diff --git a/README.rst b/README.rst index 9bbe009..cfdae3b 100644 --- a/README.rst +++ b/README.rst @@ -35,3 +35,13 @@ To use the python API, simply create a client with the endpoint:: auth_url='http://localhost:8080/v2/auth', version='2_0') hosts = c.config.hosts.list() + + +How to use it +------------- + +Config-host-update:: + + surveil config-host-update [host_name] --address [ADDRESS] --custom_fields '{"_field1": "value1", "_field2": "value2"}' + + diff --git a/surveilclient/common/http.py b/surveilclient/common/http.py index d4707f3..6183bf4 100644 --- a/surveilclient/common/http.py +++ b/surveilclient/common/http.py @@ -113,7 +113,10 @@ class HTTPClient(object): kwargs['body'] = json.dumps(kwargs['body']) resp, body = self.request(url, method, **kwargs) - return resp, json.loads(body) + if body != "": + body = json.loads(body) + + return resp, body def request(self, url, method, **kwargs): """Send an http request with the specified characteristics. diff --git a/surveilclient/common/utils.py b/surveilclient/common/utils.py index 486745b..fde19f2 100644 --- a/surveilclient/common/utils.py +++ b/surveilclient/common/utils.py @@ -53,6 +53,30 @@ def json_formatter(js): return jsonutils.dumps(js, indent=2, ensure_ascii=False) +def print_item(objs, properties): + """ Add the missing properties to the objs """ + for prop in properties: + if prop not in objs: + objs[prop] = "" + + cols = [ + 'Property', + 'Value' + ] + + """ Override the properties keys pass in parameter """ + list = [] + for value in properties: + list.append({'prop': value, 'value': objs[value].__str__()}) + + formatters = { + 'Property': lambda x: x['prop'], + 'Value': lambda x: x['value'] + } + + print_list(list, cols, formatters=formatters) + + def print_list(objs, fields, field_labels=None, formatters={}, sortby=None): field_labels = field_labels or fields pt = prettytable.PrettyTable([f for f in field_labels], diff --git a/surveilclient/tests/v2_0/config/test_hosts.py b/surveilclient/tests/v2_0/config/test_hosts.py index cf1555f..f007a3e 100644 --- a/surveilclient/tests/v2_0/config/test_hosts.py +++ b/surveilclient/tests/v2_0/config/test_hosts.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import json + import httpretty from surveilclient.tests.v2_0 import clienttest @@ -37,16 +39,60 @@ class TestHosts(clienttest.ClientTest): def test_create(self): httpretty.register_uri( httpretty.POST, "http://localhost:8080/v2/config/hosts", - body='{"host_name": "new_host"}' + body='{"host_name": "new_host", "address": "192.168.2.1"}' ) self.client.config.hosts.create( - host_name="new_host" + host_name="new_host", + address="192.168.2.1" ) self.assertEqual( - httpretty.last_request().body.decode(), - u'{"host_name": "new_host"}' + json.loads(httpretty.last_request().body.decode()), + { + "host_name": "new_host", + "address": "192.168.2.1" + } + ) + + @httpretty.activate + def test_show(self): + httpretty.register_uri( + httpretty.GET, + "http://localhost:8080/v2/config/hosts/host_name_to_show", + body='{"host_name": "host_name_to_show"}' + ) + + host = self.client.config.hosts.get( + host_name="host_name_to_show" + ) + + self.assertEqual( + host, + {"host_name": "host_name_to_show"} + ) + + @httpretty.activate + def test_update(self): + httpretty.register_uri( + httpretty.PUT, + "http://localhost:8080/v2/config/hosts/host_name_to_update", + body='{"test": "test"}' + ) + + self.client.config.hosts.update( + "host_name_to_update", + address="192.168.0.1", + check_period="24x7" + ) + + self.assertEqual( + json.loads(httpretty.last_request().body.decode()), + { + "check_period": u"24x7", + "host_name": u"host_name_to_update", + "address": u"192.168.0.1" + } ) @httpretty.activate diff --git a/surveilclient/v2_0/config/hosts.py b/surveilclient/v2_0/config/hosts.py index aca20f7..99e2ba8 100644 --- a/surveilclient/v2_0/config/hosts.py +++ b/surveilclient/v2_0/config/hosts.py @@ -11,6 +11,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import json from surveilclient.common import surveil_manager @@ -27,12 +28,37 @@ class HostsManager(surveil_manager.SurveilManager): def create(self, **kwargs): """Create a new host.""" + + if "custom_fields" in kwargs: + kwargs["custom_fields"] = json.loads(kwargs["custom_fields"]) + resp, body = self.http_client.json_request( HostsManager.base_url, 'POST', body=kwargs ) return body + def get(self, host_name): + """Get a new host.""" + resp, body = self.http_client.json_request( + HostsManager.base_url + '/' + host_name, 'GET', + body='' + ) + return body + + def update(self, host_name, **kwargs): + """Update a host.""" + kwargs["host_name"] = host_name + + if "custom_fields" in kwargs: + kwargs["custom_fields"] = json.loads(kwargs["custom_fields"]) + + resp, body = self.http_client.json_request( + HostsManager.base_url + '/' + host_name, 'PUT', + body=kwargs + ) + return body + def delete(self, host_name): """Delete a host.""" resp, body = self.http_client.request( diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py index b42044d..30acf4a 100644 --- a/surveilclient/v2_0/shell.py +++ b/surveilclient/v2_0/shell.py @@ -45,12 +45,38 @@ 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("--custom_fields") +@cliutils.arg("--notification_interval") +@cliutils.arg("--notification_period") +@cliutils.arg("--use") +def do_config_host_update(sc, args): + """Create a config host.""" + arg_names = ['address', + 'max_check_attempts', + 'check_period', + 'contacts', + 'contact_groups', + 'custom_fields', + 'notification_interval', + 'notification_period', + 'use'] + host = _dict_from_args(args, arg_names) + sc.config.hosts.update(args.host_name, **host) + + @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("--custom_fields") @cliutils.arg("--notification_interval") @cliutils.arg("--notification_period") @cliutils.arg("--use") @@ -62,6 +88,7 @@ def do_config_host_create(sc, args): 'check_period', 'contacts', 'contact_groups', + 'custom_fields', 'notification_interval', 'notification_period', 'use'] @@ -69,6 +96,24 @@ def do_config_host_create(sc, args): sc.config.hosts.create(**host) +@cliutils.arg("host_name", help="Name of the host") +def do_config_host_show(sc, args): + """Show a specific host.""" + host = sc.config.hosts.get(args.host_name) + + if args.json: + print(utils.json_formatter(host)) + elif host: + """ Specify the shown order and all the properties to display """ + hostProperties = [ + 'host_name', 'address', 'check_period', 'contact_groups', + 'contacts', 'custom_fields', 'max_check_attempts', + 'notification_interval', 'notification_period', 'use' + ] + + utils.print_item(host, hostProperties) + + @cliutils.arg("--host_name", help="Name of the host") def do_config_host_delete(sc, args): """Create a config host."""