]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#549] add an implementation for BaseHostDataSource::update
authorAndrei Pavel <andrei@isc.org>
Wed, 19 Apr 2023 16:20:37 +0000 (19:20 +0300)
committerAndrei Pavel <andrei@isc.org>
Wed, 19 Apr 2023 20:56:01 +0000 (23:56 +0300)
src/lib/dhcpsrv/base_host_data_source.h
src/lib/dhcpsrv/cfg_hosts.cc
src/lib/dhcpsrv/cfg_hosts.h
src/lib/dhcpsrv/mysql_host_data_source.cc
src/lib/dhcpsrv/pgsql_host_data_source.cc
src/lib/dhcpsrv/testutils/memory_host_data_source.cc
src/lib/dhcpsrv/testutils/memory_host_data_source.h

index 5e53424864082eabf1727236ab1034b72bb8f207..e5e42e28597a4287ab6e44b68a3eb65fbdedee4e 100644 (file)
@@ -462,7 +462,24 @@ public:
     /// @brief Attempts to update an existing host entry.
     ///
     /// @param host the host up to date with the requested changes
-    virtual void update(HostPtr const& host) = 0;
+    virtual void update(HostPtr const& host) {
+        bool deleted(false);
+        if (host->getIPv4SubnetID() != SUBNET_ID_UNUSED) {
+            std::vector<uint8_t> const& identifier(host->getIdentifier());
+            deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
+                identifier.size());
+        } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) {
+            std::vector<uint8_t> const& identifier(host->getIdentifier());
+            deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
+                identifier.size());
+        } else {
+            isc_throw(HostNotFound, "Mandatory 'subnet-id' parameter missing.");
+        }
+        if (!deleted) {
+            isc_throw(HostNotFound, "Host not updated (not found).");
+        }
+        add(host);
+    }
 
     /// @brief Return backend type
     ///
index a0765006d7b5a5874e6a5687c2a0c33a181ed3af..ac2f7b469007e2c0c91f1b65b000e4d7b52b7df3 100644 (file)
@@ -1142,24 +1142,6 @@ CfgHosts::del6(const SubnetID& /*subnet_id*/,
     return (false);
 }
 
-void
-CfgHosts::update(HostPtr const& host) {
-    bool deleted(false);
-    if (CfgMgr::instance().getFamily() == AF_INET) {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    } else {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    }
-    if (!deleted) {
-        isc_throw(HostNotFound, "Host not updated (not found).");
-    }
-    add(host);
-}
-
 bool
 CfgHosts::setIPReservationsUnique(const bool unique) {
     ip_reservations_unique_ = unique;
index 448bca65cf03136773febfb5183a6dfbffebd349..495cc81265ba22912c5fc014c499b51546acaccb 100644 (file)
@@ -589,13 +589,6 @@ public:
                       const Host::IdentifierType& identifier_type,
                       const uint8_t* identifier_begin, const size_t identifier_len);
 
-    /// @brief Implements @ref BaseHostDataSource::update() for config hosts.
-    ///
-    /// Attempts to update an existing host entry.
-    ///
-    /// @param host the host up to date with the requested changes
-    void update(HostPtr const& host);
-
     /// @brief Return backend type
     ///
     /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
index a45c21ca74694b375c3418b34e8a2eece70b9f17..938c6a61a1f1a0e6b0b1ff39378c5771ec569340 100644 (file)
@@ -3941,20 +3941,7 @@ MySqlHostDataSource::update(HostPtr const& host) {
     // updating the host to exactly as is described in the command, which may involve inserts and
     // deletes alongside updates. So let's delete and add. The delete cascades into all tables. The
     // add explicitly adds into all tables.
-    bool deleted(false);
-    if (CfgMgr::instance().getFamily() == AF_INET) {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    } else {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    }
-    if (!deleted) {
-        isc_throw(NoRowsAffected, "Host not updated (not found).");
-    }
-    add(host);
+    BaseHostDataSource::update(host);
 
     // Everything went fine, so explicitly commit the transaction.
     transaction.commit();
index 46c4497b68f14bb22824590b50a435c7a30748d2..7757977ba8439537195c18e5d71983373bd6e7ca 100644 (file)
@@ -3209,20 +3209,7 @@ PgSqlHostDataSource::update(HostPtr const& host) {
     // updating the host to exactly as is described in the command, which may involve inserts and
     // deletes alongside updates. So let's delete and add. The delete cascades into all tables. The
     // add explicitly adds into all tables.
-    bool deleted(false);
-    if (CfgMgr::instance().getFamily() == AF_INET) {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    } else {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    }
-    if (!deleted) {
-        isc_throw(NoRowsAffected, "Host not updated (not found).");
-    }
-    add(host);
+    BaseHostDataSource::update(host);
 
     // Everything went fine, so explicitly commit the transaction.
     transaction.commit();
index 5bf5d79c4e20efba53512fef8fa300391431adc9..3ea2b830c59672cc5a677497724b68639ff48b9d 100644 (file)
@@ -375,26 +375,6 @@ MemHostDataSource::del6(const SubnetID& subnet_id,
     return (false);
 }
 
-void
-MemHostDataSource::update(HostPtr const& host) {
-    bool deleted(false);
-    if (host->getIPv4SubnetID() != SUBNET_ID_UNUSED) {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) {
-        vector<uint8_t> const& identifier(host->getIdentifier());
-        deleted = del6(host->getIPv6SubnetID(), host->getIdentifierType(), identifier.data(),
-             identifier.size());
-    } else {
-        isc_throw(HostNotFound, "Mandatory 'subnet-id' parameter missing.");
-    }
-    if (!deleted) {
-        isc_throw(HostNotFound, "Host not updated (not found).");
-    }
-    store_.push_back(host);
-}
-
 size_t
 MemHostDataSource::size() const {
     return (store_.size());
index ceabe704dd2b2a33b6fe7bb75b40a6d0a661793b..799ab08ea28e5938ca97d284834d9c5122043030 100644 (file)
@@ -257,13 +257,6 @@ public:
                       const Host::IdentifierType& identifier_type,
                       const uint8_t* identifier_begin, const size_t identifier_len);
 
-    /// @brief Implements @ref BaseHostDataSource::update() for memory hosts.
-    ///
-    /// Attempts to update an existing host entry.
-    ///
-    /// @param host the host up to date with the requested changes
-    void update(HostPtr const& host);
-
     /// @brief Return backend type
     ///
     /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)