]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[153-netconf-control-socket] Extended command
authorFrancis Dupont <fdupont@isc.org>
Sun, 7 Oct 2018 21:09:23 +0000 (23:09 +0200)
committerFrancis Dupont <fdupont@isc.org>
Tue, 16 Oct 2018 16:34:34 +0000 (12:34 -0400)
src/lib/cc/command_interpreter.cc
src/lib/cc/command_interpreter.h
src/lib/cc/tests/command_interpreter_unittests.cc

index bbdc3570fcdbadea436a2b6d1dc4b31e33ce8e53..2d4d89b1e7f3803e74da84c5e84278099eff43fd 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -26,6 +26,7 @@ const char *CONTROL_COMMAND = "command";
 const char *CONTROL_RESULT = "result";
 const char *CONTROL_TEXT = "text";
 const char *CONTROL_ARGUMENTS = "arguments";
+  const char *CONTROL_SERVICE = "service";
 
 // Full version, with status, text and arguments
 ConstElementPtr
@@ -134,17 +135,34 @@ answerToText(const ConstElementPtr& msg) {
 
 ConstElementPtr
 createCommand(const std::string& command) {
-    return (createCommand(command, ElementPtr()));
+    return (createCommand(command, ElementPtr(), ""));
 }
 
 ConstElementPtr
 createCommand(const std::string& command, ConstElementPtr arg) {
+    return (createCommand(command, arg, ""));
+}
+
+ConstElementPtr
+createCommand(const std::string& command, const std::string& service) {
+    return (createCommand(command, ElementPtr(), service));
+}
+
+ConstElementPtr
+createCommand(const std::string& command,
+              ConstElementPtr arg,
+              const std::string& service) {
     ElementPtr query = Element::createMap();
     ElementPtr cmd = Element::create(command);
     query->set(CONTROL_COMMAND, cmd);
     if (arg) {
         query->set(CONTROL_ARGUMENTS, arg);
     }
+    if (!service.empty()) {
+        ElementPtr services = Element::createList();
+        services->add(Element::create(service));
+        query->set(CONTROL_SERVICE, services);
+    }
     return (query);
 }
 
index eeaa500517cef5c91f2a92ce825d588abd14b7ea..1c29bdb38447855024e56846e0c5be9410a872b3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -121,6 +121,29 @@ isc::data::ConstElementPtr createCommand(const std::string& command);
 isc::data::ConstElementPtr createCommand(const std::string& command,
                                          isc::data::ConstElementPtr arg);
 
+/// @brief Creates a standard config/command command message with no
+/// argument and with the given service (of the form
+/// { "command": "my_command", "service": [ service ] })
+///
+/// @param command The command string
+/// @param service The target service. May be empty.
+/// @return The created message
+ isc::data::ConstElementPtr createCommand(const std::string& command,
+                                          const std::string& service);
+
+/// @brief Creates a standard config/command command message with the
+/// given argument and given service (of the form
+/// { "command": "my_command", "arguments": arg, "service": [ service ] }
+///
+/// @param command The command string
+/// @param arg The optional argument for the command. This can be of
+///        any Element type. May be NULL.
+/// @param service The target service. May be empty.
+/// @return The created message
+isc::data::ConstElementPtr createCommand(const std::string& command,
+                                         isc::data::ConstElementPtr arg,
+                                         const std::string& service);
+
 /// @brief Parses the given command into a string containing the actual
 ///        command and an ElementPtr containing the optional argument.
 ///
index df381d0f46978cb73bbe00daf54ccdf290a65b49..18e1f6ab2f9be7b90fbcb1fb58eddc64f4d317a6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -105,6 +105,7 @@ TEST(CommandInterpreterTest, answerToText) {
 TEST(CommandInterpreterTest, createCommand) {
     ConstElementPtr command;
     ConstElementPtr arg;
+    string service;
 
     command = createCommand("my_command");
     ASSERT_EQ("{ \"command\": \"my_command\" }", command->str());
@@ -123,6 +124,31 @@ TEST(CommandInterpreterTest, createCommand) {
     command = createCommand("foo", arg);
     ASSERT_EQ("{ \"arguments\": { \"a\": \"map\" }, \"command\": \"foo\" }",
               command->str());
+
+    command = createCommand("my_command", "my_service");
+    ASSERT_EQ("{ \"command\": \"my_command\", "
+              "\"service\": [ \"my_service\" ] }",
+              command->str());
+
+    arg = el("1");
+    command = createCommand("my_command", arg, "my_service");
+    ASSERT_EQ("{ \"arguments\": 1, \"command\": \"my_command\", "
+              "\"service\": [ \"my_service\" ] }",
+              command->str());
+
+    arg = el("[ \"a\", \"b\" ]");
+    command = createCommand("my_cmd", arg, "my_server");
+    ASSERT_EQ("{ \"arguments\": [ \"a\", \"b\" ], "
+              "\"command\": \"my_cmd\", "
+              "\"service\": [ \"my_server\" ] }",
+              command->str());
+
+    arg = el("{ \"a\": \"map\" }");
+    command = createCommand("foo", arg, "bar");
+    ASSERT_EQ("{ \"arguments\": { \"a\": \"map\" }, "
+              "\"command\": \"foo\", "
+              "\"service\": [ \"bar\" ] }",
+              command->str());
 }
 
 // This test checks whether parseCommand function is able to parse various valid