Merge "Add commands to API"
This commit is contained in:
commit
1188567bb5
113
surveilclient/tests/v2_0/config/test_commands.py
Normal file
113
surveilclient/tests/v2_0/config/test_commands.py
Normal file
@ -0,0 +1,113 @@
|
||||
# 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 TestCommands(clienttest.ClientTest):
|
||||
|
||||
@httpretty.activate
|
||||
def test_list(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.GET,
|
||||
"http://localhost:8080/v2/config/commands",
|
||||
body='[{"command_name":"myCommand"}]'
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.client.config.commands.list(),
|
||||
[{"command_name": "myCommand"}]
|
||||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_create(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.POST, "http://localhost:8080/v2/config/commands",
|
||||
body='{"command_name": "new_command", "command_line": "new_line"}'
|
||||
)
|
||||
|
||||
self.client.config.commands.create(
|
||||
command_name="new_command",
|
||||
command_line="new_line"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
json.loads(httpretty.last_request().body.decode()),
|
||||
{
|
||||
"command_name": "new_command",
|
||||
"command_line": "new_line"
|
||||
}
|
||||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_show(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.GET,
|
||||
"http://localhost:8080/v2/config/commands/command_to_show",
|
||||
body='{"command_name": "command_to_show", "command_line": "line"}'
|
||||
)
|
||||
|
||||
command = self.client.config.commands.get(
|
||||
command_name="command_to_show"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
command,
|
||||
{
|
||||
"command_name": "command_to_show",
|
||||
"command_line": "line"
|
||||
}
|
||||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_update(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.PUT,
|
||||
"http://localhost:8080/v2/config/commands/command_to_update",
|
||||
body='{"command_line": "updated command_line"}'
|
||||
)
|
||||
|
||||
self.client.config.commands.update(
|
||||
command_name="command_to_update",
|
||||
command_line="updated command_line"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
json.loads(httpretty.last_request().body.decode()),
|
||||
{
|
||||
"command_name": "command_to_update",
|
||||
"command_line": "updated command_line"
|
||||
}
|
||||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_delete(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.DELETE,
|
||||
"http://localhost:8080/v2/config/commands/command_to_delete",
|
||||
body="body"
|
||||
)
|
||||
|
||||
body = self.client.config.commands.delete(
|
||||
command_name="command_to_delete",
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
body,
|
||||
"body"
|
||||
)
|
@ -14,6 +14,7 @@
|
||||
|
||||
from surveilclient.common import surveil_manager
|
||||
from surveilclient.v2_0.config import checkmodulations
|
||||
from surveilclient.v2_0.config import commands
|
||||
from surveilclient.v2_0.config import hosts
|
||||
from surveilclient.v2_0.config import services
|
||||
|
||||
@ -23,10 +24,11 @@ class ConfigManager(surveil_manager.SurveilManager):
|
||||
|
||||
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)
|
||||
self.checkmodulations = checkmodulations.CheckModulationsManager(
|
||||
self.http_client)
|
||||
self.commands = commands.CommandsManager(self.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(
|
||||
@ -34,4 +36,4 @@ class ConfigManager(surveil_manager.SurveilManager):
|
||||
'POST',
|
||||
body='' # Must send empty body
|
||||
)
|
||||
return body
|
||||
return body
|
||||
|
59
surveilclient/v2_0/config/commands.py
Normal file
59
surveilclient/v2_0/config/commands.py
Normal file
@ -0,0 +1,59 @@
|
||||
# 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 CommandsManager(surveil_manager.SurveilManager):
|
||||
base_url = '/config/commands'
|
||||
|
||||
def list(self):
|
||||
"""Get a list of commands."""
|
||||
resp, body = self.http_client.json_request(
|
||||
CommandsManager.base_url, 'GET'
|
||||
)
|
||||
return body
|
||||
|
||||
def create(self, **kwargs):
|
||||
"""Create a new command."""
|
||||
resp, body = self.http_client.json_request(
|
||||
CommandsManager.base_url, 'POST',
|
||||
body=kwargs
|
||||
)
|
||||
return body
|
||||
|
||||
def get(self, command_name):
|
||||
"""Get a new command."""
|
||||
resp, body = self.http_client.json_request(
|
||||
CommandsManager.base_url + '/' + command_name, 'GET',
|
||||
body=''
|
||||
)
|
||||
return body
|
||||
|
||||
def update(self, **kwargs):
|
||||
"""Update a command."""
|
||||
resp, body = self.http_client.json_request(
|
||||
CommandsManager.base_url + '/' + kwargs['command_name'], 'PUT',
|
||||
body=kwargs
|
||||
)
|
||||
return body
|
||||
|
||||
def delete(self, command_name):
|
||||
"""Delete a command."""
|
||||
resp, body = self.http_client.request(
|
||||
CommandsManager.base_url + "/" + command_name,
|
||||
'DELETE',
|
||||
body=''
|
||||
)
|
||||
return body
|
@ -229,6 +229,67 @@ def do_config_checkmodulation_delete(sc, args):
|
||||
sc.config.checkmodulations.delete(args.checkmodulation_name)
|
||||
|
||||
|
||||
def do_config_command_list(sc, args):
|
||||
"""List all config commands."""
|
||||
commands = sc.config.commands.list()
|
||||
|
||||
if args.json:
|
||||
print(utils.json_formatter(commands))
|
||||
else:
|
||||
cols = [
|
||||
'command_name',
|
||||
'command_line'
|
||||
]
|
||||
|
||||
formatters = {
|
||||
'command_name': lambda x: x['command_name'],
|
||||
'command_line': lambda x: x['command_line']
|
||||
}
|
||||
utils.print_list(commands, cols, formatters=formatters)
|
||||
|
||||
|
||||
@cliutils.arg("--command_name")
|
||||
@cliutils.arg("--command_line")
|
||||
def do_config_command_create(sc, args):
|
||||
"""Create a config check modulation."""
|
||||
arg_names = ['command_name',
|
||||
'command_line']
|
||||
command = _dict_from_args(args, arg_names)
|
||||
sc.config.commands.create(**command)
|
||||
|
||||
|
||||
@cliutils.arg("--command_name", help="Name of the command")
|
||||
def do_config_command_delete(sc, args):
|
||||
"""Delete a config command."""
|
||||
sc.config.commands.delete(args.command_name)
|
||||
|
||||
|
||||
@cliutils.arg("--command_name", help="Name of the command")
|
||||
def do_config_command_show(sc, args):
|
||||
"""Show a specific command."""
|
||||
command = sc.config.commands.get(args.command_name)
|
||||
|
||||
if args.json:
|
||||
print(utils.json_formatter(command))
|
||||
elif command:
|
||||
""" Specify the shown order and all the properties to display """
|
||||
command_properties = [
|
||||
'command_name', 'command_line'
|
||||
]
|
||||
|
||||
utils.print_item(command, command_properties)
|
||||
|
||||
|
||||
@cliutils.arg("--command_name", help="Name of the command")
|
||||
@cliutils.arg("--command_line", help="Address of the command")
|
||||
def do_config_command_update(sc, args):
|
||||
"""Update a config command."""
|
||||
arg_names = ['command_name',
|
||||
'command_line']
|
||||
command = _dict_from_args(args, arg_names)
|
||||
sc.config.commands.update(**command)
|
||||
|
||||
|
||||
def do_config_reload(sc, args):
|
||||
"""Trigger a config reload."""
|
||||
print(sc.config.reload_config()['message'])
|
||||
|
Loading…
x
Reference in New Issue
Block a user