]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[397-cb-implement-mysqlconfigbackenddhcpv6] Shared body of createUpdateOptionDef[46]
authorFrancis Dupont <fdupont@isc.org>
Sat, 13 Apr 2019 16:11:59 +0000 (18:11 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 18 Apr 2019 13:52:51 +0000 (15:52 +0200)
src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc
src/hooks/dhcp/mysql_cb/mysql_cb_dhcp6.cc
src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
src/hooks/dhcp/mysql_cb/mysql_cb_impl.h

index bd3e2c5cb40b5c593d899d276b42abcb4d5d5a60..d16da2b1ea68a40f210225e4d3ac5d23b8fe2869 100644 (file)
@@ -1664,87 +1664,13 @@ public:
     void createUpdateOptionDef4(const ServerSelector& server_selector,
                                 const OptionDefinitionPtr& option_def) {
 
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "creating or updating option definition");
-
-        ElementPtr record_types = Element::createList();
-        for (auto field : option_def->getRecordFields()) {
-            record_types->add(Element::create(static_cast<int>(field)));
-        }
-        MySqlBindingPtr record_types_binding = record_types->empty() ?
-            MySqlBinding::createNull() : MySqlBinding::createString(record_types->str());
-
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(option_def->getCode())),
-            MySqlBinding::createString(option_def->getName()),
-            MySqlBinding::createString(option_def->getOptionSpaceName().empty() ?
-                                       "dhcp4" : option_def->getOptionSpaceName()),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(option_def->getType())),
-            MySqlBinding::createTimestamp(option_def->getModificationTime()),
-            MySqlBinding::createBool(option_def->getArrayType()),
-            MySqlBinding::createString(option_def->getEncapsulatedSpace()),
-            record_types_binding,
-            createInputContextBinding(option_def)
-        };
-
-        MySqlTransaction transaction(conn_);
-
-        // Need to check if this definition already exists. We can't follow
-        // the same pattern as for shared networks and subnets, to try to insert
-        // the definition first and fall back to update if the DuplicateEntry
-        // exception is thrown, because the option code/space is not unique
-        // within the dhcp4_option_def table. Inserting another option definition
-        // with existing option code/name would not violate the key and the
-        // option definition instance would be inserted successfully. Therefore,
-        // we first fetch the option definition for the given server, code and
-        // space name. If it exists, we simply update it.
-        OptionDefinitionPtr existing_definition =
-            getOptionDef(GET_OPTION_DEF4_CODE_SPACE,
-                         server_selector,
-                         option_def->getCode(),
-                         option_def->getOptionSpaceName());
-
-        // Create scoped audit revision. As long as this instance exists
-        // no new audit revisions are created in any subsequent calls.
-        ScopedAuditRevision
-            audit_revision(this,
-                           MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION,
-                           server_selector, "option definition set",
-                           true);
-
-        if (existing_definition) {
-            // Need to add three more bindings for WHERE clause.
-            in_bindings.push_back(MySqlBinding::createString(tag));
-            in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(existing_definition->getCode()));
-            in_bindings.push_back(MySqlBinding::createString(existing_definition->getOptionSpaceName()));
-            conn_.updateDeleteQuery(MySqlConfigBackendDHCPv4Impl::UPDATE_OPTION_DEF4,
-                                    in_bindings);
-
-        } else {
-            // If the option definition doesn't exist, let's insert it.
-            conn_.insertQuery(MySqlConfigBackendDHCPv4Impl::INSERT_OPTION_DEF4,
-                              in_bindings);
-
-            // Fetch unique identifier of the inserted option definition and use it
-            // as input to the next query.
-            uint64_t id = mysql_insert_id(conn_.mysql_);
-
-            MySqlBindingCollection in_server_bindings = {
-                MySqlBinding::createInteger<uint64_t>(id), // option_def_id
-                MySqlBinding::createString(tag), // tag used to obtain server_id
-                MySqlBinding::createTimestamp(option_def->getModificationTime()), // modification_ts
-            };
-
-            // Insert association.
-            conn_.insertQuery(MySqlConfigBackendDHCPv4Impl::INSERT_OPTION_DEF4_SERVER,
-                              in_server_bindings);
-        }
-
-        transaction.commit();
+        createUpdateOptionDef
+            (server_selector, option_def, "dhcp4",
+             MySqlConfigBackendDHCPv4Impl::GET_OPTION_DEF4_CODE_SPACE,
+             MySqlConfigBackendDHCPv4Impl::INSERT_OPTION_DEF4,
+             MySqlConfigBackendDHCPv4Impl::UPDATE_OPTION_DEF4,
+             MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION,
+             MySqlConfigBackendDHCPv4Impl::INSERT_OPTION_DEF4_SERVER);
     }
 
     /// @brief Sends query to delete option definition by code and
index 9ccbc91d02ce45c915520573def7c5de72143869..63ca0bd0abcb9954652547412d31daf2f1717de0 100644 (file)
@@ -1206,7 +1206,7 @@ public:
 
                 last_network_id = out_bindings[0]->getInteger<uint64_t>();
                 last_network.reset(new SharedNetwork6(out_bindings[1]->getString()));
