]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1601] added handle-id to dhcp-enable/dhcp-disable
authorRazvan Becheriu <razvan@isc.org>
Thu, 10 Dec 2020 17:24:13 +0000 (19:24 +0200)
committerRazvan Becheriu <razvan@isc.org>
Fri, 22 Jan 2021 17:15:19 +0000 (17:15 +0000)
src/bin/dhcp4/ctrl_dhcp4_srv.cc
src/bin/dhcp6/ctrl_dhcp6_srv.cc
src/hooks/dhcp/high_availability/command_creator.cc
src/hooks/dhcp/high_availability/command_creator.h
src/hooks/dhcp/high_availability/ha_impl.cc
src/hooks/dhcp/high_availability/ha_service.cc
src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc

index af15fbbe23d2c861d42f841fd37825e701542079..8bea0fce974189154db0cac77aa1e5e4df0c0d07 100644 (file)
@@ -490,11 +490,12 @@ ControlledDhcpv4Srv::commandDhcpDisableHandler(const std::string&,
                                                ConstElementPtr args) {
     std::ostringstream message;
     int64_t max_period = 0;
-    int64_t handle_id = 0;
+    std::string handle_id;
 
     NetworkState::ControllerType type = NetworkState::COMMAND;
 
-    // Parse arguments to see if the 'max-period' parameter has been specified.
+    // Parse arguments to see if the 'max-period' of 'handle-id' parameters have
+    // been specified.
     if (args) {
         // Arguments must be a map.
         if (args->getType() != Element::map) {
@@ -524,16 +525,12 @@ ControlledDhcpv4Srv::commandDhcpDisableHandler(const std::string&,
             ConstElementPtr handle_id_element = args->get("handle-id");
             // handle-id is optional.
             if (handle_id_element) {
-                // It must be an integer, if specified.
-                if (handle_id_element->getType() != Element::integer) {
-                    message << "'handle-id' argument must be a number";
+                // It must be a string, if specified.
+                if (handle_id_element->getType() != Element::string) {
+                    message << "'handle-id' argument must be a string";
 
                 } else {
-                    // It must be positive integer.
-                    handle_id = handle_id_element->intValue();
-                    if (handle_id <= 0) {
-                        message << "'handle-id' must be positive integer";
-                    }
+                    handle_id = handle_id_element->stringValue();
                     type = NetworkState::HA;
                 }
             }
@@ -560,11 +557,11 @@ ConstElementPtr
 ControlledDhcpv4Srv::commandDhcpEnableHandler(const std::string&,
                                               ConstElementPtr args) {
     std::ostringstream message;
-    int64_t handle_id = 0;
+    std::string handle_id;
 
     NetworkState::ControllerType type = NetworkState::COMMAND;
 
-    // Parse arguments to see if the 'max-period' parameter has been specified.
+    // Parse arguments to see if the 'handle-id' parameter has been specified.
     if (args) {
         // Arguments must be a map.
         if (args->getType() != Element::map) {
@@ -574,16 +571,12 @@ ControlledDhcpv4Srv::commandDhcpEnableHandler(const std::string&,
             ConstElementPtr handle_id_element = args->get("handle-id");
             // handle-id is optional.
             if (handle_id_element) {
-                // It must be an integer, if specified.
-                if (handle_id_element->getType() != Element::integer) {
-                    message << "'handle-id' argument must be a number";
+                // It must be a string, if specified.
+                if (handle_id_element->getType() != Element::string) {
+                    message << "'handle-id' argument must be a string";
 
                 } else {
-                    // It must be positive integer.
-                    handle_id = handle_id_element->intValue();
-                    if (handle_id <= 0) {
-                        message << "'handle-id' must be positive integer";
-                    }
+                    handle_id = handle_id_element->stringValue();
                     type = NetworkState::HA;
                 }
             }
index e771b8549ff2e15dd2e9dbb92a9754f0046c7b2e..f497ccbe1461432d5e4307c2457ee4ff43b77e4d 100644 (file)
@@ -493,11 +493,12 @@ ControlledDhcpv6Srv::commandDhcpDisableHandler(const std::string&,
                                                ConstElementPtr args) {
     std::ostringstream message;
     int64_t max_period = 0;
-    int64_t handle_id = 0;
+    std::string handle_id;
 
     NetworkState::ControllerType type = NetworkState::COMMAND;
 
-    // Parse arguments to see if the 'max-period' parameter has been specified.
+    // Parse arguments to see if the 'max-period' of 'handle-id' parameters have
+    // been specified.
     if (args) {
         // Arguments must be a map.
         if (args->getType() != Element::map) {
@@ -527,16 +528,12 @@ ControlledDhcpv6Srv::commandDhcpDisableHandler(const std::string&,
             ConstElementPtr handle_id_element = args->get("handle-id");
             // handle-id is optional.
             if (handle_id_element) {
-                // It must be an integer, if specified.
-                if (handle_id_element->getType() != Element::integer) {
-                    message << "'handle-id' argument must be a number";
+                // It must be a string, if specified.
+                if (handle_id_element->getType() != Element::string) {
+                    message << "'handle-id' argument must be a string";
 
                 } else {
-                    // It must be positive integer.
-                    handle_id = handle_id_element->intValue();
-                    if (handle_id <= 0) {
-                        message << "'handle-id' must be positive integer";
-                    }
+                    handle_id = handle_id_element->stringValue();
                     type = NetworkState::HA;
                 }
             }
@@ -563,11 +560,11 @@ ConstElementPtr
 ControlledDhcpv6Srv::commandDhcpEnableHandler(const std::string&,
                                               ConstElementPtr args) {
     std::ostringstream message;
-    int64_t handle_id = 0;
+    std::string handle_id;
 
     NetworkState::ControllerType type = NetworkState::COMMAND;
 
-    // Parse arguments to see if the 'max-period' parameter has been specified.
+    // Parse arguments to see if the 'handle-id' parameter has been specified.
     if (args) {
         // Arguments must be a map.
         if (args->getType() != Element::map) {
@@ -577,16 +574,12 @@ ControlledDhcpv6Srv::commandDhcpEnableHandler(const std::string&,
             ConstElementPtr handle_id_element = args->get("handle-id");
             // handle-id is optional.
             if (handle_id_element) {
-                // It must be an integer, if specified.
-                if (handle_id_element->getType() != Element::integer) {
-                    message << "'handle-id' argument must be a number";
+                // It must be a string, if specified.
+                if (handle_id_element->getType() != Element::string) {
+                    message << "'handle-id' argument must be a string";
 
                 } else {
-                    // It must be positive integer.
-                    handle_id = handle_id_element->intValue();
-                    if (handle_id <= 0) {
-                        message << "'handle-id' must be positive integer";
-                    }
+                    handle_id = handle_id_element->stringValue();
                     type = NetworkState::HA;
                 }
             }
index 2fa8a1ff25ae502bebd7e56e64c3a0bab74b3170..2a3dc1158b962e60513cd4d82eded8d27c1333ca 100644 (file)
@@ -18,12 +18,14 @@ namespace isc {
 namespace ha {
 
 ConstElementPtr
-CommandCreator::createDHCPDisable(const unsigned int max_period,
+CommandCreator::createDHCPDisable(const std::string& service_name,
+                                  const unsigned int max_period,
                                   const HAServerType& server_type) {
     ElementPtr args;
+    args = Element::createMap();
+    args->set("handle-id", Element::create(service_name));
     // max-period is optional. A value of 0 means that it is not specified.
     if (max_period > 0) {
-        args = Element::createMap();
         args->set("max-period", Element::create(static_cast<long int>(max_period)));
     }
     ConstElementPtr command = config::createCommand("dhcp-disable", args);
@@ -32,8 +34,12 @@ CommandCreator::createDHCPDisable(const unsigned int max_period,
 }
 
 ConstElementPtr
-CommandCreator::createDHCPEnable(const HAServerType& server_type) {
-    ConstElementPtr command = config::createCommand("dhcp-enable");
+CommandCreator::createDHCPEnable(const std::string& service_name,
+                                 const HAServerType& server_type) {
+    ElementPtr args;
+    args = Element::createMap();
+    args->set("handle-id", Element::create(service_name));
+    ConstElementPtr command = config::createCommand("dhcp-enable", args);
     insertService(command, server_type);
     return (command);
 }
index e9ce338d4a05d29e72c19f87dda9cd954a5ebbe9..4d89e81eefc8e107c102315453e1ce8c3bcd8d40 100644 (file)
@@ -25,7 +25,8 @@ public:
     ///
     /// @return Pointer to the JSON representation of the command.
     static data::ConstElementPtr
-    createDHCPDisable(const unsigned int max_period,
+    createDHCPDisable(const std::string& service_name,
+                      const unsigned int max_period,
                       const HAServerType& server_type);
 
     /// @brief Creates dhcp-enable command for DHCP server.
@@ -33,7 +34,8 @@ public:
     /// @param server_type type of the DHCP server, i.e. v4 or v6.
     /// @return Pointer to the JSON representation of the command.
     static data::ConstElementPtr
-    createDHCPEnable(const HAServerType& server_type);
+    createDHCPEnable(const std::string& service_name,
+                     const HAServerType& server_type);
 
     /// @brief Creates ha-reset command.
     ///
index 84d92eb91e2dec7748807245e28d19e07f892fec..02e2f140d3b700a400c937a951d9faf0d415977f 100644 (file)
@@ -264,9 +264,7 @@ void
 HAImpl::commandProcessed(hooks::CalloutHandle& callout_handle) {
     std::string command_name;
     callout_handle.getArgument("name", command_name);
-    if (command_name == "dhcp-enable") {
-        service_->adjustNetworkState();
-    } else if (command_name == "status-get") {
+    if (command_name == "status-get") {
         // Get the response.
         ConstElementPtr response;
         callout_handle.getArgument("response", response);
index 8f62d659e77093d62e8e178d9c6444e688c32016..ab0b602f3f4ab2d1668f40be228707d41f18fccd 100644 (file)
@@ -1683,7 +1683,8 @@ HAService::asyncDisableDHCPService(HttpClient& http_client,
          HostHttpHeader(remote_config->getUrl().getHostname()));
 
     remote_config->addBasicAuthHttpHeader(request);
-    request->setBodyAsJson(CommandCreator::createDHCPDisable(max_period,
+    request->setBodyAsJson(CommandCreator::createDHCPDisable(server_name,
+                                                             max_period,
                                                              server_type_));
     request->finalize();
 
@@ -1757,7 +1758,8 @@ HAService::asyncEnableDHCPService(HttpClient& http_client,
         (HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(),
          HostHttpHeader(remote_config->getUrl().getHostname()));
     remote_config->addBasicAuthHttpHeader(request);
-    request->setBodyAsJson(CommandCreator::createDHCPEnable(server_type_));
+    request->setBodyAsJson(CommandCreator::createDHCPEnable(server_name,
+                                                            server_type_));
     request->finalize();
 
     // Response object should also be created because the HTTP client needs
index 406c4290eb457d93247798837a0c94a1c66ba20f..ad6cfb523d83e8bb1f273bcb218abc46b43f9db4 100644 (file)
@@ -150,24 +150,42 @@ testCommandBasics(const ConstElementPtr& command,
 // This test verifies that the dhcp-disable command is correct.
 TEST(CommandCreatorTest, createDHCPDisable4) {
     // Create command with max-period value set to 20.
-    ConstElementPtr command = CommandCreator::createDHCPDisable(20, HAServerType::DHCPv4);
+    ConstElementPtr command = CommandCreator::createDHCPDisable("this-server",
+                                                                20, HAServerType::DHCPv4);
     ConstElementPtr arguments;
     ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4",
                                               arguments));
+    ASSERT_EQ(2, arguments->size());
     ConstElementPtr max_period = arguments->get("max-period");
     ASSERT_TRUE(max_period);
     ASSERT_EQ(Element::integer, max_period->getType());
     EXPECT_EQ(20, max_period->intValue());
+    ConstElementPtr handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 
     // Repeat the test but this time the max-period is not specified.
-    command = CommandCreator::createDHCPDisable(0, HAServerType::DHCPv4);
-    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4"));
+    command = CommandCreator::createDHCPDisable("this-server", 0,
+                                                HAServerType::DHCPv4);
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4",
+                                              arguments));
+    ASSERT_EQ(1, arguments->size());
+    handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 }
 
 // This test verifies that the dhcp-enable command is correct.
 TEST(CommandCreatorTest, createDHCPEnable4) {
-    ConstElementPtr command = CommandCreator::createDHCPEnable(HAServerType::DHCPv4);
-    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp4"));
+    ConstElementPtr arguments;
+    ConstElementPtr command = CommandCreator::createDHCPEnable("this-server",
+                                                               HAServerType::DHCPv4);
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp4",
+                                              arguments));
+    ASSERT_EQ(1, arguments->size());
+    ConstElementPtr handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 }
 
 // This test verifies that the ha-reset command sent to DHCPv4 server is correct.
@@ -266,25 +284,43 @@ TEST(CommandCreatorTest, createLease4GetPageZeroLimit) {
 // correct.
 TEST(CommandCreatorTest, createDHCPDisable6) {
     // Create command with max-period value set to 20.
-    ConstElementPtr command = CommandCreator::createDHCPDisable(20, HAServerType::DHCPv6);
+    ConstElementPtr command = CommandCreator::createDHCPDisable("this-server",
+                                                                20, HAServerType::DHCPv6);
     ConstElementPtr arguments;
     ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6",
                                               arguments));
+    ASSERT_EQ(2, arguments->size());
     ConstElementPtr max_period = arguments->get("max-period");
     ASSERT_TRUE(max_period);
     ASSERT_EQ(Element::integer, max_period->getType());
     EXPECT_EQ(20, max_period->intValue());
+    ConstElementPtr handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 
     // Repeat the test but this time the max-period is not specified.
-    command = CommandCreator::createDHCPDisable(0, HAServerType::DHCPv6);
-    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6"));
+    command = CommandCreator::createDHCPDisable("this-server", 0,
+                                                HAServerType::DHCPv6);
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6",
+                                              arguments));
+    ASSERT_EQ(1, arguments->size());
+    handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 }
 
 // This test verifies that the dhcp-enable command (DHCPv6 case) is
 // correct.
 TEST(CommandCreatorTest, createDHCPEnable6) {
-    ConstElementPtr command = CommandCreator::createDHCPEnable(HAServerType::DHCPv6);
-    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp6"));
+    ConstElementPtr arguments;
+    ConstElementPtr command = CommandCreator::createDHCPEnable("this-server",
+                                                               HAServerType::DHCPv6);
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp6",
+                                              arguments));
+    ASSERT_EQ(1, arguments->size());
+    ConstElementPtr handle_id = arguments->get("handle-id");
+    ASSERT_TRUE(handle_id);
+    ASSERT_EQ("this-server", handle_id->stringValue());
 }
 
 // This test verifies that the ha-reset command sent to DHCPv6 server is correct.