]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5525] Added getParameterNames trac5526_base trac5530_base trac5530_before_rebase
authorFrancis Dupont <fdupont@isc.org>
Mon, 26 Feb 2018 12:48:55 +0000 (13:48 +0100)
committerFrancis Dupont <fdupont@isc.org>
Mon, 26 Feb 2018 12:48:55 +0000 (13:48 +0100)
src/lib/hooks/library_handle.cc
src/lib/hooks/library_handle.h
src/lib/hooks/tests/callout_params_library.cc

index 7aa7a052ccfa950235934c574281e8cea88022e0..4a661fcd545c2a9476ca9f225edbe290e0e83bc9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -80,7 +80,7 @@ LibraryHandle::deregisterAllCallouts(const std::string& name) {
 }
 
 isc::data::ConstElementPtr
-LibraryHandle::getParameter(const std::string& name) {
+LibraryHandle::getParameters() {
     HookLibsCollection libinfo = HooksManager::getHooksManager().getLibraryInfo();
 
     int index = index_;
@@ -103,8 +103,13 @@ LibraryHandle::getParameter(const std::string& name) {
     // * 1..numlib - indexes for actual libraries
     // * INT_MAX - post-user library callout
 
+    return (libinfo[index - 1].second);
+}
+
+isc::data::ConstElementPtr
+LibraryHandle::getParameter(const std::string& name) {
     // Try to find appropriate parameter. May return null pointer
-    isc::data::ConstElementPtr params = libinfo[index - 1].second;
+    isc::data::ConstElementPtr params = getParameters();
     if (!params) {
         return (isc::data::ConstElementPtr());
     }
@@ -113,5 +118,23 @@ LibraryHandle::getParameter(const std::string& name) {
     return (params->get(name));
 }
 
+std::vector<std::string>
+LibraryHandle::getParameterNames() {
+    std::vector<std::string> names;
+    // Find all parameter names.
+    isc::data::ConstElementPtr params = getParameters();
+    if (!params ||
+        (params->getType() != isc::data::Element::map) ||
+        (params->size() == 0)) {
+        return (names);
+    }
+    auto map = params->mapValue();
+    for (auto elem = map.begin(); elem != map.end(); ++elem) {
+        names.push_back(elem->first);
+    }
+    return (names);
+}
+
+
 } // namespace util
 } // namespace isc
index 5dfad2a909e570d2ff48b6e0c20b854d6415d5a2..983c8802a09b1ccb04e1b598c5a2b3fc753819b6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -198,6 +198,13 @@ public:
     isc::data::ConstElementPtr
     getParameter(const std::string& name);
 
+    /// @brief Returns names of configuration parameters for the library.
+    ///
+    /// This method returns a vector of strings reflecting names of
+    /// configuration parameters specified in the configuration file.
+    ///
+    std::vector<std::string> getParameterNames();
+
 private:
     /// @brief Copy constructor
     ///
@@ -218,6 +225,11 @@ private:
     /// @param Unused - should be the object to copy.
     LibraryHandle& operator=(const LibraryHandle&);
 
+    /// @brief Get configuration parameter common code.
+    ///
+    /// @return configuration parameters.
+    isc::data::ConstElementPtr getParameters();
+
     /// Back pointer to the collection object for the library
     CalloutManager* callout_manager_;
 
index b5b569a206b75afea05c15a6898007ea851ee1b0..3fb7ab309a92196dc227d0bdd8e940db059dd858 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -14,6 +14,7 @@
 #include <hooks/hooks.h>
 #include <iostream>
 
+using namespace std;
 using namespace isc::hooks;
 using namespace isc::data;
 
@@ -39,6 +40,7 @@ int load(LibraryHandle& handle) {
     ConstElementPtr int_elem     = handle.getParameter("ivalue");
     ConstElementPtr bool_elem    = handle.getParameter("bvalue");
     ConstElementPtr nonexistent  = handle.getParameter("nonexistent");
+    vector<string>  names        = handle.getParameterNames();
 
     // String handling example.
     if (!string_elem) {
@@ -51,7 +53,7 @@ int load(LibraryHandle& handle) {
         return (2);
     }
 
-    std::string str = string_elem->stringValue();
+    string str = string_elem->stringValue();
     if (str != "string value") {
         // Parameter is specified, is a string, but has unexpected value.
         //
@@ -97,6 +99,14 @@ int load(LibraryHandle& handle) {
         return (9);
     }
 
+    // Check names. As a side effect of what maps using strings are
+    // implemented names are sorted in alphabetical order.
+    if ((names.size() != 3) || (names[0] != "bvalue") ||
+        (names[1] != "ivalue") || (names[2] != "svalue")) {
+        // Expect 3 names: bvalue, ivalue, svalue.
+        return (10);
+    }
+
     // All validation steps were successful. The library has all the parameters
     // it needs, so we should report a success.
     return (0);