Browse Source

[trac5262] kea-shell extended with target service support

Wlodek Wencel 8 years ago
parent
commit
92d6db67e3

+ 13 - 4
doc/guide/shell.xml

@@ -25,7 +25,7 @@
     <title>Shell Usage</title>
     <title>Shell Usage</title>
     <para><command>kea-shell</command> is run as follows:
     <para><command>kea-shell</command> is run as follows:
 <screen>
 <screen>
-kea-shell [--host hostname] [--port number] [--timeout seconds] [command]
+kea-shell [--host hostname] [--port number] [--timeout seconds] [--service service-name] [command]
 </screen>
 </screen>
     where:
     where:
     </para>
     </para>
@@ -53,6 +53,14 @@ kea-shell [--host hostname] [--port number] [--timeout seconds] [command]
 
 
       <listitem>
       <listitem>
         <simpara>
         <simpara>
+          <command>--service <replaceable>serive-name</replaceable></command> specifies the
+          target of an command. If not given, CA will be used as target. May be used multiple times
+        </simpara>
+      </listitem>
+
+
+      <listitem>
+        <simpara>
           <command>command</command> specifies the command to be sent. If not specified,
           <command>command</command> specifies the command to be sent. If not specified,
           <command>list-commands</command> command is used.
           <command>list-commands</command> command is used.
         </simpara>
         </simpara>
@@ -89,18 +97,19 @@ kea-shell [--host hostname] [--port number] [--timeout seconds] [command]
 
 
     <para>The following shows a simple example of usage:
     <para>The following shows a simple example of usage:
 <screen>
 <screen>
-$ <userinput>kea-shell --host 192.0.2.1 --port 8001 list-commands</userinput>
+$ <userinput>kea-shell --host 192.0.2.1 --port 8001 --service dhcp4 list-commands</userinput>
 ^D
 ^D
 </screen>
 </screen>
     After the command line is entered, the program waits for command parameters to be entered.
     After the command line is entered, the program waits for command parameters to be entered.
     Since <command>list-commands</command> does not take any
     Since <command>list-commands</command> does not take any
     arguments, CTRL-D (represented in the above example by "^D") is pressed to indicate end
     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
     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 <command>dhcp4</command>.
     </para>
     </para>
 
 
     <para>It is envisaged that Kea Shell will be most frequently used in scripts. The next example
     <para>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
+    (<command> --service </command> parameter hasn't been used), along
     with the parameters specified in param.json. The result will be stored in result.json.
     with the parameters specified in param.json. The result will be stored in result.json.
 <screen>
 <screen>
 $ cat param.json
 $ cat param.json

+ 4 - 0
src/bin/shell/kea-shell.in

@@ -63,6 +63,9 @@ def shell_body():
     parser.add_argument('--timeout', type=int, default='10',
     parser.add_argument('--timeout', type=int, default='10',
                         help='Timeout (in seconds) when attempting to '
                         help='Timeout (in seconds) when attempting to '
                         'connect to CA (default: 10)')
                         '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="?",
     parser.add_argument('command', type=str, nargs="?",
                         default='list-commands',
                         default='list-commands',
                         help='command to be executed. If not specified, '
                         help='command to be executed. If not specified, '
@@ -78,6 +81,7 @@ def shell_body():
     # used by the connection.
     # used by the connection.
     params = CARequest()
     params = CARequest()
     params.command = cmd_args.command
     params.command = cmd_args.command
+    params.service = cmd_args.service
     params.http_host = cmd_args.host
     params.http_host = cmd_args.host
     params.http_port = cmd_args.port
     params.http_port = cmd_args.port
     params.timeout = cmd_args.timeout
     params.timeout = cmd_args.timeout

+ 10 - 1
src/bin/shell/kea-shell.xml

@@ -40,7 +40,7 @@
 
 
   <docinfo>
   <docinfo>
     <copyright>
     <copyright>
-      <year>2016</year>
+      <year>2017</year>
       <holder>Internet Systems Consortium, Inc. ("ISC")</holder>
       <holder>Internet Systems Consortium, Inc. ("ISC")</holder>
     </copyright>
     </copyright>
   </docinfo>
   </docinfo>
@@ -53,6 +53,7 @@
       <arg><option>--host</option></arg>
       <arg><option>--host</option></arg>
       <arg><option>--port</option></arg>
       <arg><option>--port</option></arg>
       <arg><option>--timeout</option></arg>
       <arg><option>--timeout</option></arg>
+      <arg><option>--service</option></arg>
       <arg><option>command</option></arg>
       <arg><option>command</option></arg>
     </cmdsynopsis>
     </cmdsynopsis>
   </refsynopsisdiv>
   </refsynopsisdiv>
@@ -117,6 +118,14 @@
       </varlistentry>
       </varlistentry>
 
 
       <varlistentry>
       <varlistentry>
+        <term><option>--service</option></term>
+        <listitem><para>
+          Specifies the service that is tartget of a command. If not
+          specified, Cotrol Agent will be targeted. May be used multiple times.
+        </para></listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><option>command</option></term>
         <term><option>command</option></term>
         <listitem><para>
         <listitem><para>
           Specifies the command to be sent to CA. If not
           Specifies the command to be sent to CA. If not

+ 6 - 0
src/bin/shell/kea_conn.py

@@ -16,6 +16,7 @@ class CARequest:
      - http_host - hostname of the CA
      - http_host - hostname of the CA
      - http-port - TCP port of the CA
      - http-port - TCP port of the CA
      - command - specifies the command to send (e.g. list-commands)
      - 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)
      - timeout - timeout (in ms)
      - args - extra arguments my be added here
      - args - extra arguments my be added here
      - headers - extra HTTP headers may be added here
      - headers - extra HTTP headers may be added here
@@ -25,6 +26,7 @@ class CARequest:
     http_host = ''
     http_host = ''
     http_port = 0
     http_port = 0
     command = ''
     command = ''
+    service = ''
     timeout = 0
     timeout = 0
     args = ''
     args = ''
     headers = {}
     headers = {}
@@ -39,6 +41,10 @@ class CARequest:
         this stores the output in self.content
         this stores the output in self.content
         """
         """
         self.content = '{ "command": "' + self.command + '"'
         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:
         if len(self.args) > 1:
             self.content += ', "arguments": { ' + self.args + ' }'
             self.content += ', "arguments": { ' + self.args + ' }'
         self.content += ' }'
         self.content += ' }'

+ 33 - 0
src/bin/shell/tests/shell_unittest.py.in

@@ -26,6 +26,39 @@ class CARequestUnitTest(unittest.TestCase):
         """
         """
         pass
         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):
     def test_body_without_args(self):
         """
         """
         This test verifies if the CARequest object generates the request
         This test verifies if the CARequest object generates the request