From c65cc8942269521306146cbd29ae61d9aae07b9e Mon Sep 17 00:00:00 2001 From: "flavien.peyre" Date: Fri, 8 May 2015 10:22:40 -0400 Subject: [PATCH] Added Check Modulation Change-Id: I4796892b1a8af06c65ee014c796a3d46a38e0ae6 --- .../v2_0/config/test_checkmodulations.py | 69 +++++++++++++++++++ surveilclient/v2_0/config/__init__.py | 5 +- surveilclient/v2_0/config/checkmodulations.py | 43 ++++++++++++ surveilclient/v2_0/shell.py | 39 +++++++++++ 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 surveilclient/tests/v2_0/config/test_checkmodulations.py create mode 100644 surveilclient/v2_0/config/checkmodulations.py diff --git a/surveilclient/tests/v2_0/config/test_checkmodulations.py b/surveilclient/tests/v2_0/config/test_checkmodulations.py new file mode 100644 index 0000000..422eb60 --- /dev/null +++ b/surveilclient/tests/v2_0/config/test_checkmodulations.py @@ -0,0 +1,69 @@ +# Copyright 2015 - Savoir-Faire Linux 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. +import json + +import httpretty + +from surveilclient.tests.v2_0 import clienttest + + +class TestCheckModulations(clienttest.ClientTest): + @httpretty.activate + def test_create(self): + httpretty.register_uri( + httpretty.POST, "http://localhost:8080/v2/config/checkmodulations", + body='{"message": "Ack received!"}') + + self.client.config.checkmodulations.create( + check_command='test', + check_period='test', + checkmodulation_name='test' + ) + + self.assertEqual( + json.loads(httpretty.last_request().body.decode()), + {"checkmodulation_name": "test", + "check_command": "test", + "check_period": "test"} + ) + + @httpretty.activate + def test_list(self): + httpretty.register_uri( + httpretty.GET, "http://localhost:8080/v2/config/checkmodulations", + body='[{"checkmodulation_name": "test","check_command": "test",' + '"check_period": "test"}]' + ) + + self.assertEqual( + self.client.config.checkmodulations.list(), + [{"checkmodulation_name": "test", + "check_command": "test", "check_period": "test"}] + ) + + @httpretty.activate + def test_delete(self): + httpretty.register_uri( + httpretty.DELETE, "http://localhost:8080/v2/config/" + "checkmodulations/checkmodulation_to_delete", + body='body') + + body = self.client.config.checkmodulations.delete( + checkmodulation_name='checkmodulation_to_delete' + ) + + self.assertEqual( + body, + "body" + ) \ No newline at end of file diff --git a/surveilclient/v2_0/config/__init__.py b/surveilclient/v2_0/config/__init__.py index c03f16c..65c1c41 100644 --- a/surveilclient/v2_0/config/__init__.py +++ b/surveilclient/v2_0/config/__init__.py @@ -13,6 +13,7 @@ # under the License. from surveilclient.common import surveil_manager +from surveilclient.v2_0.config import checkmodulations from surveilclient.v2_0.config import hosts from surveilclient.v2_0.config import services @@ -24,6 +25,8 @@ class ConfigManager(surveil_manager.SurveilManager): super(ConfigManager, self).__init__(http_client) self.hosts = hosts.HostsManager(self.http_client) self.services = services.ServicesManager(self.http_client) + self.checkmodulations = checkmodulations.CheckModulationsManager( + self.http_client) def reload_config(self): resp, body = self.http_client.json_request( @@ -31,4 +34,4 @@ class ConfigManager(surveil_manager.SurveilManager): 'POST', body='' # Must send empty body ) - return body + return body \ No newline at end of file diff --git a/surveilclient/v2_0/config/checkmodulations.py b/surveilclient/v2_0/config/checkmodulations.py new file mode 100644 index 0000000..0bf0331 --- /dev/null +++ b/surveilclient/v2_0/config/checkmodulations.py @@ -0,0 +1,43 @@ +# Copyright 2014-2015 - Savoir-Faire Linux 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. + +from surveilclient.common import surveil_manager + + +class CheckModulationsManager(surveil_manager.SurveilManager): + base_url = '/config/checkmodulations' + + def list(self): + """Get a list of checkmodulations.""" + resp, body = self.http_client.json_request( + CheckModulationsManager.base_url, 'GET' + ) + return body + + def create(self, **kwargs): + """Create a new checkmodulation.""" + resp, body = self.http_client.json_request( + CheckModulationsManager.base_url, 'POST', + body=kwargs + ) + return body + + def delete(self, checkmodulation_name): + """Delete a checkmodulation.""" + resp, body = self.http_client.request( + CheckModulationsManager.base_url+"/" + checkmodulation_name, + 'DELETE', + body='' + ) + return body \ No newline at end of file diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py index b42044d..3e28784 100644 --- a/surveilclient/v2_0/shell.py +++ b/surveilclient/v2_0/shell.py @@ -135,6 +135,45 @@ def do_config_service_delete(sc, args): args.service_description) +def do_config_checkmodulation_list(sc, args): + """List all config check modulations.""" + checkmodulations = sc.config.checkmodulations.list() + + if args.json: + print(utils.json_formatter(checkmodulations)) + else: + cols = [ + 'check_command', + 'check_period', + 'checkmodulation_name' + ] + + formatters = { + 'check_command': lambda x: x['check_command'], + 'check_period': lambda x: x['check_period'], + 'checkmodulation_name': lambda x: x['checkmodulation_name'] + } + utils.print_list(checkmodulations, cols, formatters=formatters) + + +@cliutils.arg("--check_command") +@cliutils.arg("--check_period") +@cliutils.arg("--checkmodulation_name") +def do_config_checkmodulation_create(sc, args): + """Create a config check modulation.""" + arg_names = ['check_command', + 'check_period', + 'checkmodulation_name'] + checkmodulation = _dict_from_args(args, arg_names) + sc.config.checkmodulations.create(**checkmodulation) + + +@cliutils.arg("--checkmodulation_name", help="Name of the check modulation") +def do_config_checkmodulation_delete(sc, args): + """Create a config check modulation.""" + sc.config.checkmodulations.delete(args.checkmodulation_name) + + def do_config_reload(sc, args): """Trigger a config reload.""" print(sc.config.reload_config()['message'])