]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#93,!35] Added deletion of subnets, networks and option defs in MySQL CB.
authorMarcin Siodelski <marcin@isc.org>
Thu, 27 Sep 2018 08:33:04 +0000 (10:33 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 8 Oct 2018 14:39:22 +0000 (16:39 +0200)
src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc
src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc

index 88667acc86b64c861a369dca6f76df9f088921c1..2e49b350b189f1df68cf55de0b57421cc55e569c 100644 (file)
@@ -59,7 +59,14 @@ public:
         UPDATE_SUBNET4,
         UPDATE_SHARED_NETWORK4,
         UPDATE_OPTION_DEF4,
+        DELETE_SUBNET4_ID,
+        DELETE_SUBNET4_PREFIX,
+        DELETE_ALL_SUBNETS4,
         DELETE_POOLS4_SUBNET_ID,
+        DELETE_SHARED_NETWORK4_NAME,
+        DELETE_ALL_SHARED_NETWORKS4,
+        DELETE_OPTION_DEF4_CODE_NAME,
+        DELETE_ALL_OPTION_DEFS4,
         NUM_STATEMENTS
     };
 
@@ -394,6 +401,40 @@ public:
         conn_.insertQuery(INSERT_POOL4, in_bindings);
     }
 
+    /// @brief Sends query to delete rows from a table.
+    ///
+    /// @param index Index of the statement to be executed.
+    void deleteFromTable(const StatementIndex& index) {
+        MySqlBindingCollection in_bindings;
+        conn_.updateDeleteQuery(index, in_bindings);
+    }
+
+    /// @brief Sends query to delete rows from a table.
+    ///
+    /// @param index Index of the statement to be executed.
+    /// @param key String value to be used as input binding to the delete
+    /// statement.
+    void deleteFromTable(const StatementIndex& index,
+                         const std::string& key) {
+        MySqlBindingCollection in_bindings = {
+            MySqlBinding::createString(key)
+        };
+        conn_.updateDeleteQuery(index, in_bindings);
+    }
+
+    /// @brief Sends query to delete subnet by id.
+    ///
+    /// @param selector Server selector.
+    /// @param subnet_id Identifier of the subnet to be deleted.
+    void deleteSubnet4(const ServerSelector& selector,
+                       const SubnetID& subnet_id) {
+        MySqlBindingCollection in_bindings;
+        in_bindings.push_back(MySqlBinding::createInteger<uint32_t>(subnet_id));
+
+        // Run DELETE.
+        conn_.updateDeleteQuery(DELETE_SUBNET4_ID, in_bindings);
+    }
+
     /// @brief Deletes pools belonging to a subnet from the database.
     ///
     /// @param subnet Pointer to the subnet for which pools should be
@@ -755,6 +796,23 @@ public:
         }
     }
 
+    /// @brief Sends query to delete option definition by code and
+    /// option space name.
+    ///
+    /// @param selector Server selector.
+    /// @param code Option code.
+    /// @param name Option name. 
+    void deleteOptionDef4(const ServerSelector& selector, const uint16_t code,
+                          const std::string& space) {
+        MySqlBindingCollection in_bindings = {
+            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)),
+            MySqlBinding::createString(space)
+        };
+
+        // Run DELETE.
+        conn_.updateDeleteQuery(DELETE_OPTION_DEF4_CODE_NAME, in_bindings);
+    }
+
     /// @brief Creates input binding for relay addresses.
     ///
     /// @param network Pointer to a shared network or subnet for which binding
@@ -1182,10 +1240,42 @@ TaggedStatementArray tagged_statements = { {
       "  user_context = ? "
       "WHERE code = ? AND space = ?" },
 
+    // Delete subnet by id.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_SUBNET4_ID,
+      "DELETE FROM dhcp4_subnet "
+      "WHERE subnet_id = ?" },
+
+    // Delete subnet by prefix.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_SUBNET4_PREFIX,
+      "DELETE FROM dhcp4_subnet "
+      "WHERE subnet_prefix = ?" },
+
+    // Delete all subnets.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_ALL_SUBNETS4,
+      "DELETE FROM dhcp4_subnet" },
+
     // Delete pools for a subnet.
     { MySqlConfigBackendDHCPv4Impl::DELETE_POOLS4_SUBNET_ID,
       "DELETE FROM dhcp4_pool "
-      "WHERE subnet_id = ?" }
+      "WHERE subnet_id = ?" },
+
+    // Delete shared network by name.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_SHARED_NETWORK4_NAME,
+      "DELETE FROM dhcp4_shared_network "
+      "WHERE name = ?" },
+
+    // Delete all shared networks.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_ALL_SHARED_NETWORKS4,
+      "DELETE FROM dhcp4_shared_network" },
+
+    // Delete option definition.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_OPTION_DEF4_CODE_NAME,
+      "DELETE FROM dhcp4_option_def "
+      "WHERE code = ? AND space = ?" },
+
+    // Delete all option definitions.
+    { MySqlConfigBackendDHCPv4Impl::DELETE_ALL_OPTION_DEFS4,
+      "DELETE FROM dhcp4_option_def" }
 }
 };
 
