diff --git a/README.rst b/README.rst index 9ba202f..816a2ab 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ You'll need to provide the Surveil API URL. You can do this with the ``--surveil-api-url`` parameter, but it's easier to just set it as environment variable:: - export SURVEIL_API_URL=http://localhost:8080/v1 + export SURVEIL_API_URL=http://localhost:8080/v2 You'll find complete documentation on the shell by running ``surveil help``. @@ -29,7 +29,7 @@ Python API To use the python API, simply create a client with the endpoint:: - from surveilclient.v1_0 import client - c = client.Client('http://localhost:8080/v1') - hosts = c.hosts.list() + from surveilclient import client + c = client.Client('http://localhost:8080/v2', version='2_0') + hosts = c.config.hosts.list() diff --git a/surveilclient/client.py b/surveilclient/client.py index 0f01948..3197a40 100644 --- a/surveilclient/client.py +++ b/surveilclient/client.py @@ -17,6 +17,6 @@ from surveilclient.common import utils def Client(*args, **kwargs): - version = kwargs.pop('version', '1_0') + version = kwargs.pop('version', '2_0') module = utils.import_versioned_module(version, 'client') return module.Client(*args, **kwargs) diff --git a/surveilclient/shell.py b/surveilclient/shell.py index 8eb311c..8705c03 100644 --- a/surveilclient/shell.py +++ b/surveilclient/shell.py @@ -29,7 +29,7 @@ import sys class SurveilShell(object): - default_api_version = '1_0' + default_api_version = '2_0' def __init__(self): self.parser = self.get_base_parser() diff --git a/surveilclient/tests/test_client.py b/surveilclient/tests/test_client.py index d343be2..2bb5d15 100644 --- a/surveilclient/tests/test_client.py +++ b/surveilclient/tests/test_client.py @@ -16,10 +16,19 @@ import unittest from surveilclient import client from surveilclient.v1_0 import client as v1_0_client +from surveilclient.v2_0 import client as v2_0_client class TestClient(unittest.TestCase): - def test_client_init(self): + def test_client_default_version(self): + sc = client.Client('http://localhost:8080/sdf') + self.assertTrue(isinstance(sc, v2_0_client.Client)) + + def test_client_init_v1(self): sc = client.Client('http://localhost:8080/v1', version='1_0') self.assertTrue(isinstance(sc, v1_0_client.Client)) + + def test_client_init_v2(self): + sc = client.Client('http://localhost:8080/v2', version='2_0') + self.assertTrue(isinstance(sc, v2_0_client.Client)) \ No newline at end of file diff --git a/surveilclient/tests/test_shell.py b/surveilclient/tests/test_shell.py index eb5ab3e..f1f585c 100644 --- a/surveilclient/tests/test_shell.py +++ b/surveilclient/tests/test_shell.py @@ -72,11 +72,11 @@ class ShellTest(ShellBase): def test_help_on_subcommand(self): required = [ - '^usage: surveil host-list', - "(?m)^List all hosts.", + '^usage: surveil config-host-list', + "(?m)^List all config hosts.", ] argstrings = [ - 'help host-list', + 'help config-host-list', ] for argstr in argstrings: help_text = self.shell(argstr) diff --git a/surveilclient/v2_0/__init__.py b/surveilclient/v2_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveilclient/v2_0/client.py b/surveilclient/v2_0/client.py new file mode 100644 index 0000000..e7951b5 --- /dev/null +++ b/surveilclient/v2_0/client.py @@ -0,0 +1,30 @@ +# 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. + +from surveilclient.common import http +from surveilclient.v2_0 import config +from surveilclient.v2_0 import status + + +class Client(object): + + """Client for the Surveil v2_0 API. + + :param string endpoint: The url of the surveil API + """ + + def __init__(self, endpoint): + self.http_client = http.HTTPClient(endpoint) + self.config = config.ConfigManager(self.http_client) + self.status = status.StatusManager(self.http_client) \ No newline at end of file diff --git a/surveilclient/v2_0/config/__init__.py b/surveilclient/v2_0/config/__init__.py new file mode 100644 index 0000000..c03f16c --- /dev/null +++ b/surveilclient/v2_0/config/__init__.py @@ -0,0 +1,34 @@ +# 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 +from surveilclient.v2_0.config import hosts +from surveilclient.v2_0.config import services + + +class ConfigManager(surveil_manager.SurveilManager): + base_url = '/config' + + def __init__(self, http_client): + super(ConfigManager, self).__init__(http_client) + self.hosts = hosts.HostsManager(self.http_client) + self.services = services.ServicesManager(self.http_client) + + def reload_config(self): + resp, body = self.http_client.json_request( + self.base_url + '/reload_config', + 'POST', + body='' # Must send empty body + ) + return body diff --git a/surveilclient/v2_0/config/hosts.py b/surveilclient/v2_0/config/hosts.py new file mode 100644 index 0000000..43cb993 --- /dev/null +++ b/surveilclient/v2_0/config/hosts.py @@ -0,0 +1,34 @@ +# 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 HostsManager(surveil_manager.SurveilManager): + base_url = '/config/hosts' + + def list(self): + """Get a list of hosts.""" + resp, body = self.http_client.json_request( + HostsManager.base_url, 'GET' + ) + return body + + def create(self, **kwargs): + """Create a new host.""" + resp, body = self.http_client.json_request( + HostsManager.base_url, 'POST', + body=kwargs + ) + return body diff --git a/surveilclient/v2_0/config/services.py b/surveilclient/v2_0/config/services.py new file mode 100644 index 0000000..099768c --- /dev/null +++ b/surveilclient/v2_0/config/services.py @@ -0,0 +1,34 @@ +# 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 ServicesManager(surveil_manager.SurveilManager): + base_url = '/config/services' + + def list(self): + """Get a list of hosts.""" + resp, body = self.http_client.json_request( + ServicesManager.base_url, 'GET' + ) + return body + + def create(self, **kwargs): + """Create a new host.""" + resp, body = self.http_client.json_request( + ServicesManager.base_url, 'POST', + body=kwargs + ) + return body diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py new file mode 100644 index 0000000..0310a50 --- /dev/null +++ b/surveilclient/v2_0/shell.py @@ -0,0 +1,112 @@ +# 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 utils + + +def do_config_host_list(sc, args): + """List all config hosts.""" + hosts = sc.config.hosts.list() + + if args.json: + print(utils.json_formatter(hosts)) + else: + cols = [ + 'host_name', + 'address', + ] + + formatters = { + 'host_name': lambda x: x['host_name'], + 'address': lambda x: x['address'], + } + utils.print_list(hosts, cols, formatters=formatters) + + +def do_config_service_list(sc, args): + """List all config services.""" + services = sc.config.services.list() + + if args.json: + print(utils.json_formatter(services)) + else: + cols = [ + 'host_name', + 'service_description', + 'check_period', + 'contact_groups', + ] + + formatters = { + 'service_description': lambda x: x['service_description'], + 'host_name': lambda x: x['host_name'], + 'check_period': lambda x: x['check_period'], + 'contact_groups': lambda x: x['contact_groups'], + } + utils.print_list(services, cols, formatters=formatters) + + +def do_config_reload(sc, args): + """Trigger a config reload.""" + print (sc.config.reload_config()['message']) + + +def do_status_host_list(sc, args): + """List all status hosts.""" + services = sc.status.hosts.list() + + if args.json: + print(utils.json_formatter(services)) + else: + cols = [ + 'host_name', + 'address', + 'state', + 'last_check', + 'plugin_output', + ] + + formatters = { + 'host_name': lambda x: x['host_name'], + 'address': lambda x: x['address'], + 'state': lambda x: x['state'], + 'last_check': lambda x: x['last_check'], + 'plugin_output': lambda x: x['plugin_output'][0:30] + '...', + } + utils.print_list(services, cols, formatters=formatters) + + +def do_status_service_list(sc, args): + """List all status services.""" + services = sc.status.services.list() + + if args.json: + print(utils.json_formatter(services)) + else: + cols = [ + 'host_name', + 'service_description', + 'state', + 'last_check', + 'plugin_output', + ] + + formatters = { + 'host_name': lambda x: x['host_name'], + 'service_description': lambda x: x['service_description'], + 'state': lambda x: x['state'], + 'last_check': lambda x: x['last_check'], + 'plugin_output': lambda x: x['plugin_output'][0:30] + '...', + } + utils.print_list(services, cols, formatters=formatters) diff --git a/surveilclient/v2_0/status/__init__.py b/surveilclient/v2_0/status/__init__.py new file mode 100644 index 0000000..244ac3c --- /dev/null +++ b/surveilclient/v2_0/status/__init__.py @@ -0,0 +1,26 @@ +# 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. + +from surveilclient.common import surveil_manager +from surveilclient.v2_0.status import hosts +from surveilclient.v2_0.status import services + + +class StatusManager(surveil_manager.SurveilManager): + base_url = '/status' + + def __init__(self, http_client): + super(StatusManager, self).__init__(http_client) + self.hosts = hosts.HostsManager(self.http_client) + self.services = services.ServicesManager(self.http_client) diff --git a/surveilclient/v2_0/status/hosts.py b/surveilclient/v2_0/status/hosts.py new file mode 100644 index 0000000..7da2750 --- /dev/null +++ b/surveilclient/v2_0/status/hosts.py @@ -0,0 +1,26 @@ +# 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 HostsManager(surveil_manager.SurveilManager): + base_url = '/status/hosts' + + def list(self, live_query={'filters': '{}'}): + """Get a list of hosts.""" + resp, body = self.http_client.json_request( + HostsManager.base_url, 'POST', body=live_query + ) + return body diff --git a/surveilclient/v2_0/status/services.py b/surveilclient/v2_0/status/services.py new file mode 100644 index 0000000..9ca619e --- /dev/null +++ b/surveilclient/v2_0/status/services.py @@ -0,0 +1,26 @@ +# 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 ServicesManager(surveil_manager.SurveilManager): + base_url = '/status/services' + + def list(self, live_query={'filters': '{}'}): + """Get a list of hosts.""" + resp, body = self.http_client.json_request( + ServicesManager.base_url, 'POST', body=live_query + ) + return body diff --git a/test-requirements.txt b/test-requirements.txt index e04aeb8..aee7f3a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,4 +4,4 @@ sphinx oslosphinx testrepository mox3>=0.7.0 -httpretty +httpretty==0.8.3