From: Andrei Pavel Date: Wed, 19 Apr 2023 16:20:37 +0000 (+0300) Subject: [#549] add an implementation for BaseHostDataSource::update X-Git-Tag: Kea-2.3.7~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e89d009a3c65cf026b26693139bb588bd2a00667;p=thirdparty%2Fkea.git [#549] add an implementation for BaseHostDataSource::update --- diff --git a/src/lib/dhcpsrv/base_host_data_source.h b/src/lib/dhcpsrv/base_host_data_source.h index 5e53424864..e5e42e2859 100644 --- a/src/lib/dhcpsrv/base_host_data_source.h +++ b/src/lib/dhcpsrv/base_host_data_source.h @@ -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 const& identifier(host->getIdentifier()); + deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(), + identifier.size()); + } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) { + std::vector 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 /// diff --git a/src/lib/dhcpsrv/cfg_hosts.cc b/src/lib/dhcpsrv/cfg_hosts.cc index a0765006d7..ac2f7b4690 100644 --- a/src/lib/dhcpsrv/cfg_hosts.cc +++ b/src/lib/dhcpsrv/cfg_hosts.cc @@ -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 const& identifier(host->getIdentifier()); - deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(), - identifier.size()); - } else { - vector 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; diff --git a/src/lib/dhcpsrv/cfg_hosts.h b/src/lib/dhcpsrv/cfg_hosts.h index 448bca65cf..495cc81265 100644 --- a/src/lib/dhcpsrv/cfg_hosts.h +++ b/src/lib/dhcpsrv/cfg_hosts.h @@ -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.) diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index a45c21ca74..938c6a61a1 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -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 const& identifier(host->getIdentifier()); - deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(), - identifier.size()); - } else { - vector 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(); diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index 46c4497b68..7757977ba8 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -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 const& identifier(host->getIdentifier()); - deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(), - identifier.size()); - } else { - vector 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(); diff --git a/src/lib/dhcpsrv/testutils/memory_host_data_source.cc b/src/lib/dhcpsrv/testutils/memory_host_data_source.cc index 5bf5d79c4e..3ea2b830c5 100644 --- a/src/lib/dhcpsrv/testutils/memory_host_data_source.cc +++ b/src/lib/dhcpsrv/testutils/memory_host_data_source.cc @@ -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 const& identifier(host->getIdentifier()); - deleted = del4(host->getIPv4SubnetID(), host->getIdentifierType(), identifier.data(), - identifier.size()); - } else if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) { - vector 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()); diff --git a/src/lib/dhcpsrv/testutils/memory_host_data_source.h b/src/lib/dhcpsrv/testutils/memory_host_data_source.h index ceabe704dd..799ab08ea2 100644 --- a/src/lib/dhcpsrv/testutils/memory_host_data_source.h +++ b/src/lib/dhcpsrv/testutils/memory_host_data_source.h @@ -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.)