@@ -1401,34 +1491,43 @@ MySqlConfigBackendDHCPv4::createUpdateGlobalParameter4(const ServerSelector& sel
 void
 MySqlConfigBackendDHCPv4::deleteSubnet4(const ServerSelector& selector,
                                         const std::string& subnet_prefix) {
+    impl_->deleteFromTable(MySqlConfigBackendDHCPv4Impl::DELETE_SUBNET4_PREFIX,
+                           subnet_prefix);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteSubnet4(const ServerSelector& selector,
                                         const SubnetID& subnet_id) {
+    impl_->deleteSubnet4(selector, subnet_id);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteAllSubnets4(const ServerSelector& selector) {
+    impl_->deleteFromTable(MySqlConfigBackendDHCPv4Impl::DELETE_ALL_SUBNETS4);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteSharedNetwork4(const ServerSelector& selector,
                                                const std::string& name) {
+    impl_->deleteFromTable(MySqlConfigBackendDHCPv4Impl::DELETE_SHARED_NETWORK4_NAME,
+                           name);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteAllSharedNetworks4(const ServerSelector& selector) {
+    impl_->deleteFromTable(MySqlConfigBackendDHCPv4Impl::DELETE_ALL_SHARED_NETWORKS4);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteOptionDef4(const ServerSelector& selector,
                                            const uint16_t code,
                                            const std::string& space) {
+    impl_->deleteOptionDef4(selector, code, space);
 }
 
 void
 MySqlConfigBackendDHCPv4::deleteAllOptionDefs4(const ServerSelector& selector) {
+    impl_->deleteFromTable(MySqlConfigBackendDHCPv4Impl::DELETE_ALL_OPTION_DEFS4);
 }
 
 void
index 62f5798bf731ff7c9f18db45b550fa30bae03305..8a2a7c50e5341bfece4888be35c08d86fb1fd3f9 100644 (file)
@@ -240,7 +240,7 @@ TEST_F(MySqlConfigBackendDHCPv4Test, getSubnet4ByPrefix) {
     EXPECT_EQ(subnet->toElement()->str(), returned_subnet->toElement()->str());
 }
 
-// Test that all subnets can be fetched.
+// Test that all subnets can be fetched and then deleted.
 TEST_F(MySqlConfigBackendDHCPv4Test, getAllSubnets4) {
     // Insert test subnets into the database. Note that the second subnet will
     // overwrite the first subnet as they use the same ID.
@@ -257,6 +257,21 @@ TEST_F(MySqlConfigBackendDHCPv4Test, getAllSubnets4) {
         EXPECT_EQ(test_subnets_[i + 1]->toElement()->str(),
                   subnets[i]->toElement()->str());
     }
+
+    // Delete first subnet by id and verify that it is gone.
+    cbptr_->deleteSubnet4(ServerSelector::UNASSIGNED(), test_subnets_[1]->getID());
+    subnets = cbptr_->getAllSubnets4(ServerSelector::UNASSIGNED());
+    ASSERT_EQ(test_subnets_.size() - 2, subnets.size());
+
+    // Delete second subnet by prefix and verify it is gone.
+    cbptr_->deleteSubnet4(ServerSelector::UNASSIGNED(), test_subnets_[2]->toText());
+    subnets = cbptr_->getAllSubnets4(ServerSelector::UNASSIGNED());
+    ASSERT_EQ(test_subnets_.size() - 3, subnets.size());
+
+    // Delete all.
+    cbptr_->deleteAllSubnets4(ServerSelector::UNASSIGNED());
+    subnets = cbptr_->getAllSubnets4(ServerSelector::UNASSIGNED());
+    ASSERT_TRUE(subnets.empty());
 }
 
 // Test that subnets modified after given time can be fetched.
@@ -342,6 +357,17 @@ TEST_F(MySqlConfigBackendDHCPv4Test, getAllSharedNetworks4) {
         EXPECT_EQ(test_networks_[i + 1]->toElement()->str(),
                   networks[i]->toElement()->str());
     }
+
+    // Delete first shared network and verify it is gone..
+    cbptr_->deleteSharedNetwork4(ServerSelector::UNASSIGNED(),
+                                 test_networks_[1]->getName());
+    networks = cbptr_->getAllSharedNetworks4(ServerSelector::UNASSIGNED());
+    ASSERT_EQ(test_networks_.size() - 2, networks.size());
+
+    // Delete all.
+    cbptr_->deleteAllSharedNetworks4(ServerSelector::UNASSIGNED());
+    networks = cbptr_->getAllSharedNetworks4(ServerSelector::UNASSIGNED());
+    ASSERT_TRUE(networks.empty());
 }
 
 // Test that shared networks modified after given time can be fetched.
@@ -431,6 +457,19 @@ TEST_F(MySqlConfigBackendDHCPv4Test, getAllOptionDefs4) {
         ASSERT_TRUE(success) << "failed for option definition " << (*def)->getCode()
             << ", option space " << (*def)->getOptionSpaceName();
     }
+
+    // Delete one of the option definitions and see if it is gone.
+    cbptr_->deleteOptionDef4(ServerSelector::UNASSIGNED(),
+                             test_option_defs_[2]->getCode(),
+                             test_option_defs_[2]->getOptionSpaceName());
+    ASSERT_FALSE(cbptr_->getOptionDef4(ServerSelector::UNASSIGNED(),
+                                       test_option_defs_[2]->getCode(),
+                                       test_option_defs_[2]->getOptionSpaceName()));
+
+    // Delete all remaining option definitions.
+    cbptr_->deleteAllOptionDefs4(ServerSelector::UNASSIGNED());
+    option_defs = cbptr_->getAllOptionDefs4(ServerSelector::UNASSIGNED());
+    ASSERT_TRUE(option_defs.empty());
 }
 
 // Test that option definitions modified after given time can be fetched.