Browse Source

use Result class; begin hosts parse

develop
Robin Thoni 9 years ago
parent
commit
eee5c0f847

+ 6
- 5
src/Business/PdnsSlave.cpp View File

@@ -11,11 +11,12 @@ PdnsSlave::PdnsSlave(const std::string &filePath)
11 11
 {
12 12
 }
13 13
 
14
-bool PdnsSlave::readConfig()
14
+BResult PdnsSlave::readConfig()
15 15
 {
16 16
     PdnsSlaveConfig conf(_filePath);
17
-    if (!conf.readConfig())
18
-        return false;
17
+    BResult res;
18
+    if (!(res = conf.readConfig()))
19
+        return res;
19 20
 
20 21
     _dhcpdFilePath = conf.getDhcpdFilePath();
21 22
     _dhcpdTemplatePath = conf.getDhcpdTemplatePath();
@@ -26,11 +27,11 @@ bool PdnsSlave::readConfig()
26 27
     return true;
27 28
 }
28 29
 
29
-bool PdnsSlave::readDhcpdTemplate()
30
+BResult PdnsSlave::readDhcpdTemplate()
30 31
 {
31 32
     std::ifstream dhcpTemplate(_dhcpdTemplatePath);
32 33
     if (!dhcpTemplate)
33
-        return false;
34
+        return BResult().error("Could not open file");
34 35
     while (!dhcpTemplate.eof())
35 36
     {
36 37
         std::string line;

+ 2
- 2
src/Business/PdnsSlave.h View File

@@ -13,9 +13,9 @@ class PdnsSlave
13 13
 public:
14 14
     PdnsSlave(const std::string& filePath);
15 15
 
16
-    bool readConfig();
16
+    BResult readConfig();
17 17
 
18
-    bool readDhcpdTemplate();
18
+    BResult readDhcpdTemplate();
19 19
 
20 20
 private:
21 21
     std::string _filePath;

+ 7
- 1
src/CMakeLists.txt View File

@@ -14,7 +14,13 @@ set(SOURCE_FILES
14 14
   DBO/SqlConfiguration.h
15 15
   DataAccess/PdnsSlaveConfig.cpp
16 16
   DataAccess/PdnsSlaveConfig.h
17
+  DataAccess/HostsConfig.cpp
18
+  DataAccess/HostsConfig.h
19
+  DBO/Actions/Action.cpp
20
+  DBO/Actions/Action.h
21
+  DBO/Result.hxx
22
+  DBO/Result.h
17 23
   )
18 24
 set(LIBS ${LIBS} jsoncpp)
19
-add_executable(pdns-slave ${SOURCE_FILES} DataAccess/HostsConfig.cpp DataAccess/HostsConfig.h)
25
+add_executable(pdns-slave ${SOURCE_FILES} DBO/Actions/ActionDelHost.cpp DBO/Actions/ActionDelHost.h DBO/Actions/ActionAddHost.cpp DBO/Actions/ActionAddHost.h)
20 26
 target_link_libraries(pdns-slave ${LIBS})

+ 67
- 5
src/DataAccess/HostsConfig.cpp View File

@@ -4,18 +4,42 @@
4 4
 
5 5
 #include <json/json.h>
6 6
 #include <fstream>
7
+#include <memory>
8
+#include <DBO/Actions/Action.h>
9
+#include <DBO/Actions/ActionAddHost.h>
10
+#include <DBO/Actions/ActionDelHost.h>
7 11
 #include "HostsConfig.h"
8 12
 
13
+#define SET_VALUE(VALUE, METHOD, TYPE) do {             \
14
+    if (action)                                         \
15
+    {                                                   \
16
+        auto v = readValue(act, defaults, VALUE);       \
17
+        if (!v || !v.is##TYPE())                        \
18
+        {                                               \
19
+            action = nullptr;                           \
20
+        }                                               \
21
+        else                                            \
22
+        {                                               \
23
+            action->METHOD(v.as##TYPE());               \
24
+        }                                               \
25
+    }                                                   \
26
+    } while (0)
27
+
28
+#define SET_VALUE_STRING(VALUE, METHOD) SET_VALUE(VALUE, METHOD, String)
29
+
30
+#define SET_VALUE_BOOL(VALUE, METHOD) SET_VALUE(VALUE, METHOD, Bool)
31
+
9 32
 HostsConfig::HostsConfig(const std::string &filePath)
10 33
     : _filePath(filePath)
11 34
 {
12 35
 }
13 36
 
14
-bool HostsConfig::readConfig()
37
+BResult HostsConfig::readConfig()
15 38
 {
39
+    BResult res;
16 40
     std::ifstream file(_filePath);
17 41
     if (!file)
18
-        return false;
42
+        return res.error("Could not open file");
19 43
     Json::Value root;
20 44
     try
21 45
     {
@@ -25,14 +49,52 @@ bool HostsConfig::readConfig()
25 49
     catch (...)
26 50
     {
27 51
         file.close();
28
-        return false;
52
+        return res.error("Could not parse JSON");
29 53
     }
30 54
     auto defaults = root["defaults"];
31 55
     auto actions = root["actions"];
32 56
     for (Json::ArrayIndex i = 0; i < actions.size(); ++i)
33 57
     {
34
-        auto action = actions[i];
35
-        auto action_type = readValue(action, defaults, "action");
58
+        auto act = actions[i];
59
+        auto action_type = readValue(act, defaults, "action");
60
+        if (!action_type)
61
+            res.addWarning("Could no find action value for item " + i);
62
+        else
63
+        {
64
+            auto action_str = action_type.asString();
65
+            std::shared_ptr<Action> actionAbs = nullptr;
66
+            if (action_str == "add_domain")
67
+            {
68
+
69
+            }
70
+            else if (action_str == "del_domain")
71
+            {
72
+
73
+            }
74
+            else if (action_str == "add_host")
75
+            {
76
+                auto action = std::make_shared<ActionAddHost>();
77
+                SET_VALUE_STRING("host", setHost);
78
+                SET_VALUE_STRING("record_value", setRecordValue);
79
+                SET_VALUE_STRING("record_type", setRecordType);
80
+                actionAbs = action;
81
+            }
82
+            else if (action_str == "del_host")
83
+            {
84
+                auto action = std::make_shared<ActionDelHost>();
85
+                SET_VALUE_STRING("host", setHost);
86
+                actionAbs = action;
87
+            }
88
+            else
89
+                res.addWarning("Unkown action " + action_str);
90
+            if (actionAbs)
91
+            {
92
+                auto action = actionAbs;
93
+                SET_VALUE_STRING("domain", setDomain);
94
+            }
95
+            else
96
+                res.addWarning("Errors occured while processing item " + i);
97
+        }
36 98
     }
37 99
     return true;
38 100
 }

+ 2
- 1
src/DataAccess/HostsConfig.h View File

@@ -6,13 +6,14 @@
6 6
 #define PDNS_SLAVE_HOSTSCONFIG_H
7 7
 
8 8
 # include <string>
9
+# include <DBO/Result.h>
9 10
 
10 11
 class HostsConfig
11 12
 {
12 13
 public:
13 14
     HostsConfig(const std::string& filePath);
14 15
 
15
-    bool readConfig();
16
+    BResult readConfig();
16 17
 
17 18
 private:
18 19
     Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name);

+ 26
- 25
src/DataAccess/PdnsSlaveConfig.cpp View File

@@ -12,11 +12,11 @@ PdnsSlaveConfig::PdnsSlaveConfig(const std::string &filePath)
12 12
 {
13 13
 }
14 14
 
15
-bool PdnsSlaveConfig::readConfig()
15
+BResult PdnsSlaveConfig::readConfig()
16 16
 {
17 17
     std::ifstream file(_filePath);
18 18
     if (!file)
19
-        return false;
19
+        return BResult().error("Could not open file");
20 20
     Json::Value root;
21 21
     try
22 22
     {
@@ -26,49 +26,50 @@ bool PdnsSlaveConfig::readConfig()
26 26
     catch (...)
27 27
     {
28 28
         file.close();
29
-        return false;
29
+        return BResult().error("Could not parse JSON");
30 30
     }
31
-    if (!readString(root, "dhcpd-file", _dhcpdFilePath))
32
-        return false;
33
-    if (!readString(root, "dhcpd-template", _dhcpdTemplatePath))
34
-        return false;
35
-    if (!readString(root, "hosts-file", _hostsPath))
36
-        return false;
37
-    if (!readSqlConfiguration(root, "master", _masterConfig))
38
-        return false;
39
-    if (!readSqlConfiguration(root, "slave", _slaveConfig))
40
-        return false;
41
-
31
+    BResult res;
32
+    if (!(res = readString(root, "dhcpd-file", _dhcpdFilePath)))
33
+        return res;
34
+    if (!(res = readString(root, "dhcpd-template", _dhcpdTemplatePath)))
35
+        return res;
36
+    if (!(res = readString(root, "hosts-file", _hostsPath)))
37
+        return res;
38
+    if (!(res = readSqlConfiguration(root, "master", _masterConfig)))
39
+        return res;
40
+    if (!(res = readSqlConfiguration(root, "slave", _slaveConfig)))
41
+        return res;
42 42
     return true;
43 43
 }
44 44
 
45
-bool PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
45
+BResult PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
46 46
 {
47 47
     std::string str;
48
-    if (!readString(value, name + "-host", str))
49
-        return false;
48
+    BResult res;
49
+    if (!(res = readString(value, name + "-host", str)))
50
+        return res;
50 51
     sqlConfiguration.setHost(str);
51 52
 
52
-    if (!readString(value, name + "-user", str))
53
-        return false;
53
+    if (!(res = readString(value, name + "-user", str)))
54
+        return res;
54 55
     sqlConfiguration.setUser(str);
55 56
 
56
-    if (!readString(value, name + "-password", str))
57
-        return false;
57
+    if (!(res = readString(value, name + "-password", str)))
58
+        return res;
58 59
     sqlConfiguration.setPassword(str);
59 60
 
60
-    if (!readString(value, name + "-database", str))
61
-        return false;
61
+    if (!(res = readString(value, name + "-database", str)))
62
+        return res;
62 63
     sqlConfiguration.setDatabase(str);
63 64
 
64 65
     return true;
65 66
 }
66 67
 
67
-bool PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
68
+BResult PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
68 69
 {
69 70
     auto v = value[name];
70 71
     if (!v)
71
-        return false;
72
+        return BResult().error("Could not find value " + name);
72 73
     dest = v.asString();
73 74
     return true;
74 75
 }

+ 4
- 3
src/DataAccess/PdnsSlaveConfig.h View File

@@ -6,6 +6,7 @@
6 6
 #define PDNS_SLAVE_PDNSSLAVECONFIG_H
7 7
 
8 8
 # include <string>
9
+# include "DBO/Result.h"
9 10
 # include "DBO/SqlConfiguration.h"
10 11
 
11 12
 namespace Json
@@ -18,7 +19,7 @@ class PdnsSlaveConfig
18 19
 public:
19 20
     PdnsSlaveConfig(const std::string& filePath);
20 21
 
21
-    bool readConfig();
22
+    BResult readConfig();
22 23
 
23 24
     const std::string &getDhcpdFilePath() const;
24 25
 
@@ -31,9 +32,9 @@ public:
31 32
     const SqlConfiguration &getSlaveConfig() const;
32 33
 
33 34
 private:
34
-    bool readString(const Json::Value& value, const std::string& name, std::string& dest);
35
+    BResult readString(const Json::Value& value, const std::string& name, std::string& dest);
35 36
 
36
-    bool readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration& sqlConfiguration);
37
+    BResult readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration& sqlConfiguration);
37 38
 
38 39
     std::string _filePath;
39 40
 

+ 6
- 2
src/Interface/MainClass.cpp View File

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include <iostream>
6 6
 #include <sysexits.h>
7
+#include <DBO/Result.h>
7 8
 #include "CommandLineParser.h"
8 9
 #include "Business/PdnsSlave.h"
9 10
 #include "MainClass.h"
@@ -31,14 +32,17 @@ int MainClass::main()
31 32
     std::cout << "Using configuration file " << filePath << std::endl;
32 33
 
33 34
     PdnsSlave pdnsSlave(filePath);
34
-    if (!pdnsSlave.readConfig())
35
+    BResult res;
36
+    if (!(res = pdnsSlave.readConfig()))
35 37
     {
36 38
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
39
+        res.print();
37 40
         return 1;
38 41
     }
39
-    if (!pdnsSlave.readDhcpdTemplate())
42
+    if (!(res = pdnsSlave.readDhcpdTemplate()))
40 43
     {
41 44
         std::cerr << "Failed to read dhcpd template" << std::endl;
45
+        res.print();
42 46
         return 2;
43 47
     }
44 48
 

Loading…
Cancel
Save