-               last_network->setId(last_network_id);
+                last_network->setId(last_network_id);
 
                 // client_class
                 if (!out_bindings[2]->amNull()) {
@@ -1889,87 +1889,13 @@ public:
     void createUpdateOptionDef6(const ServerSelector& server_selector,
                                 const OptionDefinitionPtr& option_def) {
 
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "creating or updating option definition");
-
-        ElementPtr record_types = Element::createList();
-        for (auto field : option_def->getRecordFields()) {
-            record_types->add(Element::create(static_cast<int>(field)));
-        }
-        MySqlBindingPtr record_types_binding = record_types->empty() ?
-            MySqlBinding::createNull() : MySqlBinding::createString(record_types->str());
-
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createInteger<uint16_t>(option_def->getCode()),
-            MySqlBinding::createString(option_def->getName()),
-            MySqlBinding::createString(option_def->getOptionSpaceName().empty() ?
-                                       "dhcp6" : option_def->getOptionSpaceName()),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint16_t>(option_def->getType())),
-            MySqlBinding::createTimestamp(option_def->getModificationTime()),
-            MySqlBinding::createBool(option_def->getArrayType()),
-            MySqlBinding::createString(option_def->getEncapsulatedSpace()),
-            record_types_binding,
-            createInputContextBinding(option_def)
-        };
-
-        MySqlTransaction transaction(conn_);
-
-        // Need to check if this definition already exists. We can't follow
-        // the same pattern as for shared networks and subnets, to try to insert
-        // the definition first and fall back to update if the DuplicateEntry
-        // exception is thrown, because the option code/space is not unique
-        // within the dhcp6_option_def table. Inserting another option definition
-        // with existing option code/name would not violate the key and the
-        // option definition instance would be inserted successfully. Therefore,
-        // we first fetch the option definition for the given server, code and
-        // space name. If it exists, we simply update it.
-        OptionDefinitionPtr existing_definition =
-          getOptionDef(GET_OPTION_DEF6_CODE_SPACE,
-                       server_selector,
-                       option_def->getCode(),
-                       option_def->getOptionSpaceName());
-
-        // Create scoped audit revision. As long as this instance exists
-        // no new audit revisions are created in any subsequent calls.
-        ScopedAuditRevision
-            audit_revision(this,
-                           MySqlConfigBackendDHCPv6Impl::CREATE_AUDIT_REVISION,
-                           server_selector, "option definition set",
-                           true);
-
-        if (existing_definition) {
-            // Need to add three more bindings for WHERE clause.
-            in_bindings.push_back(MySqlBinding::createString(tag));
-            in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(existing_definition->getCode()));
-            in_bindings.push_back(MySqlBinding::createString(existing_definition->getOptionSpaceName()));
-            conn_.updateDeleteQuery(MySqlConfigBackendDHCPv6Impl::UPDATE_OPTION_DEF6,
-                                    in_bindings);
-
-        } else {
-            // If the option definition doesn't exist, let's insert it.
-            conn_.insertQuery(MySqlConfigBackendDHCPv6Impl::INSERT_OPTION_DEF6,
-                              in_bindings);
-
-            // Fetch unique identifier of the inserted option definition and use it
-            // as input to the next query.
-            uint64_t id = mysql_insert_id(conn_.mysql_);
-
-            MySqlBindingCollection in_server_bindings = {
-                MySqlBinding::createInteger<uint64_t>(id), // option_def_id
-                MySqlBinding::createString(tag), // tag used to obtain server_id
-                MySqlBinding::createTimestamp(option_def->getModificationTime()), // modification_ts
-            };
-
-            // Insert association.
-            conn_.insertQuery(MySqlConfigBackendDHCPv6Impl::INSERT_OPTION_DEF6_SERVER,
-                              in_server_bindings);
-        }
-
-        transaction.commit();
+        createUpdateOptionDef
+            (server_selector, option_def, "dhcp6",
+             MySqlConfigBackendDHCPv6Impl::GET_OPTION_DEF6_CODE_SPACE,
+             MySqlConfigBackendDHCPv6Impl::INSERT_OPTION_DEF6,
+             MySqlConfigBackendDHCPv6Impl::UPDATE_OPTION_DEF6,
+             MySqlConfigBackendDHCPv6Impl::CREATE_AUDIT_REVISION,
+             MySqlConfigBackendDHCPv6Impl::INSERT_OPTION_DEF6_SERVER);
     }
 
     /// @brief Sends query to delete option definition by code and
index 7ed30a279cdac6c6586952948e17437d22a2307c..f5fa19d3eda4922fd77de7ac6e8bd4119eb46c75 100644 (file)
@@ -365,6 +365,97 @@ MySqlConfigBackendImpl::getOptionDefs(const int index,
     });
 }
 
