From 73fd28460f63efb8fe26edcf7f4f23a469782953 Mon Sep 17 00:00:00 2001 From: Wlodek Wencel Date: Tue, 25 Apr 2017 20:53:52 +0200 Subject: [PATCH] [trac5262] kea-shell extended with target service support --- doc/guide/shell.xml | 17 +++++++++--- src/bin/shell/kea-shell.in | 4 +++ src/bin/shell/kea-shell.xml | 11 +++++++- src/bin/shell/kea_conn.py | 6 +++++ src/bin/shell/tests/shell_unittest.py.in | 33 ++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) diff --git a/doc/guide/shell.xml b/doc/guide/shell.xml index 778d5c5f87..78a41e18ec 100644 --- a/doc/guide/shell.xml +++ b/doc/guide/shell.xml @@ -25,7 +25,7 @@ Shell Usage kea-shell is run as follows: -kea-shell [--host hostname] [--port number] [--timeout seconds] [command] +kea-shell [--host hostname] [--port number] [--timeout seconds] [--service service-name] [command] where: @@ -51,6 +51,14 @@ kea-shell [--host hostname] [--port number] [--timeout seconds] [command] + + + --service serive-name specifies the + target of an command. If not given, CA will be used as target. May be used multiple times + + + + command specifies the command to be sent. If not specified, @@ -89,18 +97,19 @@ kea-shell [--host hostname] [--port number] [--timeout seconds] [command] The following shows a simple example of usage: -$ kea-shell --host 192.0.2.1 --port 8001 list-commands +$ kea-shell --host 192.0.2.1 --port 8001 --service dhcp4 list-commands ^D After the command line is entered, the program waits for command parameters to be entered. Since list-commands does not take any arguments, CTRL-D (represented in the above example by "^D") is pressed to indicate end of file (and so terminate the parameter input). The Shell will then contact - the CA and print out the list of available commands returned. + the CA and print out the list of available commands returned for service named dhcp4. It is envisaged that Kea Shell will be most frequently used in scripts. The next example - shows a simple scripted execution. It sends the command "config-write" to the CA, along + shows a simple scripted execution. It sends the command "config-write" to the CA + ( --service parameter hasn't been used), along with the parameters specified in param.json. The result will be stored in result.json. $ cat param.json diff --git a/src/bin/shell/kea-shell.in b/src/bin/shell/kea-shell.in index 5bb40767de..47ee6c2999 100644 --- a/src/bin/shell/kea-shell.in +++ b/src/bin/shell/kea-shell.in @@ -63,6 +63,9 @@ def shell_body(): parser.add_argument('--timeout', type=int, default='10', help='Timeout (in seconds) when attempting to ' 'connect to CA (default: 10)') + parser.add_argument('--service', nargs="?", action="append", + help='target spcified service. If not specidied,' + 'control agent will receive command.') parser.add_argument('command', type=str, nargs="?", default='list-commands', help='command to be executed. If not specified, ' @@ -78,6 +81,7 @@ def shell_body(): # used by the connection. params = CARequest() params.command = cmd_args.command + params.service = cmd_args.service params.http_host = cmd_args.host params.http_port = cmd_args.port params.timeout = cmd_args.timeout diff --git a/src/bin/shell/kea-shell.xml b/src/bin/shell/kea-shell.xml index 3a3b2dd457..bdaca58091 100644 --- a/src/bin/shell/kea-shell.xml +++ b/src/bin/shell/kea-shell.xml @@ -40,7 +40,7 @@ - 2016 + 2017 Internet Systems Consortium, Inc. ("ISC") @@ -53,6 +53,7 @@ + @@ -116,6 +117,14 @@ + + + + Specifies the service that is tartget of a command. If not + specified, Cotrol Agent will be targeted. May be used multiple times. + + + diff --git a/src/bin/shell/kea_conn.py b/src/bin/shell/kea_conn.py index 45bd9b7490..ca4aafc8ae 100644 --- a/src/bin/shell/kea_conn.py +++ b/src/bin/shell/kea_conn.py @@ -16,6 +16,7 @@ class CARequest: - http_host - hostname of the CA - http-port - TCP port of the CA - command - specifies the command to send (e.g. list-commands) + - service - specifies service that is target for the command (e.g. dhcp4) - timeout - timeout (in ms) - args - extra arguments my be added here - headers - extra HTTP headers may be added here @@ -25,6 +26,7 @@ class CARequest: http_host = '' http_port = 0 command = '' + service = '' timeout = 0 args = '' headers = {} @@ -39,6 +41,10 @@ class CARequest: this stores the output in self.content """ self.content = '{ "command": "' + self.command + '"' + if self.service is not None: + self.service = [x for x in self.service if x] + if len(self.service) > 0: + self.content += ', "service": ["' + '","'.join(self.service) + '"]' if len(self.args) > 1: self.content += ', "arguments": { ' + self.args + ' }' self.content += ' }' diff --git a/src/bin/shell/tests/shell_unittest.py.in b/src/bin/shell/tests/shell_unittest.py.in index 8a0d1e0ede..1fd6ec2003 100644 --- a/src/bin/shell/tests/shell_unittest.py.in +++ b/src/bin/shell/tests/shell_unittest.py.in @@ -26,6 +26,39 @@ class CARequestUnitTest(unittest.TestCase): """ pass + def test_body_with_service(self): + """ + This test verifies if the CARequest object generates the request + content properly when there is one target service. + """ + request = CARequest() + request.command = "foo" + request.service= ["service1"] + request.generate_body() + self.assertEqual(request.content, '{ "command": "foo", "service": ["service1"] }') + + def test_body_with_multiple_service(self): + """ + This test verifies if the CARequest object generates the request + content properly when there are two target service. + """ + request = CARequest() + request.command = "foo" + request.service= ["service1","service2/2"] + request.generate_body() + self.assertEqual(request.content, '{ "command": "foo", "service": ["service1","service2/2"] }') + + def test_body_with_malformed_service(self): + """ + This test verifies if the CARequest object generates the request + content properly when there are two target service, one is empty + """ + request = CARequest() + request.command = "foo" + request.service= ["service1",""] + request.generate_body() + self.assertEqual(request.content, '{ "command": "foo", "service": ["service1"] }') + def test_body_without_args(self): """ This test verifies if the CARequest object generates the request -- 2.47.2