Implemented V2_0 API

Change-Id: I0b5b42abe75ff9f6d0b2cbe87a240c0766e3310e
This commit is contained in:
aviau 2015-04-21 17:54:50 -04:00
parent a9d93864dd
commit 6cc71a2636
15 changed files with 342 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

View File

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

View File

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

View File

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

View File

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

112
surveilclient/v2_0/shell.py Normal file
View File

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

View File

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

View File

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

View File

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

View File

@ -4,4 +4,4 @@ sphinx
oslosphinx
testrepository
mox3>=0.7.0
httpretty
httpretty==0.8.3