+void MySqlConfigBackendImpl::createUpdateOptionDef
+    (const db::ServerSelector& server_selector,
+     const OptionDefinitionPtr& option_def,
+     const std::string& space,
+     const int& get_option_def_code_space,
+     const int& insert_option_def,
+     const int& update_option_def,
+     const int& create_audit_revision,
+     const int& insert_option_def_server)
+{
+
+    if (server_selector.amUnassigned()) {
+        isc_throw(NotImplemented, "managing configuration for no particular server"
+                  " (unassigned) is unsupported at the moment");
+    }
+
+    auto tag = getServerTag(server_selector, "creating or updating option definition");
+
+    ElementPtr record_types = Element::createList();
+    for (auto field : option_def->getRecordFields()) {
+        record_types->add(Element::create(static_cast<int>(field)));
+    }
+    MySqlBindingPtr record_types_binding = record_types->empty() ?
+        MySqlBinding::createNull() : MySqlBinding::createString(record_types->str());
+
+    MySqlBindingCollection in_bindings = {
+        MySqlBinding::createInteger<uint16_t>(option_def->getCode()),
+        MySqlBinding::createString(option_def->getName()),
+        MySqlBinding::createString(option_def->getOptionSpaceName().empty() ?
+                                   space : option_def->getOptionSpaceName()),
+        MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(option_def->getType())),
+        MySqlBinding::createTimestamp(option_def->getModificationTime()),
+        MySqlBinding::createBool(option_def->getArrayType()),
+        MySqlBinding::createString(option_def->getEncapsulatedSpace()),
+        record_types_binding,
+        createInputContextBinding(option_def)
+    };
+
+    MySqlTransaction transaction(conn_);
+
+    // Need to check if this definition already exists. We can't follow
+    // the same pattern as for shared networks and subnets, to try to insert
+    // the definition first and fall back to update if the DuplicateEntry
+    // exception is thrown, because the option code/space is not unique
+    // within the dhcpX_option_def table. Inserting another option definition
+    // with existing option code/name would not violate the key and the
+    // option definition instance would be inserted successfully. Therefore,
+    // we first fetch the option definition for the given server, code and
+    // space name. If it exists, we simply update it.
+    OptionDefinitionPtr existing_definition =
+        getOptionDef(get_option_def_code_space,
+                     server_selector,
+                     option_def->getCode(),
+                     option_def->getOptionSpaceName());
+
+    // Create scoped audit revision. As long as this instance exists
+    // no new audit revisions are created in any subsequent calls.
+    ScopedAuditRevision audit_revision(this,
+                                       create_audit_revision,
+                                       server_selector,
+                                       "option definition set",
+                                       true);
+
+    if (existing_definition) {
+        // Need to add three more bindings for WHERE clause.
+        in_bindings.push_back(MySqlBinding::createString(tag));
+        in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(existing_definition->getCode()));
+        in_bindings.push_back(MySqlBinding::createString(existing_definition->getOptionSpaceName()));
+        conn_.updateDeleteQuery(update_option_def, in_bindings);
+
+    } else {
+        // If the option definition doesn't exist, let's insert it.
+        conn_.insertQuery(insert_option_def, in_bindings);
+
+        // Fetch unique identifier of the inserted option definition and use it
+        // as input to the next query.
+        uint64_t id = mysql_insert_id(conn_.mysql_);
+
+        MySqlBindingCollection in_server_bindings = {
+            MySqlBinding::createInteger<uint64_t>(id), // option_def_id
+            MySqlBinding::createString(tag), // tag used to obtain server_id
+            MySqlBinding::createTimestamp(option_def->getModificationTime()), // modification_ts
+        };
+
+        // Insert association.
+        conn_.insertQuery(insert_option_def_server, in_server_bindings);
+    }
+
+    transaction.commit();
+}
+
 OptionDescriptorPtr
 MySqlConfigBackendImpl::getOption(const int index,
                                   const Option::Universe& universe,
index 96fc3d05ae8e70484d53c82cc49940d2dddf92c2..ebeccec458bebb2199f528ccdc7a9953461cb346 100644 (file)
@@ -366,6 +366,28 @@ public:
                        const db::MySqlBindingCollection& in_bindings,
                        OptionDefContainer& option_defs);
 
+    /// @brief Creates or updates an option definition.
+    ///
+    /// @param server_selector Server selector.
+    /// @param option_def Option definition to be added or updated.
+    /// @param space Default option space
+    /// @param get_option_def_code_space Statement getting option
+    /// definition by code and space.
+    /// @param insert_option_def Statement inserting option definition.
+    /// @param update_option_def Statement updating option definition.
+    /// @param create_audit_revision Statement creating audit revision.
+    /// @param insert_option_def_server Statement inserting option
+    /// definition in the server table.
+    /// @throw NotImplemented if server selector is "unassigned".
+    void createUpdateOptionDef(const db::ServerSelector& server_selector,
+                               const OptionDefinitionPtr& option_def,
+                               const std::string& space,
+                               const int& get_option_def_code_space,
+                               const int& insert_option_def,
+                               const int& update_option_def,
+                               const int& create_audit_revision,
+                               const int& insert_option_def_server);
+
     /// @brief Sends query to retrieve single global option by code and
     /// option space.
     ///